Skip to content

enhance: MemoCache.query() and MemoCache.buildQueryKey() take state as one argument #3454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .changeset/poor-jobs-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'@data-client/normalizr': minor
---

BREAKING CHANGE: MemoCache.query() and MemoCache.buildQueryKey() take state as one argument

#### Before

```ts
this.memo.buildQueryKey(
schema,
args,
state.entities,
state.indexes,
key,
);
```


#### After

```ts
this.memo.buildQueryKey(
schema,
args,
state,
key,
);
```

#### Before

```ts
this.memo.query(schema, args, state.entities, state.indexes);
```

#### After

```ts
this.memo.query(schema, args, state);
```
28 changes: 4 additions & 24 deletions examples/benchmark/normalizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ import userData from './user.json' with { type: 'json' };
const { result, entities } = normalize(ProjectSchema, data);
const queryState = normalize(AllProjects, data);
const queryMemo = new MemoCache();
queryState.result = queryMemo.buildQueryKey(
AllProjects,
[],
queryState.entities,
queryState.indexes,
);
const queryInfer = queryMemo.buildQueryKey(
getSortedProjects,
[],
queryState.entities,
queryState.indexes,
);
queryState.result = queryMemo.buildQueryKey(AllProjects, [], queryState);
const queryInfer = queryMemo.buildQueryKey(getSortedProjects, [], queryState);

let githubState = normalize(User, userData);

Expand All @@ -56,12 +46,7 @@ export default function addNormlizrSuite(suite) {
curState = { ...initialState, entities: {}, endpoints: {} };
})
.add('infer All', () => {
return new MemoCache().buildQueryKey(
AllProjects,
[],
queryState.entities,
queryState.indexes,
);
return new MemoCache().buildQueryKey(AllProjects, [], queryState);
})
.add('denormalizeLong', () => {
return new MemoCache().denormalize(ProjectSchema, result, entities);
Expand All @@ -86,12 +71,7 @@ export default function addNormlizrSuite(suite) {
})
.add('queryShort 500x withCache', () => {
for (let i = 0; i < 500; ++i) {
memo.query(
User,
[{ login: 'gnoff' }],
githubState.entities,
githubState.indexes,
);
memo.query(User, [{ login: 'gnoff' }], githubState);
}
})
.add('denormalizeLong with mixin Entity', () => {
Expand Down
13 changes: 3 additions & 10 deletions packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,7 @@ export default class Controller<
const input =
shouldQuery ?
// nothing in endpoints cache, so try querying if we have a schema to do so
this.memo.buildQueryKey(
schema,
args,
state.entities as any,
state.indexes,
key,
)
this.memo.buildQueryKey(schema, args, state, key)
: cacheEndpoints;

if (!isActive) {
Expand Down Expand Up @@ -595,7 +589,7 @@ export default class Controller<
.slice(0, rest.length - 1)
.map(ensurePojo) as SchemaArgs<S>;

return this.memo.query(schema, args, state.entities as any, state.indexes);
return this.memo.query(schema, args, state);
}

/**
Expand Down Expand Up @@ -623,8 +617,7 @@ export default class Controller<
const input = this.memo.buildQueryKey(
schema,
args,
state.entities as any,
state.indexes,
state,
JSON.stringify(args),
);

Expand Down
45 changes: 9 additions & 36 deletions packages/endpoint/src/schemas/__tests__/All.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ describe.each([
indexes: {},
});
// use memocache because we don't support 'object' schemas in controller yet
expect(
new MemoCache().query(catSchema, [], state.entities, state.indexes),
).toMatchSnapshot();
expect(new MemoCache().query(catSchema, [], state)).toMatchSnapshot();
});

test('denormalizes nested in object with primitive', () => {
Expand All @@ -152,12 +150,7 @@ describe.each([
},
indexes: {},
});
const value = new MemoCache().query(
catSchema,
[],
state.entities,
state.indexes,
);
const value = new MemoCache().query(catSchema, [], state);
expect(value).not.toEqual(expect.any(Symbol));
if (typeof value === 'symbol' || value === undefined) return;
expect(createOutput(value.results)).toMatchSnapshot();
Expand All @@ -178,12 +171,7 @@ describe.each([
},
indexes: {},
});
const value = new MemoCache().query(
catSchema,
[],
state.entities,
state.indexes,
);
const value = new MemoCache().query(catSchema, [], state);
expect(value).not.toEqual(expect.any(Symbol));
if (typeof value === 'symbol' || value === undefined) return;
expect(createOutput(value.results).length).toBe(2);
Expand All @@ -206,11 +194,11 @@ describe.each([
indexes: {},
};
const memo = new MemoCache();
const value = memo.query(catSchema, [], state.entities, state.indexes);
const value = memo.query(catSchema, [], state);

expect(createOutput(value).results?.length).toBe(2);
expect(createOutput(value).results).toMatchSnapshot();
const value2 = memo.query(catSchema, [], state.entities, state.indexes);
const value2 = memo.query(catSchema, [], state);
expect(createOutput(value).results[0]).toBe(
createOutput(value2).results[0],
);
Expand All @@ -226,7 +214,7 @@ describe.each([
},
},
};
const value3 = memo.query(catSchema, [], state.entities, state.indexes);
const value3 = memo.query(catSchema, [], state);
expect(createOutput(value3).results?.length).toBe(3);
expect(createOutput(value3).results).toMatchSnapshot();
expect(createOutput(value).results[0]).toBe(
Expand All @@ -250,12 +238,7 @@ describe.each([
},
indexes: {},
});
const value = new MemoCache().query(
catSchema,
[],
state.entities,
state.indexes,
);
const value = new MemoCache().query(catSchema, [], state);
expect(createOutput(value)).toBeUndefined();
});

Expand Down Expand Up @@ -287,12 +270,7 @@ describe.each([
},
indexes: {},
});
const value = new MemoCache().query(
listSchema,
[],
state.entities,
state.indexes,
);
const value = new MemoCache().query(listSchema, [], state);
expect(createOutput(value)).toBeUndefined();
});

Expand Down Expand Up @@ -355,12 +333,7 @@ describe.each([
},
indexes: {},
});
const value = new MemoCache().query(
listSchema,
[],
state.entities,
state.indexes,
);
const value = new MemoCache().query(listSchema, [], state);
expect(value).not.toEqual(expect.any(Symbol));
if (typeof value === 'symbol') return;
expect(value).toMatchSnapshot();
Expand Down
9 changes: 3 additions & 6 deletions packages/endpoint/src/schemas/__tests__/Collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
const queryKey = memo.buildQueryKey(
userTodos,
[{ userId: '1' }],
normalizeNested.entities,
normalizeNested.indexes,
normalizeNested,
);
expect(queryKey).toBeDefined();
// now ensure our queryKey is usable
Expand All @@ -644,8 +643,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
const queryKey = memo.buildQueryKey(
userTodos,
[{ userId: '100' }],
normalizeNested.entities,
normalizeNested.indexes,
normalizeNested,
);
expect(queryKey).toBeUndefined();
});
Expand All @@ -655,8 +653,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
const queryKey = memo.buildQueryKey(
User.schema.todos,
[{ userId: '1' }],
normalizeNested.entities,
normalizeNested.indexes,
normalizeNested,
);
expect(queryKey).toBeUndefined();
});
Expand Down
Loading