A music player that connects to your cloud/distributed storage.
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: refactor worker tunnels

+81 -82
+5 -1
src/common/element.d.ts
··· 1 + import type { Tunnel } from "./worker.d.ts"; 2 + 1 3 export type BroadcastingStatus = 2 4 | { leader: true; initialLeader: boolean } 3 5 | { leader: false }; ··· 8 10 ) => string; 9 11 10 12 export type ProvisionedWorkers<T extends string> = { 11 - [K in T]: Worker | SharedWorker; 13 + [K in T]: ProvisionedWorker; 12 14 }; 15 + 16 + export type ProvisionedWorker = Worker | SharedWorker; 13 17 14 18 export type RenderArg<State = undefined> = { 15 19 html: HtmlTagFunction;
+18 -14
src/common/element.js
··· 1 1 import QS from "query-string"; 2 + import { RPCChannel } from "@kunkun/kkrpc"; 2 3 import { html, render } from "lit-html"; 3 4 4 5 import { effect, signal } from "@common/signal.js"; 5 - import { 6 - getTransferables, 7 - rpc, 8 - transfer, 9 - workerLink, 10 - workerTunnel, 11 - } from "./worker.js"; 6 + import { rpc, transfer, workerLink, workerTunnel } from "./worker.js"; 12 7 import { BrowserPostMessageIo } from "./worker/rpc.js"; 13 - import { RPCChannel } from "@kunkun/kkrpc"; 8 + 9 + // RE-EXPORT 10 + 11 + export { workerLink, workerProxy, workerTunnel } from "./worker.js"; 14 12 15 13 /** 16 - * @import {BroadcastingStatus, FnParams, FnReturn, ProvisionedWorkers} from "./element.d.ts" 14 + * @import {BroadcastingStatus, ProvisionedWorker, ProvisionedWorkers} from "./element.d.ts" 17 15 * @import {ProxiedActions, Tunnel} from "./worker.d.ts"; 18 16 * @import {Signal} from "./signal.d.ts" 19 17 */ ··· 424 422 /** 425 423 * @template {Record<string, DiffuseElement>} T 426 424 * @param {T} elements 427 - * @returns {Promise<{ [K in keyof T]: Worker | SharedWorker }>} 428 425 */ 429 426 export async function provisionWorkers(elements) { 430 427 await whenElementsDefined(elements); 431 428 432 - const entries = Object.entries(elements).map(([key, element]) => { 433 - return [key, element.createWorker()]; 429 + /** @type {Record<string, ProvisionedWorker>} */ 430 + const provisions = {}; 431 + 432 + Object.entries(elements).forEach(([key, element]) => { 433 + const worker = element.createWorker(); 434 + provisions[key] = worker; 434 435 }); 435 436 436 - return Object.fromEntries(entries); 437 + const casted = 438 + /** @type {{ [K in keyof T]: ProvisionedWorker}} */ (provisions); 439 + 440 + return casted; 437 441 } 438 442 439 443 /** 440 444 * @param {ProvisionedWorkers<any> | undefined} workers 441 445 */ 442 - export function terminateWorkers(workers) { 446 + export function terminateProvisions(workers) { 443 447 if (!workers) return; 444 448 445 449 Object.values(workers).forEach((worker) => {
+34 -34
src/common/worker.js
··· 65 65 } 66 66 67 67 /** 68 + * @template {Record<string, (...args: any[]) => any>} Actions 69 + * @param {() => MessagePort | Worker} workerLinkCreator 70 + * @returns {ProxiedActions<Actions>} 71 + */ 72 + export function workerProxy(workerLinkCreator) { 73 + /** @type {ProxiedActions<Actions> | undefined} */ 74 + let int_api; 75 + 76 + /** @returns {ProxiedActions<Actions>} */ 77 + function ensureAPI() { 78 + if (!int_api) { 79 + const io = new BrowserPostMessageIo(workerLinkCreator); 80 + 81 + /** @type {undefined | RPCChannel<{}, ProxiedActions<Actions>>} */ 82 + const rpc = new RPCChannel(io, { enableTransfer: true }); 83 + 84 + int_api = rpc.getAPI(); 85 + } 86 + 87 + return int_api; 88 + } 89 + 90 + // Create proxy that creates RPC API when needed 91 + const proxy = new Proxy(() => {}, { 92 + get: (_target, prop) => { 93 + const api = ensureAPI(); 94 + return api[prop.toString()]; 95 + }, 96 + }); 97 + 98 + return /** @type {ProxiedActions<Actions>} */ (/** @type {any} */ (proxy)); 99 + } 100 + 101 + /** 68 102 * @param {MessagePort | Worker | SharedWorker} workerOrLink 69 103 * @returns {Tunnel} 70 104 */ ··· 156 190 157 191 /** @type {undefined | RPCChannel<Actions, {}>} */ 158 192 return new RPCChannel(io, { enableTransfer: true, expose: actions }); 159 - } 160 - 161 - /** 162 - * @template {Record<string, (...args: any[]) => any>} Actions 163 - * @param {() => MessagePort | Worker} workerLinkCreator 164 - * @returns {ProxiedActions<Actions>} 165 - */ 166 - export function workerProxy(workerLinkCreator) { 167 - /** @type {ProxiedActions<Actions> | undefined} */ 168 - let int_api; 169 - 170 - /** @returns {ProxiedActions<Actions>} */ 171 - function ensureAPI() { 172 - if (!int_api) { 173 - const io = new BrowserPostMessageIo(workerLinkCreator); 174 - 175 - /** @type {undefined | RPCChannel<{}, ProxiedActions<Actions>>} */ 176 - const rpc = new RPCChannel(io, { enableTransfer: true }); 177 - 178 - int_api = rpc.getAPI(); 179 - } 180 - 181 - return int_api; 182 - } 183 - 184 - // Create proxy that creates RPC API when needed 185 - const proxy = new Proxy(() => {}, { 186 - get: (_target, prop) => { 187 - const api = ensureAPI(); 188 - return api[prop.toString()]; 189 - }, 190 - }); 191 - 192 - return /** @type {ProxiedActions<Actions>} */ (/** @type {any} */ (proxy)); 193 193 } 194 194 195 195 ////////////////////////////////////////////
+2 -2
src/components/engine/queue/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 2 2 import { signal } from "@common/signal.js"; 3 - import { listen, workerLink, workerProxy } from "@common/worker.js"; 3 + import { listen } from "@common/worker.js"; 4 4 import { hash } from "@common/index.js"; 5 5 6 6 /**
+1 -2
src/components/input/opensubsonic/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 4 3 /** 5 4 * @import {InputActions} from "@components/input/types.d.ts"
+1 -2
src/components/input/s3/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 4 3 /** 5 4 * @import {InputActions} from "@components/input/types.d.ts"
+3 -8
src/components/orchestrator/process-tracks/element.js
··· 3 3 DiffuseElement, 4 4 provisionWorkers, 5 5 query, 6 - terminateWorkers, 6 + terminateProvisions, 7 + workerProxy, 7 8 } from "@common/element.js"; 8 9 import { signal, untracked } from "@common/signal.js"; 9 - import { 10 - transfer, 11 - workerLink, 12 - workerProxy, 13 - workerTunnel, 14 - } from "@common/worker.js"; 15 10 16 11 /** 17 12 * @import {Track} from "@definitions/types.d.ts" ··· 97 92 */ 98 93 async disconnectedCallback() { 99 94 super.disconnectedCallback(); 100 - terminateWorkers(await this.#workers); 95 + terminateProvisions(await this.#workers); 101 96 } 102 97 103 98 // ACTIONS
+10 -8
src/components/orchestrator/queue-tracks/element.js
··· 1 1 import { 2 2 callWorkerWithProvisions, 3 3 DiffuseElement, 4 - provisionWorkers, 5 4 query, 6 - terminateWorkers, 5 + terminateProvisions, 7 6 whenElementsDefined, 7 + workerProxy, 8 + workerTunnel, 8 9 } from "@common/element.js"; 9 10 import { untracked } from "@common/signal.js"; 10 - import { workerProxy } from "@common/worker.js"; 11 11 12 12 /** 13 13 * @import {Track} from "@definitions/types.d.ts" ··· 64 64 this.queue = queue; 65 65 66 66 // Create new workers 67 - this.#workers = whenElementsDefined({ input, queue }).then(() => ({ 68 - input: input.createWorker(), 69 - queue: queue.worker(), 70 - })); 67 + this.#workers = whenElementsDefined({ input, queue }).then(() => { 68 + return { 69 + input: input.createWorker(), 70 + queue: queue.worker(), 71 + }; 72 + }); 71 73 72 74 // When defined 73 75 await customElements.whenDefined(this.input.localName); ··· 88 90 */ 89 91 async disconnectedCallback() { 90 92 super.disconnectedCallback(); 91 - terminateWorkers(await this.#workers); 93 + terminateProvisions(await this.#workers); 92 94 } 93 95 94 96 // 🌊
+3 -3
src/components/orchestrator/search-tracks/element.js
··· 3 3 DiffuseElement, 4 4 provisionWorkers, 5 5 query, 6 - terminateWorkers, 6 + terminateProvisions, 7 + workerProxy, 7 8 } from "@common/element.js"; 8 - import { transfer, workerProxy, workerTunnel } from "@common/worker.js"; 9 9 10 10 /** 11 11 * @import {Track} from "@definitions/types.d.ts" ··· 82 82 */ 83 83 async disconnectedCallback() { 84 84 super.disconnectedCallback(); 85 - terminateWorkers(await this.#workers); 85 + terminateProvisions(await this.#workers); 86 86 } 87 87 88 88 // 🚛
+1 -2
src/components/output/polymorphic/indexed-db/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 import { outputManager } from "../../common.js"; 4 3 5 4 /**
+1 -2
src/components/processor/artwork/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 4 3 /** 5 4 * @import {ProxiedActions} from "@common/worker.d.ts"
+1 -2
src/components/processor/metadata/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 4 3 /** 5 4 * @import {ProxiedActions} from "@common/worker.d.ts"
+1 -2
src/components/processor/search/element.js
··· 1 - import { DiffuseElement } from "@common/element.js"; 2 - import { workerProxy } from "@common/worker.js"; 1 + import { DiffuseElement, workerProxy } from "@common/element.js"; 3 2 4 3 /** 5 4 * @import {ProxiedActions} from "@common/worker.d.ts";