X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/e8d0d3ab4f8a844e15873345c0564929c80fdb74..3edbac676b6d5d58417dc813f9deefbe21d12a96:/object.js diff --git a/object.js b/object.js index 578f84f..a34423e 100644 --- a/object.js +++ b/object.js @@ -10,6 +10,64 @@ import { bind, call } from "./function.js"; import { toPrimitive, type } from "./value.js"; +/** + * An object whose properties are lazy‐loaded from the methods on the + * own properties of the provided object. + * + * This is useful when you are looking to reference properties on + * objects which, due to module dependency graphs, cannot be guaranteed + * to have been initialized yet. + * + * The resulting properties will have the same attributes (regarding + * configurability, enumerability, and writability) as the + * corresponding properties on the methods object. If a property is + * marked as writable, the method will never be called if it is set + * before it is gotten. By necessity, the resulting properties are all + * configurable before they are accessed for the first time. + * + * Methods will be called with the resulting object as their this + * value. + * + * LazyLoader objects have the same prototype as the passed methods + * object. + */ +export class LazyLoader extends null { + /** Constructs a new LazyLoader object. */ + constructor(loadMethods) { + const result = objectCreate(getPrototype(loadMethods)); + const methodKeys = getOwnPropertyKeys(loadMethods); + for (let index = 0; index < methodKeys.length; ++index) { + const methodKey = methodKeys[index]; + const { configurable, enumerable, writable } = + getOwnPropertyDescriptor(loadMethods, methodKey); + defineOwnProperty(result, methodKey, { + configurable: true, + enumerable, + get: () => { + const value = call(loadMethods[methodKey], result, []); + defineOwnProperty(result, methodKey, { + configurable, + enumerable, + value, + writable, + }); + return value; + }, + set: writable + ? ($) => + defineOwnProperty(result, methodKey, { + configurable, + enumerable, + value: $, + writable, + }) + : void {}, + }); + } + return result; + } +} + /** * A property descriptor object. *