-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathGraph.ts
More file actions
151 lines (121 loc) · 4.57 KB
/
Graph.ts
File metadata and controls
151 lines (121 loc) · 4.57 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { applyMiddleware, type Middleware } from 'handler-chain';
import { iteratorEvery } from 'iter-fest';
import { assert, check, map, object, pipe } from 'valibot';
import { IdentifierSchema, type Identifier } from './schemas/Identifier';
type GraphSubscriberRecord = {
readonly upsertedNodeIdentifiers: ReadonlySet<Identifier>;
};
type GraphSubscriber = (event: GraphSubscriberRecord) => void;
type GraphNode = { '@id': Identifier };
type GraphMiddleware<TInput extends GraphNode, TOutput extends GraphNode> = Middleware<
ReadonlyMap<Identifier, TOutput>,
ReadonlyMap<Identifier, TInput>,
{ readonly getState: () => GraphState<TOutput> }
>;
type GraphState<T extends GraphNode = GraphNode> = ReadonlyMap<Identifier, T>;
type ReadableGraph<TInput extends GraphNode, TOutput extends GraphNode> = {
readonly act: (fn: (graph: WritableGraph<TInput, TOutput>) => void) => void;
readonly getState: () => GraphState<TOutput>;
readonly subscribe: (subscriber: GraphSubscriber) => void;
};
type WritableGraph<TInput extends GraphNode, TOutput extends GraphNode> = {
readonly getState: () => GraphState<TOutput>;
readonly upsert: (...nodes: readonly TInput[]) => void;
};
const requestSchema = pipe(
map(IdentifierSchema, object({ '@id': IdentifierSchema })),
check(
// TODO: [P4] Iterator.every is since iOS 18.4, we still need to use ponyfill until we drop support of iOS 18.4.
value => iteratorEvery(value.entries(), ([key, node]) => key === node['@id']),
'Key returned in Map must match `@id` in value'
)
);
const middlewareValidator: GraphMiddleware<any, any> = () => next => request => {
assert(requestSchema, request);
const result = next(Object.freeze(request));
assert(requestSchema, result);
return Object.freeze(result);
};
class Graph<TInput extends GraphNode, TOutput extends GraphNode = TInput> implements ReadableGraph<TInput, TOutput> {
#busy = false;
#middleware: GraphMiddleware<TInput, TOutput>;
#state: GraphState<TOutput> = Object.freeze(new Map());
#subscribers: Set<GraphSubscriber> = new Set();
constructor(
firstMiddleware: GraphMiddleware<TInput, TOutput>,
...restMiddleware: readonly GraphMiddleware<TInput, TOutput>[]
) {
// Interleaves every middleware with a validator to protect request.
this.#middleware = applyMiddleware(
middlewareValidator,
...[firstMiddleware, ...restMiddleware].flatMap<GraphMiddleware<TInput, TOutput>>(middleware => [
middleware,
middlewareValidator
])
);
}
act(fn: (graph: WritableGraph<TInput, TOutput>) => void) {
if (this.#busy) {
throw new Error('Another transaction is ongoing');
}
this.#busy = true;
let record: GraphSubscriberRecord | undefined;
try {
const getState = this.getState.bind(this);
const upsertedNodes = new Map<Identifier, TInput>();
fn(
Object.freeze({
getState,
upsert(...nodes: readonly TInput[]) {
for (const node of nodes) {
const id = node['@id'];
if (upsertedNodes.has(id)) {
throw new Error(`Cannot upsert a node multiple times in a single transaction (@id = "${id}")`);
}
upsertedNodes.set(id, node);
}
}
})
);
const nextState = new Map<Identifier, TOutput>(this.#state);
const upsertedNodeIdentifiers = new Set<Identifier>();
for (const enhancedNode of this.#middleware({ getState })(() => {
throw new Error('At least one middleware must not fallthrough');
})(Object.freeze(upsertedNodes)).values()) {
nextState.set(enhancedNode['@id'], Object.freeze({ ...enhancedNode }));
upsertedNodeIdentifiers.add(enhancedNode['@id']);
}
if (upsertedNodeIdentifiers.size) {
this.#state = Object.freeze(nextState);
// After this line, there must be no more write operations on this object instance.
record = Object.freeze({ upsertedNodeIdentifiers: Object.freeze(upsertedNodeIdentifiers) });
}
} finally {
this.#busy = false;
}
if (record) {
for (const subscriber of this.#subscribers) {
subscriber(record);
}
}
}
getState(): GraphState<TOutput> {
return this.#state;
}
subscribe(subscriber: GraphSubscriber): () => void {
this.#subscribers.add(subscriber);
return () => {
this.#subscribers.delete(subscriber);
};
}
}
export default Graph;
export {
type GraphMiddleware,
type GraphNode,
type GraphState,
type GraphSubscriber,
type GraphSubscriberRecord,
type ReadableGraph,
type WritableGraph
};