Data.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { DataSystem } from "./DataSystem";
  2. /**
  3. * 数据对象
  4. * @description 请通过DataSystem.createData来实例化对象,否则将无法使用DataSystem.watch
  5. * @author 袁浩
  6. */
  7. export class Data {
  8. protected proxy: any;
  9. protected arrayKey: Map<any, PropertyKey> = new Map<any, PropertyKey>();
  10. public constructor() {
  11. return this.initProxy();
  12. }
  13. protected initProxy() {
  14. this.proxy = new Proxy(this, { set: this.setProperty.bind(this), deleteProperty: this.deleteProperty.bind(this) });
  15. return this.proxy;
  16. }
  17. protected setProperty<T>(target: T, p: PropertyKey, value: any, receiver: any): boolean {
  18. target[p] = value;
  19. if (Array.isArray(value)) {
  20. let proxy = new Proxy(value, { set: this.setArrayProperty.bind(this) });
  21. target[p] = proxy;
  22. this.arrayKey.set(proxy, p);
  23. }
  24. p != 'proxy' && DataSystem.onPropertyChange(this.proxy, p);
  25. return true;
  26. }
  27. protected setArrayProperty<T>(target: T, p: PropertyKey, value: any, receiver: any): boolean {
  28. target[p] = value;
  29. if (this.arrayKey.has(receiver)) {
  30. DataSystem.onPropertyChange(this.proxy, this.arrayKey.get(receiver));
  31. }
  32. return true;
  33. }
  34. protected deleteProperty<T>(target: T, p: PropertyKey): boolean {
  35. delete target[p];
  36. DataSystem.onPropertyChange(this, p);
  37. return true;
  38. }
  39. /**
  40. * 设值
  41. * @param key
  42. * @param value
  43. */
  44. public set(key: PropertyKey, value: any) {
  45. this[key] = value;
  46. }
  47. /**
  48. * 取值
  49. * @param key
  50. * @returns
  51. */
  52. public get(key: PropertyKey): any {
  53. return this[key];
  54. }
  55. /**
  56. * 判断是否有属性
  57. * @param key
  58. * @returns
  59. */
  60. public has(key: PropertyKey): boolean {
  61. return this[key] !== undefined;
  62. }
  63. /**
  64. * 拷贝数据
  65. * @param source 数据源
  66. */
  67. public copy(source: any, deleteOld: boolean = false) {
  68. if (deleteOld) {
  69. let obj = this.object;
  70. for (let key in obj) {
  71. if (source[key] === undefined) {
  72. delete this[key];
  73. }
  74. }
  75. }
  76. for (let key in source) {
  77. this.copyOne(key, source);
  78. }
  79. }
  80. protected copyOne(key: string, source: any) {
  81. this[key] = source[key];
  82. }
  83. /**
  84. * 基础数据对象
  85. */
  86. public get object() {
  87. var result: any = {};
  88. for (var key in this) {
  89. let value = this[key];
  90. if (
  91. key != "object" &&
  92. key != "proxy" &&
  93. key != "arrayKey" &&
  94. typeof this[key] != "function"
  95. ) {
  96. if (value instanceof Data) {
  97. result[key] = value.object;
  98. }
  99. else {
  100. result[key] = value;
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. private toJSON() {
  107. return JSON.stringify(this.object);
  108. }
  109. }