@selvajs/ui 5.0.1 → 6.0.0-beta.0
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/dist/components/compute/ComputeApp.svelte +15 -7
- package/dist/components/compute/ComputeApp.svelte.d.ts +1 -1
- package/dist/components/primitives/textarea/textarea.svelte +1 -2
- package/dist/components/viewer/SceneManager.svelte +75 -160
- package/dist/components/viewer/SceneManager.svelte.d.ts +8 -3
- package/dist/components/viewer/Viewer.svelte +20 -3
- package/dist/compute/useSolveSession.svelte.d.ts +12 -0
- package/dist/compute/useSolveSession.svelte.js +76 -0
- package/dist/external/storage.d.ts +1 -15
- package/dist/external/storage.js +4 -54
- package/dist/index.d.ts +4 -3
- package/dist/index.js +7 -4
- package/dist/public.d.ts +4 -3
- package/dist/public.js +9 -5
- package/package.json +5 -3
- package/src/lib/components/compute/ComputeApp.svelte +15 -7
- package/src/lib/components/primitives/textarea/textarea.svelte +1 -2
- package/src/lib/components/viewer/SceneManager.svelte +75 -160
- package/src/lib/components/viewer/Viewer.svelte +20 -3
- package/src/lib/compute/mesh-policy-wiring.test.ts +72 -0
- package/src/lib/compute/useSolveSession.svelte.ts +83 -0
- package/src/lib/external/storage.ts +12 -64
- package/src/lib/index.ts +15 -5
- package/src/lib/public.ts +17 -6
- package/dist/compute/computeThrottle.svelte.d.ts +0 -24
- package/dist/compute/computeThrottle.svelte.js +0 -82
- package/dist/compute/createSolveSession.svelte.d.ts +0 -66
- package/dist/compute/createSolveSession.svelte.js +0 -159
- package/dist/compute/solve-session-core.d.ts +0 -54
- package/dist/compute/solve-session-core.js +0 -87
- package/dist/compute/solveMemo.d.ts +0 -22
- package/dist/compute/solveMemo.js +0 -124
- package/dist/types/solveFn.d.ts +0 -25
- package/dist/types/solveFn.js +0 -1
- package/src/lib/compute/computeThrottle.svelte.ts +0 -109
- package/src/lib/compute/computeThrottle.test.ts +0 -106
- package/src/lib/compute/createSolveSession.svelte.ts +0 -239
- package/src/lib/compute/createSolveSession.test.ts +0 -168
- package/src/lib/compute/solve-session-core.test.ts +0 -153
- package/src/lib/compute/solve-session-core.ts +0 -122
- package/src/lib/compute/solveMemo.test.ts +0 -220
- package/src/lib/compute/solveMemo.ts +0 -140
- package/src/lib/external/storage.test.ts +0 -99
- package/src/lib/types/solveFn.ts +0 -29
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import * as THREE from 'three';
|
|
3
|
-
import { createSolveMemo, stableInputKey } from './solveMemo';
|
|
4
|
-
import type { SolveResult } from '../types/solveFn';
|
|
5
|
-
|
|
6
|
-
// Pins the client-side result memo (M2): stable keying across key order, LRU recency and
|
|
7
|
-
// eviction, hit/miss semantics, and clear(). The driver wiring is pinned separately in
|
|
8
|
-
// createSolveSession.test.ts.
|
|
9
|
-
|
|
10
|
-
const result = (tag: string): SolveResult => ({ outputs: { out: tag } });
|
|
11
|
-
|
|
12
|
-
/** A mesh-bearing result — the shape that exposed audit C1. */
|
|
13
|
-
function meshResult(tag: string): SolveResult {
|
|
14
|
-
const mesh = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), new THREE.MeshBasicMaterial());
|
|
15
|
-
mesh.name = tag;
|
|
16
|
-
return { outputs: { out: tag }, meshes: [mesh] };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Mirrors `clearScene`'s disposal of whatever the viewer currently holds. */
|
|
20
|
-
function disposeLikeViewer(res: SolveResult | undefined): void {
|
|
21
|
-
res?.meshes?.forEach((m: THREE.Object3D) =>
|
|
22
|
-
m.traverse((child) => {
|
|
23
|
-
const r = child as Partial<THREE.Mesh> & THREE.Object3D;
|
|
24
|
-
r.geometry?.dispose();
|
|
25
|
-
const mat = r.material;
|
|
26
|
-
if (!mat) return;
|
|
27
|
-
(Array.isArray(mat) ? mat : [mat]).forEach((m) => m.dispose());
|
|
28
|
-
})
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Count `dispose()` calls across ALL geometries for the duration of a test.
|
|
34
|
-
*
|
|
35
|
-
* The memo stores a private clone, so watching the object handed to `set` would prove
|
|
36
|
-
* nothing — the retained copy is deliberately unreachable. Spying the prototype observes
|
|
37
|
-
* disposal of whichever instance the memo actually owns, which is the real invariant:
|
|
38
|
-
* an entry leaving the map must release its buffers.
|
|
39
|
-
*/
|
|
40
|
-
function countDisposals(): { count: () => number; restore: () => void } {
|
|
41
|
-
const original = THREE.BufferGeometry.prototype.dispose;
|
|
42
|
-
let n = 0;
|
|
43
|
-
THREE.BufferGeometry.prototype.dispose = function (this: THREE.BufferGeometry) {
|
|
44
|
-
n++;
|
|
45
|
-
return original.call(this);
|
|
46
|
-
};
|
|
47
|
-
return {
|
|
48
|
-
count: () => n,
|
|
49
|
-
restore: () => {
|
|
50
|
-
THREE.BufferGeometry.prototype.dispose = original;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
describe('stableInputKey', () => {
|
|
56
|
-
it('is insensitive to object key order', () => {
|
|
57
|
-
expect(stableInputKey({ a: 1, b: 2 })).toBe(stableInputKey({ b: 2, a: 1 }));
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('sorts keys at every level (nested objects)', () => {
|
|
61
|
-
expect(stableInputKey({ o: { x: 1, y: 2 } })).toBe(stableInputKey({ o: { y: 2, x: 1 } }));
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('distinguishes different values', () => {
|
|
65
|
-
expect(stableInputKey({ a: 1 })).not.toBe(stableInputKey({ a: 2 }));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('preserves array order (arrays are ordered)', () => {
|
|
69
|
-
expect(stableInputKey({ a: [1, 2] })).not.toBe(stableInputKey({ a: [2, 1] }));
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('handles null and primitive values', () => {
|
|
73
|
-
expect(stableInputKey({ a: null, b: 'x', c: true })).toBe(
|
|
74
|
-
stableInputKey({ c: true, b: 'x', a: null })
|
|
75
|
-
);
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe('createSolveMemo', () => {
|
|
80
|
-
it('returns undefined on a miss', () => {
|
|
81
|
-
const memo = createSolveMemo();
|
|
82
|
-
expect(memo.get({ a: 1 })).toBeUndefined();
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('round-trips a stored result by equal inputs regardless of key order', () => {
|
|
86
|
-
const memo = createSolveMemo();
|
|
87
|
-
memo.set({ a: 1, b: 2 }, result('r'));
|
|
88
|
-
expect(memo.get({ b: 2, a: 1 })).toEqual(result('r'));
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('caches errored results (a complete, deterministic solve outcome)', () => {
|
|
92
|
-
const memo = createSolveMemo();
|
|
93
|
-
const errored: SolveResult = { outputs: {}, errors: ['boom'] };
|
|
94
|
-
memo.set({ a: 1 }, errored);
|
|
95
|
-
expect(memo.get({ a: 1 })).toEqual(errored);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('evicts the least-recently-used entry past capacity', () => {
|
|
99
|
-
const memo = createSolveMemo(2);
|
|
100
|
-
memo.set({ k: 1 }, result('1'));
|
|
101
|
-
memo.set({ k: 2 }, result('2'));
|
|
102
|
-
memo.set({ k: 3 }, result('3')); // evicts k:1
|
|
103
|
-
expect(memo.get({ k: 1 })).toBeUndefined();
|
|
104
|
-
expect(memo.get({ k: 2 })).toEqual(result('2'));
|
|
105
|
-
expect(memo.get({ k: 3 })).toEqual(result('3'));
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('a get refreshes recency, protecting the entry from eviction', () => {
|
|
109
|
-
const memo = createSolveMemo(2);
|
|
110
|
-
memo.set({ k: 1 }, result('1'));
|
|
111
|
-
memo.set({ k: 2 }, result('2'));
|
|
112
|
-
memo.get({ k: 1 }); // k:1 now most-recent
|
|
113
|
-
memo.set({ k: 3 }, result('3')); // evicts k:2, not k:1
|
|
114
|
-
expect(memo.get({ k: 1 })).toEqual(result('1'));
|
|
115
|
-
expect(memo.get({ k: 2 })).toBeUndefined();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('re-setting an existing key updates the value without growing size', () => {
|
|
119
|
-
const memo = createSolveMemo(1);
|
|
120
|
-
memo.set({ k: 1 }, result('old'));
|
|
121
|
-
memo.set({ k: 1 }, result('new'));
|
|
122
|
-
expect(memo.get({ k: 1 })).toEqual(result('new'));
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('clear() drops every entry', () => {
|
|
126
|
-
const memo = createSolveMemo();
|
|
127
|
-
memo.set({ a: 1 }, result('r'));
|
|
128
|
-
memo.clear();
|
|
129
|
-
expect(memo.get({ a: 1 })).toBeUndefined();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// Audit C1. The memo caches whole SolveResults, including live three.js objects, but the
|
|
134
|
-
// viewer's `clearScene` disposes the meshes it is handed on the next scene update. Every
|
|
135
|
-
// pre-existing test above used mesh-free results, so nothing caught it.
|
|
136
|
-
describe('createSolveMemo — GPU object ownership (audit C1)', () => {
|
|
137
|
-
it('serves a usable mesh after the viewer disposed the one it was given', () => {
|
|
138
|
-
const memo = createSolveMemo();
|
|
139
|
-
const stored = meshResult('a');
|
|
140
|
-
memo.set({ k: 1 }, stored);
|
|
141
|
-
|
|
142
|
-
// Solve 1 renders: the viewer owns and (on the next update) disposes these meshes.
|
|
143
|
-
const first = memo.get({ k: 1 })!;
|
|
144
|
-
disposeLikeViewer(first);
|
|
145
|
-
|
|
146
|
-
// Slider returns to the same value → memo hit. The served mesh must be renderable,
|
|
147
|
-
// not the corpse the viewer just disposed.
|
|
148
|
-
const second = memo.get({ k: 1 })!;
|
|
149
|
-
const geo = (second.meshes![0] as THREE.Mesh).geometry;
|
|
150
|
-
expect(geo.attributes.position).toBeDefined();
|
|
151
|
-
expect(second.meshes![0]).not.toBe(first.meshes![0]);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('never hands the same mesh instance to two consumers', () => {
|
|
155
|
-
// The scene takes ownership of what it is given (updateScene → scene.add), so two
|
|
156
|
-
// hits handing out one instance means a double-add and a shared disposal fate.
|
|
157
|
-
const memo = createSolveMemo();
|
|
158
|
-
memo.set({ k: 1 }, meshResult('a'));
|
|
159
|
-
expect(memo.get({ k: 1 })!.meshes![0]).not.toBe(memo.get({ k: 1 })!.meshes![0]);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('preserves non-mesh result fields on a hit', () => {
|
|
163
|
-
const memo = createSolveMemo();
|
|
164
|
-
const stored: SolveResult = { ...meshResult('a'), errors: ['e'], warnings: ['w'] };
|
|
165
|
-
memo.set({ k: 1 }, stored);
|
|
166
|
-
const hit = memo.get({ k: 1 })!;
|
|
167
|
-
expect(hit.outputs).toEqual({ out: 'a' });
|
|
168
|
-
expect(hit.errors).toEqual(['e']);
|
|
169
|
-
expect(hit.warnings).toEqual(['w']);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('releases GPU memory when an entry is evicted', () => {
|
|
173
|
-
const memo = createSolveMemo(1);
|
|
174
|
-
memo.set({ k: 1 }, meshResult('a'));
|
|
175
|
-
|
|
176
|
-
const spy = countDisposals();
|
|
177
|
-
try {
|
|
178
|
-
memo.set({ k: 2 }, meshResult('b')); // evicts k:1
|
|
179
|
-
expect(spy.count()).toBe(1);
|
|
180
|
-
} finally {
|
|
181
|
-
spy.restore();
|
|
182
|
-
}
|
|
183
|
-
expect(memo.get({ k: 1 })).toBeUndefined();
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it('releases GPU memory on clear() (definition switch)', () => {
|
|
187
|
-
const memo = createSolveMemo();
|
|
188
|
-
memo.set({ k: 1 }, meshResult('a'));
|
|
189
|
-
memo.set({ k: 2 }, meshResult('b'));
|
|
190
|
-
|
|
191
|
-
const spy = countDisposals();
|
|
192
|
-
try {
|
|
193
|
-
memo.clear();
|
|
194
|
-
expect(spy.count()).toBe(2);
|
|
195
|
-
} finally {
|
|
196
|
-
spy.restore();
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it('releases the old value when a key is overwritten', () => {
|
|
201
|
-
const memo = createSolveMemo();
|
|
202
|
-
memo.set({ k: 1 }, meshResult('old'));
|
|
203
|
-
|
|
204
|
-
const spy = countDisposals();
|
|
205
|
-
try {
|
|
206
|
-
memo.set({ k: 1 }, meshResult('new'));
|
|
207
|
-
expect(spy.count()).toBe(1);
|
|
208
|
-
} finally {
|
|
209
|
-
spy.restore();
|
|
210
|
-
}
|
|
211
|
-
expect(memo.get({ k: 1 })!.outputs).toEqual({ out: 'new' });
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('handles mesh-free results without touching disposal paths', () => {
|
|
215
|
-
const memo = createSolveMemo(1);
|
|
216
|
-
memo.set({ k: 1 }, result('1'));
|
|
217
|
-
memo.set({ k: 2 }, result('2')); // evicts k:1 — must not throw
|
|
218
|
-
expect(memo.get({ k: 2 })).toEqual(result('2'));
|
|
219
|
-
});
|
|
220
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
// Client-side solve result memo (M2). A small LRU keyed on a stable serialization of
|
|
2
|
-
// the solve INPUTS, sitting in front of the request/response driver. Dragging a slider
|
|
3
|
-
// back to a value already solved this session returns instantly without a network
|
|
4
|
-
// round-trip — killing slider-scrub storms before they leave the browser. It pairs with
|
|
5
|
-
// the throttle's latest-wins abort: the memo only serves values that fully solved, so a
|
|
6
|
-
// hit is always a complete result.
|
|
7
|
-
//
|
|
8
|
-
// GPU ownership (audit C1): a SolveResult carries live three.js objects, and the viewer
|
|
9
|
-
// takes ownership of every mesh array it renders — `updateScene` disposes the previous
|
|
10
|
-
// content on the next update. So the memo can neither hand out its own instances (they'd
|
|
11
|
-
// be disposed under it, then re-added dead on the next hit) nor drop entries silently
|
|
12
|
-
// (their GPU buffers would leak). It therefore keeps private copies, serves a fresh clone
|
|
13
|
-
// per hit, and disposes an entry whenever it leaves the map.
|
|
14
|
-
|
|
15
|
-
import * as THREE from 'three';
|
|
16
|
-
import type { SolveResult } from '../types/solveFn';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Deterministic string key for a set of input values. Object keys are sorted at every
|
|
20
|
-
* level so two logically-equal inputs (built in different key order) collide, matching
|
|
21
|
-
* the server's stable-input keying intent. Values are plain JSON (numbers, strings,
|
|
22
|
-
* booleans, arrays) — the projected solve inputs never contain functions or cycles.
|
|
23
|
-
*/
|
|
24
|
-
export function stableInputKey(values: Record<string, unknown>): string {
|
|
25
|
-
return serialize(values);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function serialize(value: unknown): string {
|
|
29
|
-
if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'null';
|
|
30
|
-
if (Array.isArray(value)) return `[${value.map(serialize).join(',')}]`;
|
|
31
|
-
const obj = value as Record<string, unknown>;
|
|
32
|
-
const keys = Object.keys(obj).sort();
|
|
33
|
-
return `{${keys.map((k) => `${JSON.stringify(k)}:${serialize(obj[k])}`).join(',')}}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Deep-clone a solve's scene objects so the caller owns them outright.
|
|
38
|
-
*
|
|
39
|
-
* `Object3D.clone()` copies the transform hierarchy but SHARES `geometry` and `material`
|
|
40
|
-
* by reference — which is exactly the aliasing that makes a naive clone useless here, so
|
|
41
|
-
* geometry is copied explicitly. Materials are deliberately left shared: the viewer's
|
|
42
|
-
* `clearScene` skips disposing anything in its SHARED_MATERIALS set (module-scope
|
|
43
|
-
* singletons reused across solves), and per-mesh materials are cheap to recreate but
|
|
44
|
-
* expensive to re-compile as new shader programs.
|
|
45
|
-
*/
|
|
46
|
-
function cloneSceneObjects(meshes: THREE.Object3D[]): THREE.Object3D[] {
|
|
47
|
-
return meshes.map((root) => {
|
|
48
|
-
const copy = root.clone(true);
|
|
49
|
-
const sources: THREE.Object3D[] = [];
|
|
50
|
-
root.traverse((child) => sources.push(child));
|
|
51
|
-
let i = 0;
|
|
52
|
-
copy.traverse((child) => {
|
|
53
|
-
const source = sources[i++] as Partial<THREE.Mesh> & THREE.Object3D;
|
|
54
|
-
const target = child as Partial<THREE.Mesh> & THREE.Object3D;
|
|
55
|
-
if (source.geometry) target.geometry = source.geometry.clone();
|
|
56
|
-
});
|
|
57
|
-
return copy;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Release an entry's GPU buffers. Mirrors `clearScene`'s traversal, minus materials —
|
|
63
|
-
* the memo never owns those (see {@link cloneSceneObjects}), so disposing one here would
|
|
64
|
-
* free a singleton still referenced by live scene content.
|
|
65
|
-
*/
|
|
66
|
-
function disposeSceneObjects(result: SolveResult): void {
|
|
67
|
-
result.meshes?.forEach((root: THREE.Object3D) =>
|
|
68
|
-
root.traverse((child) => {
|
|
69
|
-
(child as Partial<THREE.Mesh>).geometry?.dispose();
|
|
70
|
-
})
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export interface SolveMemo {
|
|
75
|
-
/** Returns a previously stored result for these inputs, or undefined on a miss. */
|
|
76
|
-
get(values: Record<string, unknown>): SolveResult | undefined;
|
|
77
|
-
/** Records a completed solve result under its input key (evicting the LRU tail). */
|
|
78
|
-
set(values: Record<string, unknown>, result: SolveResult): void;
|
|
79
|
-
/** Drops every entry — called when the active definition changes. */
|
|
80
|
-
clear(): void;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* A bounded LRU memo. `max` caps entries (not bytes); solve results can be large, so the
|
|
85
|
-
* default is deliberately small — this targets the tight slider-scrub loop, not a durable
|
|
86
|
-
* cache. Re-reading an entry refreshes its recency (Map insertion-order LRU).
|
|
87
|
-
*/
|
|
88
|
-
export function createSolveMemo(max = 16): SolveMemo {
|
|
89
|
-
const entries = new Map<string, SolveResult>();
|
|
90
|
-
|
|
91
|
-
/** Drop an entry and release its GPU buffers. No-op when the key is absent. */
|
|
92
|
-
function evict(key: string): void {
|
|
93
|
-
const entry = entries.get(key);
|
|
94
|
-
if (entry === undefined) return;
|
|
95
|
-
entries.delete(key);
|
|
96
|
-
disposeSceneObjects(entry);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
get(values) {
|
|
101
|
-
const key = stableInputKey(values);
|
|
102
|
-
const hit = entries.get(key);
|
|
103
|
-
if (hit === undefined) return undefined;
|
|
104
|
-
// Refresh recency: re-insert at the tail.
|
|
105
|
-
entries.delete(key);
|
|
106
|
-
entries.set(key, hit);
|
|
107
|
-
// A memo hit skips the transport entirely, so no other log line fires —
|
|
108
|
-
// this line is the only trace it wasn't a fresh solve.
|
|
109
|
-
console.info(`[Compute/memo] HIT — served from client memo (${entries.size}/${max})`);
|
|
110
|
-
// Clone on the way out: the viewer disposes what it renders, so the retained
|
|
111
|
-
// entry must never be the instance handed to it (audit C1).
|
|
112
|
-
if (!hit.meshes?.length) return hit;
|
|
113
|
-
return { ...hit, meshes: cloneSceneObjects(hit.meshes) };
|
|
114
|
-
},
|
|
115
|
-
set(values, result) {
|
|
116
|
-
const key = stableInputKey(values);
|
|
117
|
-
// Overwriting a key strands the old value's buffers unless it's disposed first.
|
|
118
|
-
evict(key);
|
|
119
|
-
// Store a private copy for the same reason `get` clones: the caller reports this
|
|
120
|
-
// same object to the viewer, which will dispose it on the next scene update.
|
|
121
|
-
entries.set(
|
|
122
|
-
key,
|
|
123
|
-
result.meshes?.length ? { ...result, meshes: cloneSceneObjects(result.meshes) } : result
|
|
124
|
-
);
|
|
125
|
-
while (entries.size > max) {
|
|
126
|
-
const oldest = entries.keys().next().value;
|
|
127
|
-
if (oldest === undefined) break;
|
|
128
|
-
evict(oldest);
|
|
129
|
-
console.info(`[Compute/memo] evicted LRU entry (cap ${max})`);
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
clear() {
|
|
133
|
-
if (entries.size > 0) {
|
|
134
|
-
console.info(`[Compute/memo] cleared ${entries.size} entries (definition changed)`);
|
|
135
|
-
}
|
|
136
|
-
entries.forEach(disposeSceneObjects);
|
|
137
|
-
entries.clear();
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
getExternalInputs,
|
|
4
|
-
writeExternalValue,
|
|
5
|
-
readExternalValue,
|
|
6
|
-
clearExternalValue
|
|
7
|
-
} from './storage';
|
|
8
|
-
import type { UISchema } from '@selvajs/schemas';
|
|
9
|
-
|
|
10
|
-
// getExternalInputs is the gate the whole client-input feature hangs on: it decides
|
|
11
|
-
// which inputs a producer route must fill. These pin the source.kind === 'client'
|
|
12
|
-
// filter and the displayName fallback. The read/write helpers are also covered with a
|
|
13
|
-
// sessionStorage stub, including the no-storage guard (SSR / node).
|
|
14
|
-
|
|
15
|
-
const input = (paramId: string, opts: { displayName?: string; source?: { kind: string } } = {}) =>
|
|
16
|
-
({ type: 'input', paramId, ...opts }) as never;
|
|
17
|
-
|
|
18
|
-
function schema(items: unknown[]): UISchema {
|
|
19
|
-
return {
|
|
20
|
-
layout: { type: 'flat', groups: [{ id: 'g', label: 'g', items }] }
|
|
21
|
-
} as unknown as UISchema;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
describe('getExternalInputs', () => {
|
|
25
|
-
it('keeps only inputs with source.kind === client', () => {
|
|
26
|
-
const s = schema([
|
|
27
|
-
input('a', { source: { kind: 'client' } }),
|
|
28
|
-
input('b', { source: { kind: 'user' } }),
|
|
29
|
-
input('c') // no source
|
|
30
|
-
]);
|
|
31
|
-
expect(getExternalInputs(s).map((e) => e.paramId)).toEqual(['a']);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('falls back to paramId when displayName is absent', () => {
|
|
35
|
-
const s = schema([input('a', { source: { kind: 'client' } })]);
|
|
36
|
-
expect(getExternalInputs(s)[0].displayName).toBe('a');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('uses displayName when present', () => {
|
|
40
|
-
const s = schema([input('a', { displayName: 'Width', source: { kind: 'client' } })]);
|
|
41
|
-
expect(getExternalInputs(s)[0].displayName).toBe('Width');
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe('read/write/clear with a sessionStorage stub', () => {
|
|
46
|
-
afterEach(() => {
|
|
47
|
-
vi.unstubAllGlobals();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
function stubStorage() {
|
|
51
|
-
const store = new Map<string, string>();
|
|
52
|
-
vi.stubGlobal('sessionStorage', {
|
|
53
|
-
getItem: (k: string) => store.get(k) ?? null,
|
|
54
|
-
setItem: (k: string, v: string) => store.set(k, v),
|
|
55
|
-
removeItem: (k: string) => store.delete(k)
|
|
56
|
-
});
|
|
57
|
-
return store;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
it('round-trips a value scoped by (scopeKey, inputId)', () => {
|
|
61
|
-
const store = stubStorage();
|
|
62
|
-
writeExternalValue({ scopeKey: 's1', inputId: 'p1', value: { x: 1 } });
|
|
63
|
-
expect(readExternalValue({ scopeKey: 's1', inputId: 'p1' })).toEqual({ x: 1 });
|
|
64
|
-
expect(store.has('external:s1:p1')).toBe(true);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('scopes values so they do not bleed across scopeKey', () => {
|
|
68
|
-
stubStorage();
|
|
69
|
-
writeExternalValue({ scopeKey: 's1', inputId: 'p1', value: 'a' });
|
|
70
|
-
expect(readExternalValue({ scopeKey: 's2', inputId: 'p1' })).toBeUndefined();
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('clear removes the value', () => {
|
|
74
|
-
stubStorage();
|
|
75
|
-
writeExternalValue({ scopeKey: 's1', inputId: 'p1', value: 'a' });
|
|
76
|
-
clearExternalValue({ scopeKey: 's1', inputId: 'p1' });
|
|
77
|
-
expect(readExternalValue({ scopeKey: 's1', inputId: 'p1' })).toBeUndefined();
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('returns undefined for malformed JSON', () => {
|
|
81
|
-
const store = stubStorage();
|
|
82
|
-
store.set('external:s1:p1', '{not json');
|
|
83
|
-
expect(readExternalValue({ scopeKey: 's1', inputId: 'p1' })).toBeUndefined();
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('no-storage guard (SSR / node)', () => {
|
|
88
|
-
it('read returns undefined and write/clear no-op when sessionStorage is absent', () => {
|
|
89
|
-
// node env: sessionStorage is undefined by default
|
|
90
|
-
expect(readExternalValue({ scopeKey: 's1', inputId: 'p1' })).toBeUndefined();
|
|
91
|
-
expect(() => writeExternalValue({ scopeKey: 's1', inputId: 'p1', value: 1 })).not.toThrow();
|
|
92
|
-
expect(() => clearExternalValue({ scopeKey: 's1', inputId: 'p1' })).not.toThrow();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('ignores empty scopeKey or inputId', () => {
|
|
96
|
-
expect(readExternalValue({ scopeKey: '', inputId: 'p1' })).toBeUndefined();
|
|
97
|
-
expect(readExternalValue({ scopeKey: 's1', inputId: '' })).toBeUndefined();
|
|
98
|
-
});
|
|
99
|
-
});
|
package/src/lib/types/solveFn.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Result returned from a solve operation.
|
|
3
|
-
*
|
|
4
|
-
* @property outputs - Key-value pairs of computed results
|
|
5
|
-
* @property meshes - Optional array of 3D mesh data generated during computation
|
|
6
|
-
* @property errors - Optional array of error messages that occurred
|
|
7
|
-
* @property warnings - Optional array of warning messages from the computation
|
|
8
|
-
*/
|
|
9
|
-
export interface SolveResult {
|
|
10
|
-
outputs: Record<string, unknown>;
|
|
11
|
-
meshes?: any[];
|
|
12
|
-
errors?: string[];
|
|
13
|
-
warnings?: string[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Function type for running a computation with given input values.
|
|
18
|
-
*
|
|
19
|
-
* Implementations should listen to the abort signal and clean up resources
|
|
20
|
-
* when the signal is triggered (e.g., when the user cancels the operation).
|
|
21
|
-
*
|
|
22
|
-
* @param values - Input parameters for the computation
|
|
23
|
-
* @param signal - AbortSignal to cancel ongoing operations
|
|
24
|
-
* @returns Promise resolving to the computation result
|
|
25
|
-
*/
|
|
26
|
-
export type SolveFn = (
|
|
27
|
-
values: Record<string, unknown>,
|
|
28
|
-
signal: AbortSignal
|
|
29
|
-
) => Promise<SolveResult>;
|