import { Base } from "../class/Base.js" import { AnyConstructor, Mixin } from "../class/Mixin.js" //--------------------------------------------------------------------------------------------------------------------- export interface Managed { readonly meta : MetaClass> } export type ManagedConstructor = AnyConstructor export class Property extends Base { name : string = undefined apply (target : Konstructor) { } getInitString (configName : string) : string { return `this.${this.name}=${configName}.${this.name};` } static decorator (this : T, props? : Partial>, cls? : T) : PropertyDecorator { return (target : InstanceType, propertyKey : string) : void => { const property = new (cls || this)() property.initialize(props) property.name = propertyKey target.meta.addProperty(property) } } } export class MetaClass extends Base { konstructor : Konstructor = undefined properties : Property[] = [] $initializer : (this : InstanceType, config? : Partial>) => any = undefined initialize> (props? : Partial) { super.initialize(props) if (!this.konstructor) { throw new Error('Required property `konstructor` missing during instantiation of ' + this) } // @ts-ignore this.instancePrototype.$meta = this } get initializer () : this[ '$initializer' ] { if (this.$initializer !== undefined) return this.$initializer let body : string = Array.from(new Set(this.properties)).map(property => property.getInitString('config')).join('') return this.$initializer = new Function('config', body) as this[ '$initializer' ] } get superclass () : AnyConstructor { return Object.getPrototypeOf(this.konstructor.prototype).constructor } get instancePrototype () : InstanceType { return this.konstructor.prototype } addProperty (property : Property) { this.properties.push(property) property.apply(this.konstructor) } } export const MetaClassC = (config : Partial>) : MetaClass => MetaClass.new(config) as MetaClass //--------------------------------------------------------------------------------------------------------------------- export class BaseManaged extends Mixin( [], (base : AnyConstructor) => class BaseManaged extends base { $meta : any constructor (...args : any[]) { super(...args) this.meta.initializer.call(this) } get meta () : MetaClass { // @ts-ignore return this.constructor.meta } static get mixinsForMetaClass () : AnyConstructor[] { return [ MetaClass ] } static get meta () : MetaClass { const proto = this.prototype if (proto.hasOwnProperty('$meta')) return proto.$meta // @ts-ignore const metaClass = Mixin(this.mixinsForMetaClass, base => base) // @ts-ignore return proto.$meta = metaClass.new({ konstructor : this }) } } ){}