forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolid-view.ts
More file actions
236 lines (211 loc) · 6.24 KB
/
solid-view.ts
File metadata and controls
236 lines (211 loc) · 6.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {
createStore,
produce,
type SetStoreFunction,
type Store,
} from 'solid-js/store';
import {
applyChange,
type Change,
type Entry,
type Format,
type HumanReadable,
type Input,
type Node,
type Output,
type Query,
type ResultType,
type Schema,
type Stream,
type TTL,
type ViewChange,
type ViewFactory,
} from '../../zero-client/src/mod.js';
export type QueryResultDetails = {
readonly type: ResultType;
};
type State = [Entry, QueryResultDetails];
const complete = {type: 'complete'} as const;
const unknown = {type: 'unknown'} as const;
export class SolidView<V> implements Output {
readonly #input: Input;
readonly #format: Format;
readonly #onDestroy: () => void;
#state: Store<State>;
#setState: SetStoreFunction<State>;
// Optimization: if the store is currently empty we build up
// the view on a plain old JS object stored at #builderRoot, and return
// that for the new state on transaction commit. This avoids building up
// large views from scratch via solid produce. The proxy object used by
// solid produce is slow and in this case we don't care about solid tracking
// the fine grained changes (everything has changed, it's all new). For a
// test case with a view with 3000 rows, each row having 2 children, this
// optimization reduced #applyChanges time from 743ms to 133ms.
#builderRoot: Entry | undefined;
#pendingChanges: ViewChange[] = [];
readonly #updateTTL: (ttl: TTL) => void;
constructor(
input: Input,
onTransactionCommit: (cb: () => void) => void,
format: Format,
onDestroy: () => void,
queryComplete: true | Promise<true>,
updateTTL: (ttl: TTL) => void,
) {
this.#input = input;
onTransactionCommit(this.#onTransactionCommit);
this.#format = format;
this.#onDestroy = onDestroy;
this.#updateTTL = updateTTL;
input.setOutput(this);
const initialRoot = this.#createEmptyRoot();
this.#applyChangesToRoot(
input.fetch({}),
node => ({type: 'add', node}),
initialRoot,
);
[this.#state, this.#setState] = createStore<State>([
initialRoot,
queryComplete === true ? complete : unknown,
]);
if (isEmptyRoot(initialRoot)) {
this.#builderRoot = this.#createEmptyRoot();
}
if (queryComplete !== true) {
void queryComplete.then(() => {
this.#setState(oldState => [oldState[0], complete]);
});
}
}
get data(): V {
return this.#state[0][''] as V;
}
get resultDetails(): QueryResultDetails {
return this.#state[1];
}
destroy(): void {
this.#onDestroy();
}
#onTransactionCommit = () => {
const builderRoot = this.#builderRoot;
if (builderRoot) {
if (!isEmptyRoot(builderRoot)) {
this.#setState(oldState => [builderRoot, oldState[1]]);
this.#builderRoot = undefined;
}
} else {
try {
this.#applyChanges(this.#pendingChanges, c => c);
} finally {
this.#pendingChanges = [];
}
}
};
push(change: Change): void {
// Delay updating the solid store state until the transaction commit
// (because each update of the solid store is quite expensive). If
// this.#builderRoot is defined apply the changes to it (we are building
// from an empty root), otherwise queue the changes to be applied
// using produce at the end of the transaction but read the relationships
// now as they are only valid to read when the push is received.
if (this.#builderRoot) {
this.#applyChangeToRoot(change, this.#builderRoot);
} else {
this.#pendingChanges.push(materializeRelationships(change));
}
}
#applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => ViewChange): void {
this.#setState(
produce((draftState: State) => {
this.#applyChangesToRoot<T>(changes, mapper, draftState[0]);
if (isEmptyRoot(draftState[0])) {
this.#builderRoot = this.#createEmptyRoot();
}
}),
);
}
#applyChangesToRoot<T>(
changes: Iterable<T>,
mapper: (v: T) => ViewChange,
root: Entry,
) {
for (const change of changes) {
this.#applyChangeToRoot(mapper(change), root);
}
}
#applyChangeToRoot(change: ViewChange, root: Entry) {
applyChange(root, change, this.#input.getSchema(), '', this.#format);
}
#createEmptyRoot(): Entry {
return {
'': this.#format.singular ? undefined : [],
};
}
updateTTL(ttl: TTL): void {
this.#updateTTL(ttl);
}
}
function materializeRelationships(change: Change): ViewChange {
switch (change.type) {
case 'add':
return {type: 'add', node: materializeNodeRelationships(change.node)};
case 'remove':
return {type: 'remove', node: materializeNodeRelationships(change.node)};
case 'child':
return {
type: 'child',
node: {row: change.node.row},
child: {
relationshipName: change.child.relationshipName,
change: materializeRelationships(change.child.change),
},
};
case 'edit':
return {
type: 'edit',
node: {row: change.node.row},
oldNode: {row: change.oldNode.row},
};
}
}
function materializeNodeRelationships(node: Node): Node {
const relationships: Record<string, () => Stream<Node>> = {};
for (const relationship in node.relationships) {
const materialized: Node[] = [];
for (const n of node.relationships[relationship]()) {
materialized.push(materializeNodeRelationships(n));
}
relationships[relationship] = () => materialized;
}
return {
row: node.row,
relationships,
};
}
function isEmptyRoot(entry: Entry) {
const data = entry[''];
return data === undefined || (Array.isArray(data) && data.length === 0);
}
export function solidViewFactory<
TSchema extends Schema,
TTable extends keyof TSchema['tables'] & string,
TReturn,
>(
_query: Query<TSchema, TTable, TReturn>,
input: Input,
format: Format,
onDestroy: () => void,
onTransactionCommit: (cb: () => void) => void,
queryComplete: true | Promise<true>,
updateTTL: (ttl: TTL) => void,
) {
return new SolidView<HumanReadable<TReturn>>(
input,
onTransactionCommit,
format,
onDestroy,
queryComplete,
updateTTL,
);
}
solidViewFactory satisfies ViewFactory<Schema, string, unknown, unknown>;