@liquid-glass-forge/dom 0.0.0-stage → 0.1.0-rc.2
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/LICENSE +21 -0
- package/NOTICE +7 -0
- package/README.md +65 -2
- package/dist/cpu.d.ts +90 -0
- package/dist/cpu.js +240 -0
- package/dist/host.d.ts +41 -0
- package/dist/host.js +244 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/mapping.d.ts +60 -0
- package/dist/mapping.js +92 -0
- package/dist/providers.d.ts +58 -0
- package/dist/providers.js +402 -0
- package/dist/scene.d.ts +70 -0
- package/dist/scene.js +494 -0
- package/dist/styles.css +23 -0
- package/package.json +39 -4
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { createSource, MAX_PIXELS } from '@liquid-glass-forge/core';
|
|
2
|
+
export class SourceError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message, options) {
|
|
5
|
+
super(message, options);
|
|
6
|
+
this.name = 'SourceError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function error(code, message, cause) {
|
|
11
|
+
return new SourceError(code, message, cause === undefined ? undefined : { cause });
|
|
12
|
+
}
|
|
13
|
+
function dimensions(width, height) {
|
|
14
|
+
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 1 || height < 1 || width * height > MAX_PIXELS) {
|
|
15
|
+
throw error('INVALID_SOURCE', `Source must have 1–${MAX_PIXELS} pixels with positive integer dimensions`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function specCopy(input) {
|
|
19
|
+
if (!input || typeof input !== 'object')
|
|
20
|
+
throw error('INVALID_SOURCE', 'Expected an explicit source');
|
|
21
|
+
switch (input.kind) {
|
|
22
|
+
case 'pixels': return Object.freeze({ kind: input.kind, width: input.width, height: input.height, data: input.data });
|
|
23
|
+
case 'image-data':
|
|
24
|
+
case 'bitmap':
|
|
25
|
+
case 'canvas':
|
|
26
|
+
case 'video':
|
|
27
|
+
if (!input.value)
|
|
28
|
+
throw error('INVALID_SOURCE', 'Source value is missing');
|
|
29
|
+
return Object.freeze({ kind: input.kind, value: input.value });
|
|
30
|
+
case 'image':
|
|
31
|
+
if (typeof input.url !== 'string' || !input.url.trim() ||
|
|
32
|
+
(input.crossOrigin !== undefined && !['anonymous', 'use-credentials', 'none'].includes(input.crossOrigin))) {
|
|
33
|
+
throw error('INVALID_SOURCE', 'Invalid image URL or crossOrigin mode');
|
|
34
|
+
}
|
|
35
|
+
return Object.freeze({ kind: 'image', url: input.url, crossOrigin: input.crossOrigin ?? 'anonymous' });
|
|
36
|
+
default: throw error('UNSUPPORTED_SOURCE', 'Unsupported source kind');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function throwIfAborted(signal) {
|
|
40
|
+
if (signal.aborted)
|
|
41
|
+
throw signal.reason;
|
|
42
|
+
}
|
|
43
|
+
function abortable(work, signal) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const abort = () => { signal.removeEventListener('abort', abort); reject(signal.reason); };
|
|
46
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
47
|
+
work.then(value => { signal.removeEventListener('abort', abort); resolve(value); }, cause => { signal.removeEventListener('abort', abort); reject(cause); });
|
|
48
|
+
if (signal.aborted)
|
|
49
|
+
abort();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function bytes(width, height, data, revision, target) {
|
|
53
|
+
try {
|
|
54
|
+
const input = createSource({ contractVersion: 1, width, height, revision, data,
|
|
55
|
+
ownership: 'borrowed', colorSpace: 'srgb', alphaMode: 'straight' });
|
|
56
|
+
if (target !== undefined) {
|
|
57
|
+
const output = createSource({ ...input, data: target, ownership: 'borrowed' });
|
|
58
|
+
if (output.data.buffer === input.data.buffer)
|
|
59
|
+
throw new TypeError('Acquisition target must not alias the borrowed source');
|
|
60
|
+
new Uint8Array(output.data.buffer, output.data.byteOffset, output.data.byteLength)
|
|
61
|
+
.set(new Uint8Array(input.data.buffer, input.data.byteOffset, input.data.byteLength));
|
|
62
|
+
return output;
|
|
63
|
+
}
|
|
64
|
+
// Copy synchronously before yielding; never detach the application's view.
|
|
65
|
+
return createSource({ ...input,
|
|
66
|
+
data: new Uint8ClampedArray(input.data.buffer, input.data.byteOffset, input.data.byteLength).slice(), ownership: 'transfer' });
|
|
67
|
+
}
|
|
68
|
+
catch (cause) {
|
|
69
|
+
throw error('INVALID_SOURCE', 'Invalid RGBA8 source storage', cause);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function raster(value, width, height, revision) {
|
|
73
|
+
dimensions(width, height);
|
|
74
|
+
if (typeof document === 'undefined')
|
|
75
|
+
throw error('UNSUPPORTED_SOURCE', 'Pixel acquisition requires a browser document');
|
|
76
|
+
const canvas = document.createElement('canvas');
|
|
77
|
+
canvas.width = width;
|
|
78
|
+
canvas.height = height;
|
|
79
|
+
try {
|
|
80
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true, colorSpace: 'srgb' });
|
|
81
|
+
if (!ctx)
|
|
82
|
+
throw error('UNSUPPORTED_SOURCE', 'Canvas2D is unavailable');
|
|
83
|
+
ctx.drawImage(value, 0, 0, width, height);
|
|
84
|
+
const image = ctx.getImageData(0, 0, width, height, { colorSpace: 'srgb' });
|
|
85
|
+
return createSource({ contractVersion: 1, width, height, revision, data: image.data,
|
|
86
|
+
ownership: 'transfer', colorSpace: 'srgb', alphaMode: 'straight' });
|
|
87
|
+
}
|
|
88
|
+
catch (cause) {
|
|
89
|
+
if (cause instanceof SourceError)
|
|
90
|
+
throw cause;
|
|
91
|
+
if (cause instanceof Error && cause.name === 'SecurityError')
|
|
92
|
+
throw error('UNREADABLE_SOURCE', 'Source is not origin-clean', cause);
|
|
93
|
+
throw error('UNAVAILABLE_SOURCE', 'Source pixels cannot be acquired', cause);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
canvas.width = 0;
|
|
97
|
+
canvas.height = 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function decodedImage(spec, revision, signal) {
|
|
101
|
+
if (typeof Image === 'undefined')
|
|
102
|
+
throw error('UNSUPPORTED_SOURCE', 'Image decoding requires a browser');
|
|
103
|
+
const image = new Image();
|
|
104
|
+
if (spec.crossOrigin !== 'none')
|
|
105
|
+
image.crossOrigin = spec.crossOrigin ?? 'anonymous';
|
|
106
|
+
const abort = () => { image.src = ''; };
|
|
107
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
108
|
+
try {
|
|
109
|
+
throwIfAborted(signal);
|
|
110
|
+
// Attach both handlers before assigning src, including the non-decode fallback.
|
|
111
|
+
const loaded = new Promise((resolve, reject) => {
|
|
112
|
+
image.onload = () => resolve();
|
|
113
|
+
image.onerror = () => reject(error('DECODE_FAILED', 'Image load or CORS request failed'));
|
|
114
|
+
});
|
|
115
|
+
image.src = spec.url;
|
|
116
|
+
if (typeof image.decode === 'function') {
|
|
117
|
+
// Observe load rejection even when native decode is the awaited capability.
|
|
118
|
+
void loaded.catch(() => { });
|
|
119
|
+
await abortable(image.decode(), signal);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
await abortable(loaded, signal);
|
|
123
|
+
}
|
|
124
|
+
throwIfAborted(signal);
|
|
125
|
+
return raster(image, image.naturalWidth, image.naturalHeight, revision);
|
|
126
|
+
}
|
|
127
|
+
catch (cause) {
|
|
128
|
+
throwIfAborted(signal);
|
|
129
|
+
if (cause instanceof SourceError)
|
|
130
|
+
throw cause;
|
|
131
|
+
throw error('DECODE_FAILED', 'Image decoding failed', cause);
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
signal.removeEventListener('abort', abort);
|
|
135
|
+
image.onload = null;
|
|
136
|
+
image.onerror = null;
|
|
137
|
+
image.src = '';
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function acquire(spec, revision, signal, target) {
|
|
141
|
+
throwIfAborted(signal);
|
|
142
|
+
switch (spec.kind) {
|
|
143
|
+
case 'pixels': return bytes(spec.width, spec.height, spec.data, revision, target);
|
|
144
|
+
case 'image-data':
|
|
145
|
+
// Older ImageData implementations expose only the original sRGB format.
|
|
146
|
+
if (spec.value.colorSpace !== undefined && spec.value.colorSpace !== 'srgb')
|
|
147
|
+
throw error('UNSUPPORTED_SOURCE', 'Only sRGB ImageData is supported');
|
|
148
|
+
return bytes(spec.value.width, spec.value.height, spec.value.data, revision, target);
|
|
149
|
+
case 'bitmap': return raster(spec.value, spec.value.width, spec.value.height, revision);
|
|
150
|
+
case 'canvas': return raster(spec.value, spec.value.width, spec.value.height, revision);
|
|
151
|
+
case 'video':
|
|
152
|
+
if (spec.value.mediaKeys)
|
|
153
|
+
throw error('UNREADABLE_SOURCE', 'Encrypted media is not a supported pixel source');
|
|
154
|
+
if (spec.value.getAttribute?.('src') && spec.value.currentSrc && spec.value.src !== spec.value.currentSrc) {
|
|
155
|
+
throw error('UNAVAILABLE_SOURCE', 'Replacement video source is not ready');
|
|
156
|
+
}
|
|
157
|
+
if (spec.value.seeking || spec.value.readyState < 2 || spec.value.error || !spec.value.videoWidth || !spec.value.videoHeight) {
|
|
158
|
+
throw error('UNAVAILABLE_SOURCE', 'Video has no readable current frame');
|
|
159
|
+
}
|
|
160
|
+
return raster(spec.value, spec.value.videoWidth, spec.value.videoHeight, revision);
|
|
161
|
+
case 'image': return decodedImage(spec, revision, signal);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function mediaKey(spec) {
|
|
165
|
+
if (spec.kind === 'canvas')
|
|
166
|
+
return { width: spec.value.width, height: spec.value.height, identity: '', time: 0, object: spec.value };
|
|
167
|
+
if (spec.kind === 'video')
|
|
168
|
+
return { width: spec.value.videoWidth, height: spec.value.videoHeight,
|
|
169
|
+
identity: `${spec.value.currentSrc || ''}\n${spec.value.src || ''}`, time: spec.value.currentTime, object: spec.value.srcObject };
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
/** Explicit borrowed inputs; read() returns a fresh owned snapshot. Latest read wins. */
|
|
173
|
+
export class SourceProvider {
|
|
174
|
+
#spec;
|
|
175
|
+
#generation = 0;
|
|
176
|
+
#revision = 0;
|
|
177
|
+
#destroyed = false;
|
|
178
|
+
#paused = false;
|
|
179
|
+
#cancel;
|
|
180
|
+
#timeoutMs;
|
|
181
|
+
#key;
|
|
182
|
+
#subscribers = new Set();
|
|
183
|
+
#unobserve;
|
|
184
|
+
#scheduled;
|
|
185
|
+
#frameCallbackFailed = false;
|
|
186
|
+
#scheduleEpoch = 0;
|
|
187
|
+
constructor(spec, options = {}) {
|
|
188
|
+
this.#spec = specCopy(spec);
|
|
189
|
+
const timeout = options.timeoutMs ?? 5000;
|
|
190
|
+
if (!Number.isFinite(timeout) || timeout < 1 || timeout > 60000)
|
|
191
|
+
throw error('INVALID_SOURCE', 'timeoutMs must be 1–60000');
|
|
192
|
+
this.#timeoutMs = timeout;
|
|
193
|
+
this.#key = mediaKey(this.#spec);
|
|
194
|
+
}
|
|
195
|
+
get generation() { return this.#generation; }
|
|
196
|
+
get revision() { return this.#revision; }
|
|
197
|
+
get state() { return this.#destroyed ? 'destroyed' : this.#paused ? 'paused' : 'ready'; }
|
|
198
|
+
#assertAlive() { if (this.#destroyed)
|
|
199
|
+
throw error('DESTROYED', 'Provider is destroyed'); }
|
|
200
|
+
#bump() {
|
|
201
|
+
if (this.#revision === Number.MAX_SAFE_INTEGER)
|
|
202
|
+
throw error('INVALID_SOURCE', 'Revision exhausted');
|
|
203
|
+
this.#revision++;
|
|
204
|
+
this.#cancel?.(error('SUPERSEDED', 'Source revision was superseded'));
|
|
205
|
+
}
|
|
206
|
+
#notify() {
|
|
207
|
+
// Each subscriber is isolated; consumer exceptions cannot strand owned callbacks.
|
|
208
|
+
for (const listener of [...this.#subscribers]) {
|
|
209
|
+
try {
|
|
210
|
+
listener();
|
|
211
|
+
}
|
|
212
|
+
catch { /* See subscribe contract. */ }
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
#refresh(force = false) {
|
|
216
|
+
const next = mediaKey(this.#spec), previous = this.#key;
|
|
217
|
+
if (!next)
|
|
218
|
+
return;
|
|
219
|
+
if (previous && (force || next.width !== previous.width || next.height !== previous.height ||
|
|
220
|
+
next.identity !== previous.identity || next.object !== previous.object || next.time !== previous.time)) {
|
|
221
|
+
if (next.identity !== previous.identity || next.object !== previous.object) {
|
|
222
|
+
if (this.#generation === Number.MAX_SAFE_INTEGER)
|
|
223
|
+
throw error('INVALID_SOURCE', 'Generation exhausted');
|
|
224
|
+
this.#generation++;
|
|
225
|
+
}
|
|
226
|
+
this.#key = next;
|
|
227
|
+
this.#bump();
|
|
228
|
+
this.#notify();
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
this.#key = next;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
#stopScheduled() { this.#scheduleEpoch++; const cancel = this.#scheduled; this.#scheduled = undefined; cancel?.(); }
|
|
235
|
+
#stopObserving() {
|
|
236
|
+
this.#stopScheduled();
|
|
237
|
+
this.#unobserve?.();
|
|
238
|
+
this.#unobserve = undefined;
|
|
239
|
+
}
|
|
240
|
+
#schedule() {
|
|
241
|
+
if (this.#scheduled || this.#destroyed || this.#paused || !this.#subscribers.size || this.#spec.kind !== 'video')
|
|
242
|
+
return;
|
|
243
|
+
const video = this.#spec.value;
|
|
244
|
+
if (video.paused || video.ended || video.readyState < 2 || video.error)
|
|
245
|
+
return;
|
|
246
|
+
const epoch = this.#scheduleEpoch;
|
|
247
|
+
let nativeFrame = false;
|
|
248
|
+
const tick = () => {
|
|
249
|
+
if (epoch !== this.#scheduleEpoch)
|
|
250
|
+
return;
|
|
251
|
+
this.#scheduled = undefined;
|
|
252
|
+
if (this.#destroyed || this.#paused || this.#spec.kind !== 'video' || this.#spec.value !== video || !this.#subscribers.size)
|
|
253
|
+
return;
|
|
254
|
+
this.#refresh(nativeFrame);
|
|
255
|
+
this.#schedule();
|
|
256
|
+
};
|
|
257
|
+
if (!this.#frameCallbackFailed && typeof video.requestVideoFrameCallback === 'function' &&
|
|
258
|
+
typeof video.cancelVideoFrameCallback === 'function') {
|
|
259
|
+
try {
|
|
260
|
+
nativeFrame = true;
|
|
261
|
+
const id = video.requestVideoFrameCallback(tick);
|
|
262
|
+
this.#scheduled = () => video.cancelVideoFrameCallback(id);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
this.#frameCallbackFailed = true;
|
|
267
|
+
nativeFrame = false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const id = setTimeout(tick, 40);
|
|
271
|
+
this.#scheduled = () => clearTimeout(id);
|
|
272
|
+
}
|
|
273
|
+
#observe() {
|
|
274
|
+
if (this.#destroyed || this.#paused || !this.#subscribers.size || this.#spec.kind !== 'video' || this.#unobserve)
|
|
275
|
+
return;
|
|
276
|
+
const video = this.#spec.value;
|
|
277
|
+
const changed = (event) => {
|
|
278
|
+
this.#stopScheduled();
|
|
279
|
+
this.#refresh(event.type !== 'timeupdate');
|
|
280
|
+
this.#schedule();
|
|
281
|
+
};
|
|
282
|
+
const events = ['loadeddata', 'resize', 'seeked', 'timeupdate', 'playing', 'pause', 'ended', 'emptied', 'loadstart', 'error', 'abort'];
|
|
283
|
+
for (const name of events)
|
|
284
|
+
video.addEventListener(name, changed);
|
|
285
|
+
this.#unobserve = () => { for (const name of events)
|
|
286
|
+
video.removeEventListener(name, changed); };
|
|
287
|
+
this.#schedule();
|
|
288
|
+
}
|
|
289
|
+
/** Observe invalidation, not pixel delivery. Exceptions from listeners are isolated. */
|
|
290
|
+
subscribe(listener) {
|
|
291
|
+
this.#assertAlive();
|
|
292
|
+
if (typeof listener !== 'function')
|
|
293
|
+
throw error('INVALID_SOURCE', 'Expected an invalidation listener');
|
|
294
|
+
const entry = () => listener();
|
|
295
|
+
this.#subscribers.add(entry);
|
|
296
|
+
this.#observe();
|
|
297
|
+
return () => { this.#subscribers.delete(entry); if (!this.#subscribers.size)
|
|
298
|
+
this.#stopObserving(); };
|
|
299
|
+
}
|
|
300
|
+
replace(spec) {
|
|
301
|
+
this.#assertAlive();
|
|
302
|
+
const next = specCopy(spec);
|
|
303
|
+
if (this.#generation === Number.MAX_SAFE_INTEGER)
|
|
304
|
+
throw error('INVALID_SOURCE', 'Generation exhausted');
|
|
305
|
+
const key = mediaKey(next);
|
|
306
|
+
this.#stopObserving();
|
|
307
|
+
this.#bump();
|
|
308
|
+
this.#generation++;
|
|
309
|
+
this.#spec = next;
|
|
310
|
+
this.#key = key;
|
|
311
|
+
this.#frameCallbackFailed = false;
|
|
312
|
+
this.#observe();
|
|
313
|
+
this.#notify();
|
|
314
|
+
}
|
|
315
|
+
invalidate() { this.#assertAlive(); this.#bump(); this.#notify(); }
|
|
316
|
+
pause() {
|
|
317
|
+
this.#assertAlive();
|
|
318
|
+
this.#paused = true;
|
|
319
|
+
this.#stopObserving();
|
|
320
|
+
this.#cancel?.(error('PAUSED', 'Provider paused'));
|
|
321
|
+
}
|
|
322
|
+
resume() {
|
|
323
|
+
this.#assertAlive();
|
|
324
|
+
if (this.#paused) {
|
|
325
|
+
this.#bump();
|
|
326
|
+
this.#paused = false;
|
|
327
|
+
this.#refresh();
|
|
328
|
+
this.#observe();
|
|
329
|
+
this.#notify();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
destroy() {
|
|
333
|
+
if (this.#destroyed)
|
|
334
|
+
return;
|
|
335
|
+
this.#destroyed = true;
|
|
336
|
+
this.#stopObserving();
|
|
337
|
+
this.#subscribers.clear();
|
|
338
|
+
this.#cancel?.(error('DESTROYED', 'Provider destroyed'));
|
|
339
|
+
// Forget borrowed objects; never close bitmaps or clear caller media/canvas.
|
|
340
|
+
this.#key = undefined;
|
|
341
|
+
this.#spec = { kind: 'pixels', width: 1, height: 1, data: new Uint8Array(4) };
|
|
342
|
+
}
|
|
343
|
+
read(options = {}) {
|
|
344
|
+
return this.#read(options);
|
|
345
|
+
}
|
|
346
|
+
/** Full synchronous RGBA8 capture into caller-owned storage; never transferred or retained. */
|
|
347
|
+
readInto(target, options = {}) {
|
|
348
|
+
if (!ArrayBuffer.isView(target))
|
|
349
|
+
return Promise.reject(error('INVALID_SOURCE', 'An attached RGBA8 acquisition target is required'));
|
|
350
|
+
return this.#read(options, target);
|
|
351
|
+
}
|
|
352
|
+
#read(options, target) {
|
|
353
|
+
if (this.#destroyed)
|
|
354
|
+
return Promise.reject(error('DESTROYED', 'Provider is destroyed'));
|
|
355
|
+
if (this.#paused)
|
|
356
|
+
return Promise.reject(error('PAUSED', 'Provider is paused'));
|
|
357
|
+
try {
|
|
358
|
+
this.#refresh();
|
|
359
|
+
}
|
|
360
|
+
catch (cause) {
|
|
361
|
+
return Promise.reject(cause);
|
|
362
|
+
}
|
|
363
|
+
if (options.signal?.aborted)
|
|
364
|
+
return Promise.reject(error('ABORTED', 'Read aborted'));
|
|
365
|
+
if (target !== undefined && this.#spec.kind !== 'pixels' && this.#spec.kind !== 'image-data')
|
|
366
|
+
return Promise.reject(error('UNSUPPORTED_SOURCE', 'Caller-owned acquisition requires explicit RGBA8 pixels or ImageData'));
|
|
367
|
+
this.#cancel?.(error('SUPERSEDED', 'A newer read superseded this read'));
|
|
368
|
+
const generation = this.#generation, revision = this.#revision;
|
|
369
|
+
return new Promise((resolve, reject) => {
|
|
370
|
+
const controller = new AbortController();
|
|
371
|
+
let settled = false;
|
|
372
|
+
const finish = (reason, source) => {
|
|
373
|
+
if (settled)
|
|
374
|
+
return;
|
|
375
|
+
settled = true;
|
|
376
|
+
clearTimeout(timer);
|
|
377
|
+
options.signal?.removeEventListener('abort', abort);
|
|
378
|
+
if (this.#cancel === cancel)
|
|
379
|
+
this.#cancel = undefined;
|
|
380
|
+
if (reason !== undefined)
|
|
381
|
+
reject(reason);
|
|
382
|
+
else if (source && !this.#destroyed && generation === this.#generation && revision === this.#revision) {
|
|
383
|
+
resolve(Object.freeze({ source, generation, revision }));
|
|
384
|
+
}
|
|
385
|
+
else
|
|
386
|
+
reject(error('SUPERSEDED', 'Stale source result'));
|
|
387
|
+
};
|
|
388
|
+
const cancel = (reason) => { controller.abort(reason); finish(reason); };
|
|
389
|
+
const abort = () => cancel(error('ABORTED', 'Read aborted'));
|
|
390
|
+
const timer = setTimeout(() => cancel(error('TIMEOUT', 'Source acquisition timed out')), this.#timeoutMs);
|
|
391
|
+
this.#cancel = cancel;
|
|
392
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
393
|
+
try {
|
|
394
|
+
// acquire is invoked now, so pixel inputs are copied before read returns.
|
|
395
|
+
Promise.resolve(acquire(this.#spec, revision, controller.signal, target)).then(source => finish(undefined, source), cause => finish(cause));
|
|
396
|
+
}
|
|
397
|
+
catch (cause) {
|
|
398
|
+
finish(cause);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
package/dist/scene.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { SourceSnapshot } from './providers.js';
|
|
2
|
+
import { type GlassHost, type GlassHostOptions, type GlassMaterial, type HostPixels, type HostCanvas, type HostPresented } from './host.js';
|
|
3
|
+
export interface SceneSource {
|
|
4
|
+
read(options?: {
|
|
5
|
+
readonly signal?: AbortSignal;
|
|
6
|
+
}): Promise<SourceSnapshot>;
|
|
7
|
+
subscribe?(listener: () => void): () => void;
|
|
8
|
+
destroy?(): void;
|
|
9
|
+
}
|
|
10
|
+
export interface FramePixels extends HostPixels {
|
|
11
|
+
readonly release?: () => void;
|
|
12
|
+
}
|
|
13
|
+
export type FrameOutput = (HostPixels | HostCanvas | HostPresented) & {
|
|
14
|
+
readonly release?: () => void;
|
|
15
|
+
};
|
|
16
|
+
export interface RenderRequest {
|
|
17
|
+
readonly snapshot: SourceSnapshot;
|
|
18
|
+
readonly width: number;
|
|
19
|
+
readonly height: number;
|
|
20
|
+
readonly dpr: number;
|
|
21
|
+
readonly rect: Readonly<{
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
}>;
|
|
27
|
+
readonly id?: string;
|
|
28
|
+
/** Synchronous native presentation registers backing ownership before writes. */
|
|
29
|
+
readonly prepareTarget?: (width: number, height: number) => HTMLCanvasElement;
|
|
30
|
+
}
|
|
31
|
+
export interface GlassRenderer {
|
|
32
|
+
initialize?(signal: AbortSignal): void | (() => void) | Promise<void | (() => void)>;
|
|
33
|
+
render(request: RenderRequest, signal: AbortSignal): FrameOutput | Promise<FrameOutput>;
|
|
34
|
+
renderBatch?(requests: readonly RenderRequest[], signal: AbortSignal): readonly FrameOutput[] | Promise<readonly FrameOutput[]>;
|
|
35
|
+
releaseSurface?(id: string): void;
|
|
36
|
+
}
|
|
37
|
+
export interface GlassSceneOptions {
|
|
38
|
+
readonly source?: SceneSource;
|
|
39
|
+
readonly renderer?: GlassRenderer;
|
|
40
|
+
readonly ownsSource?: boolean;
|
|
41
|
+
readonly timeoutMs?: number;
|
|
42
|
+
readonly signal?: AbortSignal;
|
|
43
|
+
readonly onError?: (error: unknown) => void;
|
|
44
|
+
}
|
|
45
|
+
export declare class SceneError extends Error {
|
|
46
|
+
readonly code: 'INVALID_OPTIONS' | 'TIMEOUT' | 'ABORTED' | 'PAUSED' | 'DESTROYED' | 'SUPERSEDED' | 'FAILED';
|
|
47
|
+
constructor(code: 'INVALID_OPTIONS' | 'TIMEOUT' | 'ABORTED' | 'PAUSED' | 'DESTROYED' | 'SUPERSEDED' | 'FAILED', message: string, options?: ErrorOptions);
|
|
48
|
+
}
|
|
49
|
+
export interface GlassAttachment {
|
|
50
|
+
readonly host: GlassHost;
|
|
51
|
+
update(options?: Pick<GlassHostOptions, 'material'>): void;
|
|
52
|
+
invalidate(): void;
|
|
53
|
+
dispose(): void;
|
|
54
|
+
}
|
|
55
|
+
/** Shared source ownership and dirty scheduling; the default is material-only. No animation clock. */
|
|
56
|
+
export declare class GlassScene {
|
|
57
|
+
#private;
|
|
58
|
+
constructor(options?: GlassSceneOptions);
|
|
59
|
+
get ready(): Promise<void>;
|
|
60
|
+
get state(): 'initializing' | 'ready' | 'paused' | 'failed' | 'destroyed';
|
|
61
|
+
attach(element: HTMLElement, options?: GlassHostOptions): GlassAttachment;
|
|
62
|
+
update(options?: {
|
|
63
|
+
readonly material?: GlassMaterial;
|
|
64
|
+
}): void;
|
|
65
|
+
invalidate(): void;
|
|
66
|
+
pause(): void;
|
|
67
|
+
resume(): void;
|
|
68
|
+
dispose(): void;
|
|
69
|
+
}
|
|
70
|
+
export declare function createGlassScene(options?: GlassSceneOptions): GlassScene;
|