-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat(Collector): add iterators and getters #9165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Syjalo
wants to merge
4
commits into
discordjs:main
Choose a base branch
from
Syjalo:feat/add-iterators-to-Collector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,7 +157,7 @@ class Collector extends EventEmitter { | |
} | ||
|
||
/** | ||
* Returns a promise that resolves with the next collected element; | ||
* Returns a promise that resolves with the next collected, disposed, or ignored elements; | ||
* rejects with collected elements if the collector finishes without receiving a next element | ||
* @type {Promise} | ||
* @readonly | ||
|
@@ -171,12 +171,59 @@ class Collector extends EventEmitter { | |
|
||
const cleanup = () => { | ||
this.removeListener('collect', onCollect); | ||
this.removeListener('dispose', onDispose); | ||
this.removeListener('ignore', onIgnore); | ||
this.removeListener('end', onEnd); | ||
}; | ||
|
||
const onCollect = item => { | ||
const onCollect = (...items) => { | ||
cleanup(); | ||
resolve(item); | ||
resolve(['collecting', ...items]); | ||
}; | ||
|
||
const onDispose = (...items) => { | ||
cleanup(); | ||
resolve(['disposing', ...items]); | ||
}; | ||
|
||
const onIgnore = (...items) => { | ||
cleanup(); | ||
resolve(['ignoring', ...items]); | ||
}; | ||
|
||
const onEnd = () => { | ||
cleanup(); | ||
reject(this.collected); | ||
}; | ||
|
||
this.on('collect', onCollect); | ||
this.on('dispose', onDispose); | ||
this.on('ignore', onIgnore); | ||
this.on('end', onEnd); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves with the next collected elements; | ||
* rejects with collected elements if the collector finishes without receiving a next element | ||
* @type {Promise} | ||
* @readonly | ||
*/ | ||
get nextCollecting() { | ||
return new Promise((resolve, reject) => { | ||
if (this.ended) { | ||
reject(this.collected); | ||
return; | ||
} | ||
|
||
const cleanup = () => { | ||
this.removeListener('collect', onCollect); | ||
this.removeListener('end', onEnd); | ||
}; | ||
|
||
const onCollect = (...items) => { | ||
cleanup(); | ||
resolve(items); | ||
}; | ||
|
||
const onEnd = () => { | ||
|
@@ -189,6 +236,72 @@ class Collector extends EventEmitter { | |
}); | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves with the next disposed elements; | ||
* rejects with collected elements if the collector finishes without receiving a next element | ||
* @type {Promise} | ||
* @readonly | ||
*/ | ||
get nextDisposing() { | ||
return new Promise((resolve, reject) => { | ||
if (this.ended) { | ||
reject(this.collected); | ||
return; | ||
} | ||
|
||
const cleanup = () => { | ||
this.removeListener('dispose', onDispose); | ||
this.removeListener('end', onEnd); | ||
}; | ||
|
||
const onDispose = (...items) => { | ||
cleanup(); | ||
resolve(items); | ||
}; | ||
|
||
const onEnd = () => { | ||
cleanup(); | ||
reject(this.collected); | ||
}; | ||
|
||
this.on('dispose', onDispose); | ||
this.on('end', onEnd); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves with the next ignored elements; | ||
* rejects with collected elements if the collector finishes without receiving a next element | ||
* @type {Promise} | ||
* @readonly | ||
*/ | ||
get nextIgnoring() { | ||
return new Promise((resolve, reject) => { | ||
if (this.ended) { | ||
reject(this.collected); | ||
return; | ||
} | ||
|
||
const cleanup = () => { | ||
this.removeListener('ignore', onIgnore); | ||
this.removeListener('end', onEnd); | ||
}; | ||
|
||
const onIgnore = (...items) => { | ||
cleanup(); | ||
resolve(items); | ||
}; | ||
|
||
const onEnd = () => { | ||
cleanup(); | ||
reject(this.collected); | ||
}; | ||
|
||
this.on('ignore', onIgnore); | ||
this.on('end', onEnd); | ||
}); | ||
} | ||
|
||
/** | ||
* Stops this collector and emits the `end` event. | ||
* @param {string} [reason='user'] The reason this collector is ending | ||
|
@@ -251,13 +364,17 @@ class Collector extends EventEmitter { | |
} | ||
|
||
/** | ||
* Allows collectors to be consumed with for-await-of loops | ||
* Allows collectors to be consumed with for-await-of loop for collected, disposed, and ignored elements | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of} | ||
*/ | ||
async *[Symbol.asyncIterator]() { | ||
const queue = []; | ||
const onCollect = (...item) => queue.push(item); | ||
const onCollect = (...items) => queue.push(['collecting', ...items]); | ||
const onDispose = (...items) => queue.push(['disposing', ...items]); | ||
const onIgnore = (...items) => queue.push(['ignoring', ...items]); | ||
this.on('collect', onCollect); | ||
this.on('dispose', onDispose); | ||
this.on('ignore', onIgnore); | ||
|
||
try { | ||
while (queue.length || !this.ended) { | ||
|
@@ -268,16 +385,114 @@ class Collector extends EventEmitter { | |
await new Promise(resolve => { | ||
const tick = () => { | ||
this.removeListener('collect', tick); | ||
this.removeListener('dispose', tick); | ||
this.removeListener('end', tick); | ||
Comment on lines
387
to
389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't removing the |
||
return resolve(); | ||
}; | ||
this.on('collect', tick); | ||
this.on('dispose', tick); | ||
this.on('ignore', tick); | ||
this.on('end', tick); | ||
}); | ||
} | ||
} | ||
} finally { | ||
this.removeListener('collect', onCollect); | ||
this.removeListener('dispose', onDispose); | ||
this.removeListener('ignore', onIgnore); | ||
} | ||
} | ||
|
||
/** | ||
* Allows collectors to be consumed with for-await-of loop for collected elements | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of} | ||
*/ | ||
async *collectings() { | ||
const queue = []; | ||
const onCollect = (...items) => queue.push(items); | ||
this.on('collect', onCollect); | ||
|
||
try { | ||
while (queue.length || !this.ended) { | ||
if (queue.length) { | ||
yield queue.shift(); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise(resolve => { | ||
const tick = () => { | ||
this.removeListener('collect', tick); | ||
this.removeListener('end', tick); | ||
return resolve(); | ||
}; | ||
this.on('collect', tick); | ||
this.on('end', tick); | ||
}); | ||
} | ||
} | ||
} finally { | ||
this.removeListener('collect', onCollect); | ||
} | ||
} | ||
|
||
/** | ||
* Allows collectors to be consumed with for-await-of loop for disposed elements | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of} | ||
*/ | ||
async *disposings() { | ||
const queue = []; | ||
const onDispose = (...items) => queue.push(items); | ||
this.on('dispose', onDispose); | ||
|
||
try { | ||
while (queue.length || !this.ended) { | ||
if (queue.length) { | ||
yield queue.shift(); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise(resolve => { | ||
const tick = () => { | ||
this.removeListener('dispose', tick); | ||
this.removeListener('end', tick); | ||
return resolve(); | ||
}; | ||
this.on('dispose', tick); | ||
this.on('end', tick); | ||
}); | ||
} | ||
} | ||
} finally { | ||
this.removeListener('dispose', onDispose); | ||
} | ||
} | ||
|
||
/** | ||
* Allows collectors to be consumed with for-await-of loop for ignored elements | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of} | ||
*/ | ||
async *ignorings() { | ||
const queue = []; | ||
const onIgnore = (...items) => queue.push(items); | ||
this.on('ignore', onIgnore); | ||
|
||
try { | ||
while (queue.length || !this.ended) { | ||
if (queue.length) { | ||
yield queue.shift(); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise(resolve => { | ||
const tick = () => { | ||
this.removeListener('ignore', tick); | ||
this.removeListener('end', tick); | ||
return resolve(); | ||
}; | ||
this.on('ignore', tick); | ||
this.on('end', tick); | ||
}); | ||
} | ||
} | ||
} finally { | ||
this.removeListener('ignore', onIgnore); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we shouldn't have async getters. What about
waitForCollect()
? Same for the other methods.I also think that it'd be useful to accept an
AbortSignal
so we can cancel and unregister the listeners earlier.