Properly handle multiple iterators for Events #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Due to using shared state, the current implementation is broken if multiple iterators are used on the Events class.
This is fixed in this PR by:
iterator()only returns newly emitted events, and not all emitted events during the Events instance lifetime.Additionally, this fixes it so trying to iterate further on a completed iterator returns "done", instead of a never-ending Promise.
The updated semantics are required. Otherwise the Events instance will need to hang on to all emitted events, just in case someone calls
iterator()on it again. Given that I am not aware of any existing usage of the Events feature, I cannot know if this will cause problems and should be considered a breaking change. It all depends on how earlyiterator()is called. An alternative fix, that might cause less breakage, is to add an assert that only allowiterator()to be called once.Note that with the moved state, care must be taken not to cause a leak due to two-way references. I have added logic to handle this automatically when using
for-ofthrough the implicitdestroy()call, as well as several tests.