share / share/sharedb

Emit `'changed'` on query `'ready'`?

Open
#462 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

At the moment, if you want to respond to changes to a Query, you have to listen for both the 'ready' and 'changed' events. Current usage looks like:

const query = connection.createSubscribeQuery(...)
query.on('ready', () => {
  // query.results were set for the first time
});
query.on('changed', () => {
  // query.results were updated
});

Sometimes we don't care about whether the results were set for the first time or not, and we just want to react to whenever any changes happen. For example, if you want to keep all of your query docs subscribed, you have to do something like:

function subscribe(docs) {
  docs.forEach((doc) => doc.subscribe());
}

query.on('ready', () => subscribe(query.results));
query.on('changed', () => subscribe(query.results));

It would be nice if we could simplify this to a single listener:

query.on('changed', () => {
  query.results.forEach((doc) => doc.subscribe());
});

We could achieve this by:

  • emitting 'changed' at the same time as 'ready'; or
  • introducing a new event, which fires any time query.results is touched (something like 'updated'?)

I think I would personally prefer to emit 'changed' at the same time as 'ready', just to reduce the confusion around what 'changed' vs 'updated' means, although this would be a breaking change.

That being said, there may also be cases where consumers care about only running after the first set? Could potentially be handled by firing 'changed' just before 'ready' (so consumers can check query.ready)?

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 at the Query event lifecycle used by connection.createSubscribeQuery and trace where 'ready' and 'changed' are emitted. Review the three existing comments and consumer examples before choosing whether the first result should also emit 'changed'. Done means the event semantics are decided, documented, and covered by tests without leaving the breaking-change behavior ambiguous.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.