NetBase.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**网络变量基类*/
  2. export class NetBase {
  3. private _id: string;
  4. public get Id(): string { return this._id };
  5. private dataMap: { [key: string]: any } = {};
  6. private dirtyMap: { [key: string]: any } = {};
  7. constructor(id: string) {
  8. this._id = id;
  9. this.dataMap = {};
  10. }
  11. /**初始变量值*/
  12. public initValue(data: { [key: string]: any }): void {
  13. this.dataMap = data ?? {};
  14. }
  15. /**修改某个键的值*/
  16. protected setValue(key: string, value: any): void {
  17. this.dataMap[key] = value;
  18. }
  19. protected setValueDirty(key: string, value: any): void {
  20. this.dirtyMap[key] = value;
  21. }
  22. /**获取数据的值*/
  23. protected getValue(key: string): any {
  24. return this.dataMap[key];
  25. }
  26. /**处理脏数据*/
  27. private doDirtyData(f: (data: { [key: string]: any }) => void): void {
  28. if (Object.keys(this.dirtyMap).length === 0) return null;
  29. f(this.dirtyMap);
  30. this.dirtyMap = {};
  31. }
  32. protected dispose(): void {
  33. this.dataMap = {};
  34. this.dirtyMap = {};
  35. }
  36. }