需求是针对被观察的变量和普通变量,实现自动监听,只要该变量发生值的变化,自动存储到缓存,目前是这样实现的,但是拿不到需要的数据,控制台打印出来的数据和实际拿到的数据不一致,苦于没有实现思路,所以想请教下大佬如何实现一个自动持久化存储的 storage.js ?
Mobx Version:5.13.1
storage.sync.observable = action(function (object, attr, key) {
console.warn('`storage.sync.observable` is deprecated, use `storage.sync` instead');
const value = storage.getItem(key);
console.log(111, object, attr, key);
if (!(Object.prototype.toString.call(value) == '[object Null]' || Object.prototype.toString.call(value) == '[object Undefined]')) object[attr] = value;
autorun(() => {
console.log(222, object, attr, key);
storage.setItem(key, object[attr])
});
});
Mobx Version:5.13.1
storage.sync.observable = action(function (object, attr, key) {
console.warn('`storage.sync.observable` is deprecated, use `storage.sync` instead');
const value = storage.getItem(key);
console.log(111, object, attr, key);
if (!(Object.prototype.toString.call(value) == '[object Null]' || Object.prototype.toString.call(value) == '[object Undefined]')) object[attr] = value;
autorun(() => {
console.log(222, object, attr, key);
storage.setItem(key, object[attr])
});
});