Skip to content

Commit 744689c

Browse files
committed
enhance: Perform INVALID checks against the symbol itself
1 parent 939a4b0 commit 744689c

File tree

13 files changed

+32
-25
lines changed

13 files changed

+32
-25
lines changed

packages/core/src/controller/Controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isEntity,
1818
denormalize,
1919
validateQueryKey,
20+
INVALID,
2021
} from '@data-client/normalizr';
2122

2223
import AbortOptimistic from './AbortOptimistic.js';
@@ -563,7 +564,7 @@ export default class Controller<
563564
return {
564565
data,
565566
expiryStatus: this.getExpiryStatus(
566-
typeof data === 'symbol',
567+
data === INVALID,
567568
!!endpoint.invalidIfStale || isInvalid,
568569
meta,
569570
),
@@ -590,7 +591,7 @@ export default class Controller<
590591
.map(ensurePojo) as SchemaArgs<S>;
591592

592593
const { data } = this.memo.query(schema, args, state);
593-
return typeof data === 'symbol' ? undefined : data;
594+
return data === INVALID ? undefined : data;
594595
}
595596

596597
/**
@@ -616,7 +617,7 @@ export default class Controller<
616617
const { data, paths } = this.memo.query(schema, args, state);
617618

618619
return {
619-
data: typeof data === 'symbol' ? undefined : data,
620+
data: data === INVALID ? undefined : data,
620621
countRef: this.gcPolicy.createCountRef({ paths }),
621622
};
622623
}

packages/normalizr/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function query(schema, args, state, key) {
214214
key,
215215
);
216216
const { data } = this.denormalize(schema, queryKey, state.entities, args);
217-
return typeof data === 'symbol' ? undefined : (data as any);
217+
return data === INVALID ? undefined : (data as any);
218218
}
219219
```
220220

packages/normalizr/src/denormalize/cache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { EntityInterface } from '../interface.js';
22
import { EntityPath } from '../types.js';
3+
import type { INVALID } from './symbol.js';
34

45
export default interface Cache {
56
getEntity(
67
pk: string,
78
schema: EntityInterface,
89
entity: any,
910
computeValue: (localCacheKey: Map<string, any>) => void,
10-
): object | undefined | symbol;
11+
): object | undefined | typeof INVALID;
1112
getResults(
1213
input: any,
1314
cachable: boolean,

packages/normalizr/src/denormalize/denormalize.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import getUnvisit from './unvisit.js';
44
import type { Schema } from '../interface.js';
55
import { isImmutable } from '../schemas/ImmutableUtils.js';
66
import type { DenormalizeNullable } from '../types.js';
7+
import type { INVALID } from './symbol.js';
78

89
export function denormalize<S extends Schema>(
910
schema: S | undefined,
1011
input: any,
1112
entities: any,
1213
args: readonly any[] = [],
13-
): DenormalizeNullable<S> | symbol {
14+
): DenormalizeNullable<S> | typeof INVALID {
1415
// undefined means don't do anything
1516
if (schema === undefined || input === undefined) {
1617
return input as any;

packages/normalizr/src/denormalize/localCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type Cache from './cache.js';
22
import type { EntityInterface } from '../interface.js';
33
import type { EntityPath } from '../types.js';
4+
import type { INVALID } from './symbol.js';
45

56
export default class LocalCache implements Cache {
67
private localCache = new Map<string, Map<string, any>>();
@@ -10,7 +11,7 @@ export default class LocalCache implements Cache {
1011
schema: EntityInterface,
1112
entity: any,
1213
computeValue: (localCacheKey: Map<string, any>) => void,
13-
): object | undefined | symbol {
14+
): object | undefined | typeof INVALID {
1415
const key = schema.key;
1516
if (!this.localCache.has(key)) {
1617
this.localCache.set(key, new Map<string, any>());

packages/normalizr/src/denormalize/unvisit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const getUnvisitEntity = (
1818
return function unvisitEntity(
1919
schema: EntityInterface,
2020
entityOrId: Record<string, any> | string,
21-
): object | undefined | symbol {
21+
): object | undefined | typeof INVALID {
2222
const inputIsId = typeof entityOrId !== 'object';
2323
const entity =
2424
inputIsId ? getEntity({ key: schema.key, pk: entityOrId }) : entityOrId;
@@ -92,7 +92,7 @@ function unvisitEntityObject(
9292

9393
function noCacheGetEntity(
9494
computeValue: (localCacheKey: Map<string, any>) => void,
95-
): object | undefined | symbol {
95+
): object | undefined | typeof INVALID {
9696
const localCacheKey = new Map<string, any>();
9797
computeValue(localCacheKey);
9898

packages/normalizr/src/memo/MemoCache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from './Delegate.js';
2222
import { EndpointsCache, EntityCache } from './types.js';
2323
import { QueryPath } from './types.js';
24+
import type { INVALID } from '../denormalize/symbol.js';
2425

2526
//TODO: make immutable distinction occur when initilizing MemoCache
2627

@@ -40,7 +41,7 @@ export default class MemoCache {
4041
entities: any,
4142
args: readonly any[] = [],
4243
): {
43-
data: DenormalizeNullable<S> | symbol;
44+
data: DenormalizeNullable<S> | typeof INVALID;
4445
paths: EntityPath[];
4546
} {
4647
// we already vary based on input, so we don't need endpointKey? TODO: verify
@@ -72,7 +73,7 @@ export default class MemoCache {
7273
// NOTE: different orders can result in cache busting here; but since it's just a perf penalty we will allow for now
7374
argsKey: string = JSON.stringify(args),
7475
): {
75-
data: DenormalizeNullable<S> | symbol;
76+
data: DenormalizeNullable<S> | typeof INVALID;
7677
paths: EntityPath[];
7778
} {
7879
const input = this.buildQueryKey(schema, args, state, argsKey);

packages/normalizr/src/memo/globalCache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EndpointsCache, EntityCache } from './types.js';
22
import WeakDependencyMap, { type Dep } from './WeakDependencyMap.js';
33
import type Cache from '../denormalize/cache.js';
44
import type { GetEntity } from '../denormalize/getEntities.js';
5+
import type { INVALID } from '../denormalize/symbol.js';
56
import type { EntityInterface } from '../interface.js';
67
import type { EntityPath } from '../types.js';
78

@@ -34,7 +35,7 @@ export default class GlobalCache implements Cache {
3435
schema: EntityInterface,
3536
entity: any,
3637
computeValue: (localCacheKey: Map<string, any>) => void,
37-
): object | undefined | symbol {
38+
): object | undefined | typeof INVALID {
3839
const key = schema.key;
3940
const { localCacheKey, cycleCacheKey } = this.getCacheKey(key);
4041

@@ -140,7 +141,7 @@ export default class GlobalCache implements Cache {
140141

141142
interface EntityCacheValue {
142143
dependencies: Dep<EntityPath>[];
143-
value: object | symbol | undefined;
144+
value: object | typeof INVALID | undefined;
144145
}
145146

146147
const getEntityCaches = (entityCache: EntityCache) => {

packages/normalizr/src/schemas/Array.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { INVALID } from '../denormalize/symbol.js';
12
import type { Visit } from '../interface.js';
23

34
const validateSchema = definition => {
@@ -17,7 +18,7 @@ const validateSchema = definition => {
1718
const getValues = input =>
1819
Array.isArray(input) ? input : Object.keys(input).map(key => input[key]);
1920

20-
const filterEmpty = item => item !== undefined && typeof item !== 'symbol';
21+
const filterEmpty = item => item !== undefined && item !== INVALID;
2122

2223
export const normalize = (
2324
schema: any,

packages/normalizr/src/schemas/ImmutableUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function denormalizeImmutable(
4646
const stringKey = `${key}`;
4747

4848
const item = unvisit(schema[stringKey], object.get(stringKey));
49-
if (typeof item === 'symbol') {
49+
if (item === INVALID) {
5050
deleted = true;
5151
}
5252
if (object.has(stringKey)) {

0 commit comments

Comments
 (0)