Skip to content

Commit

Permalink
refactor: rename SharedState#remoteID to more meaningful `SharedSta…
Browse files Browse the repository at this point in the history
…te#instanceId`
  • Loading branch information
b-ma committed Oct 4, 2024
1 parent 7d27345 commit d13df50
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"postversion": "node ./misc/scripts/check-changelog.js && git commit -am \"$npm_new_version\" --allow-empty"
},
"dependencies": {
"@ircam/sc-utils": "^1.3.3",
"@ircam/sc-utils": "^1.8.0",
"chalk": "^5.3.0",
"columnify": "^1.6.0",
"compression": "^1.7.1",
Expand Down
8 changes: 4 additions & 4 deletions src/common/BaseStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BaseStateManager {
// ---------------------------------------------
this[kStateManagerClient].transport.addListener(
CREATE_RESPONSE,
(reqId, stateId, remoteId, className, classDescription, initValues) => {
(reqId, stateId, instanceId, className, classDescription, initValues) => {
// cache class description to save some bandwidth
// @note: when we make the class dynamic, we will need some mecanism to
// invalidate the cached description
Expand All @@ -112,7 +112,7 @@ class BaseStateManager {
className,
classDescription,
stateId,
remoteId,
instanceId,
isOwner: true,
initValues,
filter: null, // owner cannot filter paramters
Expand All @@ -132,7 +132,7 @@ class BaseStateManager {
// ---------------------------------------------
this[kStateManagerClient].transport.addListener(
ATTACH_RESPONSE,
(reqId, stateId, remoteId, className, classDescription, currentValues, filter) => {
(reqId, stateId, instanceId, className, classDescription, currentValues, filter) => {
// cache class description to save some bandwidth
// @note: when we make the class dynamic, we will need some mecanism to
// invalidate the cached description
Expand All @@ -147,7 +147,7 @@ class BaseStateManager {
className,
classDescription,
stateId,
remoteId,
instanceId,
isOwner: false,
initValues: currentValues,
filter,
Expand Down
40 changes: 20 additions & 20 deletions src/common/SharedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const kSharedStatePromiseStore = Symbol('soundworks:shared-state-promise-
*/
class SharedState {
#id = null;
#remoteId = null;
#instanceId = null;
#className = null;
#isOwner = null;
#client = null;
Expand All @@ -115,7 +115,7 @@ class SharedState {

constructor({
stateId,
remoteId,
instanceId,
className,
classDescription,
isOwner,
Expand All @@ -127,7 +127,7 @@ class SharedState {
this.#client = manager[kStateManagerClient];
this.#className = className;
this.#id = stateId;
this.#remoteId = remoteId;
this.#instanceId = instanceId;
this.#isOwner = isOwner; // may be any node
this.#filter = filter;

Expand All @@ -144,18 +144,18 @@ ${JSON.stringify(initValues, null, 2)}`);
this[kSharedStatePromiseStore] = new PromiseStore(this.constructor.name);

// add listener for state updates
this.#client.transport.addListener(`${UPDATE_RESPONSE}-${this.#id}-${this.#remoteId}`, async (reqId, updates) => {
this.#client.transport.addListener(`${UPDATE_RESPONSE}-${this.#id}-${this.#instanceId}`, async (reqId, updates) => {
const updated = await this.#commit(updates, true, true);
this[kSharedStatePromiseStore].resolve(reqId, updated);
});

// retrieve values but do not propagate to subscriptions
this.#client.transport.addListener(`${UPDATE_ABORT}-${this.#id}-${this.#remoteId}`, async (reqId, updates) => {
this.#client.transport.addListener(`${UPDATE_ABORT}-${this.#id}-${this.#instanceId}`, async (reqId, updates) => {
const updated = await this.#commit(updates, false, true);
this[kSharedStatePromiseStore].resolve(reqId, updated);
});

this.#client.transport.addListener(`${UPDATE_NOTIFICATION}-${this.#id}-${this.#remoteId}`, async (updates) => {
this.#client.transport.addListener(`${UPDATE_NOTIFICATION}-${this.#id}-${this.#instanceId}`, async (updates) => {
// https://github.com/collective-soundworks/soundworks/issues/18
//
// # note: 2002-10-03
Expand Down Expand Up @@ -184,7 +184,7 @@ ${JSON.stringify(initValues, null, 2)}`);
// ---------------------------------------------
// state has been deleted by its creator or the class has been deleted
// ---------------------------------------------
this.#client.transport.addListener(`${DELETE_NOTIFICATION}-${this.#id}-${this.#remoteId}`, async () => {
this.#client.transport.addListener(`${DELETE_NOTIFICATION}-${this.#id}-${this.#instanceId}`, async () => {
this.#manager[kStateManagerDeleteState](this.#id);
this.#clearTransport();

Expand Down Expand Up @@ -212,7 +212,7 @@ ${JSON.stringify(initValues, null, 2)}`);
// ---------------------------------------------
// the creator has called `.delete()`
// ---------------------------------------------
this.#client.transport.addListener(`${DELETE_RESPONSE}-${this.#id}-${this.#remoteId}`, async (reqId) => {
this.#client.transport.addListener(`${DELETE_RESPONSE}-${this.#id}-${this.#instanceId}`, async (reqId) => {
this.#manager[kStateManagerDeleteState](this.#id);
this.#clearTransport();

Expand All @@ -238,7 +238,7 @@ ${JSON.stringify(initValues, null, 2)}`);
// ---------------------------------------------
// the attached node has called `.detach()`
// ---------------------------------------------
this.#client.transport.addListener(`${DETACH_RESPONSE}-${this.#id}-${this.#remoteId}`, async (reqId) => {
this.#client.transport.addListener(`${DETACH_RESPONSE}-${this.#id}-${this.#instanceId}`, async (reqId) => {
this.#manager[kStateManagerDeleteState](this.#id);
this.#clearTransport();

Expand Down Expand Up @@ -296,17 +296,17 @@ ${JSON.stringify(initValues, null, 2)}`);

#clearTransport() {
// remove listeners
this.#client.transport.removeAllListeners(`${UPDATE_RESPONSE}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${UPDATE_NOTIFICATION}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${UPDATE_ABORT}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${DELETE_NOTIFICATION}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${UPDATE_RESPONSE}-${this.#id}-${this.#instanceId}`);
this.#client.transport.removeAllListeners(`${UPDATE_NOTIFICATION}-${this.#id}-${this.#instanceId}`);
this.#client.transport.removeAllListeners(`${UPDATE_ABORT}-${this.#id}-${this.#instanceId}`);
this.#client.transport.removeAllListeners(`${DELETE_NOTIFICATION}-${this.#id}-${this.#instanceId}`);

if (this.#isOwner) {
this.#client.transport.removeAllListeners(`${DELETE_RESPONSE}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${DELETE_ERROR}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${DELETE_RESPONSE}-${this.#id}-${this.#instanceId}`);
this.#client.transport.removeAllListeners(`${DELETE_ERROR}-${this.#id}-${this.#instanceId}`);
} else {
this.#client.transport.removeAllListeners(`${DETACH_RESPONSE}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${DETACH_ERROR}-${this.#id}-${this.#remoteId}`);
this.#client.transport.removeAllListeners(`${DETACH_RESPONSE}-${this.#id}-${this.#instanceId}`);
this.#client.transport.removeAllListeners(`${DETACH_ERROR}-${this.#id}-${this.#instanceId}`);
}
}

Expand Down Expand Up @@ -554,7 +554,7 @@ ${JSON.stringify(initValues, null, 2)}`);
// go through server-side normal behavior
return new Promise((resolve, reject) => {
const reqId = this[kSharedStatePromiseStore].add(resolve, reject, 'SharedState#set', forwardParams);
this.#client.transport.emit(`${UPDATE_REQUEST}-${this.#id}-${this.#remoteId}`, reqId, updates);
this.#client.transport.emit(`${UPDATE_REQUEST}-${this.#id}-${this.#instanceId}`, reqId, updates);
});
}

Expand Down Expand Up @@ -709,12 +709,12 @@ ${JSON.stringify(initValues, null, 2)}`);
if (this.#isOwner) {
return new Promise((resolve, reject) => {
const reqId = this[kSharedStatePromiseStore].add(resolve, reject, 'SharedState#delete');
this.#client.transport.emit(`${DELETE_REQUEST}-${this.#id}-${this.#remoteId}`, reqId);
this.#client.transport.emit(`${DELETE_REQUEST}-${this.#id}-${this.#instanceId}`, reqId);
});
} else {
return new Promise((resolve, reject) => {
const reqId = this[kSharedStatePromiseStore].add(resolve, reject, 'SharedState#detach');
this.#client.transport.emit(`${DETACH_REQUEST}-${this.#id}-${this.#remoteId}`, reqId);
this.#client.transport.emit(`${DETACH_REQUEST}-${this.#id}-${this.#instanceId}`, reqId);
});
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/server/ServerStateManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { idGenerator, isString, isPlainObject } from '@ircam/sc-utils';
import { counter, isString, isPlainObject } from '@ircam/sc-utils';
import clonedeep from 'lodash/cloneDeep.js';

import BaseStateManager, {
Expand Down Expand Up @@ -34,8 +34,8 @@ import SharedStatePrivate, {
import logger from '../common/logger.js';


const generateStateId = idGenerator();
const generateRemoteId = idGenerator();
const generateStateId = counter();
const instanceIdGenerator = counter();

export const kServerStateManagerAddClient = Symbol('soundworks:server-state-manager-add-client');
export const kServerStateManagerRemoveClient = Symbol('soundworks:server-state-manager-remove-client');
Expand Down Expand Up @@ -174,14 +174,14 @@ class ServerStateManager extends BaseStateManager {
if (this.#classes.has(className)) {
try {
const classDescription = this.#classes.get(className);
const stateId = generateStateId.next().value;
const remoteId = generateRemoteId.next().value;
const stateId = generateStateId();
const instanceId = instanceIdGenerator();
const state = new SharedStatePrivate(this, className, classDescription, stateId, initValues);

// attach client to the state as owner
const isOwner = true;
const filter = null;
state[kSharedStatePrivateAttachClient](remoteId, client, isOwner, filter);
state[kSharedStatePrivateAttachClient](instanceId, client, isOwner, filter);

this.#sharedStatePrivateById.set(stateId, state);

Expand All @@ -192,7 +192,7 @@ class ServerStateManager extends BaseStateManager {
CREATE_RESPONSE,
reqId,
stateId,
remoteId,
instanceId,
className,
classDescriptionOption,
currentValues,
Expand Down Expand Up @@ -244,7 +244,7 @@ class ServerStateManager extends BaseStateManager {
// @note - we use a unique remote id to allow a client to attach
// several times to the same state.
// i.e. same state -> several remote attach on the same node
const remoteId = generateRemoteId.next().value;
const instanceId = instanceIdGenerator();
const isOwner = false;
const currentValues = state.parameters.getValues();
const classDescription = this.#classes.get(className);
Expand All @@ -262,13 +262,13 @@ class ServerStateManager extends BaseStateManager {
}
}

state[kSharedStatePrivateAttachClient](remoteId, client, isOwner, filter);
state[kSharedStatePrivateAttachClient](instanceId, client, isOwner, filter);

client.transport.emit(
ATTACH_RESPONSE,
reqId,
state.id,
remoteId,
instanceId,
className,
classDescriptionOption,
currentValues,
Expand Down Expand Up @@ -352,25 +352,25 @@ class ServerStateManager extends BaseStateManager {

// define if the client is the creator of the state, in which case
// everybody must delete it
for (let [remoteId, clientInfos] of state.attachedClients) {
for (let [instanceId, clientInfos] of state.attachedClients) {
const attachedClient = clientInfos.client;

if (nodeId === attachedClient.id && remoteId === state.creatorRemoteId) {
if (nodeId === attachedClient.id && instanceId === state.creatorInstanceId) {
deleteState = true;
}
}

for (let [remoteId, clientInfos] of state.attachedClients) {
for (let [instanceId, clientInfos] of state.attachedClients) {
const attachedClient = clientInfos.client;

if (nodeId === attachedClient.id) {
state[kSharedStatePrivateDetachClient](remoteId, attachedClient);
state[kSharedStatePrivateDetachClient](instanceId, attachedClient);
}

if (deleteState) {
if (remoteId !== state.creatorRemoteId) {
if (instanceId !== state.creatorinstanceId) {
// send notification to other attached nodes
attachedClient.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${remoteId}`);
attachedClient.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${instanceId}`);
}

this.#sharedStatePrivateById.delete(state.id);
Expand Down Expand Up @@ -452,10 +452,10 @@ class ServerStateManager extends BaseStateManager {
// @note: deleting schema
for (let [_, state] of this.#sharedStatePrivateById) {
if (state.className === className) {
for (let [remoteId, clientInfos] of state.attachedClients) {
for (let [instanceId, clientInfos] of state.attachedClients) {
const attached = clientInfos.client;
state[kSharedStatePrivateDetachClient](remoteId, attached);
attached.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${remoteId}`);
state[kSharedStatePrivateDetachClient](instanceId, attached);
attached.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${instanceId}`);
}

this.#sharedStatePrivateById.delete(state.id);
Expand Down
Loading

0 comments on commit d13df50

Please sign in to comment.