spark.createWorkerRpc
function createWorkerRpc(worker: Worker): RpcCreate an RPC instance for communicating with a Web Worker.
Convenience function that creates an RPC with appropriate defaults for worker communication (always uses origin '*' since workers don't support origin validation).
Equivalent to: new Rpc({ target: worker, origin: '*' })
Parameters
workerWorker- Web Worker instance to communicate with
Returns
Rpc– RPC instance ready to use with the workerExamples
// Create worker and RPC
const worker = new Worker('my-worker.js');
const rpc = createWorkerRpc(worker);
// Register handlers for worker messages
rpc.handle('worker.status', (status) => {
console.log('Worker status:', status);
});
// Call worker methods
const result = await rpc.call('train', { epochs: 100 });
// Clean up
rpc.destroy();
worker.terminate();See Also
- createParentRpc - Create RPC to parent window
- createSelfRpc - Create RPC in worker context