Skip to content

Commit

Permalink
Fix updateNodes not notifying when assigning
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Sep 21, 2023
1 parent 47e10c5 commit 1dad4df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function updateNodes(parent: NodeValue, obj: Record<any, any> | Array<any> | und
}
}

if (obj && !isPrimitive(obj) && !parent.lazy) {
if (obj && !parent.lazy && !isPrimitive(obj)) {
hasADiff = hasADiff || length !== lengthPrev;
const isArrDiff = hasADiff;
let didMove = false;
Expand Down Expand Up @@ -556,6 +556,12 @@ function setKey(node: NodeValue, key: string, newValue?: any, level?: number) {
// Get the child node for updating and notifying
const childNode: NodeValue = isRoot ? node : getChildNode(node, key);

if (childNode.lazy) {
// Make sure the node is activated when assigning because the node
// is never accessed through the proxy
peek(childNode);
}

// Set the raw value on the parent object
const { newValue: savedValue, prevValue, parentValue } = setNodeValue(childNode, newValue);

Expand Down
14 changes: 14 additions & 0 deletions tests/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,20 @@ describe('Listeners', () => {
{ path: ['test'], pathTypes: ['object'], valueAtPath: { test2: 'hi' }, prevAtPath: undefined },
]);
});
test('Start undefined assign something', () => {
interface Data {
test?: undefined | { test2: string };
}
const obs = observable<Data>({});
const handler = expectChangeHandler(obs);

obs.assign({ test: { test2: 'hi' } });

// TODO: The previous value here is not totally correct because it filled out the path to make set work
expect(handler).toHaveBeenCalledWith({ test: { test2: 'hi' } }, { test: undefined }, [
{ path: ['test'], pathTypes: ['object'], valueAtPath: { test2: 'hi' }, prevAtPath: undefined },
]);
});
test('Set with object should only fire listeners once', () => {
interface Data {
test: undefined | Record<string, string>;
Expand Down

0 comments on commit 1dad4df

Please sign in to comment.