Data.ts 834 B

123456789101112131415161718192021
  1. /**
  2. * 数据对象
  3. * 通过DataSystem.createData来实例化对象,否则将无法使用DataSystem.watch
  4. */
  5. export class Data {
  6. onPropertyChange: (key: PropertyKey, value: any) => void;
  7. thisObject: any;
  8. public constructor(onPropertyChange: (key: PropertyKey, value: any) => void, thisObject: any) {
  9. this.onPropertyChange = onPropertyChange;
  10. this.thisObject = thisObject;
  11. var p = new Proxy(this, { set: this.setProperty.bind(this), deleteProperty: this.deleteProperty.bind(this) });
  12. return p;
  13. }
  14. protected setProperty<T>(target: T, p: PropertyKey, value: any, receiver: any): boolean {
  15. this.onPropertyChange.call(this.thisObject, this, p, value);
  16. return true;
  17. }
  18. private deleteProperty<T>(target: T, p: PropertyKey): boolean {
  19. return true;
  20. }
  21. }