Skip to content

Commit 66e1906

Browse files
authored
enhance: MemoCache.query() and MemoCache.buildQueryKey() take state as one argument (#3454)
1 parent 8e0bba2 commit 66e1906

File tree

11 files changed

+208
-327
lines changed

11 files changed

+208
-327
lines changed

.changeset/poor-jobs-turn.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
'@data-client/normalizr': minor
3+
---
4+
5+
BREAKING CHANGE: MemoCache.query() and MemoCache.buildQueryKey() take state as one argument
6+
7+
#### Before
8+
9+
```ts
10+
this.memo.buildQueryKey(
11+
schema,
12+
args,
13+
state.entities,
14+
state.indexes,
15+
key,
16+
);
17+
```
18+
19+
20+
#### After
21+
22+
```ts
23+
this.memo.buildQueryKey(
24+
schema,
25+
args,
26+
state,
27+
key,
28+
);
29+
```
30+
31+
#### Before
32+
33+
```ts
34+
this.memo.query(schema, args, state.entities, state.indexes);
35+
```
36+
37+
#### After
38+
39+
```ts
40+
this.memo.query(schema, args, state);
41+
```

examples/benchmark/normalizr.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,8 @@ import userData from './user.json' with { type: 'json' };
2121
const { result, entities } = normalize(ProjectSchema, data);
2222
const queryState = normalize(AllProjects, data);
2323
const queryMemo = new MemoCache();
24-
queryState.result = queryMemo.buildQueryKey(
25-
AllProjects,
26-
[],
27-
queryState.entities,
28-
queryState.indexes,
29-
);
30-
const queryInfer = queryMemo.buildQueryKey(
31-
getSortedProjects,
32-
[],
33-
queryState.entities,
34-
queryState.indexes,
35-
);
24+
queryState.result = queryMemo.buildQueryKey(AllProjects, [], queryState);
25+
const queryInfer = queryMemo.buildQueryKey(getSortedProjects, [], queryState);
3626

3727
let githubState = normalize(User, userData);
3828

@@ -56,12 +46,7 @@ export default function addNormlizrSuite(suite) {
5646
curState = { ...initialState, entities: {}, endpoints: {} };
5747
})
5848
.add('infer All', () => {
59-
return new MemoCache().buildQueryKey(
60-
AllProjects,
61-
[],
62-
queryState.entities,
63-
queryState.indexes,
64-
);
49+
return new MemoCache().buildQueryKey(AllProjects, [], queryState);
6550
})
6651
.add('denormalizeLong', () => {
6752
return new MemoCache().denormalize(ProjectSchema, result, entities);
@@ -86,12 +71,7 @@ export default function addNormlizrSuite(suite) {
8671
})
8772
.add('queryShort 500x withCache', () => {
8873
for (let i = 0; i < 500; ++i) {
89-
memo.query(
90-
User,
91-
[{ login: 'gnoff' }],
92-
githubState.entities,
93-
githubState.indexes,
94-
);
74+
memo.query(User, [{ login: 'gnoff' }], githubState);
9575
}
9676
})
9777
.add('denormalizeLong with mixin Entity', () => {

packages/core/src/controller/Controller.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,7 @@ export default class Controller<
514514
const input =
515515
shouldQuery ?
516516
// nothing in endpoints cache, so try querying if we have a schema to do so
517-
this.memo.buildQueryKey(
518-
schema,
519-
args,
520-
state.entities as any,
521-
state.indexes,
522-
key,
523-
)
517+
this.memo.buildQueryKey(schema, args, state, key)
524518
: cacheEndpoints;
525519

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

598-
return this.memo.query(schema, args, state.entities as any, state.indexes);
592+
return this.memo.query(schema, args, state);
599593
}
600594

601595
/**
@@ -623,8 +617,7 @@ export default class Controller<
623617
const input = this.memo.buildQueryKey(
624618
schema,
625619
args,
626-
state.entities as any,
627-
state.indexes,
620+
state,
628621
JSON.stringify(args),
629622
);
630623

packages/endpoint/src/schemas/__tests__/All.test.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ describe.each([
135135
indexes: {},
136136
});
137137
// use memocache because we don't support 'object' schemas in controller yet
138-
expect(
139-
new MemoCache().query(catSchema, [], state.entities, state.indexes),
140-
).toMatchSnapshot();
138+
expect(new MemoCache().query(catSchema, [], state)).toMatchSnapshot();
141139
});
142140

143141
test('denormalizes nested in object with primitive', () => {
@@ -152,12 +150,7 @@ describe.each([
152150
},
153151
indexes: {},
154152
});
155-
const value = new MemoCache().query(
156-
catSchema,
157-
[],
158-
state.entities,
159-
state.indexes,
160-
);
153+
const value = new MemoCache().query(catSchema, [], state);
161154
expect(value).not.toEqual(expect.any(Symbol));
162155
if (typeof value === 'symbol' || value === undefined) return;
163156
expect(createOutput(value.results)).toMatchSnapshot();
@@ -178,12 +171,7 @@ describe.each([
178171
},
179172
indexes: {},
180173
});
181-
const value = new MemoCache().query(
182-
catSchema,
183-
[],
184-
state.entities,
185-
state.indexes,
186-
);
174+
const value = new MemoCache().query(catSchema, [], state);
187175
expect(value).not.toEqual(expect.any(Symbol));
188176
if (typeof value === 'symbol' || value === undefined) return;
189177
expect(createOutput(value.results).length).toBe(2);
@@ -206,11 +194,11 @@ describe.each([
206194
indexes: {},
207195
};
208196
const memo = new MemoCache();
209-
const value = memo.query(catSchema, [], state.entities, state.indexes);
197+
const value = memo.query(catSchema, [], state);
210198

211199
expect(createOutput(value).results?.length).toBe(2);
212200
expect(createOutput(value).results).toMatchSnapshot();
213-
const value2 = memo.query(catSchema, [], state.entities, state.indexes);
201+
const value2 = memo.query(catSchema, [], state);
214202
expect(createOutput(value).results[0]).toBe(
215203
createOutput(value2).results[0],
216204
);
@@ -226,7 +214,7 @@ describe.each([
226214
},
227215
},
228216
};
229-
const value3 = memo.query(catSchema, [], state.entities, state.indexes);
217+
const value3 = memo.query(catSchema, [], state);
230218
expect(createOutput(value3).results?.length).toBe(3);
231219
expect(createOutput(value3).results).toMatchSnapshot();
232220
expect(createOutput(value).results[0]).toBe(
@@ -250,12 +238,7 @@ describe.each([
250238
},
251239
indexes: {},
252240
});
253-
const value = new MemoCache().query(
254-
catSchema,
255-
[],
256-
state.entities,
257-
state.indexes,
258-
);
241+
const value = new MemoCache().query(catSchema, [], state);
259242
expect(createOutput(value)).toBeUndefined();
260243
});
261244

@@ -287,12 +270,7 @@ describe.each([
287270
},
288271
indexes: {},
289272
});
290-
const value = new MemoCache().query(
291-
listSchema,
292-
[],
293-
state.entities,
294-
state.indexes,
295-
);
273+
const value = new MemoCache().query(listSchema, [], state);
296274
expect(createOutput(value)).toBeUndefined();
297275
});
298276

@@ -355,12 +333,7 @@ describe.each([
355333
},
356334
indexes: {},
357335
});
358-
const value = new MemoCache().query(
359-
listSchema,
360-
[],
361-
state.entities,
362-
state.indexes,
363-
);
336+
const value = new MemoCache().query(listSchema, [], state);
364337
expect(value).not.toEqual(expect.any(Symbol));
365338
if (typeof value === 'symbol') return;
366339
expect(value).toMatchSnapshot();

packages/endpoint/src/schemas/__tests__/Collection.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
620620
const queryKey = memo.buildQueryKey(
621621
userTodos,
622622
[{ userId: '1' }],
623-
normalizeNested.entities,
624-
normalizeNested.indexes,
623+
normalizeNested,
625624
);
626625
expect(queryKey).toBeDefined();
627626
// now ensure our queryKey is usable
@@ -644,8 +643,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
644643
const queryKey = memo.buildQueryKey(
645644
userTodos,
646645
[{ userId: '100' }],
647-
normalizeNested.entities,
648-
normalizeNested.indexes,
646+
normalizeNested,
649647
);
650648
expect(queryKey).toBeUndefined();
651649
});
@@ -655,8 +653,7 @@ describe(`${schema.Collection.name} denormalization`, () => {
655653
const queryKey = memo.buildQueryKey(
656654
User.schema.todos,
657655
[{ userId: '1' }],
658-
normalizeNested.entities,
659-
normalizeNested.indexes,
656+
normalizeNested,
660657
);
661658
expect(queryKey).toBeUndefined();
662659
});

0 commit comments

Comments
 (0)