Skip to content

Emit 'changed' on query 'ready'? #462

@alecgibson

Description

@alecgibson
Collaborator

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)?

Activity

added
breaking-changeBreaking change that we may want to pull into a future major version
on Apr 15, 2021
pypmannetjies

pypmannetjies commented on Apr 15, 2021

@pypmannetjies
Contributor

Makes sense... I think a new query name would be best, as it will be confusing even if it is a breaking change that the behaviour has changed subtly.

Perhaps we can deprecate changed so that people know which one to use from now on 🤔

alecgibson

alecgibson commented on Apr 28, 2021

@alecgibson
CollaboratorAuthor

As discussed in #468 turns out that docs that are part of queries are always subscribed anyway, so this use case is less relevant, I guess.

ericyhwang

ericyhwang commented on Apr 28, 2021

@ericyhwang
Contributor

To elaborate a bit - for this use-case, there's not really a need to use "ready" or "changed", but it would be good to document the subscribe interaction between queries and docs.

Putting in some links based on the discussion:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeBreaking change that we may want to pull into a future major version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pypmannetjies@ericyhwang@alecgibson

        Issue actions

          Emit `'changed'` on query `'ready'`? · Issue #462 · share/sharedb