Skip to content

Commit

Permalink
fix(core): 优化响应式存储
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Jun 11, 2022
1 parent d9778f6 commit b307f74
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/core/src/core/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@ export function createReactive<T extends object>(

/** 创建响应式监听器 */
function createWatcher(key: string, target: any) {
return watch(() => cloneDeep(target), debounce((old, _new) => {
// 声明cache变量,便于匹配是否有循环引用的情况
const cache: any[] = [];

return watch(() => cloneDeep(target), debounce((_new) => {
// eslint-disable-next-line no-undef
GM_setValue(key, JSON.parse(JSON.stringify(old)));
GM_setValue(key, JSON.parse(JSON.stringify(_new, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// 移除
return;
}
// 收集所有的值
cache.push(value);
}
return value;
})));
}, 100), { deep: true });
}

Expand Down

0 comments on commit b307f74

Please sign in to comment.