Refactor typescript table iterators #3782
Open
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.
Description of Changes
A few main goals here:
have our iterator functions return an
Iteratorobject so that users can use its combinators likefilter()andfind()andreduce(). It's a very new JS api but we happen to know that the module code will always be run in an environment that has it* :)improve lifecycle handling for iterator handles - mainly, if an iterator is not run to completion, it will now eventually get garbage collected, whereas before we would have a resource leak.
It turns out that the easiest way to do both of those things was to turn TableIterator into a generator function, which also happens to make the code much easier to read. Hooray :)
* I did mention it in
table_cache(which isn't run in our module host) but it's fine, since that's only in the type system andIteratorObjectis defined in typescript'slib.es2015.iterable.d.tsbut is only given fancy methods inlib.esnext.iterator.d.ts- so if the user uses esnext, they'll have access to them, but otherwise not.Expected complexity level and risk
1: this better separates concerns and makes the code clearer in its purpose.
Testing