UNPKG

@typescript-package/controlled-descriptor

Version:

A lightweight TypeScript library for controlled property descriptor.

195 lines (188 loc) 6.63 kB
import { WrappedDescriptorCore } from '@typescript-package/wrapped-descriptor'; import { ControlledDescriptorController } from '@typescript-package/controller'; // Abstract. /** * @description The core abstraction class for controlled descriptors. * @export * @abstract * @class ControlledDescriptorCore * @template [O=any] The type of the object. * @template {keyof O} [K=keyof O] The type of the key. * @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The type of the value. * @template {boolean} [A=boolean] The type of the active. * @template {boolean} [N=boolean] The type of the enabled. * @template {boolean} [C=boolean] The type of the configurable. * @template {boolean} [E=boolean] The type of the enumerable. * @template {ControlledPropertyDescriptor<O, K, V, A, N, C, E, D> | PropertyDescriptor} [D=ControlledPropertyDescriptor<O, K, V, A, N, C, E, any>] The type of the previous descriptor. * @extends {WrappedDescriptorCore<O, K, V, A, N, C, E, D>} * @implements {ControlledPropertyDescriptor<O, K, V, A, N, C, E, D>} */ class ControlledDescriptorCore extends WrappedDescriptorCore { } // Abstract. /** * @description The base abstraction class for controlled descriptors. * @export * @abstract * @class ControlledDescriptorBase * @template [O=any] The type of object. * @template {keyof O} [K=keyof O] The type of the key. * @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The type of the value. * @template {boolean} [A=boolean] The type of active. * @template {boolean} [N=boolean] The type of enabled. * @template {boolean} [C=boolean] The type of configurable. * @template {boolean} [E=boolean] The type of enumerable. * @template {ControlledPropertyDescriptor<O, K, V, A, N, C, E, D> | PropertyDescriptor} [D=ControlledPropertyDescriptor<O, K, V, A, N, C, E, any>] The type of previous and `get`, `set` descriptor. * @extends {ControlledDescriptorCore<O, K, V, A, N, C, E, D>} */ class ControlledDescriptorBase extends ControlledDescriptorCore { /** * @inheritdoc */ get active() { return this.controller.active; } /** * @inheritdoc */ get controller() { return this.#controller; } /** * @inheritdoc */ get enabled() { return this.controller.enabled; } /** * @inheritdoc */ get index() { return this.controller.index; } /** * @inheritdoc */ get key() { return this.controller.key; } /** * @inheritdoc */ get onGet() { return this.controller.onGet; } /** * @inheritdoc */ get onSet() { return this.controller.onSet; } /** * @inheritdoc */ get previous() { return this.controller.previous; } /** * @inheritdoc */ get previousDescriptor() { return this.controller.previousDescriptor; } /** * @inheritdoc */ get privateKey() { return this.controller.privateKey; } /** * @inheritdoc */ get get() { return this.controller.get; } /** * @inheritdoc */ get set() { return this.controller.set; } /** * @description Privately stored controller. * @type {WrappedPropertyDescriptorController<O, K, V, A, N, C, E, D>} */ #controller; /** * Creates an instance of `WrappedDescriptorBase` child class. * @constructor * @param {O} object The object to define the descriptor on. * @param {K} key The key of the object to define the descriptor on. * @param {Partial<WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>>} attributes The property descriptor to wrap. * @param {new ( * object: O, * key: K, * attributes: Partial<WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>>, * ) => WrappedPropertyDescriptorController<O, K, V, A, N, C, E, D>} controller The controller to control behavior. */ constructor(object, key, attributes, controller) { super({ configurable: attributes.configurable, enumerable: attributes.enumerable }); // Set the controller. this.#controller = new controller(object, key, { ...attributes, ...super.wrap({ get: attributes.get, set: attributes.set }) }); } } // Abstract. /** * @description The concrete implementation controlled descriptor class with controller to control its behavior. * @export * @class ControlledDescriptor * @template [O=any] The type of object. * @template {keyof O} [K=keyof O] The type of the key. * @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The type of the value. * @template {boolean} [A=boolean] The type of the active. * @template {boolean} [N=boolean] The type of the enabled. * @template {boolean} [C=boolean] The type of the configurable. * @template {boolean} [E=boolean] The type of the enumerable. * @template {ControlledPropertyDescriptor<O, K, V, A, N, C, E, D> | PropertyDescriptor} [D=ControlledPropertyDescriptor<O, K, V, A, N, C, E, any>] The type of previous and `get`, `set` descriptor. * @extends {ControlledDescriptorBase<O, K, V, A, N, C, E, D>} */ class ControlledDescriptor extends ControlledDescriptorBase { /** * @description The string tag for the descriptor. * @public * @readonly * @type {string} */ get [Symbol.toStringTag]() { return 'ControlledDescriptor'; } /** * Creates an instance of `ControlledDescriptor`. * @constructor * @param {O} object The object of the key. * @param {K} key The object key. * @param {Partial<WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>>} [attributes={}] The attributes of the descriptor. * @param {new ( * object: O, * key: K, * descriptor: Partial<WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>>, * ) => WrappedPropertyDescriptorController<O, K, V, A, N, C, E, D>} [controller=ControlledDescriptorController] The controller to control accessors. */ constructor(object, key, attributes = {}, controller = (ControlledDescriptorController)) { super(object, key, attributes, controller); } } /* * Public API Surface of controllable-descriptor */ /** * Generated bundle index. Do not edit. */ export { ControlledDescriptor, ControlledDescriptorBase, ControlledDescriptorCore }; //# sourceMappingURL=typescript-package-controlled-descriptor.mjs.map