Skip to content

Commit

Permalink
refactor: remove context from SharedState#set
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Oct 4, 2024
1 parent 111adcc commit 3e6eb7a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/common/SharedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,7 @@ ${JSON.stringify(initValues, null, 2)}`);
}
}

const oldValues = {};
listener(currentValues, oldValues);
listener(currentValues, {});
}

return () => {
Expand Down
37 changes: 18 additions & 19 deletions src/common/SharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import logger from './logger.js';
* applied to the state.
* @param {Object} oldValues - Key / value pairs of the updated params before
* the updates has been applied to the state.
* @param {Mixed} [context=null] - Optionnal context object that has been passed
* with the values updates in the `set` call.
*/

/**
* Delete the registered {@link sharedStateCollectionOnUpdateCallback}.
*
* @callback sharedStateCollectionDeleteOnUpdateCallback
*/


/**
* The `SharedStateCollection` interface represent a collection of all states
* created from a given class name on the network.
Expand All @@ -23,7 +28,7 @@ import logger from './logger.js';
* ```
* const collection = await client.stateManager.getCollection('my-class');
* const allValues = collection.getValues();
* collection.onUpdate((state, newValues, oldValues, context) => {
* collection.onUpdate((state, newValues, oldValues) => {
* // do something
* });
* ```
Expand Down Expand Up @@ -74,9 +79,9 @@ class SharedStateCollection {
this.#onDetachCallbacks.forEach(callback => callback(state));
});

state.onUpdate((newValues, oldValues, context) => {
state.onUpdate((newValues, oldValues) => {
Array.from(this.#onUpdateCallbacks).forEach(callback => {
callback(state, newValues, oldValues, context);
callback(state, newValues, oldValues);
});
});

Expand Down Expand Up @@ -218,14 +223,11 @@ class SharedStateCollection {
/**
* Update all states of the collection with given values.
* @param {object} updates - key / value pairs of updates to apply to the state.
* @param {mixed} [context=null] - optionnal contextual object that will be propagated
* alongside the updates of the state. The context is valid only for the
* current call and will be passed as third argument to all update listeners.
*/
async set(updates, context = null) {
async set(updates) {
// we can delegate to the state.set(update) method for throwing in case of
// filtered keys, as the Promise.all will reject on first reject Promise
const promises = this.#states.map(state => state.set(updates, context));
const promises = this.#states.map(state => state.set(updates));
return Promise.all(promises);
}

Expand All @@ -235,16 +237,16 @@ class SharedStateCollection {
* @param {sharedStateCollectionOnUpdateCallback}
* callback - Callback to execute when an update is applied on a state.
* @param {Boolean} [executeListener=false] - Execute the callback immediately
* for all underlying states with current state values. (`oldValues` will be
* set to `{}`, and `context` to `null`)
* @returns {Function} - Function that delete the registered listener.
* with current state values. Note that `oldValues` will be set to `{}`.
* @returns {sharedStateCollectionDeleteOnUpdateCallback} - Function that delete
* the registered listener.
*/
onUpdate(callback, executeListener = false) {
this.#onUpdateCallbacks.add(callback);

if (executeListener === true) {
// filter `event: true` parameters from currentValues, this is missleading
// as we are in the context of a callback, not from an active read
// filter `event: true` parameters from currentValues, having them here is
// misleading as we are in the context of a callback, not from an active read
const description = this.getDescription();

this.#states.forEach(state => {
Expand All @@ -256,10 +258,7 @@ class SharedStateCollection {
}
}

const oldValues = {};
const context = null;

callback(state, currentValues, oldValues, context);
callback(state, currentValues, {});
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/states/StateManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ describe(`# StateManager`, () => {
}
});

it('hook API should be `hook(updates, currentValues, context)`', async () => {
it('hook API should be `hook(updates, currentValues)`', async () => {
return new Promise(async (resolve, reject) => {
server.stateManager.defineClass('hooked', hookSchema);
server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => {
Expand Down

0 comments on commit 3e6eb7a

Please sign in to comment.