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