Skip to content

Commit

Permalink
fix(computed): update array data fail to re-calculate used fields
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 14, 2023
1 parent 367d6dd commit 22d7a1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/reactive/ObjectDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ export class ObjectDeps<T = any> implements Deps {
}

protected proxy(currentState: Record<string, any>): any {
if (currentState === null || !isObject<Record<string, any>>(currentState)) {
if (
currentState === null ||
!isObject<Record<string, any>>(currentState) ||
Array.isArray(currentState)
) {
return currentState;
}

const nextState: object | any[] = Array.isArray(currentState) ? [] : {};
const nextState = {};
const keys = Object.keys(currentState);
const currentDeps = this.deps.slice();
let visited = false;
Expand Down
34 changes: 34 additions & 0 deletions test/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,37 @@ test('complex parameter can not hit cache', () => {
expect(spy).toBeCalledTimes(2);
spy.mockRestore();
});

test('array should always be deps', () => {
const spy = vitest.fn();

const model = defineModel('computed-from-array', {
initialState: {
x: [{ foo: 'bar' } as { foo: string; other?: string }],
y: {},
},
reducers: {
update(state, other: string) {
state.x = [{ foo: 'bar' }, { foo: 'baz', other }];
},
},
computed: {
myData() {
spy();
return this.state.x.filter((item) => item.foo === 'bar');
},
},
});

model.myData();
expect(spy).toBeCalledTimes(1);
model.update('baz');
model.myData();
expect(spy).toBeCalledTimes(2);
model.update('x');
model.myData();
expect(spy).toBeCalledTimes(3);
model.update('y');
model.myData();
expect(spy).toBeCalledTimes(4);
});

0 comments on commit 22d7a1c

Please sign in to comment.