Skip to content

Commit

Permalink
fix: state collection synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 15, 2023
1 parent 9d04dbc commit e29cc8f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/common/BaseSharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class BaseSharedStateCollection {

async _init() {
this._controller = await this._stateManager[kCreateCollectionController](this._schemaName);
this._controller.onUpdate(async (updates, context) => {
for (let state of this._states) {
await state._commit(updates, context, true, false);
}
});

this._unobserve = await this._stateManager.observe(this._schemaName, async (schemaName, stateId) => {
const state = await this._stateManager[kAttachInCollection](schemaName, stateId);
Expand Down Expand Up @@ -122,12 +127,7 @@ class BaseSharedStateCollection {
* current call and will be passed as third argument to all update listeners.
*/
async set(updates, context = null) {
const updatesResult = await this._controller.set(updates, context);
const promises = this._states.map(state => {
return state._commit(updatesResult, context, true, false);
});

return Promise.all(promises);
return await this._controller.set(updates, context);
}

/**
Expand Down
47 changes: 43 additions & 4 deletions tests/states/StateCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ describe(`# SharedStateCollection`, () => {

assert.equal(collection.size, 2);

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

await delay(50);
// should be propagated to everyone
Expand All @@ -245,7 +245,46 @@ describe(`# SharedStateCollection`, () => {
await collection.detach();
});

it.skip(`test several collections from same schema`, async () => {});
it(`test several collections from same schema`, async () => {
const state0 = await clients[0].stateManager.create('a');
const state1 = await clients[1].stateManager.create('a');
// cross attached states
const attached0 = await clients[1].stateManager.attach('a', state0.id);
const attached1 = await clients[0].stateManager.attach('a', state1.id);

const collection0 = await clients[2].stateManager.getCollection('a');
const collection1 = await clients[0].stateManager.getCollection('a');

assert.equal(collection0.size, 2);
assert.equal(collection1.size, 2);

// update from collection0
await collection0.set({ bool: true });
await delay(50);

assert.equal(state0.get('bool'), true);
assert.equal(state1.get('bool'), true);
assert.equal(attached0.get('bool'), true);
assert.equal(attached1.get('bool'), true);
assert.deepEqual(collection0.get('bool'), [true, true]);
assert.deepEqual(collection1.get('bool'), [true, true]);

await collection0.set({ int: 42 });
await delay(50);

assert.equal(state0.get('int'), 42);
assert.equal(state1.get('int'), 42);
assert.equal(attached0.get('int'), 42);
assert.equal(attached1.get('int'), 42);
assert.deepEqual(collection0.get('int'), [42, 42]);
assert.deepEqual(collection1.get('int'), [42, 42]);

await state0.delete();
await state1.delete();
await collection0.detach();
await collection1.detach();
});

it.skip(`test socket message numbers`, async () => {});

it(`"normal" state communication should work as expected`, async () => {
Expand Down

0 comments on commit e29cc8f

Please sign in to comment.