From b2be15809aa08f24280bea69384cb4952dcb0107 Mon Sep 17 00:00:00 2001 From: Tobias Ortmayr Date: Fri, 3 Jun 2022 15:44:43 +0200 Subject: [PATCH] Address review comments --- .../src/common/message-rpc/rpc-protocol.ts | 4 ++-- .../src/hosted/browser/plugin-worker.ts | 23 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/core/src/common/message-rpc/rpc-protocol.ts b/packages/core/src/common/message-rpc/rpc-protocol.ts index 059be439d97bd..c70c3fa5af5af 100644 --- a/packages/core/src/common/message-rpc/rpc-protocol.ts +++ b/packages/core/src/common/message-rpc/rpc-protocol.ts @@ -48,11 +48,11 @@ export interface RpcProtocolOptions { /** * Establish a RPC protocol on top of a given channel. By default the rpc protocol is bi-directional, meaning it is possible to send - * sends requests and notifications to the remote side (ie. acts as client) as well as receiving requests and notifications from the remote side (i.e. acts as a server). + * requests and notifications to the remote side (i.e. acts as client) as well as receiving requests and notifications from the remote side (i.e. acts as a server). * Clients can get a promise for a remote request result that will be either resolved or * rejected depending on the success of the request. Keeps track of outstanding requests and matches replies to the appropriate request * Currently, there is no timeout handling for long running requests implemented. - * The bi-directional mode can be reconfigured using the {@link RpcProtocolOptions} to construct and RPC protocol instance that acts only as client or server instead. + * The bi-directional mode can be reconfigured using the {@link RpcProtocolOptions} to construct an RPC protocol instance that acts only as client or server instead. */ export class RpcProtocol { static readonly CANCELLATION_TOKEN_KEY = 'add.cancellation.token'; diff --git a/packages/plugin-ext/src/hosted/browser/plugin-worker.ts b/packages/plugin-ext/src/hosted/browser/plugin-worker.ts index 98c222f32a432..5d4f12f91e830 100644 --- a/packages/plugin-ext/src/hosted/browser/plugin-worker.ts +++ b/packages/plugin-ext/src/hosted/browser/plugin-worker.ts @@ -27,28 +27,15 @@ export class PluginWorker { public readonly rpc: RPCProtocol; constructor() { - const emitter = new Emitter(); - this.worker = new Worker(new URL('./worker/worker-main', // @ts-expect-error (TS1343) // We compile to CommonJS but `import.meta` is still available in the browser import.meta.url)); - this.worker.onmessage = m => emitter.fire(m.data); - this.worker.onerror = e => console.error(e); - const onCloseEmitter = new Emitter(); const onMessageEmitter = new Emitter(); const onErrorEmitter = new Emitter(); - // eslint-disable-next-line arrow-body-style - this.worker.onmessage = buffer => onMessageEmitter.fire(() => { - return new Uint8ArrayReadBuffer(buffer.data); - }); - - this.worker.onerror = e => onErrorEmitter.fire(e); - onErrorEmitter.event(e => console.error(e)); - this.rpc = new RPCProtocolImpl({ close: () => { onCloseEmitter.dispose(); @@ -66,6 +53,16 @@ export class PluginWorker { onError: onErrorEmitter.event, onMessage: onMessageEmitter.event }); + + // eslint-disable-next-line arrow-body-style + this.worker.onmessage = buffer => onMessageEmitter.fire(() => { + return new Uint8ArrayReadBuffer(buffer.data); + }); + + this.worker.onmessage = m => onMessageEmitter.fire(m.data); + this.worker.onerror = e => onErrorEmitter.fire(e); + + this.worker.onerror = e => console.error(e); } }