From 5054a6ab551438b9cd9764e7d8de6abf7ba1523c Mon Sep 17 00:00:00 2001 From: b-ma Date: Sat, 6 Jan 2024 11:07:39 +0100 Subject: [PATCH] fix: separation of concerns --- src/common/BatchedTransport.js | 12 +++++++----- src/server/StateManager.js | 8 ++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/common/BatchedTransport.js b/src/common/BatchedTransport.js index d18cc2fb..f563ec08 100644 --- a/src/common/BatchedTransport.js +++ b/src/common/BatchedTransport.js @@ -10,11 +10,13 @@ class BatchedTransport { this._transport.addListener(BATCHED_TRANSPORT_CHANNEL, stack => { stack.forEach(entry => { const [channel, args] = entry; - const callbacks = this._listeners.get(channel); - - callbacks.forEach(callback => { - callback(...args); - }); + // server side the transport is the same EventEmitter instance + // for both state manager server and client, so channel might not exist + // one side or the other. + if (this._listeners.has(channel)) { + const callbacks = this._listeners.get(channel); + callbacks.forEach(callback => callback(...args)); + }; }); }); } diff --git a/src/server/StateManager.js b/src/server/StateManager.js index 27055ba9..a851b902 100644 --- a/src/server/StateManager.js +++ b/src/server/StateManager.js @@ -323,7 +323,7 @@ class StateManager extends BaseStateManager { this._observers = new Set(); this._hooksBySchemaName = new Map(); // protected - this.addClient(localClientId, this.client.transport); + this.addClient(localClientId, localTransport); } [kIsObservableState](state) { @@ -345,11 +345,7 @@ class StateManager extends BaseStateManager { * @private */ addClient(nodeId, transport) { - // server adds itself as client, and its transport is a raw EventEmitter - // so we don't want to proxy it twice with BatchedTransport. - if (nodeId !== SERVER_ID) { - transport = new BatchedTransport(transport); - } + transport = new BatchedTransport(transport); const client = { id: nodeId, transport }; this._clientByNodeId.set(nodeId, client);