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
89 lines (80 loc) · 1.9 KB
/
solid-view.ts
File metadata and controls
89 lines (80 loc) · 1.9 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
import {createStore, produce, type SetStoreFunction} from 'solid-js/store';
import {
applyChange,
type Change,
type Entry,
type Format,
type Input,
type Output,
type Query,
type QueryType,
type Smash,
type TableSchema,
type View,
} from '../../zero-advanced/src/mod.js';
export class SolidView<V extends View> implements Output {
readonly #input: Input;
readonly #format: Format;
readonly #onDestroy: () => void;
// Synthetic "root" entry that has a single "" relationship, so that we can
// treat all changes, including the root change, generically.
readonly #root: Entry;
readonly #setRoot: SetStoreFunction<Entry>;
constructor(
input: Input,
format: Format = {singular: false, relationships: {}},
onDestroy: () => void = () => {},
) {
this.#input = input;
this.#format = format;
this.#onDestroy = onDestroy;
[this.#root, this.#setRoot] = createStore({
'': format.singular ? undefined : [],
});
input.setOutput(this);
this.#setRoot(
produce(draftRoot => {
for (const node of input.fetch({})) {
applyChange(
draftRoot,
{type: 'add', node},
input.getSchema(),
'',
this.#format,
);
}
}),
);
}
get data() {
return this.#root[''] as V;
}
destroy() {
this.#onDestroy();
}
push(change: Change): void {
this.#setRoot(
produce(draftRoot => {
applyChange(
draftRoot,
change,
this.#input.getSchema(),
'',
this.#format,
);
}),
);
}
}
export function solidViewFactory<
TSchema extends TableSchema,
TReturn extends QueryType,
>(
_query: Query<TSchema, TReturn>,
input: Input,
format: Format,
onDestroy: () => void,
): SolidView<Smash<TReturn>> {
const v = new SolidView<Smash<TReturn>>(input, format, onDestroy);
return v;
}