collective-soundworks / soundworks

Creative coding framework for distributed applications based on Web technologies.
http://soundworks.dev
BSD 3-Clause "New" or "Revised" License
114 stars 7 forks source link

SharedState - add new `set` type #67

Open b-ma opened 1 year ago

b-ma commented 1 year ago

Should allow to maintain a collection of items (i.e. add(item), delete(item)) in a kind of "atomic" fashion.

For now, if we do something like:

collection.onAttach(async state => {
  const item = state.get('item');
  const myList = this.global.get('myList');

  if (myList.indexOf(item) === -1) {
    myList.push(item);
  }

  await this.global.set({ myList });
}, true);

Multiple consecutive calls just can erase the modifications from each others. We could have something like:

collection.onAttach(async state => {
  const item = state.get('item');
  const myList = this.global.get('myList');
  await myList.add(item);
}, true);

Another option would be to use a collection, but where everyone is able to delete a state, not only the owner. Probably that's the most straightforward to implement:

server.stateManager.register('my-list', schema, { enforceOwnership: false });
b-ma commented 1 year ago

Another option would be to use a collection, but where everyone is able to delete a state, not only the owner. Probably that's the most straightforward to implement:

server.stateManager.register('my-list', schema, { shareOwnership: true });

This does not work as expected as when an owner disconnects, all states owned by the node are deleted, which is good. But if everyone is owner, the states are deleted each time a single node disconnect, which is not what we want...

To Be Continued...