fast-mesh-wasm 0.2.0 → 0.3.1
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 +490 -1
- package/dist/{api-D0o2HjaU.js → api-BUy7FRx7.js} +11 -11
- package/dist/client-Bp9FRd70.js +2278 -0
- package/dist/client-m2zSdFlu.d.ts +305 -0
- package/dist/colormap-D_xH5hI5.js +303 -0
- package/dist/core-Cke_kZ2e.js +113 -0
- package/dist/core-D2X9GPfK.d.ts +15 -0
- package/dist/engine-BpnthBYQ.js +1927 -0
- package/dist/mesh-B16gV3oX.d.ts +130 -0
- package/dist/mesh-B8pae8WA.js +428 -0
- package/dist/node.d.ts +3 -2
- package/dist/node.js +5 -4
- package/dist/protocol-CAUYm1gd.js +68 -0
- package/dist/protocol-W9R-iGl5.d.ts +293 -0
- package/dist/r3f.d.ts +59 -0
- package/dist/r3f.js +165 -0
- package/dist/react-CTADh3NN.js +124 -0
- package/dist/react.d.ts +65 -0
- package/dist/react.js +1 -0
- package/dist/scene-BFd6nYRe.js +1256 -0
- package/dist/scene-like-B_3qm0u6.d.ts +925 -0
- package/dist/snapshot-Cf6NgDl7.js +104 -0
- package/dist/store-D3ErV6o-.js +1379 -0
- package/dist/store-DNnHN33A.d.ts +285 -0
- package/dist/store.d.ts +61 -0
- package/dist/store.js +105 -0
- package/dist/three.d.ts +118 -0
- package/dist/three.js +390 -0
- package/dist/views-BaZqBxlX.js +189 -0
- package/dist/wasm/fast_mesh_wasm.d.ts +475 -0
- package/dist/wasm/fast_mesh_wasm.js +1184 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +76 -0
- package/dist/wasm-threads/fast_mesh_wasm.d.ts +557 -0
- package/dist/wasm-threads/fast_mesh_wasm.js +1547 -0
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +95 -0
- package/dist/wasm-threads/package.json +7 -0
- package/dist/wasm-threads/snippets/fast-mesh-wasm-ae14781a92c2789e/inline0.js +16 -0
- package/dist/wasm-threads/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +107 -0
- package/dist/web.d.ts +3 -2
- package/dist/web.js +4 -2
- package/dist/worker-engine.d.ts +255 -0
- package/dist/worker-engine.js +1 -0
- package/dist/worker-entry.d.ts +1 -0
- package/dist/worker-entry.js +11 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.js +1 -0
- package/package.json +53 -4
- package/dist/api-HcskOwd7.d.ts +0 -43
|
@@ -0,0 +1,1379 @@
|
|
|
1
|
+
import { o as reportListenerError, t as FastMeshError } from "./core-Cke_kZ2e.js";
|
|
2
|
+
import { l as targetsOf } from "./views-BaZqBxlX.js";
|
|
3
|
+
import { i as sectionAspects } from "./protocol-CAUYm1gd.js";
|
|
4
|
+
import { i as watchStatusKeys, n as addFieldKeyed } from "./client-Bp9FRd70.js";
|
|
5
|
+
import { a as combine, c as sameData, n as EMPTY_SNAPSHOT, o as frozenCopy, t as DEFAULT_DISPLAY } from "./snapshot-Cf6NgDl7.js";
|
|
6
|
+
//#region src/store/host.ts
|
|
7
|
+
/** Whether `scene` is a Worker scene (structurally: it reports key statuses). */
|
|
8
|
+
function isWorkerScene(scene) {
|
|
9
|
+
return "status" in scene;
|
|
10
|
+
}
|
|
11
|
+
/** The host of `scene`. */
|
|
12
|
+
function hostOf(scene) {
|
|
13
|
+
return isWorkerScene(scene) ? workerHost(scene) : localHost(scene);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The errors of calls made under a key that threw at once (see `Host.track`),
|
|
17
|
+
* by key; each key whose error is set or cleared goes into `changed`.
|
|
18
|
+
*/
|
|
19
|
+
function tracker(changed) {
|
|
20
|
+
const errors = /* @__PURE__ */ new Map();
|
|
21
|
+
const track = (key, fn) => {
|
|
22
|
+
try {
|
|
23
|
+
const out = fn();
|
|
24
|
+
if (errors.delete(key)) changed.add(key);
|
|
25
|
+
return out;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
changed.add(key);
|
|
28
|
+
errors.set(key, e instanceof FastMeshError ? e : new FastMeshError("INVALID_ARGUMENT", e instanceof Error ? e.message : String(e)));
|
|
29
|
+
throw e;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const forgetErrors = (match) => {
|
|
33
|
+
for (const key of [...errors.keys()]) if (match(key)) {
|
|
34
|
+
errors.delete(key);
|
|
35
|
+
changed.add(key);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
errors,
|
|
40
|
+
track,
|
|
41
|
+
forgetErrors
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The synchronous scene. Calls apply at once, so every key is `"ready"`,
|
|
46
|
+
* except one whose latest call made through `track` threw: that key is
|
|
47
|
+
* `"error"` until a call under it succeeds. Nothing is ever pending, so
|
|
48
|
+
* `onStatus` never fires.
|
|
49
|
+
*/
|
|
50
|
+
function localHost(scene) {
|
|
51
|
+
const ids = /* @__PURE__ */ new WeakMap();
|
|
52
|
+
let next = 0;
|
|
53
|
+
const changed = /* @__PURE__ */ new Set();
|
|
54
|
+
const { errors, track, forgetErrors } = tracker(changed);
|
|
55
|
+
let rev = 0;
|
|
56
|
+
const promised = (fn) => new Promise((resolve) => resolve(fn()));
|
|
57
|
+
return {
|
|
58
|
+
mode: "local",
|
|
59
|
+
scene,
|
|
60
|
+
addMesh: (positions, indices, options) => scene.addMesh(positions, indices, options),
|
|
61
|
+
removeMesh: (mesh) => scene.removeMesh(mesh),
|
|
62
|
+
setPlacement: (mesh, placement) => scene.setPlacement(mesh, placement),
|
|
63
|
+
edit: (mesh, ops) => promised(() => scene.edit(mesh, ops)),
|
|
64
|
+
repair: (mesh, op) => promised(() => scene.repair(mesh, op)),
|
|
65
|
+
undo: () => {
|
|
66
|
+
scene.undo();
|
|
67
|
+
},
|
|
68
|
+
redo: () => {
|
|
69
|
+
scene.redo();
|
|
70
|
+
},
|
|
71
|
+
canUndo: () => scene.canUndo(),
|
|
72
|
+
canRedo: () => scene.canRedo(),
|
|
73
|
+
status: (key) => {
|
|
74
|
+
const error = errors.get(key);
|
|
75
|
+
return error === void 0 ? {
|
|
76
|
+
status: "ready",
|
|
77
|
+
revision: rev
|
|
78
|
+
} : {
|
|
79
|
+
status: "error",
|
|
80
|
+
revision: rev,
|
|
81
|
+
error
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
onStatus: () => () => {},
|
|
85
|
+
changed,
|
|
86
|
+
stop: () => {},
|
|
87
|
+
idOf: (handle) => {
|
|
88
|
+
let id = ids.get(handle);
|
|
89
|
+
if (id === void 0) {
|
|
90
|
+
id = ++next;
|
|
91
|
+
ids.set(handle, id);
|
|
92
|
+
}
|
|
93
|
+
return id;
|
|
94
|
+
},
|
|
95
|
+
track: (key, fn) => {
|
|
96
|
+
rev++;
|
|
97
|
+
return track(key, fn);
|
|
98
|
+
},
|
|
99
|
+
forgetErrors,
|
|
100
|
+
addField: (desc) => scene.addDistanceField(desc),
|
|
101
|
+
removeField: (field) => scene.removeField(field),
|
|
102
|
+
fieldValues: (field) => promised(() => scene.fieldValues(field).slice()),
|
|
103
|
+
setColoring: (mesh, rule) => scene.setColoring(mesh, rule),
|
|
104
|
+
addSection: (desc) => scene.addSection(desc),
|
|
105
|
+
setSectionPlane: (section, plane) => scene.setSectionPlane(section, plane),
|
|
106
|
+
setSectionMeshes: (section, meshes) => scene.setSectionMeshes(section, meshes),
|
|
107
|
+
setSectionFill: (section, fill) => scene.setSectionFill(section, fill),
|
|
108
|
+
removeSection: (section) => scene.removeSection(section),
|
|
109
|
+
restart: () => Promise.reject(new FastMeshError("INVALID_ARGUMENT", "restart needs a Worker scene"))
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The Worker scene: statuses, ids and history come from it as they are,
|
|
114
|
+
* except a key whose latest call made through `track` threw here, before any
|
|
115
|
+
* request went out: that key is `"error"` until a call under it succeeds.
|
|
116
|
+
*/
|
|
117
|
+
function workerHost(scene) {
|
|
118
|
+
const changed = /* @__PURE__ */ new Set();
|
|
119
|
+
const { errors, track, forgetErrors } = tracker(changed);
|
|
120
|
+
return {
|
|
121
|
+
mode: "worker",
|
|
122
|
+
scene,
|
|
123
|
+
addMesh: (positions, indices, options) => scene.addMesh(positions, indices, options),
|
|
124
|
+
removeMesh: (mesh) => scene.removeMesh(mesh),
|
|
125
|
+
setPlacement: (mesh, placement) => scene.setPlacement(mesh, placement),
|
|
126
|
+
edit: (mesh, ops) => scene.edit(mesh, ops),
|
|
127
|
+
repair: (mesh, op) => scene.repair(mesh, op),
|
|
128
|
+
undo: () => scene.undo(),
|
|
129
|
+
redo: () => scene.redo(),
|
|
130
|
+
canUndo: () => scene.canUndo,
|
|
131
|
+
canRedo: () => scene.canRedo,
|
|
132
|
+
status: (key) => {
|
|
133
|
+
const error = errors.get(key);
|
|
134
|
+
const s = scene.status(key);
|
|
135
|
+
return error === void 0 ? s : {
|
|
136
|
+
status: "error",
|
|
137
|
+
revision: s.revision,
|
|
138
|
+
error
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
onStatus: (listener) => scene.onStatus(listener),
|
|
142
|
+
changed,
|
|
143
|
+
stop: watchStatusKeys(scene, (key) => changed.add(key)),
|
|
144
|
+
idOf: (handle) => scene.idOf(handle),
|
|
145
|
+
addField: (desc, key) => key === void 0 ? scene.addDistanceField(desc) : addFieldKeyed(scene, desc, key),
|
|
146
|
+
removeField: (field) => scene.removeField(field),
|
|
147
|
+
fieldValues: (field) => scene.fetchFieldValues(field),
|
|
148
|
+
setColoring: (mesh, rule) => scene.setColoring(mesh, rule),
|
|
149
|
+
addSection: (desc) => scene.addSection(desc),
|
|
150
|
+
setSectionPlane: (section, plane) => scene.setSectionPlane(section, plane),
|
|
151
|
+
setSectionMeshes: (section, meshes) => scene.setSectionMeshes(section, meshes),
|
|
152
|
+
setSectionFill: (section, fill) => scene.setSectionFill(section, fill),
|
|
153
|
+
removeSection: (section) => scene.removeSection(section),
|
|
154
|
+
restart: () => {
|
|
155
|
+
const before = new Map(errors);
|
|
156
|
+
return scene.restart().then(() => {
|
|
157
|
+
for (const [key, error] of before) if (errors.get(key) === error) {
|
|
158
|
+
errors.delete(key);
|
|
159
|
+
changed.add(key);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
track,
|
|
164
|
+
forgetErrors
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/store/store.ts
|
|
169
|
+
/**
|
|
170
|
+
* `c` without `mesh` among its targets when it has others (the scene keeps
|
|
171
|
+
* such a field without the mesh); otherwise `c` itself.
|
|
172
|
+
*/
|
|
173
|
+
function withoutTarget(c, mesh) {
|
|
174
|
+
if (c === null || !Array.isArray(c.distanceTo) || c.distanceTo.length < 2 || !c.distanceTo.includes(mesh)) return c;
|
|
175
|
+
const to = c.distanceTo.filter((m) => m !== mesh);
|
|
176
|
+
return Object.freeze({
|
|
177
|
+
...c,
|
|
178
|
+
distanceTo: Object.freeze(to)
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/** The members of `g` that `m` is paired with, in member order. */
|
|
182
|
+
function partnersOf(g, m) {
|
|
183
|
+
return g.meshes.filter((p) => p !== m && (g.pairs === void 0 || g.pairs === "all" || g.pairs.some(([a, b]) => a === m && b === p || a === p && b === m)));
|
|
184
|
+
}
|
|
185
|
+
const COLORMAPS = [
|
|
186
|
+
"turbo",
|
|
187
|
+
"viridis",
|
|
188
|
+
"coolwarm",
|
|
189
|
+
"grayscale"
|
|
190
|
+
];
|
|
191
|
+
const SIGNS = [
|
|
192
|
+
"auto",
|
|
193
|
+
"pseudonormal",
|
|
194
|
+
"winding"
|
|
195
|
+
];
|
|
196
|
+
/** Checks a contact group's colouring up front (the checks the scene would make at the flush). */
|
|
197
|
+
function checkGroupColoring(c) {
|
|
198
|
+
if (typeof c !== "object" || c === null) throw invalid("a contact group needs a coloring");
|
|
199
|
+
const { maxDistance, range, map, sign } = c;
|
|
200
|
+
if (typeof maxDistance !== "number" || !(maxDistance >= 0)) throw invalid("coloring.maxDistance must be a number, zero or greater (Infinity allowed)");
|
|
201
|
+
if (!Array.isArray(range) || range.length !== 2 || !range.every((x) => typeof x === "number" && Number.isFinite(x)) || !(range[0] < range[1])) throw invalid("coloring.range must be [lo, hi], finite, with lo < hi");
|
|
202
|
+
const stops = typeof map === "object" && map !== null ? map.stops : void 0;
|
|
203
|
+
if (!(typeof map === "string" && COLORMAPS.includes(map)) && !Array.isArray(stops)) throw invalid("coloring.map must be a built-in colormap or { stops }");
|
|
204
|
+
if (sign !== void 0 && !SIGNS.includes(sign)) throw invalid("coloring.sign must be \"auto\", \"pseudonormal\" or \"winding\"");
|
|
205
|
+
}
|
|
206
|
+
/** The aspects of a section the store sets one by one. */
|
|
207
|
+
const ASPECTS = [
|
|
208
|
+
"plane",
|
|
209
|
+
"meshes",
|
|
210
|
+
"fill"
|
|
211
|
+
];
|
|
212
|
+
/**
|
|
213
|
+
* The aspect suffixes of a section's `set` keys (`section:<id>:<aspects>`),
|
|
214
|
+
* made by the client's own `sectionAspects` over every combination of the
|
|
215
|
+
* three aspects (the empty one gives `none`).
|
|
216
|
+
*/
|
|
217
|
+
const ASPECT_KEYS = [
|
|
218
|
+
0,
|
|
219
|
+
1,
|
|
220
|
+
2,
|
|
221
|
+
3,
|
|
222
|
+
4,
|
|
223
|
+
5,
|
|
224
|
+
6,
|
|
225
|
+
7
|
|
226
|
+
].map((bits) => sectionAspects({
|
|
227
|
+
plane: bits & 1 ? {
|
|
228
|
+
origin: [
|
|
229
|
+
0,
|
|
230
|
+
0,
|
|
231
|
+
0
|
|
232
|
+
],
|
|
233
|
+
normal: [
|
|
234
|
+
0,
|
|
235
|
+
0,
|
|
236
|
+
1
|
|
237
|
+
]
|
|
238
|
+
} : void 0,
|
|
239
|
+
meshes: bits & 2 ? [] : void 0,
|
|
240
|
+
fill: bits & 4 ? false : void 0
|
|
241
|
+
}).join("+") || "none");
|
|
242
|
+
function invalid(message) {
|
|
243
|
+
return new FastMeshError("INVALID_ARGUMENT", message);
|
|
244
|
+
}
|
|
245
|
+
/** The scene's colour rule for a declarative colouring by `field` (only the fields set). */
|
|
246
|
+
function colorRule(field, c) {
|
|
247
|
+
const rule = {
|
|
248
|
+
field,
|
|
249
|
+
map: c.map,
|
|
250
|
+
range: c.range
|
|
251
|
+
};
|
|
252
|
+
if (c.below !== void 0) rule.below = c.below;
|
|
253
|
+
if (c.above !== void 0) rule.above = c.above;
|
|
254
|
+
if (c.center !== void 0) rule.center = c.center;
|
|
255
|
+
if (c.base !== void 0) rule.base = c.base;
|
|
256
|
+
if (c.nan !== void 0) rule.nan = c.nan;
|
|
257
|
+
return rule;
|
|
258
|
+
}
|
|
259
|
+
/** Set by `SceneStore`'s static block: the internal {@link setFrameLoop}. */
|
|
260
|
+
let setFrameLoopOf;
|
|
261
|
+
/** Adds `entry` under each of `keys` in `deps`. */
|
|
262
|
+
function addDeps(deps, entry, keys) {
|
|
263
|
+
for (const k of keys) {
|
|
264
|
+
let set = deps.get(k);
|
|
265
|
+
if (set === void 0) {
|
|
266
|
+
set = /* @__PURE__ */ new Set();
|
|
267
|
+
deps.set(k, set);
|
|
268
|
+
}
|
|
269
|
+
set.add(entry);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** Removes `entry` from under each of `keys` in `deps`. */
|
|
273
|
+
function dropDeps(deps, entry, keys) {
|
|
274
|
+
for (const k of keys) {
|
|
275
|
+
const set = deps.get(k);
|
|
276
|
+
if (set !== void 0 && set.delete(entry) && set.size === 0) deps.delete(k);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/** The first state in `"error"`, as a status, or `undefined`. */
|
|
280
|
+
function firstError(states) {
|
|
281
|
+
for (const state of states) if (state.status === "error") return {
|
|
282
|
+
status: "error",
|
|
283
|
+
error: state.error
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/** Whether two key lists are equal, in order. */
|
|
287
|
+
function sameKeys(a, b) {
|
|
288
|
+
return a.length === b.length && a.every((k, i) => k === b[i]);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* An observable store over a scene. Read with {@link getSnapshot} and
|
|
292
|
+
* {@link subscribe}; change through its actions. Call {@link commitFrame}
|
|
293
|
+
* once per animation frame instead of the scene's.
|
|
294
|
+
*/
|
|
295
|
+
var SceneStore = class {
|
|
296
|
+
/** The scene, for the renderer adapters (`bindMesh`, ...). */
|
|
297
|
+
scene;
|
|
298
|
+
/** `"local"` over a `Scene`, `"worker"` over a `WorkerScene`. */
|
|
299
|
+
mode;
|
|
300
|
+
#host;
|
|
301
|
+
#meshes = /* @__PURE__ */ new Map();
|
|
302
|
+
#sections = /* @__PURE__ */ new Map();
|
|
303
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
304
|
+
#unsubscribe;
|
|
305
|
+
/** Meshes whose display changed since the last flush. */
|
|
306
|
+
#pendingDisplay = /* @__PURE__ */ new Set();
|
|
307
|
+
/** Sections whose parts changed since the last flush. */
|
|
308
|
+
#pendingSections = /* @__PURE__ */ new Set();
|
|
309
|
+
/** Meshes (tracked or just removed) whose state must be re-derived. */
|
|
310
|
+
#dirtyMeshes = /* @__PURE__ */ new Set();
|
|
311
|
+
/** Sections (tracked or just removed) whose state must be re-derived. */
|
|
312
|
+
#dirtySections = /* @__PURE__ */ new Set();
|
|
313
|
+
/** Every state must be re-derived (at the start, and when `history` starts or stops computing). */
|
|
314
|
+
#allDirty = true;
|
|
315
|
+
/** Whether `history` was "computing" when the states were last derived. */
|
|
316
|
+
#historyBusy = false;
|
|
317
|
+
/** Test-only: every refresh re-derives every state (see {@link setFullRefresh}). */
|
|
318
|
+
#fullRefresh = false;
|
|
319
|
+
/** The meshes whose state reads a status key, by key (`history` aside). */
|
|
320
|
+
#meshDeps = /* @__PURE__ */ new Map();
|
|
321
|
+
/** The sections whose state reads a status key, by key (`history` aside). */
|
|
322
|
+
#sectionDeps = /* @__PURE__ */ new Map();
|
|
323
|
+
/** Meshes that may have a colouring in flight (a superset; settled ones leave at the next refresh). */
|
|
324
|
+
#inflightMeshes = /* @__PURE__ */ new Set();
|
|
325
|
+
/** Sections that may have an aspect in flight (a superset, likewise). */
|
|
326
|
+
#inflightSections = /* @__PURE__ */ new Set();
|
|
327
|
+
/** The states in the snapshot whose status is `"computing"`. */
|
|
328
|
+
#busy = 0;
|
|
329
|
+
/** The states in the snapshot whose status is `"error"`. */
|
|
330
|
+
#failed = 0;
|
|
331
|
+
/** The first state in "error" (meshes, then sections), kept until a state enters or leaves "error". */
|
|
332
|
+
#firstFailed;
|
|
333
|
+
/** A state entered or left "error" (or changed its error) since `#firstFailed` was found. */
|
|
334
|
+
#failedMoved = false;
|
|
335
|
+
/** A scene event came: the snapshot may be stale until the next refresh. */
|
|
336
|
+
#stale = false;
|
|
337
|
+
/** A refresh on a microtask is queued (for scene events, and snapshots not yet announced). */
|
|
338
|
+
#updateQueued = false;
|
|
339
|
+
/** A snapshot was published and its listeners not yet called. */
|
|
340
|
+
#owed = false;
|
|
341
|
+
/** Contact groups by id. */
|
|
342
|
+
#groups = /* @__PURE__ */ new Map();
|
|
343
|
+
/** Meshes a contact group uncoloured: the next flush also removes their owned field. */
|
|
344
|
+
#dropFields = /* @__PURE__ */ new Set();
|
|
345
|
+
/** Fields added so far, for their names (`fast-mesh-store:<n>`). */
|
|
346
|
+
#fieldCount = 0;
|
|
347
|
+
#selection = null;
|
|
348
|
+
#snapshot = EMPTY_SNAPSHOT;
|
|
349
|
+
#frameLoop = false;
|
|
350
|
+
#flushQueued = false;
|
|
351
|
+
#disposed = false;
|
|
352
|
+
constructor(scene) {
|
|
353
|
+
this.#host = hostOf(scene);
|
|
354
|
+
this.scene = this.#host.scene;
|
|
355
|
+
this.mode = this.#host.mode;
|
|
356
|
+
const event = () => {
|
|
357
|
+
if (!this.#disposed) {
|
|
358
|
+
this.#stale = true;
|
|
359
|
+
this.#queueUpdate();
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
this.#unsubscribe = [
|
|
363
|
+
scene.onChanges(event),
|
|
364
|
+
this.#host.onStatus(event),
|
|
365
|
+
() => this.#host.stop()
|
|
366
|
+
];
|
|
367
|
+
this.#update();
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The current snapshot: frozen, replaced (never mutated) on change. It is
|
|
371
|
+
* up to date with the scene's statuses when read: a status change not yet
|
|
372
|
+
* announced refreshes it here, and its listeners are called on a microtask.
|
|
373
|
+
*/
|
|
374
|
+
getSnapshot = () => {
|
|
375
|
+
if (!this.#disposed && (this.#stale || this.#host.changed.size > 0)) {
|
|
376
|
+
this.#refresh();
|
|
377
|
+
if (this.#owed) this.#queueUpdate();
|
|
378
|
+
}
|
|
379
|
+
return this.#snapshot;
|
|
380
|
+
};
|
|
381
|
+
/**
|
|
382
|
+
* Registers a listener called after each new snapshot; returns the function
|
|
383
|
+
* that removes it. A listener that throws does not stop the others nor the
|
|
384
|
+
* action that changed the state (which has taken effect): its error is
|
|
385
|
+
* reported with `reportError` (an async rethrow where there is none), as
|
|
386
|
+
* the Worker scene does with its listeners.
|
|
387
|
+
*/
|
|
388
|
+
subscribe = (listener) => {
|
|
389
|
+
this.#listeners.add(listener);
|
|
390
|
+
return () => {
|
|
391
|
+
this.#listeners.delete(listener);
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
/**
|
|
395
|
+
* Call once per animation frame (instead of the scene's `commitFrame()`):
|
|
396
|
+
* applies the frame's declarative changes, then commits the scene's frame
|
|
397
|
+
* and returns its change records. From the first call on, declarative
|
|
398
|
+
* changes wait for it instead of applying on a microtask.
|
|
399
|
+
*/
|
|
400
|
+
commitFrame() {
|
|
401
|
+
this.#alive();
|
|
402
|
+
this.#frameLoop = true;
|
|
403
|
+
this.#flush();
|
|
404
|
+
const changes = this.#host.scene.commitFrame();
|
|
405
|
+
this.#update();
|
|
406
|
+
return changes;
|
|
407
|
+
}
|
|
408
|
+
/** Adds a mesh (default display) and returns its handle at once. */
|
|
409
|
+
addMesh(positions, indices, options) {
|
|
410
|
+
this.#alive();
|
|
411
|
+
const handle = this.#host.addMesh(positions, indices, options);
|
|
412
|
+
this.#meshes.set(handle, {
|
|
413
|
+
handle,
|
|
414
|
+
id: this.#host.idOf(handle),
|
|
415
|
+
display: DEFAULT_DISPLAY,
|
|
416
|
+
desired: DEFAULT_DISPLAY,
|
|
417
|
+
sent: null
|
|
418
|
+
});
|
|
419
|
+
this.#dirtyMeshes.add(handle);
|
|
420
|
+
this.#update();
|
|
421
|
+
return handle;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Removes a mesh; it leaves the snapshot (and the selection) at once. The
|
|
425
|
+
* scene drops the fields measured to it alone, and the colourings by them:
|
|
426
|
+
* a mesh coloured by distance to it alone shows no colouring, keeps its
|
|
427
|
+
* rule in `desired`, and is in `"error"` (`UNKNOWN_MESH`) from the next
|
|
428
|
+
* flush until its colouring changes. A colouring measured to it and other
|
|
429
|
+
* meshes keeps the others (its `distanceTo` loses the mesh). It leaves its
|
|
430
|
+
* contact groups, whose members' partners follow. Sections stop cutting it.
|
|
431
|
+
*/
|
|
432
|
+
removeMesh(mesh) {
|
|
433
|
+
this.#alive();
|
|
434
|
+
this.#host.removeMesh(mesh);
|
|
435
|
+
const entry = this.#meshes.get(mesh);
|
|
436
|
+
if (entry !== void 0) {
|
|
437
|
+
this.#meshes.delete(mesh);
|
|
438
|
+
this.#pendingDisplay.delete(entry);
|
|
439
|
+
this.#dropFields.delete(entry);
|
|
440
|
+
this.#inflightMeshes.delete(entry);
|
|
441
|
+
this.#unindexMesh(entry);
|
|
442
|
+
this.#dirtyMeshes.add(mesh);
|
|
443
|
+
const display = `display:${entry.id}`;
|
|
444
|
+
const field = entry.field === void 0 ? void 0 : `field:${entry.field.id}`;
|
|
445
|
+
this.#host.forgetErrors((k) => k === display || k === field);
|
|
446
|
+
}
|
|
447
|
+
if (this.#selection === mesh) this.#selection = null;
|
|
448
|
+
for (const other of this.#meshes.values()) {
|
|
449
|
+
const prune = (d) => {
|
|
450
|
+
const c = withoutTarget(d.coloring, mesh);
|
|
451
|
+
return c === d.coloring ? d : Object.freeze({
|
|
452
|
+
...d,
|
|
453
|
+
coloring: c
|
|
454
|
+
});
|
|
455
|
+
};
|
|
456
|
+
const before = [
|
|
457
|
+
other.display,
|
|
458
|
+
other.desired,
|
|
459
|
+
other.sent
|
|
460
|
+
];
|
|
461
|
+
other.display = prune(other.display);
|
|
462
|
+
other.desired = prune(other.desired);
|
|
463
|
+
other.sent = withoutTarget(other.sent, mesh);
|
|
464
|
+
if (other.inflight !== void 0) other.inflight.value = withoutTarget(other.inflight.value, mesh);
|
|
465
|
+
if (other.display !== before[0] || other.desired !== before[1] || other.sent !== before[2]) this.#dirtyMeshes.add(other.handle);
|
|
466
|
+
const owned = other.field;
|
|
467
|
+
if (owned === void 0 || !owned.to.includes(mesh)) continue;
|
|
468
|
+
if (owned.to.length > 1) {
|
|
469
|
+
const at = owned.to.indexOf(mesh);
|
|
470
|
+
other.field = {
|
|
471
|
+
...owned,
|
|
472
|
+
to: owned.to.filter((_, k) => k !== at),
|
|
473
|
+
toIds: owned.toIds.filter((_, k) => k !== at)
|
|
474
|
+
};
|
|
475
|
+
this.#dirtyMeshes.add(other.handle);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
const fieldKey = `field:${owned.id}`;
|
|
479
|
+
this.#host.forgetErrors((k) => k === fieldKey);
|
|
480
|
+
other.field = void 0;
|
|
481
|
+
other.sent = null;
|
|
482
|
+
other.inflight = void 0;
|
|
483
|
+
this.#dirtyMeshes.add(other.handle);
|
|
484
|
+
if (other.display.coloring !== null) other.display = Object.freeze({
|
|
485
|
+
...other.display,
|
|
486
|
+
coloring: null
|
|
487
|
+
});
|
|
488
|
+
if (other.desired.coloring !== null) {
|
|
489
|
+
this.#pendingDisplay.add(other);
|
|
490
|
+
this.#schedule();
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const without = (meshes) => meshes.includes(mesh) ? Object.freeze(meshes.filter((m) => m !== mesh)) : meshes;
|
|
494
|
+
for (const s of this.#sections.values()) {
|
|
495
|
+
const { applied, sent, desired } = s;
|
|
496
|
+
s.applied = this.#withMeshes(s.applied, without(s.applied.meshes));
|
|
497
|
+
s.sent = this.#withMeshes(s.sent, without(s.sent.meshes));
|
|
498
|
+
s.desired = this.#withMeshes(s.desired, without(s.desired.meshes));
|
|
499
|
+
if (s.applied !== applied || s.sent !== sent || s.desired !== desired) this.#dirtySections.add(s.handle);
|
|
500
|
+
const inflight = s.inflight.meshes;
|
|
501
|
+
if (inflight !== void 0) inflight.value = without(inflight.value);
|
|
502
|
+
}
|
|
503
|
+
for (const ge of this.#groups.values()) {
|
|
504
|
+
const g = ge.group;
|
|
505
|
+
if (!g.meshes.includes(mesh)) continue;
|
|
506
|
+
const pairs = g.pairs;
|
|
507
|
+
ge.group = Object.freeze({
|
|
508
|
+
...g,
|
|
509
|
+
meshes: without(g.meshes),
|
|
510
|
+
pairs: pairs === void 0 || pairs === "all" ? pairs : Object.freeze(pairs.filter(([p, q]) => p !== mesh && q !== mesh))
|
|
511
|
+
});
|
|
512
|
+
ge.given.delete(mesh);
|
|
513
|
+
for (const [m, given] of ge.given) {
|
|
514
|
+
const left = given === null ? null : withoutTarget(given, mesh);
|
|
515
|
+
ge.given.set(m, left);
|
|
516
|
+
if (left !== null && partnersOf(ge.group, m).length === 0) {
|
|
517
|
+
this.#release(m, left);
|
|
518
|
+
ge.given.set(m, null);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
this.#update();
|
|
523
|
+
}
|
|
524
|
+
/** `parts` with `meshes`, the same object when they are. */
|
|
525
|
+
#withMeshes(parts, meshes) {
|
|
526
|
+
return meshes === parts.meshes ? parts : Object.freeze({
|
|
527
|
+
...parts,
|
|
528
|
+
meshes
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
/** Sets a mesh's placement (one undo step). */
|
|
532
|
+
setPlacement(mesh, placement) {
|
|
533
|
+
this.#alive();
|
|
534
|
+
this.#host.setPlacement(mesh, placement);
|
|
535
|
+
this.#update();
|
|
536
|
+
}
|
|
537
|
+
/** Starts a drag of a mesh; see the scene's `drag()`. Ending or cancelling it updates the snapshot. */
|
|
538
|
+
drag(mesh) {
|
|
539
|
+
this.#alive();
|
|
540
|
+
const session = this.#host.scene.drag(mesh);
|
|
541
|
+
const after = () => {
|
|
542
|
+
if (!this.#disposed) this.#update();
|
|
543
|
+
};
|
|
544
|
+
return {
|
|
545
|
+
move: (placement) => session.move(placement),
|
|
546
|
+
get active() {
|
|
547
|
+
return session.active;
|
|
548
|
+
},
|
|
549
|
+
end: () => {
|
|
550
|
+
session.end();
|
|
551
|
+
after();
|
|
552
|
+
},
|
|
553
|
+
cancel: () => {
|
|
554
|
+
session.cancel();
|
|
555
|
+
after();
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
/** Edits a mesh's triangles (one undo step). */
|
|
560
|
+
edit(mesh, ops) {
|
|
561
|
+
this.#alive();
|
|
562
|
+
return this.#settled(this.#host.edit(mesh, ops));
|
|
563
|
+
}
|
|
564
|
+
/** Repairs a mesh (one undo step). */
|
|
565
|
+
repair(mesh, op) {
|
|
566
|
+
this.#alive();
|
|
567
|
+
return this.#settled(this.#host.repair(mesh, op));
|
|
568
|
+
}
|
|
569
|
+
/** Refreshes now (a synchronous host has applied the call) and once the promise settles. */
|
|
570
|
+
#settled(p) {
|
|
571
|
+
this.#update();
|
|
572
|
+
const after = () => {
|
|
573
|
+
if (!this.#disposed) this.#update();
|
|
574
|
+
};
|
|
575
|
+
return p.then((v) => {
|
|
576
|
+
after();
|
|
577
|
+
return v;
|
|
578
|
+
}, (e) => {
|
|
579
|
+
after();
|
|
580
|
+
throw e;
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
/** Undoes the latest step. */
|
|
584
|
+
undo() {
|
|
585
|
+
this.#alive();
|
|
586
|
+
this.#host.undo();
|
|
587
|
+
this.#update();
|
|
588
|
+
}
|
|
589
|
+
/** Redoes the latest undone step. */
|
|
590
|
+
redo() {
|
|
591
|
+
this.#alive();
|
|
592
|
+
this.#host.redo();
|
|
593
|
+
this.#update();
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Changes how a mesh is shown. `desired` changes at once; `display` follows
|
|
597
|
+
* at the next `commitFrame()` (or a microtask without a frame loop). A
|
|
598
|
+
* patch equal to the current request changes nothing.
|
|
599
|
+
*
|
|
600
|
+
* A `coloring` colours the mesh by distance to `distanceTo` (one mesh or
|
|
601
|
+
* several) through a field the store owns (one per mesh, named
|
|
602
|
+
* `fast-mesh-store:<n>`, kept while colouring is off and replaced only
|
|
603
|
+
* when the set of meshes in `distanceTo`, `maxDistance` or `sign`
|
|
604
|
+
* changes). `display.coloring` is the colouring
|
|
605
|
+
* last applied: on a Worker scene, once its `display:` request is ready.
|
|
606
|
+
*
|
|
607
|
+
* @throws `UNKNOWN_MESH` for a mesh not in the store, `INVALID_ARGUMENT` for a bad value.
|
|
608
|
+
*/
|
|
609
|
+
setDisplay(mesh, patch) {
|
|
610
|
+
this.#alive();
|
|
611
|
+
const entry = this.#mesh(mesh);
|
|
612
|
+
const next = { ...entry.desired };
|
|
613
|
+
if (patch.opacity !== void 0) {
|
|
614
|
+
const o = patch.opacity;
|
|
615
|
+
if (typeof o !== "number" || !(o >= 0 && o <= 1)) throw invalid("opacity must be a number in [0, 1]");
|
|
616
|
+
next.opacity = o;
|
|
617
|
+
}
|
|
618
|
+
if (patch.visible !== void 0) {
|
|
619
|
+
if (typeof patch.visible !== "boolean") throw invalid("visible must be a boolean");
|
|
620
|
+
next.visible = patch.visible;
|
|
621
|
+
}
|
|
622
|
+
if (patch.coloring !== void 0) {
|
|
623
|
+
if (patch.coloring !== null && typeof patch.coloring !== "object") throw invalid("coloring must be an object or null");
|
|
624
|
+
if (!sameData(patch.coloring, entry.desired.coloring)) next.coloring = frozenCopy(patch.coloring);
|
|
625
|
+
}
|
|
626
|
+
if (next.opacity === entry.desired.opacity && next.visible === entry.desired.visible && next.coloring === entry.desired.coloring) return;
|
|
627
|
+
entry.desired = Object.freeze(next);
|
|
628
|
+
this.#pendingDisplay.add(entry);
|
|
629
|
+
this.#dirtyMeshes.add(mesh);
|
|
630
|
+
this.#schedule();
|
|
631
|
+
this.#update();
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Sets contact group `id`, or removes it with `null`. Every member is
|
|
635
|
+
* coloured by `group.coloring` with its partners (the members it is paired
|
|
636
|
+
* with) as `distanceTo`, through one field per member measured to all of
|
|
637
|
+
* its partners: each vertex shows the smallest signed distance over them
|
|
638
|
+
* (the nearest surface, or the deepest penetration). Moving a member then
|
|
639
|
+
* recomputes only its own field and those of its partners. A member
|
|
640
|
+
* without partners shows no colouring.
|
|
641
|
+
*
|
|
642
|
+
* Members are coloured through {@link setDisplay}, so `desired` and
|
|
643
|
+
* `display` show the colourings (groups themselves are not in the
|
|
644
|
+
* snapshot). Setting the group colours every member, overriding a
|
|
645
|
+
* colouring the app gave it. A member's later `setDisplay` colouring
|
|
646
|
+
* overrides the group's: the group then leaves that member alone when it
|
|
647
|
+
* shrinks, is removed, or loses a mesh, until it is set again. Members
|
|
648
|
+
* dropped from the group, or all of them when it is removed, stop being
|
|
649
|
+
* coloured, and their store-owned fields are removed (not kept for later,
|
|
650
|
+
* as `setDisplay` keeps them). A removed mesh leaves its groups (and their
|
|
651
|
+
* pairs); a member left without partners stops being coloured.
|
|
652
|
+
*
|
|
653
|
+
* @throws `UNKNOWN_MESH` for a mesh not in the store; `INVALID_ARGUMENT`
|
|
654
|
+
* for a mesh listed twice, a mesh already in another group, a pair that
|
|
655
|
+
* is not two different members, or a bad colouring (`maxDistance`,
|
|
656
|
+
* `range`, `map` or `sign`; checked here rather than at the flush).
|
|
657
|
+
*/
|
|
658
|
+
setContactGroup(id, group) {
|
|
659
|
+
this.#alive();
|
|
660
|
+
if (typeof id !== "string") throw invalid("a contact group id must be a string");
|
|
661
|
+
const old = this.#groups.get(id);
|
|
662
|
+
if (group === null) {
|
|
663
|
+
if (old !== void 0) {
|
|
664
|
+
this.#groups.delete(id);
|
|
665
|
+
for (const [m, given] of old.given) this.#release(m, given);
|
|
666
|
+
}
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (typeof group !== "object" || !Array.isArray(group.meshes)) throw invalid("a contact group needs a list of meshes");
|
|
670
|
+
const meshes = group.meshes;
|
|
671
|
+
meshes.forEach((m, k) => {
|
|
672
|
+
this.#mesh(m);
|
|
673
|
+
if (meshes.indexOf(m) !== k) throw invalid("a contact group lists a mesh twice");
|
|
674
|
+
for (const [other, g] of this.#groups) if (other !== id && g.group.meshes.includes(m)) throw invalid(`a mesh is already in contact group "${other}"`);
|
|
675
|
+
});
|
|
676
|
+
const pairs = group.pairs;
|
|
677
|
+
if (pairs !== void 0 && pairs !== "all") {
|
|
678
|
+
if (!Array.isArray(pairs)) throw invalid("pairs must be a list of mesh pairs or \"all\"");
|
|
679
|
+
for (const pair of pairs) {
|
|
680
|
+
if (!Array.isArray(pair) || pair.length !== 2 || pair[0] === pair[1]) throw invalid("a pair needs two different meshes");
|
|
681
|
+
if (!meshes.includes(pair[0]) || !meshes.includes(pair[1])) throw invalid("a pair's meshes must be members of the group");
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
checkGroupColoring(group.coloring);
|
|
685
|
+
const next = frozenCopy({
|
|
686
|
+
meshes,
|
|
687
|
+
pairs,
|
|
688
|
+
coloring: group.coloring
|
|
689
|
+
});
|
|
690
|
+
const given = /* @__PURE__ */ new Map();
|
|
691
|
+
for (const [m, was] of old?.given ?? []) if (!next.meshes.includes(m)) this.#release(m, was);
|
|
692
|
+
this.#groups.set(id, {
|
|
693
|
+
group: next,
|
|
694
|
+
given
|
|
695
|
+
});
|
|
696
|
+
for (const m of next.meshes) {
|
|
697
|
+
const partners = partnersOf(next, m);
|
|
698
|
+
if (partners.length === 0) {
|
|
699
|
+
this.#release(m, this.#mesh(m).desired.coloring);
|
|
700
|
+
given.set(m, null);
|
|
701
|
+
} else {
|
|
702
|
+
this.setDisplay(m, { coloring: {
|
|
703
|
+
...next.coloring,
|
|
704
|
+
distanceTo: partners
|
|
705
|
+
} });
|
|
706
|
+
given.set(m, this.#mesh(m).desired.coloring);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Uncolours member `m` of a contact group, and has the next flush remove
|
|
712
|
+
* its owned field, when its requested colouring is still `given` (the
|
|
713
|
+
* one the group gave it): a colouring the app set since is left alone.
|
|
714
|
+
*/
|
|
715
|
+
#release(m, given) {
|
|
716
|
+
const entry = this.#meshes.get(m);
|
|
717
|
+
if (entry === void 0 || !sameData(entry.desired.coloring, given)) return;
|
|
718
|
+
this.#dropFields.add(entry);
|
|
719
|
+
this.#pendingDisplay.add(entry);
|
|
720
|
+
this.setDisplay(m, { coloring: null });
|
|
721
|
+
this.#schedule();
|
|
722
|
+
}
|
|
723
|
+
/** Removes `entry`'s owned field (and so a colouring by it), if any. */
|
|
724
|
+
#dropField(entry) {
|
|
725
|
+
const field = entry.field;
|
|
726
|
+
if (field === void 0) return;
|
|
727
|
+
entry.field = void 0;
|
|
728
|
+
this.#dirtyMeshes.add(entry.handle);
|
|
729
|
+
const fieldKey = `field:${field.id}`;
|
|
730
|
+
this.#host.forgetErrors((k) => k === fieldKey);
|
|
731
|
+
this.#host.removeField(field.handle);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* The targets of the store-owned field behind the mesh's colouring (the
|
|
735
|
+
* one last sent to the scene), in the field's order: the order its
|
|
736
|
+
* nearest-target indices (`Scene.fieldNearest`,
|
|
737
|
+
* `WorkerScene.fetchFieldNearest`) refer to. That is ascending scene id,
|
|
738
|
+
* not the order of `distanceTo` or of a contact group's members. A target
|
|
739
|
+
* removed since leaves the list, as it leaves the field. Frozen; `null`
|
|
740
|
+
* when the mesh has no colouring.
|
|
741
|
+
*
|
|
742
|
+
* @throws `UNKNOWN_MESH` for a mesh not in the store; `INVALID_ARGUMENT`
|
|
743
|
+
* once the store is disposed.
|
|
744
|
+
*/
|
|
745
|
+
coloringTargets(mesh) {
|
|
746
|
+
this.#alive();
|
|
747
|
+
const entry = this.#mesh(mesh);
|
|
748
|
+
return entry.sent === null || entry.field === void 0 ? null : entry.field.to;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* The values of the store-owned field behind the mesh's colouring (the one
|
|
752
|
+
* last sent to the scene), one per vertex slot, as of the field's last
|
|
753
|
+
* update: a copy the caller owns. `null` when the mesh has no colouring.
|
|
754
|
+
* On a Worker scene the request is ordered after the calls made before
|
|
755
|
+
* it, so it reads the colouring last flushed. Never a history step.
|
|
756
|
+
*
|
|
757
|
+
* Rejects with `UNKNOWN_MESH` for a mesh not in the store, and
|
|
758
|
+
* `INVALID_ARGUMENT` once the store is disposed.
|
|
759
|
+
*/
|
|
760
|
+
fetchColoringValues(mesh) {
|
|
761
|
+
return new Promise((resolve) => {
|
|
762
|
+
this.#alive();
|
|
763
|
+
const entry = this.#mesh(mesh);
|
|
764
|
+
const field = entry.field;
|
|
765
|
+
resolve(entry.sent === null || field === void 0 ? null : this.#host.fieldValues(field.handle));
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
/** Adds a plane section and returns its handle at once. */
|
|
769
|
+
addSection(desc) {
|
|
770
|
+
this.#alive();
|
|
771
|
+
const handle = this.#host.addSection(desc);
|
|
772
|
+
const parts = frozenCopy({
|
|
773
|
+
plane: desc.plane,
|
|
774
|
+
meshes: desc.meshes,
|
|
775
|
+
fill: desc.fill ?? false
|
|
776
|
+
});
|
|
777
|
+
this.#sections.set(handle, {
|
|
778
|
+
handle,
|
|
779
|
+
id: this.#host.idOf(handle),
|
|
780
|
+
applied: parts,
|
|
781
|
+
desired: parts,
|
|
782
|
+
sent: parts,
|
|
783
|
+
inflight: {}
|
|
784
|
+
});
|
|
785
|
+
this.#dirtySections.add(handle);
|
|
786
|
+
this.#update();
|
|
787
|
+
return handle;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Changes a section's plane, meshes or fill. `desired` changes at once;
|
|
791
|
+
* the section follows at the next `commitFrame()` (or a microtask without
|
|
792
|
+
* a frame loop), each aspect set only when it changed, the last value
|
|
793
|
+
* per frame winning.
|
|
794
|
+
*
|
|
795
|
+
* @throws `UNKNOWN_SECTION` for a section not in the store.
|
|
796
|
+
*/
|
|
797
|
+
setSection(section, patch) {
|
|
798
|
+
this.#alive();
|
|
799
|
+
const entry = this.#section(section);
|
|
800
|
+
const next = { ...entry.desired };
|
|
801
|
+
if (patch.plane !== void 0 && !sameData(patch.plane, next.plane)) next.plane = frozenCopy(patch.plane);
|
|
802
|
+
if (patch.meshes !== void 0 && !sameData(patch.meshes, next.meshes)) next.meshes = frozenCopy(patch.meshes);
|
|
803
|
+
if (patch.fill !== void 0) {
|
|
804
|
+
if (typeof patch.fill !== "boolean") throw invalid("fill must be a boolean");
|
|
805
|
+
next.fill = patch.fill;
|
|
806
|
+
}
|
|
807
|
+
if (next.plane === entry.desired.plane && next.meshes === entry.desired.meshes && next.fill === entry.desired.fill) return;
|
|
808
|
+
entry.desired = Object.freeze(next);
|
|
809
|
+
this.#pendingSections.add(entry);
|
|
810
|
+
this.#dirtySections.add(section);
|
|
811
|
+
this.#schedule();
|
|
812
|
+
this.#update();
|
|
813
|
+
}
|
|
814
|
+
/** Removes a section; it leaves the snapshot at once. */
|
|
815
|
+
removeSection(section) {
|
|
816
|
+
this.#alive();
|
|
817
|
+
this.#host.removeSection(section);
|
|
818
|
+
const entry = this.#sections.get(section);
|
|
819
|
+
if (entry !== void 0) {
|
|
820
|
+
this.#sections.delete(section);
|
|
821
|
+
this.#pendingSections.delete(entry);
|
|
822
|
+
this.#inflightSections.delete(entry);
|
|
823
|
+
this.#unindexSection(entry);
|
|
824
|
+
this.#dirtySections.add(section);
|
|
825
|
+
const own = `section:${entry.id}`;
|
|
826
|
+
this.#host.forgetErrors((k) => k === own || k.startsWith(`${own}:`));
|
|
827
|
+
}
|
|
828
|
+
this.#update();
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Selects a mesh, or nothing with `null`. UI state only: the scene never sees it.
|
|
832
|
+
*
|
|
833
|
+
* @throws `UNKNOWN_MESH` for a mesh not in the store.
|
|
834
|
+
*/
|
|
835
|
+
select(mesh) {
|
|
836
|
+
this.#alive();
|
|
837
|
+
if (mesh !== null) this.#mesh(mesh);
|
|
838
|
+
this.#selection = mesh;
|
|
839
|
+
this.#update();
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Restarts the Worker scene (see `WorkerScene.restart()`). Rejects with
|
|
843
|
+
* `INVALID_ARGUMENT` on a local (synchronous) scene.
|
|
844
|
+
*/
|
|
845
|
+
restart() {
|
|
846
|
+
if (this.#disposed) return Promise.reject(invalid("the store is disposed"));
|
|
847
|
+
const restarted = this.#host.restart();
|
|
848
|
+
for (const entry of this.#meshes.values()) if (!sameData(entry.display.coloring, entry.sent)) {
|
|
849
|
+
entry.inflight = {
|
|
850
|
+
value: entry.sent,
|
|
851
|
+
rev: this.#host.status(`display:${entry.id}`).revision
|
|
852
|
+
};
|
|
853
|
+
this.#inflightMeshes.add(entry);
|
|
854
|
+
}
|
|
855
|
+
for (const entry of this.#sections.values()) for (const a of ASPECTS) if (!sameData(entry.applied[a], entry.sent[a])) this.#arm(entry, a, entry.sent[a]);
|
|
856
|
+
return this.#settled(restarted);
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Removes the distance fields the store owns (`fast-mesh-store:<n>`, and
|
|
860
|
+
* so the colourings by them) and stops listening to the scene. The rest of
|
|
861
|
+
* the scene is left as it is (it is the app's). On a Worker scene the
|
|
862
|
+
* removals are queued like any call, and sent with the scene's next
|
|
863
|
+
* `commitFrame()`; errors from a scene that can no longer take them (a
|
|
864
|
+
* poisoned Worker scene, for one) are ignored. Later actions throw
|
|
865
|
+
* `INVALID_ARGUMENT`; the last snapshot stays readable.
|
|
866
|
+
*/
|
|
867
|
+
dispose() {
|
|
868
|
+
if (this.#disposed) return;
|
|
869
|
+
this.#disposed = true;
|
|
870
|
+
for (const entry of this.#meshes.values()) {
|
|
871
|
+
const field = entry.field;
|
|
872
|
+
if (field === void 0) continue;
|
|
873
|
+
entry.field = void 0;
|
|
874
|
+
try {
|
|
875
|
+
this.#host.removeField(field.handle);
|
|
876
|
+
} catch {}
|
|
877
|
+
}
|
|
878
|
+
for (const off of this.#unsubscribe.splice(0)) off();
|
|
879
|
+
this.#pendingDisplay.clear();
|
|
880
|
+
this.#pendingSections.clear();
|
|
881
|
+
}
|
|
882
|
+
#alive() {
|
|
883
|
+
if (this.#disposed) throw invalid("the store is disposed");
|
|
884
|
+
}
|
|
885
|
+
#mesh(mesh) {
|
|
886
|
+
const entry = this.#meshes.get(mesh);
|
|
887
|
+
if (entry === void 0) throw new FastMeshError("UNKNOWN_MESH", "the mesh is not in the store (removed, or not added through it)");
|
|
888
|
+
return entry;
|
|
889
|
+
}
|
|
890
|
+
#section(section) {
|
|
891
|
+
const entry = this.#sections.get(section);
|
|
892
|
+
if (entry === void 0) throw new FastMeshError("UNKNOWN_SECTION", "the section is not in the store (removed, or not added through it)");
|
|
893
|
+
return entry;
|
|
894
|
+
}
|
|
895
|
+
/** Without a frame loop, flushes on a microtask; with one, the next `commitFrame()` does. */
|
|
896
|
+
#schedule() {
|
|
897
|
+
if (this.#frameLoop || this.#flushQueued) return;
|
|
898
|
+
this.#flushQueued = true;
|
|
899
|
+
queueMicrotask(() => {
|
|
900
|
+
this.#flushQueued = false;
|
|
901
|
+
if (this.#disposed || this.#frameLoop) return;
|
|
902
|
+
this.#flush();
|
|
903
|
+
this.#update();
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Applies the declarative changes made since the last flush, the latest
|
|
908
|
+
* value of each. Never throws: a scene call that fails puts its key (and
|
|
909
|
+
* so the mesh or section) in `"error"`.
|
|
910
|
+
*/
|
|
911
|
+
#flush() {
|
|
912
|
+
for (const entry of this.#pendingDisplay) {
|
|
913
|
+
this.#dirtyMeshes.add(entry.handle);
|
|
914
|
+
const d = entry.display;
|
|
915
|
+
const want = entry.desired;
|
|
916
|
+
if (d.opacity !== want.opacity || d.visible !== want.visible) entry.display = Object.freeze({
|
|
917
|
+
...d,
|
|
918
|
+
opacity: want.opacity,
|
|
919
|
+
visible: want.visible
|
|
920
|
+
});
|
|
921
|
+
this.#flushColoring(entry);
|
|
922
|
+
}
|
|
923
|
+
this.#pendingDisplay.clear();
|
|
924
|
+
for (const entry of this.#pendingSections) {
|
|
925
|
+
this.#dirtySections.add(entry.handle);
|
|
926
|
+
this.#flushSection(entry);
|
|
927
|
+
}
|
|
928
|
+
this.#pendingSections.clear();
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* Sends a mesh's desired colouring when it differs from the one sent:
|
|
932
|
+
* resolves its owned field (reused, or replaced when its key changed),
|
|
933
|
+
* then sets the colour rule. Under the `display:` key, so a call that
|
|
934
|
+
* throws sets that key's error (and one that succeeds clears it).
|
|
935
|
+
*/
|
|
936
|
+
#flushColoring(entry) {
|
|
937
|
+
const key = `display:${entry.id}`;
|
|
938
|
+
const want = entry.desired.coloring;
|
|
939
|
+
const drop = this.#dropFields.delete(entry) && want === null;
|
|
940
|
+
try {
|
|
941
|
+
this.#host.track(key, () => {
|
|
942
|
+
if (sameData(want, entry.sent)) {
|
|
943
|
+
if (drop) this.#dropField(entry);
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
if (want === null) {
|
|
947
|
+
this.#host.setColoring(entry.handle, null);
|
|
948
|
+
if (drop) this.#dropField(entry);
|
|
949
|
+
} else {
|
|
950
|
+
const field = this.#ownedField(entry, want, key);
|
|
951
|
+
this.#host.setColoring(entry.handle, colorRule(field.handle, want));
|
|
952
|
+
}
|
|
953
|
+
entry.sent = want;
|
|
954
|
+
entry.inflight = {
|
|
955
|
+
value: want,
|
|
956
|
+
rev: this.#host.status(key).revision
|
|
957
|
+
};
|
|
958
|
+
this.#inflightMeshes.add(entry);
|
|
959
|
+
});
|
|
960
|
+
} catch {
|
|
961
|
+
if (entry.sent === null && entry.display.coloring !== null) {
|
|
962
|
+
entry.inflight = void 0;
|
|
963
|
+
entry.display = Object.freeze({
|
|
964
|
+
...entry.display,
|
|
965
|
+
coloring: null
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
/** The field for colouring `entry` by `want`: the owned one when its key matches, else a new one (replacing it). */
|
|
971
|
+
#ownedField(entry, want, key) {
|
|
972
|
+
const sign = want.sign ?? "auto";
|
|
973
|
+
const order = (m) => this.#idOf(m) ?? Number.POSITIVE_INFINITY;
|
|
974
|
+
const to = [...targetsOf(want.distanceTo)].sort((a, b) => order(a) - order(b));
|
|
975
|
+
const old = entry.field;
|
|
976
|
+
if (old !== void 0 && old.to.length === to.length && old.to.every((m, k) => m === to[k]) && Object.is(old.maxDistance, want.maxDistance) && old.sign === sign) return old;
|
|
977
|
+
if (old !== void 0) {
|
|
978
|
+
entry.field = void 0;
|
|
979
|
+
entry.sent = null;
|
|
980
|
+
this.#host.removeField(old.handle);
|
|
981
|
+
}
|
|
982
|
+
const handle = this.#host.addField({
|
|
983
|
+
name: `fast-mesh-store:${++this.#fieldCount}`,
|
|
984
|
+
on: entry.handle,
|
|
985
|
+
to,
|
|
986
|
+
maxDistance: want.maxDistance,
|
|
987
|
+
sign
|
|
988
|
+
}, key);
|
|
989
|
+
const field = {
|
|
990
|
+
handle,
|
|
991
|
+
id: this.#host.idOf(handle),
|
|
992
|
+
to: Object.freeze(to),
|
|
993
|
+
toIds: Object.freeze(to.map((m) => this.#idOf(m))),
|
|
994
|
+
maxDistance: want.maxDistance,
|
|
995
|
+
sign
|
|
996
|
+
};
|
|
997
|
+
entry.field = field;
|
|
998
|
+
return field;
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* A mesh's id in status keys, whether or not the store tracks it (a mesh
|
|
1002
|
+
* added straight to the scene); `undefined` for one the scene no longer
|
|
1003
|
+
* knows (removed).
|
|
1004
|
+
*/
|
|
1005
|
+
#idOf(mesh) {
|
|
1006
|
+
try {
|
|
1007
|
+
return this.#host.idOf(mesh);
|
|
1008
|
+
} catch {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
/** Sends each aspect of a section whose desired value differs from the one sent, under its own key. */
|
|
1013
|
+
#flushSection(entry) {
|
|
1014
|
+
for (const a of ASPECTS) {
|
|
1015
|
+
const want = entry.desired[a];
|
|
1016
|
+
if (sameData(want, entry.sent[a])) continue;
|
|
1017
|
+
try {
|
|
1018
|
+
this.#host.track(`section:${entry.id}:${a}`, () => {
|
|
1019
|
+
if (a === "plane") this.#host.setSectionPlane(entry.handle, want);
|
|
1020
|
+
else if (a === "meshes") this.#host.setSectionMeshes(entry.handle, want);
|
|
1021
|
+
else this.#host.setSectionFill(entry.handle, want);
|
|
1022
|
+
});
|
|
1023
|
+
} catch {
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
entry.sent = Object.freeze({
|
|
1027
|
+
...entry.sent,
|
|
1028
|
+
[a]: want
|
|
1029
|
+
});
|
|
1030
|
+
this.#arm(entry, a, want);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
/** Waits for the request now holding the key of a section's aspect, to apply `value`. */
|
|
1034
|
+
#arm(entry, a, value) {
|
|
1035
|
+
entry.inflight[a] = {
|
|
1036
|
+
value,
|
|
1037
|
+
rev: this.#host.status(`section:${entry.id}:${a}`).revision
|
|
1038
|
+
};
|
|
1039
|
+
this.#inflightSections.add(entry);
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Whether the request awaited under `key` is done: `"ready"` (apply it),
|
|
1043
|
+
* `"error"` (drop it), or `undefined` while it is pending.
|
|
1044
|
+
*/
|
|
1045
|
+
#outcome(key, wait) {
|
|
1046
|
+
const s = this.#host.status(key);
|
|
1047
|
+
if (s.revision < wait.rev) return;
|
|
1048
|
+
return s.status === "ready" ? "ready" : s.status === "error" ? "error" : void 0;
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Applies the colouring and section aspects whose requests are done, and
|
|
1052
|
+
* marks the states that change dirty. Only the entries that may have one
|
|
1053
|
+
* in flight are visited (all of them under the test-only full refresh).
|
|
1054
|
+
*/
|
|
1055
|
+
#applyDone() {
|
|
1056
|
+
for (const entry of this.#fullRefresh ? [...this.#meshes.values()] : this.#inflightMeshes) {
|
|
1057
|
+
const wait = entry.inflight;
|
|
1058
|
+
if (wait === void 0) {
|
|
1059
|
+
this.#inflightMeshes.delete(entry);
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1062
|
+
const done = this.#outcome(`display:${entry.id}`, wait);
|
|
1063
|
+
if (done !== void 0) {
|
|
1064
|
+
entry.inflight = void 0;
|
|
1065
|
+
this.#inflightMeshes.delete(entry);
|
|
1066
|
+
if (done === "ready" && entry.display.coloring !== wait.value) {
|
|
1067
|
+
entry.display = Object.freeze({
|
|
1068
|
+
...entry.display,
|
|
1069
|
+
coloring: wait.value
|
|
1070
|
+
});
|
|
1071
|
+
this.#dirtyMeshes.add(entry.handle);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
for (const entry of this.#fullRefresh ? [...this.#sections.values()] : this.#inflightSections) {
|
|
1076
|
+
let waiting = false;
|
|
1077
|
+
for (const a of ASPECTS) {
|
|
1078
|
+
const wait = entry.inflight[a];
|
|
1079
|
+
if (wait === void 0) continue;
|
|
1080
|
+
const done = this.#outcome(`section:${entry.id}:${a}`, wait);
|
|
1081
|
+
if (done === void 0) {
|
|
1082
|
+
waiting = true;
|
|
1083
|
+
continue;
|
|
1084
|
+
}
|
|
1085
|
+
delete entry.inflight[a];
|
|
1086
|
+
if (done === "ready" && entry.applied[a] !== wait.value) {
|
|
1087
|
+
entry.applied = Object.freeze({
|
|
1088
|
+
...entry.applied,
|
|
1089
|
+
[a]: wait.value
|
|
1090
|
+
});
|
|
1091
|
+
this.#dirtySections.add(entry.handle);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
if (!waiting) this.#inflightSections.delete(entry);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
#key(key) {
|
|
1098
|
+
const s = this.#host.status(key);
|
|
1099
|
+
return s.status === "error" ? {
|
|
1100
|
+
status: "error",
|
|
1101
|
+
error: s.error
|
|
1102
|
+
} : { status: s.status };
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* The status of a state from its own keys (all of their status, errors
|
|
1106
|
+
* included) and the keys it depends on, which only add `"computing"`:
|
|
1107
|
+
* their errors belong to the requests that made them (a failed edit
|
|
1108
|
+
* rejects its promise) and are ignored here. `history` is a dependent key
|
|
1109
|
+
* of every state. `pending` (a request waiting for the next flush that
|
|
1110
|
+
* differs from what was sent) also makes it `"computing"`.
|
|
1111
|
+
*/
|
|
1112
|
+
#status(own, dependent, pending) {
|
|
1113
|
+
const parts = own.map((k) => this.#key(k));
|
|
1114
|
+
if (pending || dependent.some((k) => this.#host.status(k).status === "computing") || this.#host.status("history").status === "computing") parts.push({ status: "computing" });
|
|
1115
|
+
return combine(parts);
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* A mesh's status: its own `mesh:` and `display:` keys and the `field:`
|
|
1119
|
+
* key of its store field, made `"computing"` also by its `placement:` and
|
|
1120
|
+
* `edit:` keys, those of the meshes it is coloured by distance to, and the
|
|
1121
|
+
* history, and while a requested colouring waits for the next flush.
|
|
1122
|
+
* (Opacity and visibility are store-only: they never make it pending.)
|
|
1123
|
+
* Indexes the mesh under the keys it read.
|
|
1124
|
+
*/
|
|
1125
|
+
#meshStatus(entry) {
|
|
1126
|
+
const id = entry.id;
|
|
1127
|
+
const own = [`mesh:${id}`, `display:${id}`];
|
|
1128
|
+
const dependent = [`placement:${id}`, `edit:${id}`];
|
|
1129
|
+
const field = entry.field;
|
|
1130
|
+
if (field !== void 0) {
|
|
1131
|
+
own.push(`field:${field.id}`);
|
|
1132
|
+
if (entry.sent !== null) {
|
|
1133
|
+
for (const to of field.toIds) if (to !== void 0 && to !== id) dependent.push(`placement:${to}`, `edit:${to}`);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
const keys = [...own, ...dependent];
|
|
1137
|
+
if (entry.keys === void 0 || !sameKeys(entry.keys, keys)) {
|
|
1138
|
+
this.#unindexMesh(entry);
|
|
1139
|
+
addDeps(this.#meshDeps, entry, keys);
|
|
1140
|
+
entry.keys = keys;
|
|
1141
|
+
}
|
|
1142
|
+
return this.#status(own, dependent, this.#pendingDisplay.has(entry) && !sameData(entry.desired.coloring, entry.sent));
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* A section's status: its own `section:` keys, made `"computing"` also by
|
|
1146
|
+
* the `placement:` and `edit:` keys of its meshes and the history, and
|
|
1147
|
+
* while a requested aspect waits for the next flush. Indexes the section
|
|
1148
|
+
* under the keys it read.
|
|
1149
|
+
*/
|
|
1150
|
+
#sectionStatus(entry) {
|
|
1151
|
+
const own = [`section:${entry.id}`, ...ASPECT_KEYS.map((a) => `section:${entry.id}:${a}`)];
|
|
1152
|
+
const dependent = [];
|
|
1153
|
+
for (const m of entry.applied.meshes) {
|
|
1154
|
+
const mesh = this.#meshes.get(m);
|
|
1155
|
+
if (mesh !== void 0) dependent.push(`placement:${mesh.id}`, `edit:${mesh.id}`);
|
|
1156
|
+
}
|
|
1157
|
+
const keys = [...own, ...dependent];
|
|
1158
|
+
if (entry.keys === void 0 || !sameKeys(entry.keys, keys)) {
|
|
1159
|
+
this.#unindexSection(entry);
|
|
1160
|
+
addDeps(this.#sectionDeps, entry, keys);
|
|
1161
|
+
entry.keys = keys;
|
|
1162
|
+
}
|
|
1163
|
+
const pending = this.#pendingSections.has(entry) && ASPECTS.some((a) => !sameData(entry.desired[a], entry.sent[a]));
|
|
1164
|
+
return this.#status(own, dependent, pending);
|
|
1165
|
+
}
|
|
1166
|
+
#unindexMesh(entry) {
|
|
1167
|
+
if (entry.keys !== void 0) {
|
|
1168
|
+
dropDeps(this.#meshDeps, entry, entry.keys);
|
|
1169
|
+
entry.keys = void 0;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
#unindexSection(entry) {
|
|
1173
|
+
if (entry.keys !== void 0) {
|
|
1174
|
+
dropDeps(this.#sectionDeps, entry, entry.keys);
|
|
1175
|
+
entry.keys = void 0;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Marks dirty the states that read a key whose status changed since the
|
|
1180
|
+
* last refresh, and clears the host's set; every state when `history`
|
|
1181
|
+
* starts or stops computing.
|
|
1182
|
+
*/
|
|
1183
|
+
#takeChanged() {
|
|
1184
|
+
const changed = this.#host.changed;
|
|
1185
|
+
for (const key of changed) {
|
|
1186
|
+
for (const entry of this.#meshDeps.get(key) ?? []) this.#dirtyMeshes.add(entry.handle);
|
|
1187
|
+
for (const entry of this.#sectionDeps.get(key) ?? []) this.#dirtySections.add(entry.handle);
|
|
1188
|
+
}
|
|
1189
|
+
changed.clear();
|
|
1190
|
+
const historyBusy = this.#host.status("history").status === "computing";
|
|
1191
|
+
if (historyBusy !== this.#historyBusy) {
|
|
1192
|
+
this.#historyBusy = historyBusy;
|
|
1193
|
+
this.#allDirty = true;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
/** Counts `state` (`by` 1) in, or (`-1`) out of, the busy and failed states. */
|
|
1197
|
+
#count(state, by) {
|
|
1198
|
+
if (state.status === "computing") this.#busy += by;
|
|
1199
|
+
else if (state.status === "error") {
|
|
1200
|
+
this.#failed += by;
|
|
1201
|
+
this.#failedMoved = true;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
/** Refreshes the snapshot, then calls the listeners when it changed. */
|
|
1205
|
+
#update() {
|
|
1206
|
+
this.#refresh();
|
|
1207
|
+
this.#notify();
|
|
1208
|
+
}
|
|
1209
|
+
/** Queues an {@link #update} on a microtask (once). */
|
|
1210
|
+
#queueUpdate() {
|
|
1211
|
+
if (this.#updateQueued) return;
|
|
1212
|
+
this.#updateQueued = true;
|
|
1213
|
+
queueMicrotask(() => {
|
|
1214
|
+
this.#updateQueued = false;
|
|
1215
|
+
if (!this.#disposed && (this.#stale || this.#host.changed.size > 0 || this.#owed)) this.#update();
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Re-derives the dirty states from the store and the scene, sharing every
|
|
1220
|
+
* other part of the previous snapshot; publishes a new snapshot only when
|
|
1221
|
+
* something changed (its listeners are called by {@link #notify}).
|
|
1222
|
+
*/
|
|
1223
|
+
#refresh() {
|
|
1224
|
+
this.#stale = false;
|
|
1225
|
+
this.#applyDone();
|
|
1226
|
+
this.#takeChanged();
|
|
1227
|
+
const prev = this.#snapshot;
|
|
1228
|
+
const revision = prev.revision + 1;
|
|
1229
|
+
let changed = false;
|
|
1230
|
+
if (this.#allDirty || this.#fullRefresh) {
|
|
1231
|
+
this.#allDirty = false;
|
|
1232
|
+
for (const h of this.#meshes.keys()) this.#dirtyMeshes.add(h);
|
|
1233
|
+
for (const h of prev.meshes.keys()) this.#dirtyMeshes.add(h);
|
|
1234
|
+
for (const h of this.#sections.keys()) this.#dirtySections.add(h);
|
|
1235
|
+
for (const h of prev.sections.keys()) this.#dirtySections.add(h);
|
|
1236
|
+
}
|
|
1237
|
+
let meshes;
|
|
1238
|
+
let added = 0;
|
|
1239
|
+
for (const handle of this.#dirtyMeshes) {
|
|
1240
|
+
const entry = this.#meshes.get(handle);
|
|
1241
|
+
const old = prev.meshes.get(handle);
|
|
1242
|
+
if (entry === void 0) {
|
|
1243
|
+
if (old !== void 0) {
|
|
1244
|
+
meshes ??= new Map(prev.meshes);
|
|
1245
|
+
meshes.delete(handle);
|
|
1246
|
+
this.#count(old, -1);
|
|
1247
|
+
}
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
const s = this.#meshStatus(entry);
|
|
1251
|
+
if (old !== void 0 && old.display === entry.display && old.desired === entry.desired && old.status === s.status && old.error === s.error) continue;
|
|
1252
|
+
const state = {
|
|
1253
|
+
display: entry.display,
|
|
1254
|
+
desired: entry.desired,
|
|
1255
|
+
status: s.status,
|
|
1256
|
+
revision
|
|
1257
|
+
};
|
|
1258
|
+
if (s.error !== void 0) state.error = s.error;
|
|
1259
|
+
meshes ??= new Map(prev.meshes);
|
|
1260
|
+
if (old === void 0) added++;
|
|
1261
|
+
else this.#count(old, -1);
|
|
1262
|
+
meshes.set(handle, Object.freeze(state));
|
|
1263
|
+
this.#count(state, 1);
|
|
1264
|
+
}
|
|
1265
|
+
this.#dirtyMeshes.clear();
|
|
1266
|
+
if (meshes !== void 0) {
|
|
1267
|
+
changed = true;
|
|
1268
|
+
if (added > 1) {
|
|
1269
|
+
const m = meshes;
|
|
1270
|
+
meshes = new Map([...this.#meshes.keys()].map((h) => [h, m.get(h)]));
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
let sections;
|
|
1274
|
+
added = 0;
|
|
1275
|
+
for (const handle of this.#dirtySections) {
|
|
1276
|
+
const entry = this.#sections.get(handle);
|
|
1277
|
+
const old = prev.sections.get(handle);
|
|
1278
|
+
if (entry === void 0) {
|
|
1279
|
+
if (old !== void 0) {
|
|
1280
|
+
sections ??= new Map(prev.sections);
|
|
1281
|
+
sections.delete(handle);
|
|
1282
|
+
this.#count(old, -1);
|
|
1283
|
+
}
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
const s = this.#sectionStatus(entry);
|
|
1287
|
+
const a = entry.applied;
|
|
1288
|
+
if (old !== void 0 && old.plane === a.plane && old.meshes === a.meshes && old.fill === a.fill && old.desired === entry.desired && old.status === s.status && old.error === s.error) continue;
|
|
1289
|
+
const state = {
|
|
1290
|
+
...a,
|
|
1291
|
+
desired: entry.desired,
|
|
1292
|
+
status: s.status,
|
|
1293
|
+
revision
|
|
1294
|
+
};
|
|
1295
|
+
if (s.error !== void 0) state.error = s.error;
|
|
1296
|
+
sections ??= new Map(prev.sections);
|
|
1297
|
+
if (old === void 0) added++;
|
|
1298
|
+
else this.#count(old, -1);
|
|
1299
|
+
sections.set(handle, Object.freeze(state));
|
|
1300
|
+
this.#count(state, 1);
|
|
1301
|
+
}
|
|
1302
|
+
this.#dirtySections.clear();
|
|
1303
|
+
if (sections !== void 0) {
|
|
1304
|
+
changed = true;
|
|
1305
|
+
if (added > 1) {
|
|
1306
|
+
const m = sections;
|
|
1307
|
+
sections = new Map([...this.#sections.keys()].map((h) => [h, m.get(h)]));
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
const canUndo = this.#host.canUndo();
|
|
1311
|
+
const canRedo = this.#host.canRedo();
|
|
1312
|
+
let history = prev.history;
|
|
1313
|
+
if (history.canUndo !== canUndo || history.canRedo !== canRedo) {
|
|
1314
|
+
history = Object.freeze({
|
|
1315
|
+
canUndo,
|
|
1316
|
+
canRedo
|
|
1317
|
+
});
|
|
1318
|
+
changed = true;
|
|
1319
|
+
}
|
|
1320
|
+
if (prev.selection !== this.#selection) changed = true;
|
|
1321
|
+
const meshStates = meshes ?? prev.meshes;
|
|
1322
|
+
const sectionStates = sections ?? prev.sections;
|
|
1323
|
+
const historyKey = this.#key("history");
|
|
1324
|
+
let overall = { status: "idle" };
|
|
1325
|
+
if (this.#failedMoved) {
|
|
1326
|
+
this.#failedMoved = false;
|
|
1327
|
+
this.#firstFailed = this.#failed > 0 ? firstError(meshStates.values()) ?? firstError(sectionStates.values()) : void 0;
|
|
1328
|
+
}
|
|
1329
|
+
if (this.#firstFailed !== void 0) overall = this.#firstFailed;
|
|
1330
|
+
else if (historyKey.status === "error") overall = historyKey;
|
|
1331
|
+
else if (this.#busy > 0 || historyKey.status === "computing") overall = { status: "computing" };
|
|
1332
|
+
const status = overall.status === "ready" ? "idle" : overall.status;
|
|
1333
|
+
if (status !== prev.status || overall.error !== prev.error) changed = true;
|
|
1334
|
+
if (!changed) return;
|
|
1335
|
+
const snap = {
|
|
1336
|
+
revision,
|
|
1337
|
+
meshes: meshes === void 0 ? prev.meshes : Object.freeze(meshes),
|
|
1338
|
+
history,
|
|
1339
|
+
sections: sections === void 0 ? prev.sections : Object.freeze(sections),
|
|
1340
|
+
selection: this.#selection,
|
|
1341
|
+
status
|
|
1342
|
+
};
|
|
1343
|
+
if (overall.error !== void 0) snap.error = overall.error;
|
|
1344
|
+
this.#snapshot = Object.freeze(snap);
|
|
1345
|
+
this.#owed = true;
|
|
1346
|
+
}
|
|
1347
|
+
/** Calls the listeners once for the snapshots published since they were last called. */
|
|
1348
|
+
#notify() {
|
|
1349
|
+
if (!this.#owed) return;
|
|
1350
|
+
this.#owed = false;
|
|
1351
|
+
for (const listener of [...this.#listeners]) try {
|
|
1352
|
+
listener();
|
|
1353
|
+
} catch (error) {
|
|
1354
|
+
reportListenerError(error);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
static {
|
|
1358
|
+
setFrameLoopOf = (store, on) => {
|
|
1359
|
+
store.#frameLoop = on;
|
|
1360
|
+
if (!on && !store.#disposed && (store.#pendingDisplay.size > 0 || store.#pendingSections.size > 0)) store.#schedule();
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
/** A store over `scene` (a `Scene` or a `WorkerScene`). */
|
|
1365
|
+
function createSceneStore(scene) {
|
|
1366
|
+
return new SceneStore(scene);
|
|
1367
|
+
}
|
|
1368
|
+
/**
|
|
1369
|
+
* Internal, not exported from `fast-mesh-wasm/store`: switches the store's
|
|
1370
|
+
* flushing. `commitFrame()` switches it on (declarative changes wait for the
|
|
1371
|
+
* next frame); `false` switches it back to flushing on a microtask, and
|
|
1372
|
+
* flushes the changes waiting for a frame. The r3f frame driver calls it
|
|
1373
|
+
* when the store's last driver unmounts.
|
|
1374
|
+
*/
|
|
1375
|
+
function setFrameLoop(store, on) {
|
|
1376
|
+
setFrameLoopOf(store, on);
|
|
1377
|
+
}
|
|
1378
|
+
//#endregion
|
|
1379
|
+
export { createSceneStore as n, setFrameLoop as r, SceneStore as t };
|