feat(glob-cache): streaming API, use async iterables under the hood #124
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.
"Streaming" / async iterables API
Uses
fastGlob.stream
by default, plusfor await ... of
.The main default export is async generator
async function * () {}
and it returns async iterable, so you need to calliterable.next()
or to usefor await
loop (which requiresasync
function).Promise API
There is also exported
globCache.promise
API, which isasync function
and so returns a Promise.Important to note. By default the Promise API resolves to an empty array. That's intentional since we also have the so-called Hooks API and so it's unnecessary to pollute the memory when you are using it. If you don't use the Hooks API, but just want the results then pass
buffered: true
and the promise will resolve to an array of Context objects. You can later filter thisresults
array of contexts byctx.changed
orctx.notFound
.Hooks API
It's not recommended to use the Hooks API when using the Stream API.
Previously we had just a single
options.hook
function, now it isoptions.hooks
. And we needed to determine based onctx.valid
andctx.missing
booleans.Now we just have hooks for everything -
hooks.changed
,hooks.notChanged
,hooks.found
,hooks.notFound
andhooks.always
. It's pretty obvious by their names. Hooks can also be async functions - using async/await or regular function returning a promise.The
ctx.valid
andctx.missing
are replaced byctx.changed
andctx.notFound
- both has almost the same meaning as previously. Both are available through all the hooks. In combination with hooks it becomes great.Same as above applies for the hooks calls.
Notice that
changed
hook is also called when "not found" (i.e. first hit).