@sealpixel/element 1.0.0-rc.21 → 1.0.0-rc.22

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.
@@ -11,7 +11,8 @@ import { type Renderer } from '@sealpixel/core';
11
11
  * Until the worker has answered once, it holds no state, so a failure moves
12
12
  * every pending call to the inline renderer in the order the calls were made.
13
13
  * After that, the worker holds the loaded image and the inline renderer does
14
- * not, so a failure rejects the pending calls instead.
14
+ * not, so a failure rejects the pending calls instead, and every later call
15
+ * fails with the same error.
15
16
  */
16
17
  export declare class FallbackRenderer implements Renderer {
17
18
  private readonly worker;
@@ -20,7 +21,10 @@ export declare class FallbackRenderer implements Renderer {
20
21
  private readonly pending;
21
22
  private answered;
22
23
  private fellBack;
23
- /** Set when the fallback could not start either; every call fails with it. */
24
+ /**
25
+ * Set when the worker failed after its first answer, or when the fallback
26
+ * could not start either; every call fails with it.
27
+ */
24
28
  private broken;
25
29
  constructor(worker: Worker, fallback: () => Renderer);
26
30
  init(): Promise<{
@@ -11,7 +11,8 @@ import { SealpixelError, WorkerRenderer } from '@sealpixel/core';
11
11
  * Until the worker has answered once, it holds no state, so a failure moves
12
12
  * every pending call to the inline renderer in the order the calls were made.
13
13
  * After that, the worker holds the loaded image and the inline renderer does
14
- * not, so a failure rejects the pending calls instead.
14
+ * not, so a failure rejects the pending calls instead, and every later call
15
+ * fails with the same error.
15
16
  */
16
17
  export class FallbackRenderer {
17
18
  worker;
@@ -20,14 +21,21 @@ export class FallbackRenderer {
20
21
  pending = new Set();
21
22
  answered = false;
22
23
  fellBack = false;
23
- /** Set when the fallback could not start either; every call fails with it. */
24
+ /**
25
+ * Set when the worker failed after its first answer, or when the fallback
26
+ * could not start either; every call fails with it.
27
+ */
24
28
  broken = null;
25
29
  constructor(worker, fallback) {
26
30
  this.worker = worker;
27
31
  this.fallback = fallback;
28
- this.current = new WorkerRenderer(worker);
32
+ // These listeners must run before the ones of `WorkerRenderer`, which
33
+ // rejects its pending calls on the same events. The browser settles those
34
+ // rejections between two listeners. If the `WorkerRenderer` came first,
35
+ // the calls would fail before this class could move them to the fallback.
29
36
  worker.addEventListener('error', this.onWorkerError);
30
37
  worker.addEventListener('messageerror', this.onWorkerError);
38
+ this.current = new WorkerRenderer(worker);
31
39
  }
32
40
  init() {
33
41
  return this.call((r) => r.init());
@@ -98,11 +106,15 @@ export class FallbackRenderer {
98
106
  const failed = this.current;
99
107
  if (this.answered) {
100
108
  // The worker has the loaded image. Nothing else has it, so the calls
101
- // waiting on the worker fail, and the session reports the error.
109
+ // waiting on the worker fail, and the session reports the error. The
110
+ // worker may never answer again, so later calls fail at once too.
102
111
  const error = new SealpixelError('KERNEL_FAILED', `the render worker failed${reason}`);
112
+ this.broken = error;
113
+ this.detach();
103
114
  for (const call of this.pending)
104
115
  call.reject(error);
105
116
  this.pending.clear();
117
+ failed.dispose();
106
118
  return;
107
119
  }
108
120
  // The worker never started: nothing is lost by starting over inline.
@@ -1 +1 @@
1
- {"version":3,"file":"fallback-renderer.js","sourceRoot":"","sources":["../src/fallback-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAQhF;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,gBAAgB;IASR;IACA;IATX,OAAO,CAAW;IACT,OAAO,GAAG,IAAI,GAAG,EAAQ,CAAC;IACnC,QAAQ,GAAG,KAAK,CAAC;IACjB,QAAQ,GAAG,KAAK,CAAC;IACzB,8EAA8E;IACtE,MAAM,GAAY,IAAI,CAAC;IAE/B,YACmB,MAAc,EACd,QAAwB;QADxB,WAAM,GAAN,MAAM,CAAQ;QACd,aAAQ,GAAR,QAAQ,CAAgB;QAEzC,IAAI,CAAC,OAAO,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;QAC1C,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QACrD,MAAM,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,MAAM,oBAAoB,CAAC,CAAC;QACvF,IAAI,CAAC,MAAM,EAAE,CAAC;QACd,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC;YACH,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;YACpB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,OAAO;gBAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACpD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC,CAAC;IAEM,MAAM;QACZ,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QAC7D,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IACtE,CAAC;CACF","sourcesContent":["import { type Renderer, SealpixelError, WorkerRenderer } from '@sealpixel/core';\n\ninterface Call {\n run: (renderer: Renderer) => Promise<unknown>;\n resolve: (value: unknown) => void;\n reject: (error: unknown) => void;\n}\n\n/**\n * The bundled worker, with the inline renderer as the fallback when the worker\n * never starts.\n *\n * `new Worker()` throws only for a URL it cannot parse. A worker script that\n * does not load (a 404, a Content Security Policy, an import that fails inside\n * it) reports through an `error` event, and a `WorkerRenderer` alone would\n * wait for its answers forever.\n *\n * Until the worker has answered once, it holds no state, so a failure moves\n * every pending call to the inline renderer in the order the calls were made.\n * After that, the worker holds the loaded image and the inline renderer does\n * not, so a failure rejects the pending calls instead.\n */\nexport class FallbackRenderer implements Renderer {\n private current: Renderer;\n private readonly pending = new Set<Call>();\n private answered = false;\n private fellBack = false;\n /** Set when the fallback could not start either; every call fails with it. */\n private broken: unknown = null;\n\n constructor(\n private readonly worker: Worker,\n private readonly fallback: () => Renderer,\n ) {\n this.current = new WorkerRenderer(worker);\n worker.addEventListener('error', this.onWorkerError);\n worker.addEventListener('messageerror', this.onWorkerError);\n }\n\n init() {\n return this.call((r) => r.init());\n }\n load(...args: Parameters<Renderer['load']>) {\n return this.call((r) => r.load(...args));\n }\n loadWorkingCopy(...args: Parameters<Renderer['loadWorkingCopy']>) {\n return this.call((r) => r.loadWorkingCopy(...args));\n }\n loadWorkingCopyEncoded(...args: Parameters<Renderer['loadWorkingCopyEncoded']>) {\n return this.call((r) => r.loadWorkingCopyEncoded(...args));\n }\n render(...args: Parameters<Renderer['render']>) {\n return this.call((r) => r.render(...args));\n }\n export(...args: Parameters<Renderer['export']>) {\n return this.call((r) => r.export(...args));\n }\n geometry(...args: Parameters<Renderer['geometry']>) {\n return this.call((r) => r.geometry(...args));\n }\n unload() {\n return this.call((r) => r.unload());\n }\n putAsset(...args: Parameters<Renderer['putAsset']>) {\n return this.call((r) => r.putAsset(...args));\n }\n dispose() {\n this.detach();\n this.current.dispose();\n for (const call of this.pending)\n call.reject(new SealpixelError('ABORTED', 'renderer disposed'));\n this.pending.clear();\n }\n\n private call<T>(run: (renderer: Renderer) => Promise<T>): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n if (this.broken !== null) {\n reject(this.broken);\n return;\n }\n const call: Call = { run, resolve: resolve as (value: unknown) => void, reject };\n this.pending.add(call);\n this.start(call);\n });\n }\n\n private start(call: Call): void {\n const renderer = this.current;\n call.run(renderer).then(\n (value) => {\n // A late answer from a renderer that has been replaced is not this call's answer.\n if (renderer !== this.current || !this.pending.has(call)) return;\n if (!this.fellBack) this.answered = true;\n this.pending.delete(call);\n call.resolve(value);\n },\n (error) => {\n if (renderer !== this.current || !this.pending.has(call)) return;\n if (!this.fellBack) this.answered = true;\n this.pending.delete(call);\n call.reject(error);\n },\n );\n }\n\n private onWorkerError = (event: Event) => {\n const reason = event instanceof ErrorEvent && event.message ? `: ${event.message}` : '';\n const failed = this.current;\n if (this.answered) {\n // The worker has the loaded image. Nothing else has it, so the calls\n // waiting on the worker fail, and the session reports the error.\n const error = new SealpixelError('KERNEL_FAILED', `the render worker failed${reason}`);\n for (const call of this.pending) call.reject(error);\n this.pending.clear();\n return;\n }\n // The worker never started: nothing is lost by starting over inline.\n console.warn(`[sealpixel] the render worker did not start${reason}; rendering inline`);\n this.detach();\n this.fellBack = true;\n try {\n this.current = this.fallback();\n } catch (error) {\n this.broken = error;\n for (const call of this.pending) call.reject(error);\n this.pending.clear();\n return;\n } finally {\n failed.dispose();\n }\n for (const call of this.pending) this.start(call);\n };\n\n private detach(): void {\n this.worker.removeEventListener('error', this.onWorkerError);\n this.worker.removeEventListener('messageerror', this.onWorkerError);\n }\n}\n"]}
1
+ {"version":3,"file":"fallback-renderer.js","sourceRoot":"","sources":["../src/fallback-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAQhF;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,gBAAgB;IAYR;IACA;IAZX,OAAO,CAAW;IACT,OAAO,GAAG,IAAI,GAAG,EAAQ,CAAC;IACnC,QAAQ,GAAG,KAAK,CAAC;IACjB,QAAQ,GAAG,KAAK,CAAC;IACzB;;;OAGG;IACK,MAAM,GAAY,IAAI,CAAC;IAE/B,YACmB,MAAc,EACd,QAAwB;QADxB,WAAM,GAAN,MAAM,CAAQ;QACd,aAAQ,GAAR,QAAQ,CAAgB;QAEzC,sEAAsE;QACtE,0EAA0E;QAC1E,wEAAwE;QACxE,0EAA0E;QAC1E,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QACrD,MAAM,CAAC,gBAAgB,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AAC,KAAK,CAAC,CAAC;YACpD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,MAAM,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QACD,qEAAqE;QACrE,OAAO,CAAC,IAAI,CAAC,8CAA8C,MAAM,oBAAoB,CAAC,CAAC;QACvF,IAAI,CAAC,MAAM,EAAE,CAAC;QACd,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC;YACH,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;YACpB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,OAAO;gBAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACpD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC,CAAC;IAEM,MAAM;QACZ,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QAC7D,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IACtE,CAAC;CACF","sourcesContent":["import { type Renderer, SealpixelError, WorkerRenderer } from '@sealpixel/core';\n\ninterface Call {\n run: (renderer: Renderer) => Promise<unknown>;\n resolve: (value: unknown) => void;\n reject: (error: unknown) => void;\n}\n\n/**\n * The bundled worker, with the inline renderer as the fallback when the worker\n * never starts.\n *\n * `new Worker()` throws only for a URL it cannot parse. A worker script that\n * does not load (a 404, a Content Security Policy, an import that fails inside\n * it) reports through an `error` event, and a `WorkerRenderer` alone would\n * wait for its answers forever.\n *\n * Until the worker has answered once, it holds no state, so a failure moves\n * every pending call to the inline renderer in the order the calls were made.\n * After that, the worker holds the loaded image and the inline renderer does\n * not, so a failure rejects the pending calls instead, and every later call\n * fails with the same error.\n */\nexport class FallbackRenderer implements Renderer {\n private current: Renderer;\n private readonly pending = new Set<Call>();\n private answered = false;\n private fellBack = false;\n /**\n * Set when the worker failed after its first answer, or when the fallback\n * could not start either; every call fails with it.\n */\n private broken: unknown = null;\n\n constructor(\n private readonly worker: Worker,\n private readonly fallback: () => Renderer,\n ) {\n // These listeners must run before the ones of `WorkerRenderer`, which\n // rejects its pending calls on the same events. The browser settles those\n // rejections between two listeners. If the `WorkerRenderer` came first,\n // the calls would fail before this class could move them to the fallback.\n worker.addEventListener('error', this.onWorkerError);\n worker.addEventListener('messageerror', this.onWorkerError);\n this.current = new WorkerRenderer(worker);\n }\n\n init() {\n return this.call((r) => r.init());\n }\n load(...args: Parameters<Renderer['load']>) {\n return this.call((r) => r.load(...args));\n }\n loadWorkingCopy(...args: Parameters<Renderer['loadWorkingCopy']>) {\n return this.call((r) => r.loadWorkingCopy(...args));\n }\n loadWorkingCopyEncoded(...args: Parameters<Renderer['loadWorkingCopyEncoded']>) {\n return this.call((r) => r.loadWorkingCopyEncoded(...args));\n }\n render(...args: Parameters<Renderer['render']>) {\n return this.call((r) => r.render(...args));\n }\n export(...args: Parameters<Renderer['export']>) {\n return this.call((r) => r.export(...args));\n }\n geometry(...args: Parameters<Renderer['geometry']>) {\n return this.call((r) => r.geometry(...args));\n }\n unload() {\n return this.call((r) => r.unload());\n }\n putAsset(...args: Parameters<Renderer['putAsset']>) {\n return this.call((r) => r.putAsset(...args));\n }\n dispose() {\n this.detach();\n this.current.dispose();\n for (const call of this.pending)\n call.reject(new SealpixelError('ABORTED', 'renderer disposed'));\n this.pending.clear();\n }\n\n private call<T>(run: (renderer: Renderer) => Promise<T>): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n if (this.broken !== null) {\n reject(this.broken);\n return;\n }\n const call: Call = { run, resolve: resolve as (value: unknown) => void, reject };\n this.pending.add(call);\n this.start(call);\n });\n }\n\n private start(call: Call): void {\n const renderer = this.current;\n call.run(renderer).then(\n (value) => {\n // A late answer from a renderer that has been replaced is not this call's answer.\n if (renderer !== this.current || !this.pending.has(call)) return;\n if (!this.fellBack) this.answered = true;\n this.pending.delete(call);\n call.resolve(value);\n },\n (error) => {\n if (renderer !== this.current || !this.pending.has(call)) return;\n if (!this.fellBack) this.answered = true;\n this.pending.delete(call);\n call.reject(error);\n },\n );\n }\n\n private onWorkerError = (event: Event) => {\n const reason = event instanceof ErrorEvent && event.message ? `: ${event.message}` : '';\n const failed = this.current;\n if (this.answered) {\n // The worker has the loaded image. Nothing else has it, so the calls\n // waiting on the worker fail, and the session reports the error. The\n // worker may never answer again, so later calls fail at once too.\n const error = new SealpixelError('KERNEL_FAILED', `the render worker failed${reason}`);\n this.broken = error;\n this.detach();\n for (const call of this.pending) call.reject(error);\n this.pending.clear();\n failed.dispose();\n return;\n }\n // The worker never started: nothing is lost by starting over inline.\n console.warn(`[sealpixel] the render worker did not start${reason}; rendering inline`);\n this.detach();\n this.fellBack = true;\n try {\n this.current = this.fallback();\n } catch (error) {\n this.broken = error;\n for (const call of this.pending) call.reject(error);\n this.pending.clear();\n return;\n } finally {\n failed.dispose();\n }\n for (const call of this.pending) this.start(call);\n };\n\n private detach(): void {\n this.worker.removeEventListener('error', this.onWorkerError);\n this.worker.removeEventListener('messageerror', this.onWorkerError);\n }\n}\n"]}
package/dist/index.js CHANGED
@@ -33,8 +33,11 @@ export function createDefaultRenderer(plugins = defaultPlugins) {
33
33
  // renderer then moves to the inline renderer. See FallbackRenderer.
34
34
  return new FallbackRenderer(worker, inline);
35
35
  }
36
- catch {
37
- // fall through to inline
36
+ catch (error) {
37
+ // `new Worker()` throws for a URL it cannot parse. Warn like a worker
38
+ // that fails to load (see FallbackRenderer), then render inline.
39
+ const reason = error instanceof Error && error.message ? `: ${error.message}` : '';
40
+ console.warn(`[sealpixel] the render worker did not start${reason}; rendering inline`);
38
41
  }
39
42
  }
40
43
  return inline();
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAkB,cAAc,EAAiB,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC;AAEtC;;;;;;;;GAQG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAgC,cAAc;IAClF,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;IACjD,MAAM,aAAa,GACjB,MAAM,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM;QAChC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACvF,MAAM,MAAM,GAAG,GAAG,EAAE,CAClB,IAAI,cAAc,CAAC,EAAE,OAAO,EAAE,mBAAmB,EAAE,EAAE,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC;IACrF,IAAI,aAAa,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QACnD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;YACvF,sEAAsE;YACtE,oEAAoE;YACpE,OAAO,IAAI,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9C,CAAC;QAAC,MAAM,CAAC;YACP,yBAAyB;QAC3B,CAAC;IACH,CAAC;IACD,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC;AAED,eAAe,CAAC,sBAAsB,KAAK,qBAAqB,CAAC","sourcesContent":["/**\n * `import '@sealpixel/element'` registers `<sealpixel-editor>` and friends with the\n * first-party plugins and a worker-backed renderer. Bundlers that understand\n * `new Worker(new URL(..., import.meta.url), { type: 'module' })` (Vite,\n * webpack 5, esbuild with the worker plugin, Parcel) emit the worker chunk.\n */\nimport { type AnyPlugin, InlineRenderer, type Renderer } from '@sealpixel/core';\nimport { allPlugins, defaultPlugins, renderingPlugins } from '@sealpixel/plugins';\nimport { SealpixelEditor } from '@sealpixel/ui';\nimport '@sealpixel/ui/define';\nimport { FallbackRenderer } from './fallback-renderer.js';\n\nexport * from '@sealpixel/ui';\nexport { allPlugins, defaultPlugins };\n\n/**\n * Starts the bundled worker when the plugin list renders like the default one,\n * with or without options; options never reach the renderer, and neither does\n * a plugin without operations (`assist`), so those are ignored in the\n * comparison. A custom plugin list needs a custom worker (see\n * `startSealpixelWorker`), so it falls back to rendering inline. A worker that\n * does not load (a 404, a Content Security Policy, a failing import) also\n * falls back to rendering inline.\n */\nexport function createDefaultRenderer(plugins: readonly AnyPlugin[] = defaultPlugins): Renderer {\n const wanted = renderingPlugins(plugins);\n const bundled = renderingPlugins(defaultPlugins);\n const sameAsDefault =\n wanted.length === bundled.length &&\n wanted.every((p, i) => p.id === bundled[i]?.id && p.version === bundled[i]?.version);\n const inline = () =>\n new InlineRenderer({ plugins, maxClientMegapixels: 16, workingCopyMaxSide: 2048 });\n if (sameAsDefault && typeof Worker !== 'undefined') {\n try {\n const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });\n // A worker that fails to load reports it later, through an event; the\n // renderer then moves to the inline renderer. See FallbackRenderer.\n return new FallbackRenderer(worker, inline);\n } catch {\n // fall through to inline\n }\n }\n return inline();\n}\n\nSealpixelEditor.defaultRendererFactory ??= createDefaultRenderer;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAkB,cAAc,EAAiB,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC;AAEtC;;;;;;;;GAQG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAgC,cAAc;IAClF,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;IACjD,MAAM,aAAa,GACjB,MAAM,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM;QAChC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACvF,MAAM,MAAM,GAAG,GAAG,EAAE,CAClB,IAAI,cAAc,CAAC,EAAE,OAAO,EAAE,mBAAmB,EAAE,EAAE,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC;IACrF,IAAI,aAAa,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QACnD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;YACvF,sEAAsE;YACtE,oEAAoE;YACpE,OAAO,IAAI,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,sEAAsE;YACtE,iEAAiE;YACjE,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACnF,OAAO,CAAC,IAAI,CAAC,8CAA8C,MAAM,oBAAoB,CAAC,CAAC;QACzF,CAAC;IACH,CAAC;IACD,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC;AAED,eAAe,CAAC,sBAAsB,KAAK,qBAAqB,CAAC","sourcesContent":["/**\n * `import '@sealpixel/element'` registers `<sealpixel-editor>` and friends with the\n * first-party plugins and a worker-backed renderer. Bundlers that understand\n * `new Worker(new URL(..., import.meta.url), { type: 'module' })` (Vite,\n * webpack 5, esbuild with the worker plugin, Parcel) emit the worker chunk.\n */\nimport { type AnyPlugin, InlineRenderer, type Renderer } from '@sealpixel/core';\nimport { allPlugins, defaultPlugins, renderingPlugins } from '@sealpixel/plugins';\nimport { SealpixelEditor } from '@sealpixel/ui';\nimport '@sealpixel/ui/define';\nimport { FallbackRenderer } from './fallback-renderer.js';\n\nexport * from '@sealpixel/ui';\nexport { allPlugins, defaultPlugins };\n\n/**\n * Starts the bundled worker when the plugin list renders like the default one,\n * with or without options; options never reach the renderer, and neither does\n * a plugin without operations (`assist`), so those are ignored in the\n * comparison. A custom plugin list needs a custom worker (see\n * `startSealpixelWorker`), so it falls back to rendering inline. A worker that\n * does not load (a 404, a Content Security Policy, a failing import) also\n * falls back to rendering inline.\n */\nexport function createDefaultRenderer(plugins: readonly AnyPlugin[] = defaultPlugins): Renderer {\n const wanted = renderingPlugins(plugins);\n const bundled = renderingPlugins(defaultPlugins);\n const sameAsDefault =\n wanted.length === bundled.length &&\n wanted.every((p, i) => p.id === bundled[i]?.id && p.version === bundled[i]?.version);\n const inline = () =>\n new InlineRenderer({ plugins, maxClientMegapixels: 16, workingCopyMaxSide: 2048 });\n if (sameAsDefault && typeof Worker !== 'undefined') {\n try {\n const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });\n // A worker that fails to load reports it later, through an event; the\n // renderer then moves to the inline renderer. See FallbackRenderer.\n return new FallbackRenderer(worker, inline);\n } catch (error) {\n // `new Worker()` throws for a URL it cannot parse. Warn like a worker\n // that fails to load (see FallbackRenderer), then render inline.\n const reason = error instanceof Error && error.message ? `: ${error.message}` : '';\n console.warn(`[sealpixel] the render worker did not start${reason}; rendering inline`);\n }\n }\n return inline();\n}\n\nSealpixelEditor.defaultRendererFactory ??= createDefaultRenderer;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealpixel/element",
3
- "version": "1.0.0-rc.21",
3
+ "version": "1.0.0-rc.22",
4
4
  "description": "Drop-in <sealpixel-editor> with the default first-party plugins and a bundled render worker. Import once, use the tag.",
5
5
  "license": "BUSL-1.1",
6
6
  "repository": {
@@ -28,18 +28,21 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "@sealpixel/core": "1.0.0-rc.21",
32
- "@sealpixel/plugins": "1.0.0-rc.21",
33
- "@sealpixel/ui": "1.0.0-rc.21",
34
- "@sealpixel/wasm": "1.0.0-rc.21"
31
+ "@sealpixel/core": "1.0.0-rc.22",
32
+ "@sealpixel/plugins": "1.0.0-rc.22",
33
+ "@sealpixel/ui": "1.0.0-rc.22",
34
+ "@sealpixel/wasm": "1.0.0-rc.22"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@sealpixel/config": "0.0.0",
38
38
  "@types/node": "^24.0.0",
39
- "typescript": "^6.0.0"
39
+ "happy-dom": "^20.0.0",
40
+ "typescript": "^6.0.0",
41
+ "vitest": "^5.0.0"
40
42
  },
41
43
  "scripts": {
42
44
  "build": "tsc -p tsconfig.build.json",
43
- "typecheck": "tsc -p tsconfig.json --noEmit"
45
+ "typecheck": "tsc -p tsconfig.json --noEmit",
46
+ "test": "vitest run"
44
47
  }
45
48
  }