/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { IoThread } from "./IoThread.js";
/**
 * Base class for ShareWorker threads.
 */
export class SharedWorkerThread extends IoThread {
    constructor() {
        super(...arguments);
        this.ports = [];
    }
    async initialize() {
        globalThis.addEventListener('connect', this.connectHandler.bind(this));
    }
    connectHandler(e) {
        const port = e.ports[0];
        this.ports.push(port);
        port.addEventListener('message', this.messageHandler.bind(this));
        port.start();
        port.postMessage('ready');
    }
    postMessage(message) {
        this.ports.forEach(p => p.postMessage(message));
    }
    messageHandler(e) {
        if (e.data === 'init') {
            e.ports.forEach(p => p.postMessage('ready'));
            return;
        }
        const event = e.data;
        this.handleMessage(event);
    }
}
//# sourceMappingURL=SharedWorkerThread.js.map