Skip to content

Commit 24e119b

Browse files
committed
feat: Normalize into immutablejs state
1 parent 35ef3f0 commit 24e119b

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

packages/normalizr/src/normalize/NormalizeDelegate.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
EntityTable,
33
NormalizedIndex,
44
INormalizeDelegate,
@@ -7,12 +7,22 @@ import {
77
import { getCheckLoop } from './getCheckLoop.js';
88
import { POJODelegate } from '../delegate/Delegate.js';
99
import { INVALID } from '../denormalize/symbol.js';
10+
import { NormalizedSchema } from '../types.js';
1011

1112
/** Full normalize() logic for POJO state */
1213
export class NormalizeDelegate
1314
extends POJODelegate
14-
implements INormalizeDelegate
15+
implements INormalizeDelegate, NormalizedSchema<EntityTable, any>
1516
{
17+
// declare readonly normalized: NormalizedSchema<E, R>;
18+
declare result: any;
19+
declare readonly entities: EntityTable;
20+
declare readonly indexes: {
21+
[entityKey: string]: {
22+
[indexName: string]: { [lookup: string]: string };
23+
};
24+
};
25+
1626
declare readonly entitiesMeta: {
1727
[entityKey: string]: {
1828
[pk: string]: {
@@ -26,8 +36,10 @@ export class NormalizeDelegate
2636
declare readonly meta: { fetchedAt: number; date: number; expiresAt: number };
2737
declare checkLoop: (entityKey: string, pk: string, input: object) => boolean;
2838

29-
protected newEntities = new Map<string, Map<string, any>>();
30-
protected newIndexes = new Map<string, Map<string, any>>();
39+
protected new = {
40+
entities: new Map<string, Map<string, any>>(),
41+
indexes: new Map<string, Map<string, any>>(),
42+
};
3143

3244
constructor(
3345
state: {
@@ -46,7 +58,15 @@ export class NormalizeDelegate
4658
actionMeta: { fetchedAt: number; date: number; expiresAt: number },
4759
) {
4860
super(state);
49-
this.entitiesMeta = state.entitiesMeta;
61+
// this.normalized = NormalizedSchema<E, R> = {
62+
// result: '' as any,
63+
// entities: { ...state.entities },
64+
// indexes: { ...state.indexes },
65+
// entitiesMeta: { ...state.entitiesMeta },
66+
// };
67+
this.entities = { ...state.entities };
68+
this.indexes = { ...state.indexes };
69+
this.entitiesMeta = { ...state.entitiesMeta };
5070
this.meta = actionMeta;
5171
this.checkLoop = getCheckLoop();
5272
}
@@ -57,8 +77,8 @@ export class NormalizeDelegate
5777

5878
protected getNewEntities(key: string): Map<string, any> {
5979
// first time we come across this type of entity
60-
if (!this.newEntities.has(key)) {
61-
this.newEntities.set(key, new Map());
80+
if (!this.new.entities.has(key)) {
81+
this.new.entities.set(key, new Map());
6282
// we will be editing these, so we need to clone them first
6383
this.entities[key] = {
6484
...this.entities[key],
@@ -68,15 +88,15 @@ export class NormalizeDelegate
6888
};
6989
}
7090

71-
return this.newEntities.get(key) as Map<string, any>;
91+
return this.new.entities.get(key) as Map<string, any>;
7292
}
7393

7494
protected getNewIndexes(key: string): Map<string, any> {
75-
if (!this.newIndexes.has(key)) {
76-
this.newIndexes.set(key, new Map());
95+
if (!this.new.indexes.has(key)) {
96+
this.new.indexes.set(key, new Map());
7797
this.indexes[key] = { ...this.indexes[key] };
7898
}
79-
return this.newIndexes.get(key) as Map<string, any>;
99+
return this.new.indexes.get(key) as Map<string, any>;
80100
}
81101

82102
/** Updates an entity using merge lifecycles when it has previously been set */

packages/normalizr/src/normalize/normalize.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ See https://dataclient.io/rest/api/RestEndpoint#parseResponse for more informati
7979
}
8080
}
8181

82-
const ret: NormalizedSchema<E, R> = {
83-
result: '' as any,
84-
entities: { ...entities },
85-
indexes: { ...indexes },
86-
entitiesMeta: { ...entitiesMeta },
87-
};
88-
const visit = getVisit(new NormalizeDelegate(ret, meta));
89-
ret.result = visit(schema, input, input, undefined, args);
90-
return ret;
82+
const delegate = new NormalizeDelegate(
83+
{ entities, indexes, entitiesMeta },
84+
meta,
85+
);
86+
const visit = getVisit(delegate);
87+
delegate.result = visit(schema, input, input, undefined, args);
88+
return delegate as any;
9189
};
9290

9391
function expectedSchemaType(schema: Schema) {

0 commit comments

Comments
 (0)