告别本地缓存,不方便问题

不多说上代码
LocalStorageModelComp.ts
`
import { app } from ‘db://app-game/core/app’;

import { ecs } from ‘db://app-game/libs/ecs/ECS’;

import { smc } from ‘…/…/common/ecs/SingletonModuleComp’;

import { GameSaveData } from ‘…/LocalStorageManager.type’;

/**

  • 本地缓存模型组件 - 支持深度嵌套属性监听

*/

@ecs.register(‘LSM’)

export class LocalStorageModelComp extends ecs.Comp {

/** 存档槽位 */

public slot: number = 0;

public initData: GameSaveData = {

player: {

  basic: {

    uid: `xiaxing`,

    avatar: 'default',

    nickname: '夏星',

  },

  scene: {

    guanKai: 1,

  },

  bag: {},

},

buildingInfo: {

  buildings: {},

},

};

// 用于缓存代理对象的WeakMap

private proxyCache = new WeakMap();

// 创建深度代理

public createDeepProxy(target: any, path: string = ‘’): any {

// 如果不是对象,直接返回

if (typeof target !== 'object' || target === null) {

  return target;

}

// 检查缓存中是否已有代理

if (this.proxyCache.has(target)) {

  return this.proxyCache.get(target);

}

// 处理数组情况

if (Array.isArray(target)) {

  const arrayProxy = new Proxy(target, {

    get: (obj, prop) => {

      // 处理数组方法

      if (prop === 'push') {

        return (...args: any[]) => {

          app.log.trace(`数组操作: ${path}.push(${args.map(a => JSON.stringify(a)).join(', ')})`);

          const result = Array.prototype.push.apply(obj, args);

          smc.storage.refresh();

          return result;

        };

      }

      if (prop === 'pop') {

        return () => {

          const value = obj[obj.length - 1];

          app.log.trace(`数组操作: ${path}.pop(), 值: ${JSON.stringify(value)}`);

          const result = Array.prototype.pop.call(obj);

          smc.storage.refresh();

          return result;

        };

      }

      if (prop === 'splice') {

        return (...args: any[]) => {

          app.log.trace(`数组操作: ${path}.splice(${args.join(', ')})`);

          const result = Array.prototype.splice.apply(obj, args);

          smc.storage.refresh();

          return result;

        };

      }

      // 普通属性获取

      const value = Reflect.get(obj, prop);

      if (typeof prop === 'string' && !isNaN(parseInt(prop))) {

        const fullPath = `${path}[${prop}]`;

        app.log.trace(`正在获取数组元素:${fullPath}`);

      }

      // 如果值是对象,返回其代理版本

      if (typeof value === 'object' && value !== null) {

        const elementPath = `${path}[${String(prop)}]`;

        return this.createDeepProxy(value, elementPath);

      }

      return value;

    },

    set: (obj, prop, value) => {

      const oldValue = obj[prop];

      const fullPath = `${path}[${String(prop)}]`;

      const result = Reflect.set(obj, prop, value);

      app.log.trace(

        `正在设置数组元素:${fullPath}, 旧值:${JSON.stringify(oldValue)}, 新值:${JSON.stringify(value)}`

      );

      smc.storage.refresh();

      return result;

    },

  });

  this.proxyCache.set(target, arrayProxy);

  return arrayProxy;

}

// 创建对象代理

const proxy = new Proxy(target, {

  get: (obj, prop) => {

    const value = Reflect.get(obj, prop);

    const fullPath = path ? `${path}.${String(prop)}` : String(prop);

    app.log.trace(`正在获取对象属性:${fullPath}`);

    // 如果值是对象,返回其代理版本

    if (typeof value === 'object' && value !== null) {

      return this.createDeepProxy(value, fullPath);

    }

    return value;

  },

  set: (obj, prop, value) => {

    const fullPath = path ? `${path}.${String(prop)}` : String(prop);

    const oldValue = obj[prop];

    const result = Reflect.set(obj, prop, value);

    app.log.trace(`正在设置对象属性:${fullPath}, 旧值:${JSON.stringify(oldValue)}, 新值:${JSON.stringify(value)}`);

    smc.storage.refresh();

    return result;

  },

  deleteProperty: (obj, prop) => {

    const fullPath = path ? `${path}.${String(prop)}` : String(prop);

    const oldValue = obj[prop];

    const result = Reflect.deleteProperty(obj, prop);

    app.log.trace(`正在删除对象属性:${fullPath}, 旧值:${JSON.stringify(oldValue)}`);

    smc.storage.refresh();

    return result;

  },

});

// 缓存代理

this.proxyCache.set(target, proxy);

return proxy;

}

public data: GameSaveData = null;

public autoSaveTimer: number = 0;

reset(): void {

this.data = null;

this.proxyCache = new WeakMap();

}

}
`
在刷新的这个smc.storage.refresh();
里面写整体的数据保存就行

以后触发更改initData就会自动去触发刷新