Skip to content

Commit

Permalink
feat: end with an error in relax mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 5, 2023
1 parent 9e1533c commit cc781b4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ Multiple items arrays are merged. Muliple options are merged as well.
- `call`
Execute one or several items.
- `end(error|options)`
Close the scheduler. No further items is allowed to register with `call`. In such case, an error is thrown. It returns a promise which resolve once all previously scheduled items resolved. When `end` is called and each is in paused state, all paused items are resolved with `undefined` or an error if any.
Close the scheduler. No further items is allowed to register with `call`, or an error is thrown. It returns a promise which resolve once all previously scheduled items resolved. When `end` is called and each is in paused state, all paused items are resolved with `undefined` or an error if any.
Available options:
- `error`
Reject the returned promise and every registered items which is not yet executed with an error. In `relax` mode, only the returned promise is rejected with an error.
- `force`
Skip the execution of registered items which are not yet scheduled for execution. The items resolve with undefined or the value associated with the error option.
- `options`
Get all options with no argument, get a single option with one argument, and set the value of an option with two arguments.
- `pause`
Expand Down
12 changes: 6 additions & 6 deletions dist/each.cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ function index() {
}
return wrap(all(items));
};
promise.end = async function(options = {}) {
if(!is_object_literal(options) && typeof options === 'object'){
options = { error: options };
promise.end = async function(opts = {}) {
if(!is_object_literal(opts) && typeof opts === 'object'){
opts = { error: opts };
}
state.paused = false;
state.closed = true;
if (options.error){
state.error = options.error;
if (opts.error && !options.relax){
state.error = opts.error;
}
// In force mode, unschedule all scheduled items
if (options.force) {
if (opts.force) {
while (state.stack.length !== 0) {
const item = state.stack.shift();
item.resolve.call();
Expand Down
12 changes: 6 additions & 6 deletions dist/each.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ function index() {
}
return wrap(all(items));
};
promise.end = async function(options = {}) {
if(!is_object_literal(options) && typeof options === 'object'){
options = { error: options };
promise.end = async function(opts = {}) {
if(!is_object_literal(opts) && typeof opts === 'object'){
opts = { error: opts };
}
state.paused = false;
state.closed = true;
if (options.error){
state.error = options.error;
if (opts.error && !options.relax){
state.error = opts.error;
}
// In force mode, unschedule all scheduled items
if (options.force) {
if (opts.force) {
while (state.stack.length !== 0) {
const item = state.stack.shift();
item.resolve.call();
Expand Down
12 changes: 6 additions & 6 deletions dist/each.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@
}
return wrap(all(items));
};
promise.end = async function(options = {}) {
if(!is_object_literal(options) && typeof options === 'object'){
options = { error: options };
promise.end = async function(opts = {}) {
if(!is_object_literal(opts) && typeof opts === 'object'){
opts = { error: opts };
}
state.paused = false;
state.closed = true;
if (options.error){
state.error = options.error;
if (opts.error && !options.relax){
state.error = opts.error;
}
// In force mode, unschedule all scheduled items
if (options.force) {
if (opts.force) {
while (state.stack.length !== 0) {
const item = state.stack.shift();
item.resolve.call();
Expand Down
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ export default function() {
}
return wrap(all(items));
};
promise.end = async function(options = {}) {
if(!is_object_literal(options) && typeof options === 'object'){
options = { error: options };
promise.end = async function(opts = {}) {
if(!is_object_literal(opts) && typeof opts === 'object'){
opts = { error: opts };
}
state.paused = false;
state.closed = true;
if (options.error){
state.error = options.error;
if (opts.error && !options.relax){
state.error = opts.error;
}
// In force mode, unschedule all scheduled items
if (options.force) {
if (opts.force) {
while (state.stack.length !== 0) {
const item = state.stack.shift();
item.resolve.call();
Expand Down
11 changes: 11 additions & 0 deletions test/api.end.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ describe 'api.end', ->
resolve()
.catch reject
, 20

it 'has no effect if each.relax is active', ->
scheduler = each [
new Promise (resolve) -> setTimeout (-> resolve 1), 10
new Promise (resolve) -> setTimeout (-> resolve 2), 100
new Promise (resolve) -> setTimeout (-> resolve 3), 100
], relax: true
setTimeout (->
scheduler.end(new Error('closing'))
), 20
scheduler.should.be.resolvedWith [ 1, 2, 3 ]

describe 'state.pause', ->

Expand Down

0 comments on commit cc781b4

Please sign in to comment.