import { QueryFollower } from "https://deno.land/x/earthstar@v9.3.3/mod.browser.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
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)
The query being followed. Has some limitations:
historyMode
must beall
orderBy
must belocalIndex ASC
- limit can NOT be set.
Methods
Permanently shut down the QueryFollower, unhooking from the storage and stopping the processing of events.
Begins the process of catching up with existing documents (if needed), then switches to live mode.
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 storage. 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.