import {Func, supplierFn,supplier} from "../Interface"; import {Supplier} from "../utils/Supplier"; import {AbstractDecorator} from "./AbstractDecorator"; import {affectFn, AttributeProperties, DecoratorSink, propertiesDescriptor} from "./DecoratorInterfaces"; export class DecoratorAttributes extends AbstractDecorator implements AttributeProperties{ protected decorator: string; prop:propertiesDescriptor; constructor(prop:propertiesDescriptor = null ) {super();} setEnumerable(state:boolean):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.get().enumerable = state; slf.affect(); } } } setWrite(state:boolean):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.affect(); slf.get().writable = state; } } } setConfigurable(state:boolean):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.affect(); slf.get().configurable = state; } } } setValue(value:T):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.affect(); slf.get().value = value; } } } setMethod(value:Function):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.affect(); (slf.get()).value = function(...args:Object[]):T{ args.push(slf.getName()); return value.apply(this,args); }; } } } propertyName(consumer:Func):AttributeProperties{ let slf:this = this; return new class extends DecoratorBuilderStateOp{ constructor() {super(slf);} /***@Override**/ public affect:affectFn = ()=>{ slf.affect(); let t:this= this; this.setProperty(new class extends Supplier{ get:supplierFn = ():string=> consumer.call(null,t.getName()); }); } } } readOnly():AttributeProperties{ return this.setConfigurable(false).setWrite(false); } final():AttributeProperties{ return this.setConfigurable(false).setWrite(false); } getSink():DecoratorSink{ return >this; } /***@ts-ignore*/ setDecorator(decorator:string):AttributeProperties{ this.decorator = decorator; return this; } /*** * */ public static empty():AttributeProperties{return new DecoratorAttributes(null);} } /**** * */ class DecoratorBuilderStateOp extends DecoratorAttributes{ private readonly up: DecoratorAttributes; constructor(up: DecoratorAttributes) { super(up.prop); this.up = up; } get():propertiesDescriptor {return this.up.get();} setProp(prop: propertiesDescriptor): void { this.up.setProp(prop);} setName(value: string): void {this.up.setName(value);} getName(): string {return this.up.getName();} getPropertyKey(): string {return this.up.getPropertyKey();} setProperty(supplier: supplier): void {this.up.setProperty(supplier);} setDecorator(decorator: string): AttributeProperties {this.up.setDecorator(decorator);return this;} getDecorator(): string { return this.up.getDecorator();} }