Skip to content

Commit

Permalink
feat(shared-state): remove batch transport time, introduce undesirabl…
Browse files Browse the repository at this point in the history
…e side effects
  • Loading branch information
b-ma committed Apr 26, 2024
1 parent f419e9f commit 8ff67a9
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 258 deletions.
32 changes: 1 addition & 31 deletions src/common/BaseStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,6 @@ class BaseStateManager {
this._observeRequestCallbacks = new Map(); // Map <reqId, [observedSchemaName, callback, options]>

this._promiseStore = new PromiseStore();

this._options = {
transportBatchTimeout: 0,
};
}

/**
* Configure
*
*/
configure(options) {
if (!(typeof options === 'object') || options === null) {
throw new TypeError(`Cannot execute 'configure' on 'BaseStateManager': given option is not a valid options object`);
}

if (options.transportBatchTimeout !== undefined) {
if (!Number.isFinite(options.transportBatchTimeout) || options.transportBatchTimeout < 0) {
throw new TypeError(`Failed to execute 'configure' on 'BaseStateManager': The provided option 'transportBatchTimeout' must be equal to or greater than 0`);
}

if (options.transportBatchTimeout !== 0 && options.transportBatchTimeout < 1) {
options.transportBatchTimeout = 1;
console.warn(`Warning: Executing 'configure' on 'BaseStateManager': The provided option 'transportBatchTimeout' has been clamped to 1, make sure the given 'transportBatchTimeout' is expressed in milliseconds`);
}

this._options.transportBatchTimeout = options.transportBatchTimeout;
}
}

/**
Expand All @@ -70,10 +43,7 @@ class BaseStateManager {
* Must implement a basic EventEmitter API.
*/
init(id, transport) {
const batchedTransport = new BatchedTransport(transport, {
transportBatchTimeout: this._options.transportBatchTimeout,
});

const batchedTransport = new BatchedTransport(transport);
this.client = { id, transport: batchedTransport };

// ---------------------------------------------
Expand Down
17 changes: 2 additions & 15 deletions src/common/BatchedTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ import { BATCHED_TRANSPORT_CHANNEL } from './constants.js';
* @private
*/
class BatchedTransport {
constructor(transport, {
transportBatchTimeout = 0, // in ms
} = {}) {
if (!Number.isFinite(transportBatchTimeout) || transportBatchTimeout < 0) {
throw new TypeError(`Failed to construct BatchedTransport: The provided option 'transportBatchTimeout' must equal to or greater than 0`);
}

constructor(transport) {
this._transport = transport;
this._listeners = new Map();
this._stack = [];
this._transportBatchTimeout = transportBatchTimeout;
this._sending = false;

this._transport.addListener(BATCHED_TRANSPORT_CHANNEL, stack => {
Expand Down Expand Up @@ -52,13 +45,7 @@ class BatchedTransport {

if (!this._sending) {
this._sending = true;

if (this._transportBatchTimeout === 0) {
await false;
} else {
await delay(this._transportBatchTimeout);
}

await false;
this._sending = false;
const stack = this._stack;
this._stack = [];
Expand Down
2 changes: 0 additions & 2 deletions src/common/PromiseStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ export default class PromiseStore {

// reject all pending request
flush() {
console.log('flush called');
for (let [_reqId, entry] of this.store) {
const { reject, type } = entry;
console.log('reject err');
reject(new Error(`[${this.name}] Discard promise "${type}", cannot resolve`));
}

Expand Down
9 changes: 5 additions & 4 deletions src/server/StateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,12 @@ class StateManager extends BaseStateManager {
* @private
*/
addClient(nodeId, transport) {
transport = new BatchedTransport(transport, {
transportBatchTimeout: this._options.transportBatchTimeout,
});
const batchedTransport = new BatchedTransport(transport);
const client = {
id: nodeId,
transport: batchedTransport,
};

const client = { id: nodeId, transport };
this._clientByNodeId.set(nodeId, client);

// ---------------------------------------------
Expand Down
Loading

0 comments on commit 8ff67a9

Please sign in to comment.