2019-03-03 15:07:49 +01:00
|
|
|
export function prop(object: Object, name: string, f: () => any): void {
|
2018-06-05 09:24:47 +02:00
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
2019-03-03 15:07:49 +01:00
|
|
|
get: function() {
|
|
|
|
let value = f.call(this);
|
|
|
|
this[name] = value;
|
|
|
|
return value;
|
2018-06-05 09:24:47 +02:00
|
|
|
},
|
2019-03-03 15:07:49 +01:00
|
|
|
set: function(value) {
|
2018-06-05 09:24:47 +02:00
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: value,
|
2019-03-03 15:07:49 +01:00
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
export function value<T>(f: () => T): () => T {
|
|
|
|
let value: T;
|
|
|
|
return function(): T {
|
|
|
|
if (value !== undefined) return value;
|
|
|
|
value = f();
|
|
|
|
return value;
|
|
|
|
};
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|