share / share/sharedb

Inconsistent order of events

Open
#233 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
6.5k
Forks
456
Avg merge
1d 5h
Merged PRs (30d)
5

Description

Currently many events are emitted synchronously in response to local function calls, for example submitOp emits an op event synchronously. This leads to some unexpected behaviours, for example:

doc.on((op, source) => { if (source === '1') submitOp(op1, '2') })
doc.on((op, source) => console.log(source))
doc.submitOp(op1, '1')

prints:

2
1

instead of:

1
2

which reflects the actual order of operations.

Here's what happens in the example above:

  1. op1 is submitted.
  2. An 'op' event is emitted with source '1'.
  3. The first handler is called with source '1' and submits op2.
  4. An 'op' event is emitted with source '2'.
  5. The first handler is called with source '2' and exits.
  6. The second handler is called with source '2' and prints it.
  7. The second handler is called with source '1' and prints it.

In order to deal with this issue perhaps we could use an approach similar to MutationObserver, where synchronous operations are recorded in an Array and then a single event is emitted asynchronously with the list as a parameter?

Related issues:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing submitOp and the synchronous 'op' event handlers in the example, then read the linked issue 206 and pull request 129 alongside the MutationObserver reference. The work is done only when the intended event and callback ordering is agreed and the behavior is consistently defined for nested submissions.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.