doxum 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/advanced.cjs +3 -2
- package/dist/advanced.cjs.map +1 -1
- package/dist/advanced.d.cts +1 -1
- package/dist/advanced.d.ts +1 -1
- package/dist/advanced.js +2 -1
- package/dist/advanced.js.map +1 -1
- package/dist/{contract-BT53Gg94.d.ts → contract-CATCCVu5.d.ts} +1 -1
- package/dist/{definition-BlMeHwdz.d.ts → definition-B6J0SSNa.d.ts} +38 -37
- package/dist/{definition-kxnWUvX9.d.cts → definition-DN6yW2vi.d.cts} +37 -36
- package/dist/definition-Z6TagMup.cjs +107 -0
- package/dist/definition-Z6TagMup.cjs.map +1 -0
- package/dist/definition-c6XQXzvK.js +72 -0
- package/dist/definition-c6XQXzvK.js.map +1 -0
- package/dist/index.cjs +2054 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -3
- package/dist/index.d.ts +17 -4
- package/dist/index.js +2001 -2
- package/dist/index.js.map +1 -1
- package/dist/integration-B7VDgAOJ.cjs +30 -0
- package/dist/integration-B7VDgAOJ.cjs.map +1 -0
- package/dist/{integration-Bco8ZB16.js → integration-CxVhq-KD.js} +3 -7
- package/dist/integration-CxVhq-KD.js.map +1 -0
- package/dist/integration.cjs +4 -9
- package/dist/integration.d.cts +2 -7
- package/dist/integration.d.ts +2 -7
- package/dist/integration.js +3 -3
- package/dist/local-sync.d.ts +1 -1
- package/dist/notification-CDSUfiip.cjs +398 -0
- package/dist/notification-CDSUfiip.cjs.map +1 -0
- package/dist/notification-IQIJyH3Q.js +285 -0
- package/dist/notification-IQIJyH3Q.js.map +1 -0
- package/dist/react.cjs +6 -52
- package/dist/react.cjs.map +1 -1
- package/dist/react.js +4 -50
- package/dist/react.js.map +1 -1
- package/dist/{definition-177L4Amk.cjs → scope-Ezs-Y3TY.cjs} +1 -118
- package/dist/scope-Ezs-Y3TY.cjs.map +1 -0
- package/dist/{definition-DXrKMVs7.js → scope-d-L87NeC.js} +2 -65
- package/dist/scope-d-L87NeC.js.map +1 -0
- package/package.json +1 -1
- package/skills/doxum-runtime/references/guide.en.md +2 -1
- package/skills/doxum-runtime/references/guide.zh-CN.md +2 -1
- package/skills/doxum-runtime/references/projections.en.md +15 -6
- package/skills/doxum-runtime/references/projections.zh-CN.md +13 -5
- package/dist/definition-177L4Amk.cjs.map +0 -1
- package/dist/definition-DXrKMVs7.js.map +0 -1
- package/dist/integration-Bco8ZB16.js.map +0 -1
- package/dist/integration-CEUn96tx.cjs +0 -52
- package/dist/integration-CEUn96tx.cjs.map +0 -1
- package/dist/store-CDeqZEE3.d.cts +0 -28
- package/dist/store-CtgvCVFg.js +0 -2222
- package/dist/store-CtgvCVFg.js.map +0 -1
- package/dist/store-DcDpDgjk.cjs +0 -2401
- package/dist/store-DcDpDgjk.cjs.map +0 -1
- package/dist/store-P5dJ7TVe.d.ts +0 -28
package/dist/index.js
CHANGED
|
@@ -1,7 +1,194 @@
|
|
|
1
1
|
import { A as insert, D as equal, F as remove, L as profile, M as keys, N as matches, _ as resolveTreeContainer, a as compiledShape, c as debugKey, d as overlaps, f as read$1, g as resolveLocated, h as resolveContainer, i as AddressIndex, j as install, l as memberKey, n as fail, o as contains, p as resolveAddress, r as invalidValue, s as createAddressResolver, t as MutationRejected, v as resolveValue } from "./issue-DVaGQGeP.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { a as ParseError, c as copyValue, i as snapshot, l as equalValue, n as createAccess, o as checkKey, r as replace, s as checkValue, t as collectionAccess, u as parse } from "./scope-d-L87NeC.js";
|
|
3
|
+
import { _ as affected, a as documentReadableOwner, d as accessOf, f as bindRuntimeAccess, g as TransactionRejected, h as DocumentReentrancyError, i as disposeNotification, l as subscribeRoot, m as DocumentDisposedError, n as bindDocumentReadable, o as notify, p as readWith, r as createNotification, s as shareNotification, t as attachProjection, u as subscribeTargets, v as belongs, y as same } from "./notification-IQIJyH3Q.js";
|
|
4
4
|
import { n as bindRuntimeDriver, o as decodeChanges, r as disposeRuntimeDriver, s as sealChanges, t as assertRuntimeWritable } from "./driver-CSnhMjbP.js";
|
|
5
|
+
import { a as input, i as derive, o as observe, r as definitionOf } from "./definition-c6XQXzvK.js";
|
|
6
|
+
//#region core/src/schema.ts
|
|
7
|
+
const node = (value) => Object.freeze(value);
|
|
8
|
+
const field = (validator) => node({
|
|
9
|
+
kind: "field",
|
|
10
|
+
...validator ? { validator } : {}
|
|
11
|
+
});
|
|
12
|
+
const optional = (value) => {
|
|
13
|
+
if (![
|
|
14
|
+
"field",
|
|
15
|
+
"variant",
|
|
16
|
+
"map",
|
|
17
|
+
"list",
|
|
18
|
+
"tree"
|
|
19
|
+
].includes(value.kind)) throw new TypeError("Only field, variant, map, list and tree nodes support optional presence.");
|
|
20
|
+
return node({
|
|
21
|
+
...value,
|
|
22
|
+
optional: true
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const object = (shape) => node({
|
|
26
|
+
kind: "object",
|
|
27
|
+
shape: Object.freeze({ ...shape })
|
|
28
|
+
});
|
|
29
|
+
const variant = (tag, variants) => node({
|
|
30
|
+
kind: "variant",
|
|
31
|
+
tag,
|
|
32
|
+
variants: Object.freeze({ ...variants })
|
|
33
|
+
});
|
|
34
|
+
const table = (value, options) => node({
|
|
35
|
+
kind: "table",
|
|
36
|
+
value,
|
|
37
|
+
...options
|
|
38
|
+
});
|
|
39
|
+
const map = (value, options) => node({
|
|
40
|
+
kind: "map",
|
|
41
|
+
value,
|
|
42
|
+
...options
|
|
43
|
+
});
|
|
44
|
+
const list = (value, config) => node({
|
|
45
|
+
kind: "list",
|
|
46
|
+
keyOf: config.keyOf,
|
|
47
|
+
value
|
|
48
|
+
});
|
|
49
|
+
const tree = (value) => node({
|
|
50
|
+
kind: "tree",
|
|
51
|
+
value
|
|
52
|
+
});
|
|
53
|
+
const paths = /* @__PURE__ */ new WeakMap();
|
|
54
|
+
const pathProxy = (nodes, address, owner) => {
|
|
55
|
+
const proxy = new Proxy({}, { get: (_target, property) => {
|
|
56
|
+
if (typeof property !== "string") return void 0;
|
|
57
|
+
if (nodes.every((node) => node.kind === "table" || node.kind === "map") && property === "item") return (id) => {
|
|
58
|
+
if (typeof id !== "string") throw new TypeError("Collection keys must be strings.");
|
|
59
|
+
for (const node of nodes) if (node.kind === "table" || node.kind === "map") {
|
|
60
|
+
const invalid = checkKey(node.key, id, [...address, id]);
|
|
61
|
+
if (invalid) throw new TypeError(invalid.message);
|
|
62
|
+
}
|
|
63
|
+
return pathProxy(nodes.map((node) => node.value), [...address, id], owner);
|
|
64
|
+
};
|
|
65
|
+
const children = nodes.flatMap((node) => {
|
|
66
|
+
if (node.kind === "object") return Object.hasOwn(node.shape, property) ? [node.shape[property]] : [];
|
|
67
|
+
if (node.kind !== "variant") return [];
|
|
68
|
+
if (property === node.tag) return [field()];
|
|
69
|
+
return Object.values(node.variants).flatMap((branch) => Object.hasOwn(branch.shape, property) ? [branch.shape[property]] : []);
|
|
70
|
+
});
|
|
71
|
+
if (!children.length) throw new TypeError(`Invalid schema path segment: ${property}`);
|
|
72
|
+
return pathProxy(children, [...address, property], owner);
|
|
73
|
+
} });
|
|
74
|
+
paths.set(proxy, {
|
|
75
|
+
nodes,
|
|
76
|
+
address,
|
|
77
|
+
owner
|
|
78
|
+
});
|
|
79
|
+
return proxy;
|
|
80
|
+
};
|
|
81
|
+
const compilePath = (owner, kind, pick) => {
|
|
82
|
+
const scope = {};
|
|
83
|
+
const value = pick(pathProxy([owner], [], scope));
|
|
84
|
+
const selected = typeof value === "object" && value !== null ? paths.get(value) : void 0;
|
|
85
|
+
if (!selected || selected.owner !== scope) throw new TypeError("Selector must return a path from its callback.");
|
|
86
|
+
if (kind === "collection" && !selected.nodes.every((node) => node.kind === "table" || node.kind === "map")) throw new TypeError("Collection selectors require a table or map path.");
|
|
87
|
+
return Object.freeze({
|
|
88
|
+
kind,
|
|
89
|
+
schema: owner,
|
|
90
|
+
address: Object.freeze([...selected.address])
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region core/src/impact.ts
|
|
95
|
+
const queries = /* @__PURE__ */ new WeakMap();
|
|
96
|
+
const affectsTarget = (impact, value) => queries.get(impact).affects(value);
|
|
97
|
+
const collectionImpact = (impact, selector) => queries.get(impact).collection(selector);
|
|
98
|
+
const createImpact = (schema, changes) => {
|
|
99
|
+
const reset = changes.changes.some((change) => change.kind === "reset");
|
|
100
|
+
let values;
|
|
101
|
+
let orders;
|
|
102
|
+
const index = () => {
|
|
103
|
+
if (!values) {
|
|
104
|
+
profile.impact.index();
|
|
105
|
+
values = new AddressIndex();
|
|
106
|
+
orders = new AddressIndex();
|
|
107
|
+
for (const change of changes.changes) if (change.kind === "members") {
|
|
108
|
+
for (const member of change.members) values.add(change.at, true, member.key);
|
|
109
|
+
if (change.order) orders.add(change.at, true);
|
|
110
|
+
} else if (change.kind === "tree") values.add(change.at, true);
|
|
111
|
+
}
|
|
112
|
+
return values;
|
|
113
|
+
};
|
|
114
|
+
const cache = /* @__PURE__ */ new Map();
|
|
115
|
+
const implementation = {
|
|
116
|
+
affects(value) {
|
|
117
|
+
profile.impact.affects();
|
|
118
|
+
if (!belongs(value, schema)) return false;
|
|
119
|
+
if (reset) return true;
|
|
120
|
+
return affected(value, index(), orders);
|
|
121
|
+
},
|
|
122
|
+
collection(selector) {
|
|
123
|
+
if (selector.schema !== schema) throw new TypeError("Collection belongs to another root model.");
|
|
124
|
+
const key = JSON.stringify(selector.address), cached = cache.get(key);
|
|
125
|
+
if (cached) return cached;
|
|
126
|
+
if (reset) {
|
|
127
|
+
const result = { kind: "reset" };
|
|
128
|
+
cache.set(key, result);
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
const at = selector.address;
|
|
132
|
+
const added = /* @__PURE__ */ new Set(), removed = /* @__PURE__ */ new Set(), updated = /* @__PURE__ */ new Set();
|
|
133
|
+
let orderChanged = false;
|
|
134
|
+
for (const change of changes.changes) {
|
|
135
|
+
if (change.kind === "reset") continue;
|
|
136
|
+
if (change.kind === "members") {
|
|
137
|
+
if (change.at.length < at.length && contains(change.at, at)) {
|
|
138
|
+
if (change.members.some((member) => member.key === at[change.at.length])) {
|
|
139
|
+
const result = { kind: "reset" };
|
|
140
|
+
cache.set(key, result);
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
} else if (contains(at, change.at)) if (change.at.length === at.length) {
|
|
144
|
+
if (change.order) {
|
|
145
|
+
const positions = new Map(change.order.after.map((id, i) => [id, i]));
|
|
146
|
+
let previous = -1;
|
|
147
|
+
for (const id of change.order.before) {
|
|
148
|
+
const position = positions.get(id);
|
|
149
|
+
if (position === void 0) continue;
|
|
150
|
+
if (position < previous) orderChanged = true;
|
|
151
|
+
previous = position;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const member of change.members) if (member.kind === "added") added.add(member.key);
|
|
155
|
+
else if (member.kind === "removed") removed.add(member.key);
|
|
156
|
+
else updated.add(member.key);
|
|
157
|
+
} else updated.add(change.at[at.length]);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (change.kind === "tree" && contains(change.at, at)) {
|
|
161
|
+
const result = { kind: "reset" };
|
|
162
|
+
cache.set(key, result);
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
if (!contains(at, change.at)) continue;
|
|
166
|
+
if (change.at.length === at.length) continue;
|
|
167
|
+
const id = change.at[at.length];
|
|
168
|
+
updated.add(id);
|
|
169
|
+
}
|
|
170
|
+
added.forEach((id) => updated.delete(id));
|
|
171
|
+
removed.forEach((id) => updated.delete(id));
|
|
172
|
+
const result = {
|
|
173
|
+
kind: "incremental",
|
|
174
|
+
added,
|
|
175
|
+
removed,
|
|
176
|
+
updated,
|
|
177
|
+
orderChanged
|
|
178
|
+
};
|
|
179
|
+
cache.set(key, result);
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
const impact = Object.freeze({
|
|
184
|
+
kind: reset ? "reset" : "incremental",
|
|
185
|
+
affects: (pick) => implementation.affects(compilePath(schema, "value", pick)),
|
|
186
|
+
collection: (pick) => implementation.collection(compilePath(schema, "collection", pick))
|
|
187
|
+
});
|
|
188
|
+
queries.set(impact, implementation);
|
|
189
|
+
return impact;
|
|
190
|
+
};
|
|
191
|
+
//#endregion
|
|
5
192
|
//#region core/src/history.ts
|
|
6
193
|
const createHistory = (input) => {
|
|
7
194
|
let undos = [];
|
|
@@ -916,6 +1103,1818 @@ const asReadable = (runtime) => {
|
|
|
916
1103
|
//#region core/src/projection/select.ts
|
|
917
1104
|
const read = (runtime, selector) => readWith(runtime, selector);
|
|
918
1105
|
//#endregion
|
|
1106
|
+
//#region core/src/projection/contract.ts
|
|
1107
|
+
var ProjectionError = class extends Error {
|
|
1108
|
+
constructor(phase, identity, revisions, cause) {
|
|
1109
|
+
super(`Projection ${phase} failed: ${identity}`, { cause });
|
|
1110
|
+
this.phase = phase;
|
|
1111
|
+
this.identity = identity;
|
|
1112
|
+
this.revisions = revisions;
|
|
1113
|
+
this.name = "ProjectionError";
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
var ProjectionDisposedError = class extends Error {
|
|
1117
|
+
constructor() {
|
|
1118
|
+
super("Projection has been disposed.");
|
|
1119
|
+
this.name = "ProjectionDisposedError";
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
//#endregion
|
|
1123
|
+
//#region core/src/projection/scheduler.ts
|
|
1124
|
+
const projectionHandles = /* @__PURE__ */ new WeakSet();
|
|
1125
|
+
const createScheduler = (onError) => {
|
|
1126
|
+
let disposed = false;
|
|
1127
|
+
let depth = 0;
|
|
1128
|
+
let batchSequence = 0;
|
|
1129
|
+
let activeBatch;
|
|
1130
|
+
let phase = "idle";
|
|
1131
|
+
let sequence = 0;
|
|
1132
|
+
const records = /* @__PURE__ */ new Map();
|
|
1133
|
+
const nodes = /* @__PURE__ */ new Set();
|
|
1134
|
+
const pending = /* @__PURE__ */ new Set();
|
|
1135
|
+
const heap = [];
|
|
1136
|
+
const queued = /* @__PURE__ */ new Set();
|
|
1137
|
+
const completed = [];
|
|
1138
|
+
const emissions = /* @__PURE__ */ new Set();
|
|
1139
|
+
const errors = [];
|
|
1140
|
+
let reportingFailures = [];
|
|
1141
|
+
const guards = /* @__PURE__ */ new Set();
|
|
1142
|
+
const cleanups = /* @__PURE__ */ new Set();
|
|
1143
|
+
const assertActive = () => {
|
|
1144
|
+
if (disposed) throw new ProjectionDisposedError();
|
|
1145
|
+
};
|
|
1146
|
+
const assertIdle = () => {
|
|
1147
|
+
assertActive();
|
|
1148
|
+
if (phase !== "idle") throw new Error("Projection cannot be re-entered during processing or notification.");
|
|
1149
|
+
};
|
|
1150
|
+
const lock = (value) => guards.forEach((guard) => guard(value));
|
|
1151
|
+
const enqueue = (node) => {
|
|
1152
|
+
if (node.disposed || queued.has(node)) return;
|
|
1153
|
+
queued.add(node);
|
|
1154
|
+
profile.projection("scheduledNodes");
|
|
1155
|
+
let i = heap.length;
|
|
1156
|
+
heap.push(node);
|
|
1157
|
+
while (i > 0) {
|
|
1158
|
+
const p = i - 1 >> 1;
|
|
1159
|
+
if (heap[p].order < node.order) break;
|
|
1160
|
+
heap[i] = heap[p];
|
|
1161
|
+
i = p;
|
|
1162
|
+
}
|
|
1163
|
+
heap[i] = node;
|
|
1164
|
+
};
|
|
1165
|
+
const pop = () => {
|
|
1166
|
+
const first = heap[0];
|
|
1167
|
+
const last = heap.pop();
|
|
1168
|
+
if (heap.length) {
|
|
1169
|
+
let i = 0;
|
|
1170
|
+
while (i * 2 + 1 < heap.length) {
|
|
1171
|
+
let c = i * 2 + 1;
|
|
1172
|
+
if (c + 1 < heap.length && heap[c + 1].order < heap[c].order) c++;
|
|
1173
|
+
if (last.order < heap[c].order) break;
|
|
1174
|
+
heap[i] = heap[c];
|
|
1175
|
+
i = c;
|
|
1176
|
+
}
|
|
1177
|
+
heap[i] = last;
|
|
1178
|
+
}
|
|
1179
|
+
return first;
|
|
1180
|
+
};
|
|
1181
|
+
const errorFor = (node, cause, kind = "processor") => new ProjectionError(kind, node.name, node.sources.map((source) => source.revision()), cause);
|
|
1182
|
+
const capture = (source) => {
|
|
1183
|
+
if (disposed) return;
|
|
1184
|
+
if (phase !== "idle") {
|
|
1185
|
+
source.fault = new ProjectionError("source", "reentrant source", [source.revision()], /* @__PURE__ */ new Error("Source changed while projection was running."));
|
|
1186
|
+
errors.push(source.fault);
|
|
1187
|
+
throw source.fault;
|
|
1188
|
+
}
|
|
1189
|
+
pending.add(source);
|
|
1190
|
+
profile.projection("sourceEvents");
|
|
1191
|
+
if (source.fault) errors.push(source.fault);
|
|
1192
|
+
};
|
|
1193
|
+
const settle = () => {
|
|
1194
|
+
if (disposed || depth || phase !== "idle") return;
|
|
1195
|
+
phase = "compute";
|
|
1196
|
+
lock(true);
|
|
1197
|
+
try {
|
|
1198
|
+
pending.forEach((source) => source.consumers.forEach(enqueue));
|
|
1199
|
+
while (heap.length) {
|
|
1200
|
+
const node = pop();
|
|
1201
|
+
if (node.disposed) continue;
|
|
1202
|
+
const wasFaulted = node.fault !== void 0;
|
|
1203
|
+
profile.projection("processedNodes");
|
|
1204
|
+
let changed = false;
|
|
1205
|
+
const blocked = node.sources.find((source) => source.fault || source.disposed);
|
|
1206
|
+
if (blocked) node.fault = errorFor(node, blocked.fault ?? new ProjectionDisposedError(), "blocked");
|
|
1207
|
+
else {
|
|
1208
|
+
const build = node.forced || wasFaulted || node.sources.some((source) => source.reset());
|
|
1209
|
+
try {
|
|
1210
|
+
changed = node.evaluate(build);
|
|
1211
|
+
const failure = node.sources.find((source) => source.fault)?.fault;
|
|
1212
|
+
if (failure) throw failure;
|
|
1213
|
+
node.fault = void 0;
|
|
1214
|
+
} catch (cause) {
|
|
1215
|
+
const error = errorFor(node, cause);
|
|
1216
|
+
errors.push(error);
|
|
1217
|
+
node.fault = error;
|
|
1218
|
+
if (!build) try {
|
|
1219
|
+
changed = node.evaluate(true);
|
|
1220
|
+
const failure = node.sources.find((source) => source.fault)?.fault;
|
|
1221
|
+
if (failure) throw failure;
|
|
1222
|
+
node.fault = void 0;
|
|
1223
|
+
} catch (cause) {
|
|
1224
|
+
node.fault = errorFor(node, cause);
|
|
1225
|
+
errors.push(node.fault);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
node.forced = false;
|
|
1230
|
+
node.statusChanged = wasFaulted !== (node.fault !== void 0);
|
|
1231
|
+
completed.push(node);
|
|
1232
|
+
if (changed || wasFaulted || node.fault) {
|
|
1233
|
+
profile.projection("publishedNodes");
|
|
1234
|
+
emissions.add(node);
|
|
1235
|
+
node.consumers.forEach(enqueue);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
completed.forEach((node) => node.publish());
|
|
1239
|
+
} finally {
|
|
1240
|
+
lock(false);
|
|
1241
|
+
phase = "idle";
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1244
|
+
const flush = () => {
|
|
1245
|
+
if (disposed || depth) return [];
|
|
1246
|
+
if (pending.size || completed.length) profile.projection("flushes");
|
|
1247
|
+
phase = "notify";
|
|
1248
|
+
lock(true);
|
|
1249
|
+
const reportFailures = [];
|
|
1250
|
+
try {
|
|
1251
|
+
emissions.forEach((node) => node.emit((listener) => {
|
|
1252
|
+
try {
|
|
1253
|
+
listener();
|
|
1254
|
+
} catch (cause) {
|
|
1255
|
+
errors.push(errorFor(node, cause, "listener"));
|
|
1256
|
+
}
|
|
1257
|
+
}));
|
|
1258
|
+
for (const error of errors.slice()) try {
|
|
1259
|
+
onError(error);
|
|
1260
|
+
} catch (cause) {
|
|
1261
|
+
reportFailures.push(cause);
|
|
1262
|
+
}
|
|
1263
|
+
return Object.freeze([...errors.map((error) => Object.freeze({
|
|
1264
|
+
phase: error.phase === "listener" ? "listener" : "processor",
|
|
1265
|
+
error
|
|
1266
|
+
})), ...reportFailures.map((error) => Object.freeze({
|
|
1267
|
+
phase: "listener",
|
|
1268
|
+
error
|
|
1269
|
+
}))]);
|
|
1270
|
+
} finally {
|
|
1271
|
+
pending.forEach((source) => source.clear());
|
|
1272
|
+
completed.forEach((node) => node.clear());
|
|
1273
|
+
pending.clear();
|
|
1274
|
+
queued.clear();
|
|
1275
|
+
completed.length = 0;
|
|
1276
|
+
emissions.clear();
|
|
1277
|
+
errors.length = 0;
|
|
1278
|
+
reportingFailures = reportFailures;
|
|
1279
|
+
lock(false);
|
|
1280
|
+
phase = "idle";
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
const run = () => {
|
|
1284
|
+
settle();
|
|
1285
|
+
const result = flush();
|
|
1286
|
+
const failures = reportingFailures;
|
|
1287
|
+
reportingFailures = [];
|
|
1288
|
+
if (failures.length) throw new AggregateError(failures, "Projection error reporter failed.");
|
|
1289
|
+
return result;
|
|
1290
|
+
};
|
|
1291
|
+
const batch = (optionsOrCallback, maybeCallback) => {
|
|
1292
|
+
assertIdle();
|
|
1293
|
+
const options = typeof optionsOrCallback === "function" ? void 0 : optionsOrCallback;
|
|
1294
|
+
const callback = typeof optionsOrCallback === "function" ? optionsOrCallback : maybeCallback;
|
|
1295
|
+
if (!callback) throw new TypeError("Projection batch requires a callback.");
|
|
1296
|
+
if (depth === 0) activeBatch = Object.freeze({
|
|
1297
|
+
id: ++batchSequence,
|
|
1298
|
+
...options?.cause === void 0 ? {} : { cause: options.cause }
|
|
1299
|
+
});
|
|
1300
|
+
depth++;
|
|
1301
|
+
let failed = false;
|
|
1302
|
+
try {
|
|
1303
|
+
const value = callback();
|
|
1304
|
+
assertSynchronous(value);
|
|
1305
|
+
return value;
|
|
1306
|
+
} catch (error) {
|
|
1307
|
+
failed = true;
|
|
1308
|
+
throw error;
|
|
1309
|
+
} finally {
|
|
1310
|
+
depth--;
|
|
1311
|
+
if (!depth) try {
|
|
1312
|
+
if (failed) try {
|
|
1313
|
+
run();
|
|
1314
|
+
} catch {}
|
|
1315
|
+
else run();
|
|
1316
|
+
} finally {
|
|
1317
|
+
activeBatch = void 0;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
return {
|
|
1322
|
+
assertActive,
|
|
1323
|
+
assertIdle,
|
|
1324
|
+
capture,
|
|
1325
|
+
settle,
|
|
1326
|
+
flush,
|
|
1327
|
+
run,
|
|
1328
|
+
records,
|
|
1329
|
+
guards,
|
|
1330
|
+
cleanups,
|
|
1331
|
+
get active() {
|
|
1332
|
+
return !disposed;
|
|
1333
|
+
},
|
|
1334
|
+
register(handle, record) {
|
|
1335
|
+
assertIdle();
|
|
1336
|
+
records.set(handle, record);
|
|
1337
|
+
projectionHandles.add(handle);
|
|
1338
|
+
},
|
|
1339
|
+
unregisterSource(handle) {
|
|
1340
|
+
const record = records.get(handle);
|
|
1341
|
+
if (record) {
|
|
1342
|
+
record.disposed = true;
|
|
1343
|
+
pending.delete(record);
|
|
1344
|
+
record.clear();
|
|
1345
|
+
records.delete(handle);
|
|
1346
|
+
}
|
|
1347
|
+
},
|
|
1348
|
+
source(handle) {
|
|
1349
|
+
assertActive();
|
|
1350
|
+
const source = records.get(handle);
|
|
1351
|
+
if (!source || source.disposed) throw new Error("Unknown, disposed, or foreign projection source.");
|
|
1352
|
+
return source;
|
|
1353
|
+
},
|
|
1354
|
+
addNode(node) {
|
|
1355
|
+
nodes.add(node);
|
|
1356
|
+
node.sources.forEach((source) => source.consumers.add(node));
|
|
1357
|
+
},
|
|
1358
|
+
order: () => sequence++,
|
|
1359
|
+
initialize(run) {
|
|
1360
|
+
assertIdle();
|
|
1361
|
+
phase = "compute";
|
|
1362
|
+
lock(true);
|
|
1363
|
+
try {
|
|
1364
|
+
return run();
|
|
1365
|
+
} finally {
|
|
1366
|
+
lock(false);
|
|
1367
|
+
phase = "idle";
|
|
1368
|
+
}
|
|
1369
|
+
},
|
|
1370
|
+
rebuild(node) {
|
|
1371
|
+
assertIdle();
|
|
1372
|
+
if (node.disposed) throw new ProjectionDisposedError();
|
|
1373
|
+
node.forced = true;
|
|
1374
|
+
enqueue(node);
|
|
1375
|
+
run();
|
|
1376
|
+
},
|
|
1377
|
+
disposeNode(node) {
|
|
1378
|
+
if (node.disposed) return;
|
|
1379
|
+
assertIdle();
|
|
1380
|
+
if (node.consumers.size) throw new Error("Cannot dispose a projection node with downstream consumers.");
|
|
1381
|
+
node.disposed = true;
|
|
1382
|
+
node.sources.forEach((source) => source.consumers.delete(node));
|
|
1383
|
+
nodes.delete(node);
|
|
1384
|
+
node.release();
|
|
1385
|
+
for (const [handle, record] of records) if (record === node) records.delete(handle);
|
|
1386
|
+
},
|
|
1387
|
+
batch,
|
|
1388
|
+
batchContext: () => activeBatch,
|
|
1389
|
+
dispose() {
|
|
1390
|
+
if (disposed) return;
|
|
1391
|
+
assertIdle();
|
|
1392
|
+
disposed = true;
|
|
1393
|
+
const failures = [];
|
|
1394
|
+
cleanups.forEach((cleanup) => {
|
|
1395
|
+
try {
|
|
1396
|
+
cleanup();
|
|
1397
|
+
} catch (error) {
|
|
1398
|
+
failures.push(error);
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1401
|
+
cleanups.clear();
|
|
1402
|
+
nodes.forEach((node) => {
|
|
1403
|
+
node.disposed = true;
|
|
1404
|
+
node.release();
|
|
1405
|
+
node.consumers.clear();
|
|
1406
|
+
});
|
|
1407
|
+
records.forEach((source) => {
|
|
1408
|
+
source.disposed = true;
|
|
1409
|
+
source.consumers.clear();
|
|
1410
|
+
source.clear();
|
|
1411
|
+
});
|
|
1412
|
+
nodes.clear();
|
|
1413
|
+
records.clear();
|
|
1414
|
+
pending.clear();
|
|
1415
|
+
heap.length = 0;
|
|
1416
|
+
completed.length = 0;
|
|
1417
|
+
queued.clear();
|
|
1418
|
+
emissions.clear();
|
|
1419
|
+
guards.clear();
|
|
1420
|
+
errors.length = 0;
|
|
1421
|
+
reportingFailures = [];
|
|
1422
|
+
if (failures.length) throw new AggregateError(failures, "Projection cleanup failed.");
|
|
1423
|
+
},
|
|
1424
|
+
debug: () => ({
|
|
1425
|
+
nodes: nodes.size,
|
|
1426
|
+
sources: records.size - nodes.size,
|
|
1427
|
+
subscriptions: cleanups.size,
|
|
1428
|
+
pending: pending.size + heap.length
|
|
1429
|
+
})
|
|
1430
|
+
};
|
|
1431
|
+
};
|
|
1432
|
+
const assertSynchronous = (value) => {
|
|
1433
|
+
if (value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function") throw new TypeError("Projection callbacks must be synchronous.");
|
|
1434
|
+
};
|
|
1435
|
+
const assertScope = (active) => {
|
|
1436
|
+
if (!active()) throw new Error("Projection reader or writer is no longer active.");
|
|
1437
|
+
};
|
|
1438
|
+
//#endregion
|
|
1439
|
+
//#region core/src/projection/node.ts
|
|
1440
|
+
const createNode = (scheduler, spec, behavior) => {
|
|
1441
|
+
scheduler.assertIdle();
|
|
1442
|
+
const entries = Object.entries(spec.sources).map(([key, handle]) => [key, scheduler.source(handle)]);
|
|
1443
|
+
const order = scheduler.order();
|
|
1444
|
+
const node = {
|
|
1445
|
+
...behavior,
|
|
1446
|
+
publish: () => {
|
|
1447
|
+
if (node.fault) behavior.clear();
|
|
1448
|
+
else behavior.publish();
|
|
1449
|
+
},
|
|
1450
|
+
order,
|
|
1451
|
+
name: spec.name ? `${spec.name} (${order})` : `projection-${order}`,
|
|
1452
|
+
sources: entries.map(([, source]) => source),
|
|
1453
|
+
consumers: /* @__PURE__ */ new Set(),
|
|
1454
|
+
disposed: false,
|
|
1455
|
+
fault: void 0,
|
|
1456
|
+
forced: false,
|
|
1457
|
+
statusChanged: false,
|
|
1458
|
+
evaluate: (build) => {
|
|
1459
|
+
let active = true;
|
|
1460
|
+
try {
|
|
1461
|
+
const scopeActive = () => active;
|
|
1462
|
+
const inputs = Object.fromEntries(entries.map(([key, source]) => [key, source.context(scopeActive)]));
|
|
1463
|
+
return behavior.evaluate(inputs, build, scopeActive);
|
|
1464
|
+
} finally {
|
|
1465
|
+
active = false;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
};
|
|
1469
|
+
scheduler.initialize(() => {
|
|
1470
|
+
const invalid = node.sources.find((source) => source.fault || source.disposed);
|
|
1471
|
+
if (invalid) throw invalid.fault ?? new ProjectionDisposedError();
|
|
1472
|
+
node.evaluate(true);
|
|
1473
|
+
node.publish();
|
|
1474
|
+
node.clear();
|
|
1475
|
+
});
|
|
1476
|
+
const check = () => {
|
|
1477
|
+
if (node.disposed || !scheduler.active) throw new ProjectionDisposedError();
|
|
1478
|
+
if (node.fault) throw node.fault;
|
|
1479
|
+
};
|
|
1480
|
+
return {
|
|
1481
|
+
node,
|
|
1482
|
+
check,
|
|
1483
|
+
install(handle) {
|
|
1484
|
+
scheduler.register(handle, node);
|
|
1485
|
+
scheduler.addNode(node);
|
|
1486
|
+
},
|
|
1487
|
+
rebuild: () => scheduler.rebuild(node),
|
|
1488
|
+
dispose: () => scheduler.disposeNode(node)
|
|
1489
|
+
};
|
|
1490
|
+
};
|
|
1491
|
+
//#endregion
|
|
1492
|
+
//#region core/src/projection/collection.ts
|
|
1493
|
+
const collectionHandles = /* @__PURE__ */ new WeakSet();
|
|
1494
|
+
const readonlySet = (values) => {
|
|
1495
|
+
const set = new Set(values);
|
|
1496
|
+
const view = Object.freeze({
|
|
1497
|
+
get size() {
|
|
1498
|
+
return set.size;
|
|
1499
|
+
},
|
|
1500
|
+
has: (value) => set.has(value),
|
|
1501
|
+
entries: () => set.entries(),
|
|
1502
|
+
keys: () => set.keys(),
|
|
1503
|
+
values: () => set.values(),
|
|
1504
|
+
[Symbol.iterator]: () => set.values(),
|
|
1505
|
+
forEach: (callback, thisArg) => set.forEach((value) => callback.call(thisArg, value, value, view))
|
|
1506
|
+
});
|
|
1507
|
+
return view;
|
|
1508
|
+
};
|
|
1509
|
+
const createCollection = (scheduler, spec) => {
|
|
1510
|
+
const values = /* @__PURE__ */ new Map();
|
|
1511
|
+
let ids = Object.freeze([]);
|
|
1512
|
+
let staged = /* @__PURE__ */ new Map();
|
|
1513
|
+
let nextIds = ids;
|
|
1514
|
+
let change;
|
|
1515
|
+
let instance;
|
|
1516
|
+
let initialized = false;
|
|
1517
|
+
let reset = false;
|
|
1518
|
+
let revision = 0;
|
|
1519
|
+
let idsRevision = 0;
|
|
1520
|
+
let batch;
|
|
1521
|
+
let cause;
|
|
1522
|
+
let publishedTransitions = Object.freeze([]);
|
|
1523
|
+
let all;
|
|
1524
|
+
const items = /* @__PURE__ */ new Map();
|
|
1525
|
+
const subscribedItems = /* @__PURE__ */ new Set();
|
|
1526
|
+
const idsListeners = /* @__PURE__ */ new Set();
|
|
1527
|
+
const allListeners = /* @__PURE__ */ new Set();
|
|
1528
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1529
|
+
const changedKeys = /* @__PURE__ */ new Set();
|
|
1530
|
+
let orderChanged = false;
|
|
1531
|
+
const current = Object.freeze({
|
|
1532
|
+
get: (key) => {
|
|
1533
|
+
owner.check();
|
|
1534
|
+
return values.get(key);
|
|
1535
|
+
},
|
|
1536
|
+
has: (key) => {
|
|
1537
|
+
owner.check();
|
|
1538
|
+
return values.has(key);
|
|
1539
|
+
},
|
|
1540
|
+
ids: () => {
|
|
1541
|
+
owner.check();
|
|
1542
|
+
return ids;
|
|
1543
|
+
}
|
|
1544
|
+
});
|
|
1545
|
+
const hasNext = (key) => staged.has(key) ? staged.get(key).present : values.has(key);
|
|
1546
|
+
const getNext = (key) => {
|
|
1547
|
+
const entry = staged.get(key);
|
|
1548
|
+
return entry ? entry.present ? entry.value : void 0 : values.get(key);
|
|
1549
|
+
};
|
|
1550
|
+
const owner = createNode(scheduler, spec, {
|
|
1551
|
+
evaluate: (sources, build, active) => {
|
|
1552
|
+
const metadata = Object.values(sources).find((value) => value && typeof value === "object" && ("batch" in value && value.batch !== void 0 || "cause" in value && value.cause !== void 0));
|
|
1553
|
+
batch = scheduler.batchContext() ?? metadata?.batch;
|
|
1554
|
+
cause = metadata?.cause ?? batch?.cause;
|
|
1555
|
+
staged = /* @__PURE__ */ new Map();
|
|
1556
|
+
nextIds = ids;
|
|
1557
|
+
change = void 0;
|
|
1558
|
+
changedKeys.clear();
|
|
1559
|
+
let explicitOrder;
|
|
1560
|
+
let cleared = false;
|
|
1561
|
+
const nextHas = (key) => staged.has(key) ? staged.get(key).present : !cleared && values.has(key);
|
|
1562
|
+
const nextGet = (key) => {
|
|
1563
|
+
const entry = staged.get(key);
|
|
1564
|
+
return entry ? entry.present ? entry.value : void 0 : cleared ? void 0 : values.get(key);
|
|
1565
|
+
};
|
|
1566
|
+
const deriveIds = () => {
|
|
1567
|
+
const result = cleared ? [] : ids.filter(nextHas);
|
|
1568
|
+
for (const [key, entry] of staged) if (entry.present && (cleared || !values.has(key))) result.push(key);
|
|
1569
|
+
return result;
|
|
1570
|
+
};
|
|
1571
|
+
const read = (next) => ({
|
|
1572
|
+
get: (key) => {
|
|
1573
|
+
assertScope(active);
|
|
1574
|
+
return next ? nextGet(key) : values.get(key);
|
|
1575
|
+
},
|
|
1576
|
+
has: (key) => {
|
|
1577
|
+
assertScope(active);
|
|
1578
|
+
return next ? nextHas(key) : values.has(key);
|
|
1579
|
+
},
|
|
1580
|
+
ids: () => {
|
|
1581
|
+
assertScope(active);
|
|
1582
|
+
return next ? Object.freeze(explicitOrder ? explicitOrder.slice() : deriveIds()) : ids;
|
|
1583
|
+
}
|
|
1584
|
+
});
|
|
1585
|
+
const writer = {
|
|
1586
|
+
set: (key, value) => {
|
|
1587
|
+
assertScope(active);
|
|
1588
|
+
if (typeof key !== "string") throw new TypeError("Projection keys must be strings.");
|
|
1589
|
+
staged.set(key, {
|
|
1590
|
+
present: true,
|
|
1591
|
+
value
|
|
1592
|
+
});
|
|
1593
|
+
},
|
|
1594
|
+
remove: (key) => {
|
|
1595
|
+
assertScope(active);
|
|
1596
|
+
staged.set(key, { present: false });
|
|
1597
|
+
},
|
|
1598
|
+
order: (order) => {
|
|
1599
|
+
assertScope(active);
|
|
1600
|
+
explicitOrder = order.slice();
|
|
1601
|
+
},
|
|
1602
|
+
replace: (entries) => {
|
|
1603
|
+
assertScope(active);
|
|
1604
|
+
staged.clear();
|
|
1605
|
+
cleared = true;
|
|
1606
|
+
const order = [];
|
|
1607
|
+
for (const [key, value] of entries) {
|
|
1608
|
+
if (staged.has(key)) throw new TypeError("Duplicate projection key.");
|
|
1609
|
+
writer.set(key, value);
|
|
1610
|
+
order.push(key);
|
|
1611
|
+
}
|
|
1612
|
+
explicitOrder = order;
|
|
1613
|
+
}
|
|
1614
|
+
};
|
|
1615
|
+
const input = {
|
|
1616
|
+
sources,
|
|
1617
|
+
previous: read(false),
|
|
1618
|
+
next: read(true),
|
|
1619
|
+
writer
|
|
1620
|
+
};
|
|
1621
|
+
if (build || !instance) {
|
|
1622
|
+
profile.materialized.rebuilt();
|
|
1623
|
+
instance = void 0;
|
|
1624
|
+
cleared = true;
|
|
1625
|
+
const built = spec.build(input);
|
|
1626
|
+
assertSynchronous(built);
|
|
1627
|
+
instance = built;
|
|
1628
|
+
} else {
|
|
1629
|
+
profile.materialized.updated();
|
|
1630
|
+
const result = instance.update(input);
|
|
1631
|
+
assertSynchronous(result);
|
|
1632
|
+
if (result?.kind === "rebuild") {
|
|
1633
|
+
staged.clear();
|
|
1634
|
+
cleared = true;
|
|
1635
|
+
explicitOrder = void 0;
|
|
1636
|
+
instance = void 0;
|
|
1637
|
+
const built = spec.build(input);
|
|
1638
|
+
assertSynchronous(built);
|
|
1639
|
+
instance = built;
|
|
1640
|
+
build = true;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
if (cleared) {
|
|
1644
|
+
for (const key of values.keys()) if (!staged.has(key)) staged.set(key, { present: false });
|
|
1645
|
+
}
|
|
1646
|
+
const added = /* @__PURE__ */ new Set();
|
|
1647
|
+
const removed = /* @__PURE__ */ new Set();
|
|
1648
|
+
const updated = /* @__PURE__ */ new Set();
|
|
1649
|
+
profile.projection("touchedKeys", staged.size);
|
|
1650
|
+
for (const [key, entry] of staged) {
|
|
1651
|
+
const existed = values.has(key);
|
|
1652
|
+
if (entry.present) if (!existed) added.add(key);
|
|
1653
|
+
else if (!(spec.isEqual ?? Object.is)(values.get(key), entry.value)) updated.add(key);
|
|
1654
|
+
else staged.delete(key);
|
|
1655
|
+
else if (existed) removed.add(key);
|
|
1656
|
+
else staged.delete(key);
|
|
1657
|
+
}
|
|
1658
|
+
if (explicitOrder || added.size || removed.size) {
|
|
1659
|
+
const order = explicitOrder ?? [...ids.filter((key) => !removed.has(key)), ...added];
|
|
1660
|
+
if (new Set(order).size !== order.length || order.length !== values.size + added.size - removed.size || order.some((key) => !hasNext(key))) throw new TypeError("Projection order must contain every key exactly once.");
|
|
1661
|
+
nextIds = ids.length === order.length && ids.every((key, i) => key === order[i]) ? ids : Object.freeze(order.slice());
|
|
1662
|
+
}
|
|
1663
|
+
orderChanged = nextIds !== ids;
|
|
1664
|
+
if (added.size || removed.size || updated.size || orderChanged) {
|
|
1665
|
+
let commonOrderChanged = false;
|
|
1666
|
+
if (orderChanged) {
|
|
1667
|
+
const before = ids.filter((key) => !removed.has(key));
|
|
1668
|
+
const after = nextIds.filter((key) => !added.has(key));
|
|
1669
|
+
commonOrderChanged = before.some((key, i) => after[i] !== key);
|
|
1670
|
+
}
|
|
1671
|
+
change = Object.freeze({
|
|
1672
|
+
kind: "incremental",
|
|
1673
|
+
added: readonlySet(added),
|
|
1674
|
+
removed: readonlySet(removed),
|
|
1675
|
+
updated: readonlySet(updated),
|
|
1676
|
+
orderChanged: commonOrderChanged
|
|
1677
|
+
});
|
|
1678
|
+
[
|
|
1679
|
+
...added,
|
|
1680
|
+
...removed,
|
|
1681
|
+
...updated
|
|
1682
|
+
].forEach((key) => changedKeys.add(key));
|
|
1683
|
+
profile.projection("changedKeys", changedKeys.size);
|
|
1684
|
+
}
|
|
1685
|
+
publishedTransitions = Object.freeze([...changedKeys].flatMap((key) => {
|
|
1686
|
+
const beforePresent = values.has(key);
|
|
1687
|
+
const afterPresent = hasNext(key);
|
|
1688
|
+
if (beforePresent === afterPresent) {
|
|
1689
|
+
if (!beforePresent) return [];
|
|
1690
|
+
if ((spec.isEqual ?? Object.is)(values.get(key), getNext(key))) return [];
|
|
1691
|
+
}
|
|
1692
|
+
return [{
|
|
1693
|
+
key,
|
|
1694
|
+
kind: !beforePresent ? "added" : !afterPresent ? "removed" : "updated",
|
|
1695
|
+
before: beforePresent ? values.get(key) : void 0,
|
|
1696
|
+
after: afterPresent ? getNext(key) : void 0
|
|
1697
|
+
}];
|
|
1698
|
+
}));
|
|
1699
|
+
reset = build;
|
|
1700
|
+
return change !== void 0;
|
|
1701
|
+
},
|
|
1702
|
+
context: (active) => {
|
|
1703
|
+
const transitions = (keys) => {
|
|
1704
|
+
assertScope(active);
|
|
1705
|
+
const selected = keys ? new Set(keys) : void 0;
|
|
1706
|
+
return Object.freeze(publishedTransitions.filter((transition) => !selected || selected.has(transition.key)));
|
|
1707
|
+
};
|
|
1708
|
+
const input = {
|
|
1709
|
+
get: (key) => {
|
|
1710
|
+
assertScope(active);
|
|
1711
|
+
return getNext(key);
|
|
1712
|
+
},
|
|
1713
|
+
has: (key) => {
|
|
1714
|
+
assertScope(active);
|
|
1715
|
+
return hasNext(key);
|
|
1716
|
+
},
|
|
1717
|
+
ids: () => {
|
|
1718
|
+
assertScope(active);
|
|
1719
|
+
return nextIds;
|
|
1720
|
+
},
|
|
1721
|
+
previous: Object.freeze({
|
|
1722
|
+
get: (key) => {
|
|
1723
|
+
assertScope(active);
|
|
1724
|
+
return values.get(key);
|
|
1725
|
+
},
|
|
1726
|
+
has: (key) => {
|
|
1727
|
+
assertScope(active);
|
|
1728
|
+
return values.has(key);
|
|
1729
|
+
},
|
|
1730
|
+
ids: () => {
|
|
1731
|
+
assertScope(active);
|
|
1732
|
+
return ids;
|
|
1733
|
+
}
|
|
1734
|
+
}),
|
|
1735
|
+
change,
|
|
1736
|
+
revision: revision + (change ? 1 : 0),
|
|
1737
|
+
reset,
|
|
1738
|
+
transitions,
|
|
1739
|
+
batch,
|
|
1740
|
+
cause
|
|
1741
|
+
};
|
|
1742
|
+
return Object.freeze(input);
|
|
1743
|
+
},
|
|
1744
|
+
revision: () => revision,
|
|
1745
|
+
reset: () => reset,
|
|
1746
|
+
publish: () => {
|
|
1747
|
+
for (const [key, entry] of staged) if (entry.present) values.set(key, entry.value);
|
|
1748
|
+
else values.delete(key);
|
|
1749
|
+
if (change) {
|
|
1750
|
+
if (initialized) revision++;
|
|
1751
|
+
if (orderChanged && initialized) idsRevision++;
|
|
1752
|
+
all = void 0;
|
|
1753
|
+
changedKeys.forEach((key) => {
|
|
1754
|
+
const item = items.get(key);
|
|
1755
|
+
if (item && initialized) item.revision++;
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
ids = nextIds;
|
|
1759
|
+
initialized = true;
|
|
1760
|
+
},
|
|
1761
|
+
emit: (call) => {
|
|
1762
|
+
profile.materialized.notification();
|
|
1763
|
+
const fault = owner.node.fault !== void 0;
|
|
1764
|
+
const resetEvent = Object.freeze({ kind: "reset" });
|
|
1765
|
+
Array.from(listeners).forEach((listener) => call(() => listener(change ?? resetEvent)));
|
|
1766
|
+
if (orderChanged || !change || fault || owner.node.statusChanged) Array.from(idsListeners).forEach((listener) => call(listener));
|
|
1767
|
+
Array.from(allListeners).forEach((listener) => call(listener));
|
|
1768
|
+
if (!change || fault || owner.node.statusChanged) for (const item of subscribedItems) Array.from(item.listeners ?? []).forEach((listener) => call(listener));
|
|
1769
|
+
else for (const key of changedKeys) Array.from(items.get(key)?.listeners ?? []).forEach((listener) => call(listener));
|
|
1770
|
+
},
|
|
1771
|
+
clear: () => {
|
|
1772
|
+
staged.clear();
|
|
1773
|
+
nextIds = ids;
|
|
1774
|
+
change = void 0;
|
|
1775
|
+
reset = false;
|
|
1776
|
+
changedKeys.clear();
|
|
1777
|
+
publishedTransitions = Object.freeze([]);
|
|
1778
|
+
batch = void 0;
|
|
1779
|
+
cause = void 0;
|
|
1780
|
+
orderChanged = false;
|
|
1781
|
+
},
|
|
1782
|
+
release: () => {
|
|
1783
|
+
values.clear();
|
|
1784
|
+
staged.clear();
|
|
1785
|
+
items.clear();
|
|
1786
|
+
subscribedItems.clear();
|
|
1787
|
+
idsListeners.clear();
|
|
1788
|
+
allListeners.clear();
|
|
1789
|
+
listeners.clear();
|
|
1790
|
+
ids = nextIds = Object.freeze([]);
|
|
1791
|
+
all = void 0;
|
|
1792
|
+
instance = void 0;
|
|
1793
|
+
}
|
|
1794
|
+
});
|
|
1795
|
+
const handle = {
|
|
1796
|
+
current: () => {
|
|
1797
|
+
owner.check();
|
|
1798
|
+
return current;
|
|
1799
|
+
},
|
|
1800
|
+
ids: {
|
|
1801
|
+
current: () => {
|
|
1802
|
+
owner.check();
|
|
1803
|
+
return ids;
|
|
1804
|
+
},
|
|
1805
|
+
revision: () => {
|
|
1806
|
+
owner.check();
|
|
1807
|
+
return idsRevision;
|
|
1808
|
+
},
|
|
1809
|
+
subscribe: (listener) => {
|
|
1810
|
+
owner.check();
|
|
1811
|
+
idsListeners.add(listener);
|
|
1812
|
+
return () => {
|
|
1813
|
+
idsListeners.delete(listener);
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
},
|
|
1817
|
+
all: {
|
|
1818
|
+
current: () => {
|
|
1819
|
+
owner.check();
|
|
1820
|
+
if (!all) {
|
|
1821
|
+
profile.collectionView.arrayCopied();
|
|
1822
|
+
all = Object.freeze(ids.map((key) => values.get(key)));
|
|
1823
|
+
}
|
|
1824
|
+
return all;
|
|
1825
|
+
},
|
|
1826
|
+
revision: () => {
|
|
1827
|
+
owner.check();
|
|
1828
|
+
return revision;
|
|
1829
|
+
},
|
|
1830
|
+
subscribe: (listener) => {
|
|
1831
|
+
owner.check();
|
|
1832
|
+
allListeners.add(listener);
|
|
1833
|
+
return () => {
|
|
1834
|
+
allListeners.delete(listener);
|
|
1835
|
+
};
|
|
1836
|
+
}
|
|
1837
|
+
},
|
|
1838
|
+
item: (key) => {
|
|
1839
|
+
owner.check();
|
|
1840
|
+
const old = items.get(key);
|
|
1841
|
+
if (old) return old.readable;
|
|
1842
|
+
const readable = Object.freeze({
|
|
1843
|
+
current: () => {
|
|
1844
|
+
owner.check();
|
|
1845
|
+
return values.get(key);
|
|
1846
|
+
},
|
|
1847
|
+
revision: () => {
|
|
1848
|
+
owner.check();
|
|
1849
|
+
return item.revision;
|
|
1850
|
+
},
|
|
1851
|
+
subscribe: (listener) => {
|
|
1852
|
+
owner.check();
|
|
1853
|
+
const set = item.listeners ??= /* @__PURE__ */ new Set();
|
|
1854
|
+
set.add(listener);
|
|
1855
|
+
subscribedItems.add(item);
|
|
1856
|
+
return () => {
|
|
1857
|
+
set.delete(listener);
|
|
1858
|
+
if (!set.size) subscribedItems.delete(item);
|
|
1859
|
+
};
|
|
1860
|
+
}
|
|
1861
|
+
});
|
|
1862
|
+
const item = {
|
|
1863
|
+
readable,
|
|
1864
|
+
revision: 0
|
|
1865
|
+
};
|
|
1866
|
+
projectionHandles.add(readable);
|
|
1867
|
+
items.set(key, item);
|
|
1868
|
+
return readable;
|
|
1869
|
+
},
|
|
1870
|
+
revision: () => {
|
|
1871
|
+
owner.check();
|
|
1872
|
+
return revision;
|
|
1873
|
+
},
|
|
1874
|
+
subscribe: (listener) => {
|
|
1875
|
+
owner.check();
|
|
1876
|
+
listeners.add(listener);
|
|
1877
|
+
return () => {
|
|
1878
|
+
listeners.delete(listener);
|
|
1879
|
+
};
|
|
1880
|
+
},
|
|
1881
|
+
rebuild: owner.rebuild,
|
|
1882
|
+
dispose: owner.dispose
|
|
1883
|
+
};
|
|
1884
|
+
projectionHandles.add(handle.ids);
|
|
1885
|
+
projectionHandles.add(handle.all);
|
|
1886
|
+
collectionHandles.add(handle);
|
|
1887
|
+
owner.install(handle);
|
|
1888
|
+
return Object.freeze(handle);
|
|
1889
|
+
};
|
|
1890
|
+
//#endregion
|
|
1891
|
+
//#region core/src/projection/source.ts
|
|
1892
|
+
const createSources = (scheduler) => {
|
|
1893
|
+
const documents = /* @__PURE__ */ new Map();
|
|
1894
|
+
const readables = /* @__PURE__ */ new Map();
|
|
1895
|
+
const collections = /* @__PURE__ */ new WeakSet();
|
|
1896
|
+
const externalSources = /* @__PURE__ */ new WeakMap();
|
|
1897
|
+
const document = (runtime) => {
|
|
1898
|
+
scheduler.assertIdle();
|
|
1899
|
+
const state = accessOf(runtime);
|
|
1900
|
+
if (state.disposed) throw new Error("Document has been disposed.");
|
|
1901
|
+
const existing = documents.get(state);
|
|
1902
|
+
if (existing) return existing;
|
|
1903
|
+
const bindings = [];
|
|
1904
|
+
let batch;
|
|
1905
|
+
const make = (input) => {
|
|
1906
|
+
const collection = "collection" in input ? input.collection : void 0;
|
|
1907
|
+
const targets = "targets" in input ? input.targets : [input.collection];
|
|
1908
|
+
if (state.disposed) throw new Error("Document has been disposed.");
|
|
1909
|
+
targets.forEach((value) => {
|
|
1910
|
+
if (!belongs(value, state.schema)) throw new Error("Projection target belongs to another schema.");
|
|
1911
|
+
});
|
|
1912
|
+
const old = bindings.find((entry) => Boolean(collection) === collections.has(entry.handle) && entry.targets.length === targets.length && entry.targets.every((value, i) => same(value, targets[i])));
|
|
1913
|
+
if (old) return old.handle;
|
|
1914
|
+
let commits = [];
|
|
1915
|
+
let reset = false;
|
|
1916
|
+
let orderDirty = false;
|
|
1917
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1918
|
+
let candidates;
|
|
1919
|
+
const record = {
|
|
1920
|
+
consumers: /* @__PURE__ */ new Set(),
|
|
1921
|
+
disposed: false,
|
|
1922
|
+
fault: void 0,
|
|
1923
|
+
revision: runtime.revision,
|
|
1924
|
+
reset: () => reset,
|
|
1925
|
+
clear: () => {
|
|
1926
|
+
commits = [];
|
|
1927
|
+
reset = orderDirty = false;
|
|
1928
|
+
batch = void 0;
|
|
1929
|
+
keys.clear();
|
|
1930
|
+
candidates = void 0;
|
|
1931
|
+
},
|
|
1932
|
+
context: (active) => {
|
|
1933
|
+
assertScope(active);
|
|
1934
|
+
if (state.disposed) throw new Error("Document has been disposed.");
|
|
1935
|
+
const context = {
|
|
1936
|
+
state,
|
|
1937
|
+
active
|
|
1938
|
+
};
|
|
1939
|
+
const read = collection ? collectionAccess(context, collection.address) : createAccess(context);
|
|
1940
|
+
return Object.freeze({
|
|
1941
|
+
read,
|
|
1942
|
+
revision: runtime.revision(),
|
|
1943
|
+
commits: Object.freeze(commits.slice()),
|
|
1944
|
+
reset: record.reset(),
|
|
1945
|
+
batch,
|
|
1946
|
+
cause: batch?.cause,
|
|
1947
|
+
...collection ? { candidates: candidates ??= Object.freeze({
|
|
1948
|
+
keys: Object.freeze([...keys]),
|
|
1949
|
+
orderDirty
|
|
1950
|
+
}) } : {}
|
|
1951
|
+
});
|
|
1952
|
+
}
|
|
1953
|
+
};
|
|
1954
|
+
const handle = collection ? {} : {
|
|
1955
|
+
collection: (pick) => {
|
|
1956
|
+
scheduler.assertIdle();
|
|
1957
|
+
return make({ collection: compilePath(state.schema, "collection", pick) });
|
|
1958
|
+
},
|
|
1959
|
+
targets: (...selected) => {
|
|
1960
|
+
scheduler.assertIdle();
|
|
1961
|
+
if (!selected.length) throw new TypeError("Expected at least one target.");
|
|
1962
|
+
return make({ targets: selected.map((pick) => compilePath(state.schema, "value", pick)) });
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
Object.freeze(handle);
|
|
1966
|
+
if (collection) collections.add(handle);
|
|
1967
|
+
scheduler.register(handle, record);
|
|
1968
|
+
const receive = (commit) => {
|
|
1969
|
+
commits.push(commit);
|
|
1970
|
+
reset ||= commit.impact.kind === "reset";
|
|
1971
|
+
if (collection) {
|
|
1972
|
+
const change = collectionImpact(commit.impact, collection);
|
|
1973
|
+
if (change.kind === "reset") reset = true;
|
|
1974
|
+
else {
|
|
1975
|
+
change.added.forEach((key) => keys.add(key));
|
|
1976
|
+
change.removed.forEach((key) => keys.add(key));
|
|
1977
|
+
change.updated.forEach((key) => keys.add(key));
|
|
1978
|
+
orderDirty ||= Boolean(change.added.size || change.removed.size || change.orderChanged);
|
|
1979
|
+
}
|
|
1980
|
+
candidates = void 0;
|
|
1981
|
+
}
|
|
1982
|
+
};
|
|
1983
|
+
bindings.push({
|
|
1984
|
+
handle,
|
|
1985
|
+
targets,
|
|
1986
|
+
record,
|
|
1987
|
+
receive
|
|
1988
|
+
});
|
|
1989
|
+
return handle;
|
|
1990
|
+
};
|
|
1991
|
+
const handle = make({ targets: [] });
|
|
1992
|
+
const guard = (locked) => {
|
|
1993
|
+
state.projectionLocks = (state.projectionLocks ?? 0) + (locked ? 1 : -1);
|
|
1994
|
+
};
|
|
1995
|
+
scheduler.guards.add(guard);
|
|
1996
|
+
const unsubscribe = attachProjection(runtime, {
|
|
1997
|
+
capture: (commit) => {
|
|
1998
|
+
for (const binding of bindings) if (!binding.targets.length || binding.targets.some((value) => affectsTarget(commit.impact, value))) {
|
|
1999
|
+
binding.receive(commit);
|
|
2000
|
+
batch = scheduler.batchContext();
|
|
2001
|
+
scheduler.capture(binding.record);
|
|
2002
|
+
}
|
|
2003
|
+
},
|
|
2004
|
+
settle: scheduler.settle,
|
|
2005
|
+
flush: scheduler.flush,
|
|
2006
|
+
dispose: () => {
|
|
2007
|
+
bindings.forEach(({ record }) => {
|
|
2008
|
+
record.fault = new ProjectionError("source", "document disposed", [runtime.revision()], /* @__PURE__ */ new Error("Document has been disposed."));
|
|
2009
|
+
scheduler.capture(record);
|
|
2010
|
+
});
|
|
2011
|
+
scheduler.run();
|
|
2012
|
+
}
|
|
2013
|
+
});
|
|
2014
|
+
scheduler.cleanups.add(() => {
|
|
2015
|
+
unsubscribe();
|
|
2016
|
+
scheduler.guards.delete(guard);
|
|
2017
|
+
});
|
|
2018
|
+
documents.set(state, handle);
|
|
2019
|
+
return handle;
|
|
2020
|
+
};
|
|
2021
|
+
const valueSource = (initial, equal) => {
|
|
2022
|
+
let value = initial;
|
|
2023
|
+
let previous = initial;
|
|
2024
|
+
let revision = 0;
|
|
2025
|
+
let detail;
|
|
2026
|
+
let cause;
|
|
2027
|
+
let batch;
|
|
2028
|
+
let reset = false;
|
|
2029
|
+
let pending = false;
|
|
2030
|
+
const handle = Object.freeze({});
|
|
2031
|
+
const record = {
|
|
2032
|
+
consumers: /* @__PURE__ */ new Set(),
|
|
2033
|
+
disposed: false,
|
|
2034
|
+
fault: void 0,
|
|
2035
|
+
revision: () => revision,
|
|
2036
|
+
reset: () => reset,
|
|
2037
|
+
clear: () => {
|
|
2038
|
+
previous = value;
|
|
2039
|
+
pending = false;
|
|
2040
|
+
detail = void 0;
|
|
2041
|
+
cause = void 0;
|
|
2042
|
+
batch = void 0;
|
|
2043
|
+
reset = false;
|
|
2044
|
+
},
|
|
2045
|
+
context: (active) => {
|
|
2046
|
+
assertScope(active);
|
|
2047
|
+
return Object.freeze({
|
|
2048
|
+
value,
|
|
2049
|
+
previous,
|
|
2050
|
+
revision,
|
|
2051
|
+
changed: !equal(previous, value),
|
|
2052
|
+
reset,
|
|
2053
|
+
detail,
|
|
2054
|
+
cause,
|
|
2055
|
+
batch
|
|
2056
|
+
});
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
2059
|
+
scheduler.register(handle, record);
|
|
2060
|
+
const accept = (next, metadata) => {
|
|
2061
|
+
scheduler.assertIdle();
|
|
2062
|
+
const recovering = record.fault !== void 0;
|
|
2063
|
+
const eventful = metadata !== void 0 && (metadata.reset === true || metadata.revision !== void 0 && metadata.revision !== revision || metadata.detail !== void 0 || metadata.cause !== void 0 || metadata.batch !== void 0);
|
|
2064
|
+
if (equal(value, next) && !recovering && !eventful) return;
|
|
2065
|
+
record.fault = void 0;
|
|
2066
|
+
if (!pending) previous = value;
|
|
2067
|
+
value = next;
|
|
2068
|
+
revision = metadata?.revision ?? revision + 1;
|
|
2069
|
+
detail = metadata?.detail;
|
|
2070
|
+
cause = metadata?.cause ?? cause;
|
|
2071
|
+
batch = scheduler.batchContext() ?? metadata?.batch ?? batch;
|
|
2072
|
+
reset ||= metadata?.reset ?? false;
|
|
2073
|
+
scheduler.capture(record);
|
|
2074
|
+
pending = true;
|
|
2075
|
+
};
|
|
2076
|
+
return {
|
|
2077
|
+
source: handle,
|
|
2078
|
+
accept,
|
|
2079
|
+
set(next) {
|
|
2080
|
+
accept(next);
|
|
2081
|
+
scheduler.run();
|
|
2082
|
+
}
|
|
2083
|
+
};
|
|
2084
|
+
};
|
|
2085
|
+
const externalValueSource = (port, equal) => {
|
|
2086
|
+
scheduler.assertIdle();
|
|
2087
|
+
const old = externalSources.get(port);
|
|
2088
|
+
if (old) return old;
|
|
2089
|
+
const input = valueSource(port.current(), equal);
|
|
2090
|
+
const fail = (cause) => {
|
|
2091
|
+
const record = scheduler.source(input.source);
|
|
2092
|
+
record.fault = new ProjectionError("source", "external source", [record.revision()], cause);
|
|
2093
|
+
scheduler.capture(record);
|
|
2094
|
+
};
|
|
2095
|
+
let unsubscribe;
|
|
2096
|
+
try {
|
|
2097
|
+
unsubscribe = port.subscribe((event) => {
|
|
2098
|
+
try {
|
|
2099
|
+
input.accept(event.value, {
|
|
2100
|
+
revision: event.revision,
|
|
2101
|
+
reset: event.reset,
|
|
2102
|
+
detail: event.detail,
|
|
2103
|
+
cause: event.cause,
|
|
2104
|
+
batch: event.batch
|
|
2105
|
+
});
|
|
2106
|
+
} catch (cause) {
|
|
2107
|
+
fail(cause);
|
|
2108
|
+
}
|
|
2109
|
+
scheduler.run();
|
|
2110
|
+
});
|
|
2111
|
+
} catch (cause) {
|
|
2112
|
+
unsubscribe?.();
|
|
2113
|
+
scheduler.unregisterSource(input.source);
|
|
2114
|
+
throw cause;
|
|
2115
|
+
}
|
|
2116
|
+
scheduler.cleanups.add(() => unsubscribe?.());
|
|
2117
|
+
externalSources.set(port, input.source);
|
|
2118
|
+
return input.source;
|
|
2119
|
+
};
|
|
2120
|
+
const externalCollectionSource = (port) => {
|
|
2121
|
+
scheduler.assertIdle();
|
|
2122
|
+
const old = externalSources.get(port);
|
|
2123
|
+
if (old) return old;
|
|
2124
|
+
let read = port.current();
|
|
2125
|
+
let previous = read;
|
|
2126
|
+
let revision = port.revision();
|
|
2127
|
+
let change;
|
|
2128
|
+
let reset = false;
|
|
2129
|
+
let detail;
|
|
2130
|
+
let cause;
|
|
2131
|
+
let batch;
|
|
2132
|
+
let publishedTransitions = Object.freeze([]);
|
|
2133
|
+
let transitionKeys = /* @__PURE__ */ new Set();
|
|
2134
|
+
let pending = false;
|
|
2135
|
+
const handle = Object.freeze({});
|
|
2136
|
+
const record = {
|
|
2137
|
+
consumers: /* @__PURE__ */ new Set(),
|
|
2138
|
+
disposed: false,
|
|
2139
|
+
fault: void 0,
|
|
2140
|
+
revision: () => revision,
|
|
2141
|
+
reset: () => reset,
|
|
2142
|
+
clear: () => {
|
|
2143
|
+
previous = read;
|
|
2144
|
+
pending = false;
|
|
2145
|
+
change = void 0;
|
|
2146
|
+
reset = false;
|
|
2147
|
+
detail = void 0;
|
|
2148
|
+
cause = void 0;
|
|
2149
|
+
batch = void 0;
|
|
2150
|
+
publishedTransitions = Object.freeze([]);
|
|
2151
|
+
transitionKeys.clear();
|
|
2152
|
+
},
|
|
2153
|
+
context: (active) => {
|
|
2154
|
+
assertScope(active);
|
|
2155
|
+
const currentRead = Object.freeze({
|
|
2156
|
+
get: (key) => {
|
|
2157
|
+
assertScope(active);
|
|
2158
|
+
return read.get(key);
|
|
2159
|
+
},
|
|
2160
|
+
has: (key) => {
|
|
2161
|
+
assertScope(active);
|
|
2162
|
+
return read.has(key);
|
|
2163
|
+
},
|
|
2164
|
+
ids: () => {
|
|
2165
|
+
assertScope(active);
|
|
2166
|
+
return read.ids();
|
|
2167
|
+
}
|
|
2168
|
+
});
|
|
2169
|
+
const previousRead = Object.freeze({
|
|
2170
|
+
get: (key) => {
|
|
2171
|
+
assertScope(active);
|
|
2172
|
+
return previous.get(key);
|
|
2173
|
+
},
|
|
2174
|
+
has: (key) => {
|
|
2175
|
+
assertScope(active);
|
|
2176
|
+
return previous.has(key);
|
|
2177
|
+
},
|
|
2178
|
+
ids: () => {
|
|
2179
|
+
assertScope(active);
|
|
2180
|
+
return previous.ids();
|
|
2181
|
+
}
|
|
2182
|
+
});
|
|
2183
|
+
return Object.freeze({
|
|
2184
|
+
...currentRead,
|
|
2185
|
+
previous: previousRead,
|
|
2186
|
+
change,
|
|
2187
|
+
revision,
|
|
2188
|
+
reset,
|
|
2189
|
+
transitions: (keys) => {
|
|
2190
|
+
assertScope(active);
|
|
2191
|
+
const selected = keys ? new Set(keys) : void 0;
|
|
2192
|
+
return Object.freeze(publishedTransitions.filter((transition) => !selected || selected.has(transition.key)));
|
|
2193
|
+
},
|
|
2194
|
+
detail,
|
|
2195
|
+
cause,
|
|
2196
|
+
batch
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2199
|
+
};
|
|
2200
|
+
scheduler.register(handle, record);
|
|
2201
|
+
collections.add(handle);
|
|
2202
|
+
const fail = (error) => {
|
|
2203
|
+
record.fault = new ProjectionError("source", "external collection source", [revision], error);
|
|
2204
|
+
scheduler.capture(record);
|
|
2205
|
+
};
|
|
2206
|
+
const addTransitionKeys = (impact) => {
|
|
2207
|
+
if (impact?.kind === "incremental") {
|
|
2208
|
+
impact.added.forEach((key) => transitionKeys.add(key));
|
|
2209
|
+
impact.removed.forEach((key) => transitionKeys.add(key));
|
|
2210
|
+
impact.updated.forEach((key) => transitionKeys.add(key));
|
|
2211
|
+
}
|
|
2212
|
+
};
|
|
2213
|
+
const recomputeTransitions = () => {
|
|
2214
|
+
const transitions = [];
|
|
2215
|
+
for (const key of transitionKeys) {
|
|
2216
|
+
const beforePresent = previous.has(key);
|
|
2217
|
+
const afterPresent = read.has(key);
|
|
2218
|
+
const before = beforePresent ? previous.get(key) : void 0;
|
|
2219
|
+
const after = afterPresent ? read.get(key) : void 0;
|
|
2220
|
+
if (beforePresent === afterPresent && Object.is(before, after)) continue;
|
|
2221
|
+
transitions.push({
|
|
2222
|
+
key,
|
|
2223
|
+
kind: !beforePresent ? "added" : !afterPresent ? "removed" : "updated",
|
|
2224
|
+
before,
|
|
2225
|
+
after
|
|
2226
|
+
});
|
|
2227
|
+
}
|
|
2228
|
+
publishedTransitions = Object.freeze(transitions);
|
|
2229
|
+
};
|
|
2230
|
+
const deriveChange = () => {
|
|
2231
|
+
if (reset) return Object.freeze({ kind: "reset" });
|
|
2232
|
+
const added = /* @__PURE__ */ new Set();
|
|
2233
|
+
const removed = /* @__PURE__ */ new Set();
|
|
2234
|
+
const updated = /* @__PURE__ */ new Set();
|
|
2235
|
+
publishedTransitions.forEach((transition) => {
|
|
2236
|
+
if (transition.kind === "added") added.add(transition.key);
|
|
2237
|
+
else if (transition.kind === "removed") removed.add(transition.key);
|
|
2238
|
+
else updated.add(transition.key);
|
|
2239
|
+
});
|
|
2240
|
+
const beforeIds = previous.ids();
|
|
2241
|
+
const afterIds = read.ids();
|
|
2242
|
+
const beforeCommon = beforeIds.filter((key) => read.has(key));
|
|
2243
|
+
const afterCommon = afterIds.filter((key) => previous.has(key));
|
|
2244
|
+
const orderChanged = beforeCommon.length !== afterCommon.length || beforeCommon.some((key, index) => afterCommon[index] !== key);
|
|
2245
|
+
if (!added.size && !removed.size && !updated.size && !orderChanged) return void 0;
|
|
2246
|
+
return Object.freeze({
|
|
2247
|
+
kind: "incremental",
|
|
2248
|
+
added,
|
|
2249
|
+
removed,
|
|
2250
|
+
updated,
|
|
2251
|
+
orderChanged
|
|
2252
|
+
});
|
|
2253
|
+
};
|
|
2254
|
+
let unsubscribe;
|
|
2255
|
+
try {
|
|
2256
|
+
unsubscribe = port.subscribe((event) => {
|
|
2257
|
+
try {
|
|
2258
|
+
scheduler.assertIdle();
|
|
2259
|
+
if (!pending) previous = event.previous;
|
|
2260
|
+
read = port.current();
|
|
2261
|
+
revision = event.revision;
|
|
2262
|
+
addTransitionKeys(event.change);
|
|
2263
|
+
reset ||= event.reset || event.change?.kind === "reset";
|
|
2264
|
+
detail = event.detail;
|
|
2265
|
+
cause = event.cause ?? cause;
|
|
2266
|
+
batch = scheduler.batchContext() ?? event.batch;
|
|
2267
|
+
recomputeTransitions();
|
|
2268
|
+
change = deriveChange();
|
|
2269
|
+
record.fault = void 0;
|
|
2270
|
+
scheduler.capture(record);
|
|
2271
|
+
pending = true;
|
|
2272
|
+
} catch (error) {
|
|
2273
|
+
fail(error);
|
|
2274
|
+
}
|
|
2275
|
+
scheduler.run();
|
|
2276
|
+
});
|
|
2277
|
+
} catch (error) {
|
|
2278
|
+
unsubscribe?.();
|
|
2279
|
+
scheduler.unregisterSource(handle);
|
|
2280
|
+
throw error;
|
|
2281
|
+
}
|
|
2282
|
+
scheduler.cleanups.add(() => unsubscribe?.());
|
|
2283
|
+
externalSources.set(port, handle);
|
|
2284
|
+
return handle;
|
|
2285
|
+
};
|
|
2286
|
+
return {
|
|
2287
|
+
dispose: () => {
|
|
2288
|
+
documents.clear();
|
|
2289
|
+
readables.clear();
|
|
2290
|
+
},
|
|
2291
|
+
document,
|
|
2292
|
+
fromSource: (source, options) => externalValueSource(source, options?.isEqual ?? Object.is),
|
|
2293
|
+
fromCollectionSource: (source) => externalCollectionSource(source),
|
|
2294
|
+
input: (initial, options) => {
|
|
2295
|
+
const { source, set } = valueSource(initial, options?.isEqual ?? Object.is);
|
|
2296
|
+
return Object.freeze({
|
|
2297
|
+
source,
|
|
2298
|
+
set
|
|
2299
|
+
});
|
|
2300
|
+
},
|
|
2301
|
+
fromReadable: (readable, options) => {
|
|
2302
|
+
scheduler.assertIdle();
|
|
2303
|
+
if (projectionHandles.has(readable)) throw new Error("Projection nodes must be declared directly as sources.");
|
|
2304
|
+
const equal = options?.isEqual ?? Object.is;
|
|
2305
|
+
let bindings = readables.get(readable);
|
|
2306
|
+
const old = bindings?.get(equal);
|
|
2307
|
+
if (old) return old.source;
|
|
2308
|
+
const input = valueSource(readable.current(), equal);
|
|
2309
|
+
if (bindings) {
|
|
2310
|
+
bindings.set(equal, {
|
|
2311
|
+
source: input.source,
|
|
2312
|
+
accept: (value) => input.accept(value)
|
|
2313
|
+
});
|
|
2314
|
+
return input.source;
|
|
2315
|
+
}
|
|
2316
|
+
bindings = new Map([[equal, {
|
|
2317
|
+
source: input.source,
|
|
2318
|
+
accept: (value) => input.accept(value)
|
|
2319
|
+
}]]);
|
|
2320
|
+
const members = bindings;
|
|
2321
|
+
const receive = (settle = true) => {
|
|
2322
|
+
if (!scheduler.active) return;
|
|
2323
|
+
let value;
|
|
2324
|
+
try {
|
|
2325
|
+
value = readable.current();
|
|
2326
|
+
} catch (cause) {
|
|
2327
|
+
for (const member of members.values()) {
|
|
2328
|
+
const record = scheduler.source(member.source);
|
|
2329
|
+
record.fault = new ProjectionError("source", "external readable", [record.revision()], cause);
|
|
2330
|
+
scheduler.capture(record);
|
|
2331
|
+
}
|
|
2332
|
+
if (settle) scheduler.run();
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2335
|
+
for (const member of members.values()) try {
|
|
2336
|
+
member.accept(value);
|
|
2337
|
+
} catch (cause) {
|
|
2338
|
+
const record = scheduler.source(member.source);
|
|
2339
|
+
record.fault = new ProjectionError("source", "external readable", [record.revision()], cause);
|
|
2340
|
+
scheduler.capture(record);
|
|
2341
|
+
}
|
|
2342
|
+
if (settle) scheduler.run();
|
|
2343
|
+
};
|
|
2344
|
+
let unsubscribe;
|
|
2345
|
+
let detach;
|
|
2346
|
+
try {
|
|
2347
|
+
const owner = documentReadableOwner(readable);
|
|
2348
|
+
if (owner) {
|
|
2349
|
+
document(owner);
|
|
2350
|
+
detach = attachProjection(owner, {
|
|
2351
|
+
capture: () => receive(false),
|
|
2352
|
+
settle: scheduler.settle,
|
|
2353
|
+
flush: scheduler.flush,
|
|
2354
|
+
dispose: () => {
|
|
2355
|
+
for (const member of members.values()) {
|
|
2356
|
+
const record = scheduler.source(member.source);
|
|
2357
|
+
record.fault = new ProjectionError("source", "document readable disposed", [record.revision()], /* @__PURE__ */ new Error("Document has been disposed."));
|
|
2358
|
+
scheduler.capture(record);
|
|
2359
|
+
}
|
|
2360
|
+
scheduler.run();
|
|
2361
|
+
}
|
|
2362
|
+
});
|
|
2363
|
+
}
|
|
2364
|
+
unsubscribe = readable.subscribe(() => receive());
|
|
2365
|
+
input.accept(readable.current());
|
|
2366
|
+
} catch (error) {
|
|
2367
|
+
try {
|
|
2368
|
+
unsubscribe?.();
|
|
2369
|
+
detach?.();
|
|
2370
|
+
} finally {
|
|
2371
|
+
scheduler.unregisterSource(input.source);
|
|
2372
|
+
}
|
|
2373
|
+
throw error;
|
|
2374
|
+
}
|
|
2375
|
+
scheduler.cleanups.add(unsubscribe);
|
|
2376
|
+
if (detach) scheduler.cleanups.add(detach);
|
|
2377
|
+
readables.set(readable, bindings);
|
|
2378
|
+
scheduler.run();
|
|
2379
|
+
return input.source;
|
|
2380
|
+
},
|
|
2381
|
+
assertCollection: (handle) => {
|
|
2382
|
+
scheduler.source(handle);
|
|
2383
|
+
if (!collections.has(handle) && !collectionHandles.has(handle)) throw new Error("map requires a collection source.");
|
|
2384
|
+
}
|
|
2385
|
+
};
|
|
2386
|
+
};
|
|
2387
|
+
//#endregion
|
|
2388
|
+
//#region core/src/projection/value.ts
|
|
2389
|
+
const createValue = (scheduler, spec, options) => {
|
|
2390
|
+
let instance;
|
|
2391
|
+
let value;
|
|
2392
|
+
let next;
|
|
2393
|
+
let previous;
|
|
2394
|
+
let initialized = false;
|
|
2395
|
+
let revision = 0;
|
|
2396
|
+
let changed = false;
|
|
2397
|
+
let reset = false;
|
|
2398
|
+
let batch;
|
|
2399
|
+
let cause;
|
|
2400
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
2401
|
+
const owner = createNode(scheduler, spec, {
|
|
2402
|
+
evaluate: (sources, build) => {
|
|
2403
|
+
const metadata = Object.values(sources).find((value) => value && typeof value === "object" && ("batch" in value && value.batch !== void 0 || "cause" in value && value.cause !== void 0));
|
|
2404
|
+
batch = scheduler.batchContext() ?? metadata?.batch;
|
|
2405
|
+
cause = metadata?.cause ?? batch?.cause;
|
|
2406
|
+
let candidate;
|
|
2407
|
+
if (build || !instance) {
|
|
2408
|
+
profile.materialized.rebuilt();
|
|
2409
|
+
instance = void 0;
|
|
2410
|
+
const built = spec.build(sources);
|
|
2411
|
+
assertSynchronous(built);
|
|
2412
|
+
instance = built;
|
|
2413
|
+
candidate = built.value;
|
|
2414
|
+
} else {
|
|
2415
|
+
profile.materialized.updated();
|
|
2416
|
+
const result = instance.update(sources);
|
|
2417
|
+
assertSynchronous(result);
|
|
2418
|
+
if (result.kind === "rebuild") {
|
|
2419
|
+
instance = void 0;
|
|
2420
|
+
const built = spec.build(sources);
|
|
2421
|
+
assertSynchronous(built);
|
|
2422
|
+
instance = built;
|
|
2423
|
+
candidate = built.value;
|
|
2424
|
+
build = true;
|
|
2425
|
+
} else candidate = result.kind === "changed" ? result.value : value;
|
|
2426
|
+
}
|
|
2427
|
+
previous = value;
|
|
2428
|
+
changed = !initialized || !(options?.isEqual ?? Object.is)(value, candidate);
|
|
2429
|
+
next = changed ? candidate : value;
|
|
2430
|
+
reset = build;
|
|
2431
|
+
return changed;
|
|
2432
|
+
},
|
|
2433
|
+
context: (active) => {
|
|
2434
|
+
assertScope(active);
|
|
2435
|
+
return Object.freeze({
|
|
2436
|
+
value: next,
|
|
2437
|
+
previous,
|
|
2438
|
+
changed,
|
|
2439
|
+
revision: revision + (changed ? 1 : 0),
|
|
2440
|
+
reset,
|
|
2441
|
+
cause,
|
|
2442
|
+
batch
|
|
2443
|
+
});
|
|
2444
|
+
},
|
|
2445
|
+
revision: () => revision,
|
|
2446
|
+
reset: () => reset,
|
|
2447
|
+
publish: () => {
|
|
2448
|
+
if (changed && initialized) revision++;
|
|
2449
|
+
value = next;
|
|
2450
|
+
initialized = true;
|
|
2451
|
+
},
|
|
2452
|
+
emit: (call) => {
|
|
2453
|
+
profile.materialized.notification();
|
|
2454
|
+
Array.from(listeners).forEach((listener) => call(listener));
|
|
2455
|
+
},
|
|
2456
|
+
clear: () => {
|
|
2457
|
+
next = value;
|
|
2458
|
+
previous = value;
|
|
2459
|
+
changed = false;
|
|
2460
|
+
reset = false;
|
|
2461
|
+
batch = void 0;
|
|
2462
|
+
cause = void 0;
|
|
2463
|
+
},
|
|
2464
|
+
release: () => {
|
|
2465
|
+
listeners.clear();
|
|
2466
|
+
instance = void 0;
|
|
2467
|
+
value = next = previous = void 0;
|
|
2468
|
+
}
|
|
2469
|
+
});
|
|
2470
|
+
const handle = Object.freeze({
|
|
2471
|
+
current: () => {
|
|
2472
|
+
owner.check();
|
|
2473
|
+
return value;
|
|
2474
|
+
},
|
|
2475
|
+
revision: () => {
|
|
2476
|
+
owner.check();
|
|
2477
|
+
return revision;
|
|
2478
|
+
},
|
|
2479
|
+
subscribe: (listener) => {
|
|
2480
|
+
owner.check();
|
|
2481
|
+
listeners.add(listener);
|
|
2482
|
+
return () => {
|
|
2483
|
+
listeners.delete(listener);
|
|
2484
|
+
};
|
|
2485
|
+
},
|
|
2486
|
+
rebuild: owner.rebuild,
|
|
2487
|
+
dispose: owner.dispose
|
|
2488
|
+
});
|
|
2489
|
+
owner.install(handle);
|
|
2490
|
+
return handle;
|
|
2491
|
+
};
|
|
2492
|
+
//#endregion
|
|
2493
|
+
//#region core/src/projection/runtime.ts
|
|
2494
|
+
const createRuntimeExecutor = (options) => {
|
|
2495
|
+
const scheduler = createScheduler(options.onError);
|
|
2496
|
+
const sources = createSources(scheduler);
|
|
2497
|
+
function value(input, computeOrOptions, options) {
|
|
2498
|
+
if (typeof computeOrOptions !== "function") return createValue(scheduler, input, computeOrOptions);
|
|
2499
|
+
const compute = (input) => {
|
|
2500
|
+
const result = computeOrOptions(input);
|
|
2501
|
+
assertSynchronous(result);
|
|
2502
|
+
return result;
|
|
2503
|
+
};
|
|
2504
|
+
return createValue(scheduler, {
|
|
2505
|
+
sources: input,
|
|
2506
|
+
build: (input) => ({
|
|
2507
|
+
value: compute(input),
|
|
2508
|
+
update: (input) => ({
|
|
2509
|
+
kind: "changed",
|
|
2510
|
+
value: compute(input)
|
|
2511
|
+
})
|
|
2512
|
+
})
|
|
2513
|
+
}, options);
|
|
2514
|
+
}
|
|
2515
|
+
const map = (source, mapper, options) => {
|
|
2516
|
+
sources.assertCollection(source);
|
|
2517
|
+
const calculate = (id, entry) => {
|
|
2518
|
+
const value = mapper(id, entry);
|
|
2519
|
+
assertSynchronous(value);
|
|
2520
|
+
return value;
|
|
2521
|
+
};
|
|
2522
|
+
return createCollection(scheduler, {
|
|
2523
|
+
sources: { source },
|
|
2524
|
+
isEqual: options?.isEqual,
|
|
2525
|
+
build: ({ sources, writer }) => {
|
|
2526
|
+
const input = sources.source;
|
|
2527
|
+
const read = "read" in input ? input.read : input;
|
|
2528
|
+
const ids = read.ids();
|
|
2529
|
+
profile.collectionView.idsScanned(ids.length);
|
|
2530
|
+
for (const id of ids) {
|
|
2531
|
+
profile.collectionView.mapped();
|
|
2532
|
+
writer.set(id, calculate(id, read.get(id)));
|
|
2533
|
+
}
|
|
2534
|
+
writer.order(ids);
|
|
2535
|
+
return { update: ({ sources, writer }) => {
|
|
2536
|
+
const input = sources.source;
|
|
2537
|
+
if (input.reset) return { kind: "rebuild" };
|
|
2538
|
+
const read = "read" in input ? input.read : input;
|
|
2539
|
+
let keys;
|
|
2540
|
+
let structural;
|
|
2541
|
+
if ("candidates" in input) {
|
|
2542
|
+
keys = input.candidates.keys;
|
|
2543
|
+
structural = input.candidates.orderDirty;
|
|
2544
|
+
} else {
|
|
2545
|
+
const change = input.change;
|
|
2546
|
+
if (!change) return;
|
|
2547
|
+
if (change.kind === "reset") return { kind: "rebuild" };
|
|
2548
|
+
keys = new Set([
|
|
2549
|
+
...change.added,
|
|
2550
|
+
...change.removed,
|
|
2551
|
+
...change.updated
|
|
2552
|
+
]);
|
|
2553
|
+
structural = Boolean(change.added.size || change.removed.size || change.orderChanged);
|
|
2554
|
+
}
|
|
2555
|
+
for (const key of keys) if (!read.has(key)) writer.remove(key);
|
|
2556
|
+
else {
|
|
2557
|
+
profile.collectionView.mapped();
|
|
2558
|
+
writer.set(key, calculate(key, read.get(key)));
|
|
2559
|
+
}
|
|
2560
|
+
if (structural) {
|
|
2561
|
+
const ids = read.ids();
|
|
2562
|
+
profile.collectionView.idsScanned(ids.length);
|
|
2563
|
+
writer.order(ids);
|
|
2564
|
+
}
|
|
2565
|
+
} };
|
|
2566
|
+
}
|
|
2567
|
+
});
|
|
2568
|
+
};
|
|
2569
|
+
const runtime = {
|
|
2570
|
+
document: sources.document,
|
|
2571
|
+
fromSource: (source, options) => sources.fromSource(source, options),
|
|
2572
|
+
fromCollectionSource: (source) => sources.fromCollectionSource(source),
|
|
2573
|
+
input: sources.input,
|
|
2574
|
+
fromReadable: sources.fromReadable,
|
|
2575
|
+
value,
|
|
2576
|
+
collection: () => (spec) => createCollection(scheduler, spec),
|
|
2577
|
+
map,
|
|
2578
|
+
batch: scheduler.batch,
|
|
2579
|
+
dispose: () => {
|
|
2580
|
+
try {
|
|
2581
|
+
scheduler.dispose();
|
|
2582
|
+
} finally {
|
|
2583
|
+
if (!scheduler.active) sources.dispose();
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
};
|
|
2587
|
+
return Object.freeze(runtime);
|
|
2588
|
+
};
|
|
2589
|
+
//#endregion
|
|
2590
|
+
//#region core/src/projection/store.ts
|
|
2591
|
+
let activeTracker;
|
|
2592
|
+
const trackKey = (key) => {
|
|
2593
|
+
activeTracker?.keys.add(key);
|
|
2594
|
+
};
|
|
2595
|
+
const trackStructure = () => {
|
|
2596
|
+
if (!activeTracker) return;
|
|
2597
|
+
activeTracker.structure = true;
|
|
2598
|
+
};
|
|
2599
|
+
const trackAll = () => {
|
|
2600
|
+
if (!activeTracker) return;
|
|
2601
|
+
activeTracker.all = true;
|
|
2602
|
+
};
|
|
2603
|
+
const mapView = (read) => {
|
|
2604
|
+
const keys = read.ids();
|
|
2605
|
+
const source = /* @__PURE__ */ new Map();
|
|
2606
|
+
for (const key of keys) source.set(key, read.get(key));
|
|
2607
|
+
const view = {
|
|
2608
|
+
get: (key) => {
|
|
2609
|
+
trackKey(key);
|
|
2610
|
+
return source.get(key);
|
|
2611
|
+
},
|
|
2612
|
+
has: (key) => {
|
|
2613
|
+
trackKey(key);
|
|
2614
|
+
return source.has(key);
|
|
2615
|
+
},
|
|
2616
|
+
get size() {
|
|
2617
|
+
trackStructure();
|
|
2618
|
+
return source.size;
|
|
2619
|
+
},
|
|
2620
|
+
keys: () => {
|
|
2621
|
+
trackStructure();
|
|
2622
|
+
return source.keys();
|
|
2623
|
+
},
|
|
2624
|
+
values: () => {
|
|
2625
|
+
trackAll();
|
|
2626
|
+
return source.values();
|
|
2627
|
+
},
|
|
2628
|
+
entries: () => {
|
|
2629
|
+
trackAll();
|
|
2630
|
+
return source.entries();
|
|
2631
|
+
},
|
|
2632
|
+
forEach: (callback, thisArg) => {
|
|
2633
|
+
trackAll();
|
|
2634
|
+
source.forEach((value, key) => callback.call(thisArg, value, key, view));
|
|
2635
|
+
},
|
|
2636
|
+
[Symbol.iterator]: () => {
|
|
2637
|
+
trackAll();
|
|
2638
|
+
return source[Symbol.iterator]();
|
|
2639
|
+
}
|
|
2640
|
+
};
|
|
2641
|
+
return Object.freeze(view);
|
|
2642
|
+
};
|
|
2643
|
+
const isMapLike = (value) => value instanceof Map || value !== null && typeof value === "object" && typeof value.get === "function" && typeof value.keys === "function" && typeof value.entries === "function";
|
|
2644
|
+
const mapRead = (value) => ({
|
|
2645
|
+
get: (key) => value.get(key),
|
|
2646
|
+
has: (key) => value.has(key),
|
|
2647
|
+
ids: () => Object.freeze([...value.keys()])
|
|
2648
|
+
});
|
|
2649
|
+
const mapChange = (previous, current) => {
|
|
2650
|
+
const added = /* @__PURE__ */ new Set();
|
|
2651
|
+
const removed = /* @__PURE__ */ new Set();
|
|
2652
|
+
const updated = /* @__PURE__ */ new Set();
|
|
2653
|
+
for (const key of previous.keys()) if (!current.has(key)) removed.add(key);
|
|
2654
|
+
else if (!Object.is(previous.get(key), current.get(key))) updated.add(key);
|
|
2655
|
+
for (const key of current.keys()) if (!previous.has(key)) added.add(key);
|
|
2656
|
+
const before = [...previous.keys()].filter((key) => current.has(key));
|
|
2657
|
+
const after = [...current.keys()].filter((key) => previous.has(key));
|
|
2658
|
+
const orderChanged = before.length !== after.length || before.some((key, index) => key !== after[index]);
|
|
2659
|
+
if (!added.size && !removed.size && !updated.size && !orderChanged) return void 0;
|
|
2660
|
+
return {
|
|
2661
|
+
kind: "incremental",
|
|
2662
|
+
added,
|
|
2663
|
+
removed,
|
|
2664
|
+
updated,
|
|
2665
|
+
orderChanged
|
|
2666
|
+
};
|
|
2667
|
+
};
|
|
2668
|
+
const sameSelection = (left, right) => {
|
|
2669
|
+
if (left.all !== right.all || left.structure !== right.structure || left.keys.size !== right.keys.size) return false;
|
|
2670
|
+
for (const key of left.keys) if (!right.keys.has(key)) return false;
|
|
2671
|
+
return true;
|
|
2672
|
+
};
|
|
2673
|
+
const selectionAffects = (selection, change) => {
|
|
2674
|
+
if (!change || change.kind === "reset") return true;
|
|
2675
|
+
if (selection.all && (change.added.size || change.removed.size || change.updated.size || change.orderChanged)) return true;
|
|
2676
|
+
if (selection.structure && (change.added.size || change.removed.size || change.orderChanged)) return true;
|
|
2677
|
+
for (const key of selection.keys) if (change.added.has(key) || change.removed.has(key) || change.updated.has(key)) return true;
|
|
2678
|
+
return false;
|
|
2679
|
+
};
|
|
2680
|
+
const currentValue = (event) => {
|
|
2681
|
+
if (!event || typeof event !== "object") return event;
|
|
2682
|
+
if ("value" in event) return event.value;
|
|
2683
|
+
if ("read" in event) return snapshot(event.read);
|
|
2684
|
+
if ("ids" in event && typeof event.ids === "function") return mapView(event);
|
|
2685
|
+
return event;
|
|
2686
|
+
};
|
|
2687
|
+
const sourceOf = (instance) => instance;
|
|
2688
|
+
const createProjectionRuntime = (options) => {
|
|
2689
|
+
const runtime = createRuntimeExecutor({ onError: options?.onError ?? (() => void 0) });
|
|
2690
|
+
const instances = /* @__PURE__ */ new WeakMap();
|
|
2691
|
+
const inputs = /* @__PURE__ */ new WeakMap();
|
|
2692
|
+
const collections = /* @__PURE__ */ new WeakMap();
|
|
2693
|
+
const materialize = (projection) => {
|
|
2694
|
+
const old = instances.get(projection);
|
|
2695
|
+
if (old) return old;
|
|
2696
|
+
const definition = definitionOf(projection);
|
|
2697
|
+
let instance;
|
|
2698
|
+
switch (definition.kind) {
|
|
2699
|
+
case "input": {
|
|
2700
|
+
const input = runtime.input(definition.initial, { isEqual: definition.isEqual });
|
|
2701
|
+
inputs.set(projection, input);
|
|
2702
|
+
instance = runtime.value({ input: input.source }, ({ input }) => input.value);
|
|
2703
|
+
break;
|
|
2704
|
+
}
|
|
2705
|
+
case "readable": {
|
|
2706
|
+
const source = runtime.fromReadable(definition.readable, { isEqual: definition.isEqual });
|
|
2707
|
+
instance = runtime.value({ source }, ({ source }) => source.value);
|
|
2708
|
+
break;
|
|
2709
|
+
}
|
|
2710
|
+
case "source-value":
|
|
2711
|
+
{
|
|
2712
|
+
const source = runtime.fromSource(definition.source);
|
|
2713
|
+
instance = runtime.value({ source }, ({ source }) => source.value);
|
|
2714
|
+
}
|
|
2715
|
+
break;
|
|
2716
|
+
case "source-collection": {
|
|
2717
|
+
const source = runtime.fromCollectionSource(definition.source);
|
|
2718
|
+
instance = runtime.map(source, (_key, value) => value);
|
|
2719
|
+
break;
|
|
2720
|
+
}
|
|
2721
|
+
case "document": {
|
|
2722
|
+
const document = runtime.document(definition.document);
|
|
2723
|
+
if (!definition.selector) {
|
|
2724
|
+
instance = runtime.value({ document }, ({ document }) => snapshot(document.read));
|
|
2725
|
+
break;
|
|
2726
|
+
}
|
|
2727
|
+
const state = accessOf(definition.document);
|
|
2728
|
+
try {
|
|
2729
|
+
const collection = document.collection(definition.selector);
|
|
2730
|
+
instance = runtime.map(collection, (_id, entry) => snapshot(entry));
|
|
2731
|
+
break;
|
|
2732
|
+
} catch {
|
|
2733
|
+
const selected = document.targets(definition.selector);
|
|
2734
|
+
const address = compilePath(state.schema, "value", definition.selector).address;
|
|
2735
|
+
instance = runtime.value({ selected }, () => {
|
|
2736
|
+
return read$1(snapshot(state.document), address, state.schema);
|
|
2737
|
+
});
|
|
2738
|
+
}
|
|
2739
|
+
break;
|
|
2740
|
+
}
|
|
2741
|
+
case "derive": {
|
|
2742
|
+
const sources = Object.fromEntries(definition.dependencies.map((dependency, index) => [`d${index}`, sourceOf(materialize(dependency))]));
|
|
2743
|
+
instance = runtime.value(sources, (values) => definition.compute(...Object.values(values).map(currentValue)), { isEqual: definition.isEqual });
|
|
2744
|
+
break;
|
|
2745
|
+
}
|
|
2746
|
+
case "incremental-value": {
|
|
2747
|
+
const sources = Object.fromEntries(Object.entries(definition.dependencies).map(([key, dependency]) => [key, sourceOf(materialize(dependency))]));
|
|
2748
|
+
instance = runtime.value({
|
|
2749
|
+
sources,
|
|
2750
|
+
build: definition.build
|
|
2751
|
+
}, { isEqual: definition.isEqual });
|
|
2752
|
+
break;
|
|
2753
|
+
}
|
|
2754
|
+
case "incremental-collection": {
|
|
2755
|
+
const sources = Object.fromEntries(Object.entries(definition.dependencies).map(([key, dependency]) => [key, sourceOf(materialize(dependency))]));
|
|
2756
|
+
instance = runtime.collection()({
|
|
2757
|
+
sources,
|
|
2758
|
+
build: definition.build,
|
|
2759
|
+
isEqual: definition.isEqual,
|
|
2760
|
+
name: definition.name
|
|
2761
|
+
});
|
|
2762
|
+
break;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
instances.set(projection, instance);
|
|
2766
|
+
return instance;
|
|
2767
|
+
};
|
|
2768
|
+
const get = (projection) => {
|
|
2769
|
+
const instance = materialize(projection);
|
|
2770
|
+
const current = instance.current();
|
|
2771
|
+
if (current && typeof current === "object" && "ids" in current && typeof current.ids === "function") {
|
|
2772
|
+
const existing = collections.get(instance);
|
|
2773
|
+
const revision = instance.revision();
|
|
2774
|
+
if (existing?.revision === revision) return existing.value;
|
|
2775
|
+
const value = mapView(current);
|
|
2776
|
+
collections.set(instance, {
|
|
2777
|
+
revision,
|
|
2778
|
+
value
|
|
2779
|
+
});
|
|
2780
|
+
return value;
|
|
2781
|
+
}
|
|
2782
|
+
if (isMapLike(current)) {
|
|
2783
|
+
const existing = collections.get(instance);
|
|
2784
|
+
const revision = instance.revision();
|
|
2785
|
+
if (existing?.revision === revision) return existing.value;
|
|
2786
|
+
const value = mapView(mapRead(current));
|
|
2787
|
+
collections.set(instance, {
|
|
2788
|
+
revision,
|
|
2789
|
+
value
|
|
2790
|
+
});
|
|
2791
|
+
return value;
|
|
2792
|
+
}
|
|
2793
|
+
return current;
|
|
2794
|
+
};
|
|
2795
|
+
function readable(projection, selector, equality = Object.is) {
|
|
2796
|
+
if (!selector) return Object.freeze({
|
|
2797
|
+
current: () => get(projection),
|
|
2798
|
+
revision: () => materialize(projection).revision(),
|
|
2799
|
+
subscribe: (listener) => materialize(projection).subscribe(listener)
|
|
2800
|
+
});
|
|
2801
|
+
let initialized = false;
|
|
2802
|
+
let value;
|
|
2803
|
+
let selectedRevision = 0;
|
|
2804
|
+
let sourceRevision = -1;
|
|
2805
|
+
let selection = Object.freeze({
|
|
2806
|
+
keys: /* @__PURE__ */ new Set(),
|
|
2807
|
+
all: true,
|
|
2808
|
+
structure: false
|
|
2809
|
+
});
|
|
2810
|
+
let previousCollection;
|
|
2811
|
+
let unsubscribeSource;
|
|
2812
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
2813
|
+
const updateCollectionSnapshot = () => {
|
|
2814
|
+
const current = get(projection);
|
|
2815
|
+
previousCollection = isMapLike(current) ? current : void 0;
|
|
2816
|
+
};
|
|
2817
|
+
const evaluate = () => {
|
|
2818
|
+
const tracker = {
|
|
2819
|
+
keys: /* @__PURE__ */ new Set(),
|
|
2820
|
+
all: false,
|
|
2821
|
+
structure: false
|
|
2822
|
+
};
|
|
2823
|
+
const previousTracker = activeTracker;
|
|
2824
|
+
activeTracker = tracker;
|
|
2825
|
+
let source;
|
|
2826
|
+
let next;
|
|
2827
|
+
try {
|
|
2828
|
+
source = get(projection);
|
|
2829
|
+
next = selector(source);
|
|
2830
|
+
} finally {
|
|
2831
|
+
activeTracker = previousTracker;
|
|
2832
|
+
}
|
|
2833
|
+
const nextSelection = Object.freeze({
|
|
2834
|
+
keys: new Set(tracker.keys),
|
|
2835
|
+
all: tracker.all || isMapLike(source) && tracker.keys.size === 0,
|
|
2836
|
+
structure: tracker.structure
|
|
2837
|
+
});
|
|
2838
|
+
const changed = !initialized || !equality(value, next);
|
|
2839
|
+
if (changed) {
|
|
2840
|
+
if (initialized) selectedRevision++;
|
|
2841
|
+
value = next;
|
|
2842
|
+
}
|
|
2843
|
+
selection = nextSelection;
|
|
2844
|
+
initialized = true;
|
|
2845
|
+
sourceRevision = materialize(projection).revision();
|
|
2846
|
+
updateCollectionSnapshot();
|
|
2847
|
+
return changed;
|
|
2848
|
+
};
|
|
2849
|
+
const ensureCurrent = () => {
|
|
2850
|
+
const revision = materialize(projection).revision();
|
|
2851
|
+
if (!initialized || !unsubscribeSource && sourceRevision !== revision) evaluate();
|
|
2852
|
+
return value;
|
|
2853
|
+
};
|
|
2854
|
+
const onProjectionChange = (incoming) => {
|
|
2855
|
+
let change = incoming;
|
|
2856
|
+
if (!change && previousCollection) {
|
|
2857
|
+
const current = get(projection);
|
|
2858
|
+
if (isMapLike(current)) {
|
|
2859
|
+
change = mapChange(previousCollection, current);
|
|
2860
|
+
previousCollection = current;
|
|
2861
|
+
if (!change) {
|
|
2862
|
+
sourceRevision = materialize(projection).revision();
|
|
2863
|
+
return;
|
|
2864
|
+
}
|
|
2865
|
+
} else previousCollection = void 0;
|
|
2866
|
+
}
|
|
2867
|
+
if (!selectionAffects(selection, change)) {
|
|
2868
|
+
sourceRevision = materialize(projection).revision();
|
|
2869
|
+
updateCollectionSnapshot();
|
|
2870
|
+
return;
|
|
2871
|
+
}
|
|
2872
|
+
const previousSelection = selection;
|
|
2873
|
+
const changed = evaluate();
|
|
2874
|
+
if (unsubscribeSource && !sameSelection(previousSelection, selection)) {
|
|
2875
|
+
unsubscribeSource();
|
|
2876
|
+
unsubscribeSource = materialize(projection).subscribe(onProjectionChange);
|
|
2877
|
+
}
|
|
2878
|
+
if (changed) Array.from(listeners).forEach((listener) => listener());
|
|
2879
|
+
};
|
|
2880
|
+
const installSource = () => {
|
|
2881
|
+
if (!unsubscribeSource) unsubscribeSource = materialize(projection).subscribe(onProjectionChange);
|
|
2882
|
+
};
|
|
2883
|
+
return Object.freeze({
|
|
2884
|
+
current: ensureCurrent,
|
|
2885
|
+
revision: () => {
|
|
2886
|
+
ensureCurrent();
|
|
2887
|
+
return selectedRevision;
|
|
2888
|
+
},
|
|
2889
|
+
subscribe: (listener) => {
|
|
2890
|
+
ensureCurrent();
|
|
2891
|
+
listeners.add(listener);
|
|
2892
|
+
installSource();
|
|
2893
|
+
return () => {
|
|
2894
|
+
listeners.delete(listener);
|
|
2895
|
+
if (!listeners.size && unsubscribeSource) {
|
|
2896
|
+
unsubscribeSource();
|
|
2897
|
+
unsubscribeSource = void 0;
|
|
2898
|
+
}
|
|
2899
|
+
};
|
|
2900
|
+
}
|
|
2901
|
+
});
|
|
2902
|
+
}
|
|
2903
|
+
const store = {
|
|
2904
|
+
get,
|
|
2905
|
+
readable,
|
|
2906
|
+
set: ((input, value) => {
|
|
2907
|
+
materialize(input);
|
|
2908
|
+
const target = inputs.get(input);
|
|
2909
|
+
if (!target) throw new TypeError("Projection is not an input.");
|
|
2910
|
+
target.set(value);
|
|
2911
|
+
}),
|
|
2912
|
+
batch: runtime.batch,
|
|
2913
|
+
dispose: runtime.dispose
|
|
2914
|
+
};
|
|
2915
|
+
return Object.freeze(store);
|
|
2916
|
+
};
|
|
2917
|
+
//#endregion
|
|
919
2918
|
export { DocumentDisposedError, DocumentReentrancyError, ParseError, ProjectionDisposedError, ProjectionError, TransactionRejected, asReadable, createDocument, createProjectionRuntime, derive, field, input, list, map, object, observe, optional, parse, read, replace, snapshot, table, tree, variant };
|
|
920
2919
|
|
|
921
2920
|
//# sourceMappingURL=index.js.map
|