In the current implementation, PubSubAsyncIterator registers its subscriptions on the very first call to next, and goes on to wait for the first event before resolving. As such, a user of the iterator has no way of determining that the iterator has properly subscribed before it receives a first event.
This is a problem because some events can be published in the time it takes the iterator to subscribe.
Example use case: I have a subscription that returns a list of posts initially, then a new post every so often. Right now, I have no way of knowing when I can safely yield my list of initial posts without possibly missing posts that were created while my iterator was subscribing. The flow is:
create iterator
yield initial posts
yield next from iterator
iterator subscribes
yield next from iterator
yield rest from iterator
Between 2. and 3.i., if any new posts come in, they will be entirely missed by my subscription. However, I have no way of knowing when 3.i. occurs without also waiting for 3.ii., which might happen at any point in the future.
The flow I'm looking for is:
create iterator
iterator subscribes
yield initial posts
yield rest from iterator
I see two ways to fix this issue:
Make PubSubEngine.asyncIterator async. Instead of pubsub.asyncIterator, it would be await pubsub.asyncIterator. To avoid breaking changes, it could be called something else (e.g. PubSubEngine.subscribedAsyncIterator or whatever).
Expose a subscribe: () => Promise<void> method on PubSubAsyncIterator that resolves when the async iterator has fully subscribed.
In the current implementation,
PubSubAsyncIterator
registers its subscriptions on the very first call tonext
, and goes on to wait for the first event before resolving. As such, a user of the iterator has no way of determining that the iterator has properly subscribed before it receives a first event.This is a problem because some events can be published in the time it takes the iterator to subscribe.
Example use case: I have a subscription that returns a list of posts initially, then a new post every so often. Right now, I have no way of knowing when I can safely yield my list of initial posts without possibly missing posts that were created while my iterator was subscribing. The flow is:
Between 2. and 3.i., if any new posts come in, they will be entirely missed by my subscription. However, I have no way of knowing when 3.i. occurs without also waiting for 3.ii., which might happen at any point in the future.
The flow I'm looking for is:
I see two ways to fix this issue:
PubSubEngine.asyncIterator
async. Instead ofpubsub.asyncIterator
, it would beawait pubsub.asyncIterator
. To avoid breaking changes, it could be called something else (e.g.PubSubEngine.subscribedAsyncIterator
or whatever).subscribe: () => Promise<void>
method onPubSubAsyncIterator
that resolves when the async iterator has fully subscribed.