Skip to content

Commit

Permalink
style: linting + clean comments
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Jan 6, 2024
1 parent f3aa1ce commit 6fbafa3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/common/BaseStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class BaseStateManager {
try {
await collection._init();
} catch (err) {
console.log(err.message)
console.log(err.message);
throw new Error(`Cannot create collection, schema "${schemaName}" does not exists`);
}

Expand Down
8 changes: 6 additions & 2 deletions src/common/BatchedTransport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { BATCHED_TRANSPORT_CHANNEL } from './constants.js'
import { BATCHED_TRANSPORT_CHANNEL } from './constants.js';

/**
* This class proxies transports given the SharedStateManager to batch messages
* @private
*/
class BatchedTransport {
constructor(transport) {
this._transport = transport;
Expand All @@ -16,7 +20,7 @@ class BatchedTransport {
if (this._listeners.has(channel)) {
const callbacks = this._listeners.get(channel);
callbacks.forEach(callback => callback(...args));
};
}
});
});
}
Expand Down
35 changes: 12 additions & 23 deletions src/common/SharedStatePrivate.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,7 @@ class SharedStatePrivate {
let hasUpdates = false;

for (let name in updates) {
// from v3.1.0 - the `filteredUpdates` check is made using 'fast-deep-equal'
// cf. https://github.com/epoberezkin/fast-deep-equal
// therefore unchanged objects are not considered changed
// nor propagated anymore.
// until v3.0.4 - we checked the `schema[name].type === 'any'`, to always consider
// objects as dirty, because if the state is attached locally, we
// compare the Object instances instead of their values.
// @note - this should be made more robust but how?
const [newValue, changed] = this._parameters.set(name, updates[name]);

// if `filterChange` is set to `false` we don't check if the value
// has been changed or not, it is always propagated to client states
const { event, filterChange } = this._parameters.getSchema(name);
Expand All @@ -96,33 +87,31 @@ class SharedStatePrivate {
}

if (hasUpdates) {
// Collection Controller logic

// Collection controller logic
if (isOwner && inCollection) {
// notify the requester back
// no need to check for attached clients as it is not observable
// notify the collection controller back
client.transport.emit(
`${UPDATE_RESPONSE}-${this.id}-${remoteId}`,
reqId, filteredUpdates, context
reqId, filteredUpdates, context,
);

// loop through all private states
for (let [stateId, state] of this._manager._sharedStatePrivateById) {
for (let [_stateId, state] of this._manager._sharedStatePrivateById) {
// pick all states with same schema name and not this
if (state !== this && this.schemaName === state.schemaName) {
// notify all attached clients except those who belong to a collection
// i.e. !isOwner && inCollection, they will be notified by their own
// collection controller
for (let [remoteId, clientInfos] of state._attachedClients) {
const { client, isOwner, inCollection } = clientInfos;

if (!clientInfos.isOwner && clientInfos.inCollection) {
continue;
} else {
const peer = clientInfos.client;

peer.transport.emit(
`${UPDATE_NOTIFICATION}-${state.id}-${remoteId}`,
filteredUpdates, context
filteredUpdates, context,
);
}
}
Expand All @@ -131,7 +120,7 @@ class SharedStatePrivate {
} else {

// Normal case

//
// We need to handle cases where:
// - client state (client.id: 2) sends a request
// - server attached state (client.id: -1) spot a problem and overrides the value
Expand All @@ -146,14 +135,14 @@ class SharedStatePrivate {
// is synchronous it can break ordering if a subscription function makes
// itself an update in reaction to an update. Propagating to server last
// alllows to maintain network messages order consistent.

//
// @note - remoteId correspond to unique remote state id

// propagate RESPONSE to the client that originates the request if not the server
if (client.id !== -1) {
client.transport.emit(
`${UPDATE_RESPONSE}-${this.id}-${remoteId}`,
reqId, filteredUpdates, context
reqId, filteredUpdates, context,
);
}

Expand All @@ -164,7 +153,7 @@ class SharedStatePrivate {
if (remoteId !== peerRemoteId && peer.id !== -1) {
peer.transport.emit(
`${UPDATE_NOTIFICATION}-${this.id}-${peerRemoteId}`,
filteredUpdates, context
filteredUpdates, context,
);
}
}
Expand All @@ -173,7 +162,7 @@ class SharedStatePrivate {
if (client.id === -1) {
client.transport.emit(
`${UPDATE_RESPONSE}-${this.id}-${remoteId}`,
reqId, filteredUpdates, context
reqId, filteredUpdates, context,
);
}

Expand All @@ -184,7 +173,7 @@ class SharedStatePrivate {
if (remoteId !== peerRemoteId && peer.id === -1) {
peer.transport.emit(
`${UPDATE_NOTIFICATION}-${this.id}-${peerRemoteId}`,
filteredUpdates, context
filteredUpdates, context,
);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/server/Sockets.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Worker } from 'node:worker_threads';
import path from 'node:path';
import querystring from 'querystring';
import { default as WebSocket, WebSocketServer } from 'ws';

import Socket from './Socket.js';
import networkLatencyWorker from './audit-network-latency.worker.js';

// this crashes when bundling server to cjs module for Max, fallback to
// string worker for now
// this crashes when bundling server to cjs module for Max, fallback to cjs worker
// const __dirname = fileURLToPath(new URL('.', import.meta.url));

/**
Expand Down
10 changes: 5 additions & 5 deletions src/server/StateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class StateManager extends BaseStateManager {
const remoteId = generateRemoteId.next().value;
const state = new SharedStatePrivate(stateId, schemaName, schema, this, initValues);

// attach client to the state as owner
// attach client to the state as owner
const isOwner = true;
state._attachClient(remoteId, client, isOwner, inCollection);

Expand All @@ -374,7 +374,7 @@ class StateManager extends BaseStateManager {

client.transport.emit(
CREATE_RESPONSE,
reqId, stateId, remoteId, schemaName, schemaOption, currentValues
reqId, stateId, remoteId, schemaName, schemaOption, currentValues,
);

const isObservable = this[kIsObservableState](state);
Expand All @@ -396,7 +396,7 @@ class StateManager extends BaseStateManager {

client.transport.emit(CREATE_ERROR, reqId, msg);
}
}
},
);

// ---------------------------------------------
Expand Down Expand Up @@ -437,7 +437,7 @@ class StateManager extends BaseStateManager {

client.transport.emit(
ATTACH_RESPONSE,
reqId, state.id, remoteId, schemaName, schemaOption, currentValues
reqId, state.id, remoteId, schemaName, schemaOption, currentValues,
);

} else {
Expand All @@ -452,7 +452,7 @@ class StateManager extends BaseStateManager {

client.transport.emit(ATTACH_ERROR, reqId, msg);
}
}
},
);

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

0 comments on commit 6fbafa3

Please sign in to comment.