diff --git a/src/common/element.d.ts b/src/common/element.d.ts index 5f5fcdb5..52f40d9e 100644 --- a/src/common/element.d.ts +++ b/src/common/element.d.ts @@ -13,3 +13,10 @@ export type RenderArg = { html: HtmlTagFunction; state: State; }; + +export type WorkerOpts = { + forceNew?: boolean | { + self?: boolean; + dependencies?: Record; + }; +}; diff --git a/src/common/element.js b/src/common/element.js index 7750dfe6..f0c1b823 100644 --- a/src/common/element.js +++ b/src/common/element.js @@ -9,7 +9,7 @@ import { BrowserPostMessageIo } from "./worker/rpc.js"; export { keyed } from "lit-html/directives/keyed.js"; /** - * @import {BroadcastingStatus, ProvisionedWorker, ProvisionedWorkers} from "./element.d.ts" + * @import {BroadcastingStatus, ProvisionedWorker, ProvisionedWorkers, WorkerOpts} from "./element.d.ts" * @import {ProxiedActions, Tunnel} from "./worker.d.ts"; * @import {Signal} from "./signal.d.ts" */ @@ -166,21 +166,25 @@ export class DiffuseElement extends HTMLElement { /** * @template {Record any>} Actions + * @param {WorkerOpts} [opts] * @returns {ProxiedActions} */ - workerProxy() { + workerProxy(opts) { return workerProxy( - () => this.workerTunnel().port, + () => this.workerTunnel(opts).port, ); } /** - * @param {{ newWorker?: boolean }} [opts] + * @param {WorkerOpts} [opts] */ - workerTunnel({ newWorker } = {}) { + workerTunnel({ forceNew } = {}) { // Creates a MessagePort that is connected to the worker. // All the dependencies are added automatically. - const worker = newWorker ? this.createWorker() : this.worker(); + const worker = forceNew === true || + (typeof forceNew === "object" && forceNew.self === true) + ? this.createWorker() + : this.worker(); const deps = this.dependencies(); let toWorker; @@ -194,7 +198,12 @@ export class DiffuseElement extends HTMLElement { /** @type {Array<[string, Tunnel]>} */ const ports = Object.entries(deps).map( /** @param {[string, DiffuseElement]} _ */ - ([k, v]) => [k, v.workerTunnel()], + ([k, v]) => { + const n = typeof forceNew === "object" + ? forceNew.dependencies?.[k] ?? false + : false; + return [k, v.workerTunnel({ forceNew: n })]; + }, ); const decoded = await decodeMessage(msg); diff --git a/src/components/orchestrator/process-tracks/element.js b/src/components/orchestrator/process-tracks/element.js index 30bdd4f5..9c93762f 100644 --- a/src/components/orchestrator/process-tracks/element.js +++ b/src/components/orchestrator/process-tracks/element.js @@ -28,7 +28,11 @@ class ProcessTracksOrchestrator extends DiffuseElement { constructor() { super(); - this.#proxy = this.workerProxy(); + this.#proxy = this.workerProxy({ + forceNew: { + dependencies: { input: true }, + }, + }); } // SIGNALS diff --git a/src/components/orchestrator/search-tracks/element.js b/src/components/orchestrator/search-tracks/element.js index 68a5b856..5e244094 100644 --- a/src/components/orchestrator/search-tracks/element.js +++ b/src/components/orchestrator/search-tracks/element.js @@ -27,7 +27,13 @@ class SearchTracksOrchestrator extends DiffuseElement { constructor() { super(); - this.#proxy = this.workerProxy(); + this.#proxy = this.workerProxy({ + forceNew: { + dependencies: { + input: true, + }, + }, + }); } // LIFECYCLE