Skip to content

Commit 292339e

Browse files
committed
enhance: MemoCache.query returns {data, paths} just like denormalize
1 parent 7d8eef9 commit 292339e

File tree

5 files changed

+51
-42
lines changed

5 files changed

+51
-42
lines changed

.changeset/stale-socks-accept.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@data-client/normalizr': minor
3+
---
4+
5+
MemoCache.query returns `{ data, paths }` just like denormalize
6+
7+
#### Before
8+
9+
```ts
10+
const data = this.memo.query(schema, args, state);
11+
```
12+
13+
#### After
14+
15+
```ts
16+
const { data } = this.memo.query(schema, args, state);
17+
```

packages/core/src/controller/Controller.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ export default class Controller<
589589
.slice(0, rest.length - 1)
590590
.map(ensurePojo) as SchemaArgs<S>;
591591

592-
return this.memo.query(schema, args, state);
592+
const { data } = this.memo.query(schema, args, state);
593+
return typeof data === 'symbol' ? undefined : (data as any);
593594
}
594595

595596
/**
@@ -612,27 +613,10 @@ export default class Controller<
612613
.slice(0, rest.length - 1)
613614
.map(ensurePojo) as SchemaArgs<S>;
614615

615-
// TODO: breaking: Switch back to this.memo.query(schema, args, state.entities as any, state.indexes) to do
616-
// this logic
617-
const input = this.memo.buildQueryKey(
618-
schema,
619-
args,
620-
state,
621-
JSON.stringify(args),
622-
);
616+
const { data, paths } = this.memo.query(schema, args, state);
623617

624-
if (!input) {
625-
return { data: undefined, countRef: () => () => undefined };
626-
}
627-
628-
const { data, paths } = this.memo.denormalize(
629-
schema,
630-
input,
631-
state.entities,
632-
args,
633-
);
634618
return {
635-
data: typeof data === 'symbol' ? undefined : (data as any),
619+
data: typeof data === 'symbol' ? undefined : data,
636620
countRef: this.gcPolicy.createCountRef({ paths }),
637621
};
638622
}

packages/normalizr/src/memo/MemoCache.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ export default class MemoCache {
7171
state: StateInterface,
7272
// NOTE: different orders can result in cache busting here; but since it's just a perf penalty we will allow for now
7373
argsKey: string = JSON.stringify(args),
74-
): DenormalizeNullable<S> | undefined {
74+
): {
75+
data: DenormalizeNullable<S> | symbol;
76+
paths: EntityPath[];
77+
} {
7578
const input = this.buildQueryKey(schema, args, state, argsKey);
7679

7780
if (!input) {
78-
return;
81+
return { data: undefined as any, paths: [] };
7982
}
8083

81-
const { data } = this.denormalize(schema, input, state.entities, args);
82-
return typeof data === 'symbol' ? undefined : (data as any);
84+
return this.denormalize(schema, input, state.entities, args);
8385
}
8486

8587
buildQueryKey<S extends Schema>(

website/src/components/Playground/editor-types/@data-client/core.d.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,20 @@ declare class MemoCache {
224224
paths: EntityPath[];
225225
};
226226
/** Compute denormalized form maintaining referential equality for same inputs */
227-
query<S extends Schema>(schema: S, args: readonly any[], entities: Record<string, Record<string, any> | undefined> | {
228-
getIn(k: string[]): any;
229-
}, indexes: NormalizedIndex | {
230-
getIn(k: string[]): any;
231-
}, argsKey?: string): DenormalizeNullable<S> | undefined;
232-
buildQueryKey<S extends Schema>(schema: S, args: readonly any[], entities: Record<string, Record<string, any> | undefined> | {
227+
query<S extends Schema>(schema: S, args: readonly any[], state: StateInterface, argsKey?: string): {
228+
data: DenormalizeNullable<S> | symbol;
229+
paths: EntityPath[];
230+
};
231+
buildQueryKey<S extends Schema>(schema: S, args: readonly any[], state: StateInterface, argsKey?: string): NormalizeNullable<S>;
232+
}
233+
type StateInterface = {
234+
entities: Record<string, Record<string, any> | undefined> | {
233235
getIn(k: string[]): any;
234-
}, indexes: NormalizedIndex | {
236+
};
237+
indexes: NormalizedIndex | {
235238
getIn(k: string[]): any;
236-
}, argsKey?: string): NormalizeNullable<S>;
237-
}
239+
};
240+
};
238241

239242
/** https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-4.html#the-noinfer-utility-type */
240243
type NI<T> = NoInfer<T>;

website/src/components/Playground/editor-types/@data-client/normalizr.d.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,20 @@ declare class MemoCache {
264264
paths: EntityPath[];
265265
};
266266
/** Compute denormalized form maintaining referential equality for same inputs */
267-
query<S extends Schema>(schema: S, args: readonly any[], entities: Record<string, Record<string, any> | undefined> | {
268-
getIn(k: string[]): any;
269-
}, indexes: NormalizedIndex | {
270-
getIn(k: string[]): any;
271-
}, argsKey?: string): DenormalizeNullable<S> | undefined;
272-
buildQueryKey<S extends Schema>(schema: S, args: readonly any[], entities: Record<string, Record<string, any> | undefined> | {
267+
query<S extends Schema>(schema: S, args: readonly any[], state: StateInterface, argsKey?: string): {
268+
data: DenormalizeNullable<S> | symbol;
269+
paths: EntityPath[];
270+
};
271+
buildQueryKey<S extends Schema>(schema: S, args: readonly any[], state: StateInterface, argsKey?: string): NormalizeNullable<S>;
272+
}
273+
type StateInterface = {
274+
entities: Record<string, Record<string, any> | undefined> | {
273275
getIn(k: string[]): any;
274-
}, indexes: NormalizedIndex | {
276+
};
277+
indexes: NormalizedIndex | {
275278
getIn(k: string[]): any;
276-
}, argsKey?: string): NormalizeNullable<S>;
277-
}
279+
};
280+
};
278281

279282
/** https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-4.html#the-noinfer-utility-type */
280283
type NI<T> = NoInfer<T>;

0 commit comments

Comments
 (0)