-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathDelegate.ts
More file actions
90 lines (75 loc) · 2.39 KB
/
Delegate.ts
File metadata and controls
90 lines (75 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Dep } from './WeakDependencyMap.js';
import type {
EntityTable,
NormalizedIndex,
IQueryDelegate,
} from '../interface.js';
import { QueryPath, IndexPath } from './types.js';
import { INVALID } from '../denormalize/symbol.js';
export const getDependency =
(delegate: IBaseDelegate) =>
(args: QueryPath): QueryPath | undefined =>
// ignore third arg so we only track
args.length === 3 ?
delegate.getIndex(args[0], args[1])
: delegate.getEntity(...(args as [any]));
export interface IBaseDelegate {
entities: any;
indexes: any;
getEntity(entityKey: string | symbol, pk?: string): any;
getIndex(key: string, field: string): any;
}
export class BaseDelegate implements IBaseDelegate {
declare entities: EntityTable;
declare indexes: {
[entityKey: string]: {
[indexName: string]: { [lookup: string]: string };
};
};
constructor({
entities,
indexes,
}: {
entities: EntityTable;
indexes: NormalizedIndex;
}) {
this.entities = entities;
this.indexes = indexes;
}
getEntity(
entityKey: string | symbol,
): { readonly [pk: string]: any } | undefined;
getEntity(entityKey: string | symbol, pk: string | number): any;
getEntity(entityKey: string, pk?: any): any {
return pk ? this.entities[entityKey]?.[pk] : this.entities[entityKey];
}
// this is different return value than QuerySnapshot
getIndex(key: string, field: string) {
return this.indexes[key]?.[field];
}
}
export class TrackingQueryDelegate implements IQueryDelegate {
readonly INVALID = INVALID;
declare protected snap: IBaseDelegate;
// first dep path is ignored
// we start with schema object, then lookup any 'touched' members and their paths
declare readonly dependencies: Dep<QueryPath>[];
constructor(snap: IBaseDelegate, schema: any) {
this.snap = snap;
this.dependencies = [{ path: [''], entity: schema }];
}
getIndex(...path: IndexPath): string | undefined {
const entity = this.snap.getIndex(path[0], path[1]);
this.dependencies.push({ path, entity });
return entity?.[path[2]];
}
getEntity(
entityKey: string | symbol,
): { readonly [pk: string]: any } | undefined;
getEntity(entityKey: string | symbol, pk: string | number): any;
getEntity(...path: any): any {
const entity = this.snap.getEntity(...(path as [any]));
this.dependencies.push({ path, entity });
return entity;
}
}