Skip to content

Commit

Permalink
enhance: Make argsKey optional in memo.query and memo.buildQueryKey
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jun 27, 2024
1 parent 6a91d7b commit 4a96d27
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 93 deletions.
10 changes: 1 addition & 9 deletions examples/benchmark/normalizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ const { result, entities } = normalize(ProjectSchema, data);
const queryState = normalize(ProjectQuery, data);
const queryMemo = new MemoCache();
queryState.result = queryMemo.buildQueryKey(
'',
ProjectQuery,
[],
queryState.entities,
queryState.indexes,
);
const queryInfer = queryMemo.buildQueryKey(
'',
ProjectQuerySorted,
[],
queryState.entities,
Expand Down Expand Up @@ -57,17 +55,11 @@ export default function addNormlizrSuite(suite) {
let curState = initialState;
return suite
.add('normalizeLong', () => {
normalize(
ProjectSchema,
data,
actionMeta,
curState,
);
normalize(ProjectSchema, data, actionMeta, curState);
curState = { ...initialState, entities: {}, endpoints: {} };
})
.add('infer All', () => {
return new MemoCache().buildQueryKey(
'',
ProjectQuery,
[],
queryState.entities,
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ export default class Controller<
shouldQuery ?
// nothing in endpoints cache, so try querying if we have a schema to do so
this.memo.buildQueryKey(
key,
schema,
args,
state.entities as any,
state.indexes,
key,
)
: cacheEndpoints;

Expand Down Expand Up @@ -500,16 +500,7 @@ export default class Controller<
.slice(0, rest.length - 1)
.map(ensurePojo) as SchemaArgs<S>;

// NOTE: different orders can result in cache busting here; but since it's just a perf penalty we will allow for now
const key = JSON.stringify(args);

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

private getSchemaResponse<T>(
Expand Down
15 changes: 5 additions & 10 deletions packages/endpoint/src/schemas/__tests__/All.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe.each([
};
const sch = new schema.All(Cat);
expect(
new MemoCache().query('', sch, [], createInput(entities), {}),
new MemoCache().query(sch, [], createInput(entities), {}),
).toMatchSnapshot();
});

Expand All @@ -129,7 +129,7 @@ describe.each([
},
};
expect(
new MemoCache().query('', catSchema, [], createInput(entities), {}),
new MemoCache().query(catSchema, [], createInput(entities), {}),
).toMatchSnapshot();
});

Expand All @@ -143,7 +143,6 @@ describe.each([
},
};
const value = new MemoCache().query(
'',
catSchema,
[],
createInput(entities),
Expand All @@ -167,7 +166,6 @@ describe.each([
},
};
const value = new MemoCache().query(
'',
catSchema,
[],
createInput(entities) as any,
Expand All @@ -191,11 +189,11 @@ describe.each([
},
};
const memo = new MemoCache();
const value = memo.query('', catSchema, [], entities, {});
const value = memo.query(catSchema, [], entities, {});

expect(createOutput(value).results?.length).toBe(2);
expect(createOutput(value).results).toMatchSnapshot();
const value2 = memo.query('', catSchema, [], entities, {});
const value2 = memo.query(catSchema, [], entities, {});
expect(createOutput(value).results[0]).toBe(
createOutput(value2).results[0],
);
Expand All @@ -208,7 +206,7 @@ describe.each([
3: { id: '3', name: 'Jelico' },
},
};
const value3 = memo.query('', catSchema, [], entities, {});
const value3 = memo.query(catSchema, [], entities, {});
expect(createOutput(value3).results?.length).toBe(3);
expect(createOutput(value3).results).toMatchSnapshot();
expect(createOutput(value).results[0]).toBe(
Expand All @@ -231,7 +229,6 @@ describe.each([
};

const value = new MemoCache().query(
'',
catSchema,
[],
createInput(entities),
Expand Down Expand Up @@ -267,7 +264,6 @@ describe.each([
},
};
const value = new MemoCache().query(
'',
listSchema,
[],
createInput(entities),
Expand Down Expand Up @@ -331,7 +327,6 @@ describe.each([
},
};
const value = new MemoCache().query(
'',
listSchema,
[],
createInput(entities) as any,
Expand Down
3 changes: 0 additions & 3 deletions packages/endpoint/src/schemas/__tests__/Collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ describe(`${schema.Collection.name} denormalization`, () => {
it('should buildQueryKey with matching args', () => {
const memo = new MemoCache();
const queryKey = memo.buildQueryKey(
'',
userTodos,
[{ userId: '1' }],
normalizeNested.entities,
Expand All @@ -543,7 +542,6 @@ describe(`${schema.Collection.name} denormalization`, () => {
it('should buildQueryKey undefined when not in cache', () => {
const memo = new MemoCache();
const queryKey = memo.buildQueryKey(
'',
userTodos,
[{ userId: '100' }],
normalizeNested.entities,
Expand All @@ -555,7 +553,6 @@ describe(`${schema.Collection.name} denormalization`, () => {
it('should buildQueryKey undefined with nested Collection', () => {
const memo = new MemoCache();
const queryKey = memo.buildQueryKey(
'',
User.schema.todos,
[{ userId: '1' }],
normalizeNested.entities,
Expand Down
18 changes: 7 additions & 11 deletions packages/endpoint/src/schemas/__tests__/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe.each([
},
};
const users: DenormalizeNullable<typeof sortedUsers> | symbol =
new MemoCache().query('', sortedUsers, [], createInput(entities), {});
new MemoCache().query(sortedUsers, [], createInput(entities), {});
expect(users).not.toEqual(expect.any(Symbol));
if (typeof users === 'symbol') return;
expect(users && users[0].name).toBe('Zeta');
Expand All @@ -95,7 +95,6 @@ describe.each([
};
expect(
new MemoCache().query(
'',
sortedUsers,
[{ asc: true }],
createInput(entities),
Expand All @@ -111,7 +110,7 @@ describe.each([
2: { id: '2', name: 'Jake' },
},
};
const data = new MemoCache().query('', sortedUsers, [], entities, {});
const data = new MemoCache().query(sortedUsers, [], entities, {});

expect(createOutput(data)).toEqual(undefined);
});
Expand Down Expand Up @@ -147,7 +146,6 @@ describe.each([
const totalCount:
| DenormalizeNullable<typeof userCountByAdmin>
| symbol = new MemoCache().query(
'',
userCountByAdmin,
[],
createInput(entities),
Expand All @@ -158,7 +156,6 @@ describe.each([
const nonAdminCount:
| DenormalizeNullable<typeof userCountByAdmin>
| symbol = new MemoCache().query(
'',
userCountByAdmin,
[{ isAdmin: false }],
createInput(entities),
Expand All @@ -168,7 +165,6 @@ describe.each([
const adminCount:
| DenormalizeNullable<typeof userCountByAdmin>
| symbol = new MemoCache().query(
'',
userCountByAdmin,
[{ isAdmin: true }],
createInput(entities),
Expand Down Expand Up @@ -208,7 +204,7 @@ describe('top level schema', () => {
[new schema.Collection([User]).pk({}, undefined, '', [])]: [1, 2, 3, 4],
},
};
const users = new MemoCache().query('', sortedUsers, [], entities, {});
const users = new MemoCache().query(sortedUsers, [], entities, {});
expect(users).not.toEqual(expect.any(Symbol));
if (typeof users === 'symbol') return;
expect(users && users[0].name).toBe('Zeta');
Expand All @@ -224,7 +220,7 @@ describe('top level schema', () => {
4: { id: '4', name: 'Alpha' },
},
};
const users = new MemoCache().query('', sortedUsers, [], entities, {});
const users = new MemoCache().query(sortedUsers, [], entities, {});
expect(users).toBeUndefined();
});

Expand All @@ -239,7 +235,7 @@ describe('top level schema', () => {
return sorted.reverse();
},
);
const users = new MemoCache().query('', allSortedUsers, [], {}, {});
const users = new MemoCache().query(allSortedUsers, [], {}, {});
expect(users).toBeUndefined();
});

Expand All @@ -254,7 +250,7 @@ describe('top level schema', () => {
return sorted.reverse();
},
);
const users = new MemoCache().query('', allSortedUsers, [], {}, {});
const users = new MemoCache().query(allSortedUsers, [], {}, {});
expect(users).toBeUndefined();
});

Expand All @@ -266,7 +262,7 @@ describe('top level schema', () => {
},
};

const value = new MemoCache().query('', sortedUsers, [], entities, {});
const value = new MemoCache().query(sortedUsers, [], entities, {});

expect(value).toEqual(undefined);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/normalizr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ const memo = new MemoCache();

const { data, paths } = memo.denormalize(schema, input, state.entities, args);

const data = memo.query(schema, key, args, state.entities, state.indexes);
const data = memo.query(schema, args, state.entities, state.indexes);

function query(schema, key, args, state) {
function query(schema, args, state, key) {
const queryKey = memo.buildQueryKey(
key,
schema,
args,
state.entities,
state.indexes,
key,
);
const { data } = this.denormalize(schema, queryKey, state.entities, args);
return typeof data === 'symbol' ? undefined : (data as any);
Expand Down
Loading

0 comments on commit 4a96d27

Please sign in to comment.