import { type IQueryFollower } from "https://deno.land/x/earthstar@v9.3.3/src/query-follower/query-follower-types.ts";
Subscribe to the ongoing results of a query, optionally including old existing docs.
const myFollower = new QueryFollower(replica, myQuery);
myFollower.bus.on(async (event: LiveQueryEvent) => {
if (event.kind === 'existing' || event.kind === 'success') {
doSomething(event.doc)
}
});
await qf.hatch();
Properties
replica: IReplica
query: Query
The query being followed. Has some limitations:
historyMode
must beall
orderBy
must belocalIndex ASC
- limit can NOT be set.
bus: Simplebus<LiveQueryEvent>
Use this to subcribe to events with a callback, which will be called blockingly (one event at a time, one callback at a time).
For now it's tricky to close a query follower from inside its own event handler; you have to do it using setTimeout or you'll deadlock on the bus's lock.
qf.bus.on(await (event) => {
setTimeout(() => qf.close(), 0);
});
(because you can't send an event from inside an event handler)
Methods
state(): QueryFollowerState
Returns the follower's state, which can be in two modes:
- catching up with the backlog
- caught up; processing new events as they happen. When the query follower is in catching-up mode, it runs independently on its own schedule. When it's in live mode, it processes each doc as it's written, blockingly, which means it provides backpressure all the way back up to whatever is trying to ingest() docs into the replica. There is not currently an easy way to know when a query follower has caught up and switched to live mode, except to listen for the 'idle' event on its bus.
Begins the process of catching up with existing documents (if needed), then switches to live mode.