Skip to content

Commit

Permalink
feat: add set method on state collections
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Aug 11, 2023
1 parent b78bc8c commit 435c8db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/common/BaseSharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ class BaseSharedStateCollection {
return this._states.map(state => state.get(name));
}

/**
* 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) {
const promises = this._states.map(state => state.set(updates, context));
return Promise.all(promises);
}

/**
* Subscribe to any state update of the collection.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/common.state-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,18 @@ describe(`common::StateManager`, () => {
state.delete();
});

it.only(`collection.set(updates, context = null)`, async () => {
const state = await client.stateManager.create('a');
const collection = await clients[1].stateManager.getCollection('a');

const results = await collection.set({ bool: true });
const expected = [ { bool: true } ];

assert.deepEqual(expected, results);

state.delete();
});

it(`collection.onUpdate(callback)`, async () => {
const state = await client.stateManager.create('a');

Expand Down

0 comments on commit 435c8db

Please sign in to comment.