import { decorator_compatibility } from "./utils"; /** * Property Decorator. Apply to a static property that contains an object. * Subclasses will inherit the property and any top-level keys, but can be assigned to independently * * ```js * class A { * @InheritedDict static object = { foo: 1, bar: 3 }; * } * * class B { } * B.object['foo'] = 4 * B.object['meh'] = 2 * * A.object['foo'] // = 1 * B.object['foo'] // = 4 * A.object['meh'] // = undefined * B.object['meh'] // = 2 * A.object['bar'] // = 3 * B.object['bar'] // = 3 * ``` */ export const InheritedDict = decorator_compatibility({ legacy(target, key, descriptor?) { const prop_key = `_${String(key)}`; function getDictionary() { if (this[prop_key] == null || !this.hasOwnProperty(prop_key)) { const dict = {}; const proto = Object.getPrototypeOf(this); if (proto[key]) { Object.setPrototypeOf(dict, proto[key]); } this[prop_key] = dict; } return this[prop_key]; } if (target[key]) Object.assign(getDictionary.call(target), target[key]) return { get() { return getDictionary.call(this); }, set(v) { const obj = getDictionary.call(this); Object.assign(obj, v); } } as PropertyDescriptor as any; }, stage3(value, context) { if (context.kind == "accessor" && context.static) { const key = context.name; const prop_key = `_${String(key)}`; function getDictionary() { if (this[prop_key] == null || !this.hasOwnProperty(prop_key)) { const dict = {}; const proto = Object.getPrototypeOf(this); if (proto[key]) { Object.setPrototypeOf(dict, proto[key]); } this[prop_key] = dict; } return this[prop_key]; } return { get() { return getDictionary.call(this); }, set(v) { const obj = getDictionary.call(this); Object.assign(obj, v); }, } } }, })