Match-id-930581f027f9f3c888ded7e7eac3fb5f165bcb4e
This commit is contained in:
parent
dd7b65c45a
commit
914425a545
|
@ -31,9 +31,7 @@ export function getObserver(rawObj: any): Observer {
|
||||||
return rawObj[OBSERVER_KEY];
|
return rawObj[OBSERVER_KEY];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createProxy(rawObj: any, isHookObserver: boolean, listener: { current: (...args) => any }): any {
|
export function createProxy(rawObj: any, listener: { current: (...args) => any }, isHookObserver = true): any {
|
||||||
isHookObserver = isHookObserver || true;
|
|
||||||
|
|
||||||
// 不是对象(是原始数据类型)不用代理
|
// 不是对象(是原始数据类型)不用代理
|
||||||
if (!(rawObj && isObject(rawObj))) {
|
if (!(rawObj && isObject(rawObj))) {
|
||||||
return rawObj;
|
return rawObj;
|
||||||
|
|
|
@ -35,7 +35,7 @@ function set(rawObj: any[], key: string, value: any, receiver: any) {
|
||||||
|
|
||||||
if (!isSame(newValue, oldValue)) {
|
if (!isSame(newValue, oldValue)) {
|
||||||
// 值不一样,触发监听器
|
// 值不一样,触发监听器
|
||||||
if (observer.watchers[key]) {
|
if (observer.watchers?.[key]) {
|
||||||
observer.watchers[key].forEach(cb => {
|
observer.watchers[key].forEach(cb => {
|
||||||
cb(key, oldValue, newValue, mutation);
|
cb(key, oldValue, newValue, mutation);
|
||||||
});
|
});
|
||||||
|
@ -97,7 +97,7 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) =
|
||||||
// 对于value也需要进一步代理
|
// 对于value也需要进一步代理
|
||||||
const valProxy = singleLevel
|
const valProxy = singleLevel
|
||||||
? value
|
? value
|
||||||
: createProxy(value, hookObserverMap.get(rawObj), {
|
: createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
|
@ -108,7 +108,9 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) =
|
||||||
listener.current(mutation);
|
listener.current(mutation);
|
||||||
listeners.forEach(lst => lst(mutation));
|
listeners.forEach(lst => lst(mutation));
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
|
|
||||||
return valProxy;
|
return valProxy;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,18 +38,20 @@ export function createMapProxy(
|
||||||
const value = rawObj.get(keyProxy);
|
const value = rawObj.get(keyProxy);
|
||||||
|
|
||||||
// 对于value也需要进一步代理
|
// 对于value也需要进一步代理
|
||||||
const valProxy = createProxy(value, hookObserverMap.get(rawObj), {
|
const valProxy = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, [key]: change.mutation.from },
|
{ ...rawObj, [key]: change.mutation.from },
|
||||||
{ ...rawObj, [key]: change.mutation.to }
|
{ ...rawObj, [key]: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
|
|
||||||
return valProxy;
|
return valProxy;
|
||||||
}
|
}
|
||||||
|
@ -84,19 +86,21 @@ export function createMapProxy(
|
||||||
oldData = [...Array.from(rawObj.entries())];
|
oldData = [...Array.from(rawObj.entries())];
|
||||||
} else {
|
} else {
|
||||||
// NEW VALUE
|
// NEW VALUE
|
||||||
const keyProxy = createProxy(key, hookObserverMap.get(rawObj), {
|
const keyProxy = createProxy(key, {
|
||||||
current: change => {
|
current: change => {
|
||||||
// KEY CHANGE
|
// KEY CHANGE
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, ['_keyChange']: change.mutation.from },
|
{ ...rawObj, ['_keyChange']: change.mutation.from },
|
||||||
{ ...rawObj, ['_keyChange']: change.mutation.to }
|
{ ...rawObj, ['_keyChange']: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
proxies.set(key, keyProxy);
|
proxies.set(key, keyProxy);
|
||||||
|
|
||||||
rawObj.set(keyProxy, value);
|
rawObj.set(keyProxy, value);
|
||||||
|
@ -180,32 +184,36 @@ export function createMapProxy(
|
||||||
const observer = getObserver(rawObj);
|
const observer = getObserver(rawObj);
|
||||||
observer.useProp(COLLECTION_CHANGE);
|
observer.useProp(COLLECTION_CHANGE);
|
||||||
rawObj.forEach((value, key) => {
|
rawObj.forEach((value, key) => {
|
||||||
const keyProxy = createProxy(value, hookObserverMap.get(rawObj), {
|
const keyProxy = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
//KEY ATTRIBUTES CHANGED
|
//KEY ATTRIBUTES CHANGED
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, ['_keyChange']: change.mutation.from },
|
{ ...rawObj, ['_keyChange']: change.mutation.from },
|
||||||
{ ...rawObj, ['_keyChange']: change.mutation.to }
|
{ ...rawObj, ['_keyChange']: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
const valProxy = createProxy(key, hookObserverMap.get(rawObj), {
|
);
|
||||||
current: change => {
|
const valProxy = createProxy(key, {
|
||||||
// VALUE ATTRIBUTE CHANGED
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
// VALUE ATTRIBUTE CHANGED
|
||||||
change.parents.push(rawObj);
|
if (!change.parents) change.parents = [];
|
||||||
let mutation = resolveMutation(
|
change.parents.push(rawObj);
|
||||||
{ ...rawObj, key: change.mutation.from },
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, key: change.mutation.to }
|
{ ...rawObj, key: change.mutation.from },
|
||||||
);
|
{ ...rawObj, key: change.mutation.to }
|
||||||
listener.current({ ...change, mutation });
|
);
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listener.current({ ...change, mutation });
|
||||||
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
// 最后一个参数要返回代理对象
|
// 最后一个参数要返回代理对象
|
||||||
return callback(keyProxy, valProxy, rawObj);
|
return callback(keyProxy, valProxy, rawObj);
|
||||||
});
|
});
|
||||||
|
@ -221,18 +229,20 @@ export function createMapProxy(
|
||||||
const { value, done } = rawIt.next();
|
const { value, done } = rawIt.next();
|
||||||
if (done) {
|
if (done) {
|
||||||
return {
|
return {
|
||||||
value: createProxy(value, hookObserver, {
|
value: createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, [value]: change.mutation.from },
|
{ ...rawObj, [value]: change.mutation.from },
|
||||||
{ ...rawObj, [value]: change.mutation.to }
|
{ ...rawObj, [value]: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}),
|
hookObserver
|
||||||
|
),
|
||||||
done,
|
done,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -242,45 +252,51 @@ export function createMapProxy(
|
||||||
if (type === 'entries') {
|
if (type === 'entries') {
|
||||||
//ENTRY CHANGED
|
//ENTRY CHANGED
|
||||||
newVal = [
|
newVal = [
|
||||||
createProxy(value[0], hookObserver, {
|
createProxy(value[0], {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, ['itemChange']: { key: change.mutation.from, value: value[1] } },
|
{ ...rawObj, ['itemChange']: { key: change.mutation.from, value: value[1] } },
|
||||||
{ ...rawObj, ['itemChange']: { key: change.mutation.to, value: value[1] } }
|
{ ...rawObj, ['itemChange']: { key: change.mutation.to, value: value[1] } }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}),
|
hookObserver
|
||||||
createProxy(value[1], hookObserver, {
|
),
|
||||||
current: change => {
|
createProxy(value[1], {
|
||||||
if (!change.parents) change.parents = [];
|
current: change => {
|
||||||
change.parents.push(rawObj);
|
if (!change.parents) change.parents = [];
|
||||||
let mutation = resolveMutation(
|
change.parents.push(rawObj);
|
||||||
{ ...rawObj, item: { key: value[0], value: change.mutation.from } },
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, item: { key: value[0], value: change.mutation.to } }
|
{ ...rawObj, item: { key: value[0], value: change.mutation.from } },
|
||||||
);
|
{ ...rawObj, item: { key: value[0], value: change.mutation.to } }
|
||||||
listener.current({ ...change, mutation });
|
);
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listener.current({ ...change, mutation });
|
||||||
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}),
|
hookObserver
|
||||||
|
),
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
// SINGLE VALUE CHANGED
|
// SINGLE VALUE CHANGED
|
||||||
newVal = createProxy(value, hookObserver, {
|
newVal = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.from },
|
{ ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.from },
|
||||||
{ ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.to }
|
{ ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserver
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { value: newVal, done };
|
return { value: newVal, done };
|
||||||
|
|
|
@ -29,7 +29,7 @@ function set(rawObj: object, key: string, value: any, receiver: any): boolean {
|
||||||
const mutation = isPanelActive() ? resolveMutation(oldObject, rawObj) : resolveMutation(null, rawObj);
|
const mutation = isPanelActive() ? resolveMutation(oldObject, rawObj) : resolveMutation(null, rawObj);
|
||||||
|
|
||||||
if (!isSame(newValue, oldValue)) {
|
if (!isSame(newValue, oldValue)) {
|
||||||
if (observer.watchers[key]) {
|
if (observer.watchers?.[key]) {
|
||||||
observer.watchers[key].forEach(cb => {
|
observer.watchers[key].forEach(cb => {
|
||||||
cb(key, oldValue, newValue, mutation);
|
cb(key, oldValue, newValue, mutation);
|
||||||
});
|
});
|
||||||
|
@ -87,7 +87,7 @@ export function createObjectProxy<T extends object>(
|
||||||
// 对于value也需要进一步代理
|
// 对于value也需要进一步代理
|
||||||
const valProxy = singleLevel
|
const valProxy = singleLevel
|
||||||
? value
|
? value
|
||||||
: createProxy(value, hookObserverMap.get(rawObj), {
|
: createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
|
@ -98,7 +98,9 @@ export function createObjectProxy<T extends object>(
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
|
|
||||||
return valProxy;
|
return valProxy;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,26 +29,28 @@ export function createSetProxy<T extends object>(
|
||||||
// Set的add方法
|
// Set的add方法
|
||||||
function add(rawObj: { add: (any) => void; has: (any) => boolean; values: () => any[] }, value: any): Object {
|
function add(rawObj: { add: (any) => void; has: (any) => boolean; values: () => any[] }, value: any): Object {
|
||||||
if (!rawObj.has(proxies.get(value))) {
|
if (!rawObj.has(proxies.get(value))) {
|
||||||
const proxy = createProxy(value, hookObserverMap.get(rawObj), {
|
const proxy = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, valueChange: change.mutation.from },
|
{ ...rawObj, valueChange: change.mutation.from },
|
||||||
{ ...rawObj, valueChange: change.mutation.to }
|
{ ...rawObj, valueChange: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({
|
listener.current({
|
||||||
...change,
|
|
||||||
mutation,
|
|
||||||
});
|
|
||||||
listeners.forEach(lst =>
|
|
||||||
lst({
|
|
||||||
...change,
|
...change,
|
||||||
mutation,
|
mutation,
|
||||||
})
|
});
|
||||||
);
|
listeners.forEach(lst =>
|
||||||
|
lst({
|
||||||
|
...change,
|
||||||
|
mutation,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
const oldValues = Array.from(rawObj.values());
|
const oldValues = Array.from(rawObj.values());
|
||||||
|
|
||||||
proxies.set(value, proxy);
|
proxies.set(value, proxy);
|
||||||
|
@ -210,13 +212,13 @@ export function createSetProxy<T extends object>(
|
||||||
};
|
};
|
||||||
const { value, done } = rawIt.next();
|
const { value, done } = rawIt.next();
|
||||||
if (done) {
|
if (done) {
|
||||||
return { value: createProxy(value, hookObserver, currentListener), done };
|
return { value: createProxy(value, currentListener, hookObserver), done };
|
||||||
}
|
}
|
||||||
|
|
||||||
observer.useProp(COLLECTION_CHANGE);
|
observer.useProp(COLLECTION_CHANGE);
|
||||||
|
|
||||||
let newVal;
|
let newVal;
|
||||||
newVal = createProxy(value, hookObserver, currentListener);
|
newVal = createProxy(value, currentListener, hookObserver);
|
||||||
|
|
||||||
return { value: newVal, done };
|
return { value: newVal, done };
|
||||||
},
|
},
|
||||||
|
@ -274,8 +276,8 @@ export function createSetProxy<T extends object>(
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const valProxy = createProxy(value, hookObserverMap.get(rawObj), currentListener);
|
const valProxy = createProxy(value, currentListener, hookObserverMap.get(rawObj));
|
||||||
const keyProxy = createProxy(key, hookObserverMap.get(rawObj), currentListener);
|
const keyProxy = createProxy(key, currentListener, hookObserverMap.get(rawObj));
|
||||||
// 最后一个参数要返回代理对象
|
// 最后一个参数要返回代理对象
|
||||||
return callback(valProxy, keyProxy, rawObj);
|
return callback(valProxy, keyProxy, rawObj);
|
||||||
});
|
});
|
||||||
|
|
|
@ -42,18 +42,20 @@ export function createWeakMapProxy(
|
||||||
|
|
||||||
const value = rawObj.get(key);
|
const value = rawObj.get(key);
|
||||||
// 对于value也需要进一步代理
|
// 对于value也需要进一步代理
|
||||||
const valProxy = createProxy(value, hookObserverMap.get(rawObj), {
|
const valProxy = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, [key]: change.mutation.from },
|
{ ...rawObj, [key]: change.mutation.from },
|
||||||
{ ...rawObj, [key]: change.mutation.to }
|
{ ...rawObj, [key]: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
|
|
||||||
return valProxy;
|
return valProxy;
|
||||||
}
|
}
|
||||||
|
@ -116,7 +118,7 @@ export function createWeakMapProxy(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valChange) {
|
if (valChange) {
|
||||||
if (observer.watchers[key]) {
|
if (observer.watchers?.[key]) {
|
||||||
observer.watchers[key].forEach(cb => {
|
observer.watchers[key].forEach(cb => {
|
||||||
cb(key, oldValue, newValue, mutation);
|
cb(key, oldValue, newValue, mutation);
|
||||||
});
|
});
|
||||||
|
|
|
@ -66,18 +66,20 @@ export function createWeakSetProxy<T extends object>(
|
||||||
// Set的add方法
|
// Set的add方法
|
||||||
function add(rawObj: { add: (any) => void; has: (any) => boolean }, value: any): Object {
|
function add(rawObj: { add: (any) => void; has: (any) => boolean }, value: any): Object {
|
||||||
if (!rawObj.has(proxies.get(value))) {
|
if (!rawObj.has(proxies.get(value))) {
|
||||||
const proxy = createProxy(value, hookObserverMap.get(rawObj), {
|
const proxy = createProxy(value, {
|
||||||
current: change => {
|
current: change => {
|
||||||
if (!change.parents) change.parents = [];
|
if (!change.parents) change.parents = [];
|
||||||
change.parents.push(rawObj);
|
change.parents.push(rawObj);
|
||||||
let mutation = resolveMutation(
|
let mutation = resolveMutation(
|
||||||
{ ...rawObj, [value]: change.mutation.from },
|
{ ...rawObj, [value]: change.mutation.from },
|
||||||
{ ...rawObj, [value]: change.mutation.to }
|
{ ...rawObj, [value]: change.mutation.to }
|
||||||
);
|
);
|
||||||
listener.current({ ...change, mutation });
|
listener.current({ ...change, mutation });
|
||||||
listeners.forEach(lst => lst({ ...change, mutation }));
|
listeners.forEach(lst => lst({ ...change, mutation }));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
hookObserverMap.get(rawObj)
|
||||||
|
);
|
||||||
|
|
||||||
proxies.set(value, proxy);
|
proxies.set(value, proxy);
|
||||||
|
|
||||||
|
|
|
@ -161,7 +161,7 @@ export function createStore<S extends object, A extends UserActions<S>, C extend
|
||||||
current: listener => {},
|
current: listener => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const proxyObj = createProxy(config.state, !config.options?.isReduxAdapter, listener);
|
const proxyObj = createProxy(config.state, listener, !config.options?.isReduxAdapter);
|
||||||
|
|
||||||
proxyObj.$pending = false;
|
proxyObj.$pending = false;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue