Skip to content

Commit 6d7e1ef

Browse files
committed
enhance: Remove INVALID symbol from endpoints in favor of delegate access
1 parent ce792ac commit 6d7e1ef

File tree

21 files changed

+114
-31
lines changed

21 files changed

+114
-31
lines changed

.changeset/major-boxes-glow.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@data-client/normalizr': minor
3+
'@data-client/endpoint': minor
4+
'@data-client/rest': minor
5+
'@data-client/graphql': minor
6+
---
7+
8+
Add delegate.INVALID to queryKey
9+
10+
This is used in schema.All.queryKey().

.changeset/open-shrimps-warn.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@data-client/normalizr': minor
3+
'@data-client/endpoint': minor
4+
'@data-client/rest': minor
5+
'@data-client/graphql': minor
6+
---
7+
8+
Add delegate.invalidate() to normalization
9+
10+
#### Before
11+
12+
```ts
13+
delegate.setEntity(this as any, pk, INVALID);
14+
```
15+
16+
#### After
17+
18+
```ts
19+
delegate.invalidate(this as any, pk);
20+
```

.changeset/tough-areas-show.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@data-client/endpoint': minor
3+
'@data-client/graphql': minor
4+
'@data-client/rest': minor
5+
---
6+
7+
Remove `INVALID` symbol export
8+
9+
Schemas can use delegate.invalidate() in normalize() or return delegate.INVALID in queryKey().

packages/endpoint/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export type { Array, Invalidate, Collection, DefaultArgs } from './schema.js';
1616
export { default as Entity } from './schemas/Entity.js';
1717
export { default as EntityMixin } from './schemas/EntityMixin.js';
1818
export { default as validateRequired } from './validateRequired.js';
19-
export { INVALID } from './special.js';
2019
export type {
2120
EndpointInterface,
2221
ReadEndpoint,

packages/endpoint/src/interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ export interface GetIndex {
133133
export interface IQueryDelegate {
134134
getEntity: GetEntity;
135135
getIndex: GetIndex;
136+
/** Return to consider results invalid */
137+
INVALID: symbol;
136138
}
137139

138140
/** Helpers during schema.normalize() */
@@ -154,6 +156,8 @@ export interface INormalizeDelegate {
154156
entity: any,
155157
meta?: { fetchedAt: number; date: number; expiresAt: number },
156158
): void;
159+
/** Invalidates an entity, potentially triggering suspense */
160+
invalidate(schema: { key: string; indexes?: any }, pk: string): void;
157161
/** Returns true when we're in a cycle, so we should not continue recursing */
158162
checkLoop(key: string, pk: string, input: object): boolean;
159163
}

packages/endpoint/src/schemas/All.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ArraySchema from './Array.js';
22
import { IQueryDelegate, Visit } from '../interface.js';
33
import { EntityInterface, EntityMap, SchemaFunction } from '../schema.js';
4-
import { INVALID } from '../special.js';
54

65
/**
76
* Retrieves all entities in cache
@@ -29,7 +28,7 @@ export default class AllSchema<
2928
if (this.isSingleSchema) {
3029
const entitiesEntry = delegate.getEntity(this.schema.key);
3130
// we must wait until there are entries for any 'All' query to be Valid
32-
if (entitiesEntry === undefined) return INVALID;
31+
if (entitiesEntry === undefined) return delegate.INVALID;
3332
return Object.values(entitiesEntry).map(
3433
entity => entity && this.schema.pk(entity),
3534
);
@@ -47,7 +46,7 @@ export default class AllSchema<
4746
},
4847
);
4948
// we need at least one table entry of the Union for this to count as Valid.
50-
if (!found) return INVALID;
49+
if (!found) return delegate.INVALID;
5150
return list;
5251
}
5352
}

packages/endpoint/src/schemas/EntityMixin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import type {
33
Visit,
44
IQueryDelegate,
55
INormalizeDelegate,
6-
Mergeable,
76
} from '../interface.js';
87
import { AbstractInstanceType } from '../normal.js';
9-
import { INVALID } from '../special.js';
108
import type {
119
IEntityClass,
1210
IEntityInstance,
@@ -256,8 +254,7 @@ export default function EntityMixin<TBase extends Constructor>(
256254
id = `${this.pk(input, parent, key, args)}`;
257255
// TODO: add undefined id check
258256

259-
// set directly: any queued updates are meaningless with delete
260-
delegate.setEntity(this, id, INVALID);
257+
delegate.invalidate(this, id);
261258
return id;
262259
}
263260
id = this.pk(processedEntity, parent, key, args);

packages/endpoint/src/schemas/Invalidate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {
44
SchemaSimple,
55
} from '../interface.js';
66
import type { AbstractInstanceType } from '../normal.js';
7-
import { INVALID } from '../special.js';
87

98
/**
109
* Marks entity as Invalid.
@@ -74,7 +73,7 @@ export default class Invalidate<
7473

7574
// any queued updates are meaningless with delete, so we should just set it
7675
// and creates will have a different pk
77-
delegate.setEntity(this as any, pk, INVALID);
76+
delegate.invalidate(this as any, pk);
7877
return pk;
7978
}
8079

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// eslint-env jest
22
import { initialState, State, Controller } from '@data-client/core';
3-
import { normalize, MemoCache, denormalize } from '@data-client/normalizr';
3+
import {
4+
normalize,
5+
MemoCache,
6+
denormalize,
7+
INVALID,
8+
} from '@data-client/normalizr';
49
import { IDEntity } from '__tests__/new';
510

611
import { schema } from '../..';
712
import { fromJSState } from './denormalize';
8-
import { INVALID } from '../../special';
913

1014
let dateSpy: jest.SpyInstance<number, []>;
1115
beforeAll(() => {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// eslint-env jest
2-
import { Schema, normalize } from '@data-client/normalizr';
2+
import { INVALID, Schema, normalize } from '@data-client/normalizr';
33
import { IDEntity } from '__tests__/new';
44
import { fromJS } from 'immutable';
55

66
import { SimpleMemoCache, fromJSEntities } from './denormalize';
77
import { schema } from '../..';
8-
import { INVALID } from '../../special';
98
import Entity from '../Entity';
109

1110
let dateSpy: jest.SpyInstance;

0 commit comments

Comments
 (0)