The documentation on observeAsync() states:
Before observeAsync
returns, added
(or addedAt
) will be called zero or more times to deliver the initial results of the query.
This is true when the added()
or addedAt()
methods are synchronous but not true if they are asynchronous.
Is this something that can be addressed?
What version of Meteor are you using? It looks like in 2.16 async callbacks were not taken care of, but the latest sources seem to indicate otherwise.
Hi, the latest 3.1.2
If you think this is working as expected, I will go back and double check my experience …
This is the code that awaits initial add
events:
this._cache.docs.forEach((doc: any, id: string) => {
if (!(handle._id in this._handles!)) {
throw Error("handle got removed before sending initial adds!");
}
const { _id, ...fields } = handle.nonMutatingCallbacks ? doc : EJSON.clone(doc);
const promise = this._ordered ?
add(id, fields, null) :
add(id, fields);
addPromises.push(promise);
});
await Promise.all(addPromises);
But now that I look into it a bit more maybe tailable cursors are not handled (because there is no “initialization”):
return self.tail(cursorDescription, function (doc) {
var id = doc._id;
delete doc._id;
// The ts is an implementation detail. Hide it.
delete doc.ts;
if (ordered) {
callbacks.addedBefore(id, doc, null);
} else {
callbacks.added(id, doc);
}
});
Yes, sorry, I should have been clearer…
The added()
is called before the observerAsync()
returns, but the effect of the add is not present (i.e. the additions are not present).
So the overall effect is as if the added()
had not been called at all so far as the state is concerned when observeAsync()
returns.
Hope this helps
Nope makes sense. add
is called during the initialization phase. The question is whether those calls are awaited before the initialization phase is considered done.