Skip to content

Commit

Permalink
fix(SharedStateCollection): do not propagate event parameters on firs…
Browse files Browse the repository at this point in the history
…t onUpdate call when executeListener is true
  • Loading branch information
b-ma committed Oct 1, 2024
1 parent 7e372fa commit f1a7b44
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/common/SharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,19 @@ class SharedStateCollection {
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
const schema = this.getSchema();

this.#states.forEach(state => {
const currentValues = state.getValues();

for (let name in schema) {
if (schema[name].event === true) {
delete currentValues[name];
}
}

const oldValues = {};
const context = null;

Expand Down
23 changes: 23 additions & 0 deletions tests/states/StateCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,29 @@ describe(`# SharedStateCollection`, () => {
await state.delete();
await delay(50);
});

it('should not propagate event parameters on first call if `executeListener=true`', async () => {
server.stateManager.registerSchema('with-event', {
bool: { type: 'boolean', event: true, },
int: { type: 'integer', default: 20, },
});
const state = await server.stateManager.create('with-event');
const collection = await server.stateManager.getCollection('with-event');

let onUpdateCalled = false;
collection.onUpdate((state, newValues, oldValues, context) => {
onUpdateCalled = true;
assert.deepEqual(newValues, { int: 20 });
assert.deepEqual(oldValues, {});
assert.deepEqual(context, null);
}, true);

await delay(10);

assert.equal(onUpdateCalled, true);
server.stateManager.deleteSchema('with-event');
});

});

describe(`## onAttach(callback)`, () => {
Expand Down

0 comments on commit f1a7b44

Please sign in to comment.