Skip to content

Commit

Permalink
Check for observer being undefined in notifyPaths function
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel committed Feb 25, 2024
1 parent ef31bd8 commit 03870d7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib",
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
}
}
8 changes: 5 additions & 3 deletions src/hooks/useObservers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const useObservers = <T>(): ObserversControl<T> => {
observersMap.current.set(path, new ObserverArray());
}

return observersMap.current.get(path).add(observer as Observer<unknown>);
return observersMap.current.get(path)!.add(observer as Observer<unknown>);
}, []);

const stopObserving = useCallback(<V>(path: Pxth<V>, observerKey: ObserverKey) => {
Expand Down Expand Up @@ -90,8 +90,10 @@ export const useObservers = <T>(): ObserversControl<T> => {
batchUpdate({ paths, origin, values });
paths.forEach((path) => {
const observer = observersMap.current.get(path);
const subValue = deepGet(values, path);
observer.call(subValue);
if (observer) {
const subValue = deepGet(values, path);
observer.call(subValue);
}
});
},
[batchUpdate],
Expand Down
2 changes: 1 addition & 1 deletion src/typings/PxthMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class PxthMap<Value> {
private _values: Record<string, Value> = {};
private _keys: Record<string, Pxth<unknown>> = {};

public get = <V>(key: Pxth<V>): Value => {
public get = <V>(key: Pxth<V>): Value | undefined => {
return this._values[hashPxth(key)];
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/areProxyMapsEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const areProxyMapsEqual = (map1: ProxyMapSource<unknown>, map2: ProxyMapS

for (const [key, value] of proxyMap1.entries()) {
const value2 = proxyMap2.get(key);
if (!proxyMap2.has(key) || !samePxth(value, value2)) {
if (!proxyMap2.has(key) || !samePxth(value, value2!)) {
return false;
}
}
Expand Down

0 comments on commit 03870d7

Please sign in to comment.