bitcoinjs-lib/ts_src/payments/lazy.ts
2019-03-07 12:33:47 +09:00

28 lines
618 B
TypeScript

export function prop(object: {}, name: string, f: () => any): void {
Object.defineProperty(object, name, {
configurable: true,
enumerable: true,
get() {
const _value = f.call(this);
this[name] = _value;
return _value;
},
set(_value) {
Object.defineProperty(this, name, {
configurable: true,
enumerable: true,
value: _value,
writable: true,
});
},
});
}
export function value<T>(f: () => T): () => T {
let _value: T;
return (): T => {
if (_value !== undefined) return _value;
_value = f();
return _value;
};
}