-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
WIP: Add allSettled mode for Collector class #233
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,35 @@ class Collector { | |
count = 0; | ||
exact = true; | ||
timeout = 0; | ||
isRejectable = true; | ||
#fulfill = null; | ||
#reject = null; | ||
#timer = null; | ||
#cause = null; | ||
|
||
constructor(keys, { exact = true, timeout = 0 } = {}) { | ||
constructor(keys, { exact = true, timeout = 0, isRejectable = true } = {}) { | ||
this.keys = keys; | ||
this.isRejectable = isRejectable; | ||
if (exact === false) this.exact = false; | ||
if (typeof timeout === 'number') this.#timeout(timeout); | ||
} | ||
|
||
#setData(key, data) { | ||
if (!this.isRejectable) { | ||
this.set(key, getSettled(null, data)); | ||
return; | ||
} | ||
this.set(key, data); | ||
} | ||
|
||
#setError(key, err) { | ||
if (!this.isRejectable) { | ||
this.set(key, getSettled(err)); | ||
return; | ||
} | ||
this.fail(err); | ||
} | ||
|
||
#timeout(msec) { | ||
this.timeout = msec; | ||
if (this.#timer) { | ||
|
@@ -48,23 +66,23 @@ class Collector { | |
|
||
take(key, fn, ...args) { | ||
fn(...args, (err, data) => { | ||
if (err) this.fail(err); | ||
else this.set(key, data); | ||
if (err) this.#setError(key, err); | ||
else this.#setData(key, data); | ||
}); | ||
} | ||
|
||
wait(key, fn, ...args) { | ||
fn(...args).then( | ||
(data) => this.set(key, data), | ||
(err) => this.fail(err), | ||
(data) => this.#setData(key, data), | ||
(err) => this.#setError(key, err), | ||
); | ||
} | ||
|
||
collect(sources) { | ||
for (const [key, collector] of Object.entries(sources)) { | ||
collector.then( | ||
(data) => this.set(key, data), | ||
(err) => this.fail(err), | ||
(data) => this.#setData(key, data), | ||
(err) => this.#setError(key, err), | ||
); | ||
} | ||
} | ||
|
@@ -88,6 +106,13 @@ class Collector { | |
} | ||
} | ||
|
||
function getSettled(err = null, value = null) { | ||
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. It shouldn't be a separate function, it should be inside a class 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. I made getSettled as static method in class Collector before, but @tshemsedinov said said that the function is better. |
||
if (err) { | ||
return { status: 'rejected', reason: err }; | ||
} | ||
return { status: 'fulfilled', value }; | ||
} | ||
|
||
const collect = (keys, options) => new Collector(keys, options); | ||
|
||
module.exports = { Collector, collect }; |
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.
We should check if
isRejectable
is booleanThere 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.
Fixed