Skip to content

Commit

Permalink
feat: new api error function
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 14, 2023
1 parent 6f8e542 commit 3743472
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 28 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Multiple items arrays are merged. Muliple options are merged as well.
Reject the returned promise and every registered items which is not yet executed with an error. All scheduled items not yet executed are resolved with an error. In `relax` mode, only the promise returned by `end` 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.
- `error(error|null)`
Place the scheduler in an error state, all future registered items will be rejected. Use `null` to set the scheduler to a normal state.
- `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
32 changes: 25 additions & 7 deletions dist/each.cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ function index() {
if (!state.stack.length) {
return;
}
if (state.error && !options.relax) {
while (state.stack.length) {
const item = state.stack.shift();
item.reject.call(null, state.error);
}
return;
}
if (state.paused) {
return;
}
Expand All @@ -97,9 +90,21 @@ function index() {
const item = state.stack.shift();
if (item.type === 'END') {
if(state.stack.length !== 0) console.error('INVALID_STATE');
if(state.error && !options.relax){
item.reject(state.error);
}else {
item.resolve();
}
return;
} else if (item.type === 'ERROR') {
state.error = item.value;
item.resolve();
return;
}
if (state.error && !options.relax) {
item.reject.call(null, state.error);
return;
}
state.running++;
try {
state.count++;
Expand Down Expand Up @@ -212,6 +217,19 @@ function index() {
})
);
};
promise.error = function(error) {
return catcher(
new Promise(function(resolve, reject) {
state.stack.push({
type: 'ERROR',
resolve: resolve,
reject: reject,
value: error,
});
internal.pump();
})
);
};
promise.resume = function() {
state.paused = false;
const defers = state.defers;
Expand Down
32 changes: 25 additions & 7 deletions dist/each.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ function index() {
if (!state.stack.length) {
return;
}
if (state.error && !options.relax) {
while (state.stack.length) {
const item = state.stack.shift();
item.reject.call(null, state.error);
}
return;
}
if (state.paused) {
return;
}
Expand All @@ -95,9 +88,21 @@ function index() {
const item = state.stack.shift();
if (item.type === 'END') {
if(state.stack.length !== 0) console.error('INVALID_STATE');
if(state.error && !options.relax){
item.reject(state.error);
}else {
item.resolve();
}
return;
} else if (item.type === 'ERROR') {
state.error = item.value;
item.resolve();
return;
}
if (state.error && !options.relax) {
item.reject.call(null, state.error);
return;
}
state.running++;
try {
state.count++;
Expand Down Expand Up @@ -210,6 +215,19 @@ function index() {
})
);
};
promise.error = function(error) {
return catcher(
new Promise(function(resolve, reject) {
state.stack.push({
type: 'ERROR',
resolve: resolve,
reject: reject,
value: error,
});
internal.pump();
})
);
};
promise.resume = function() {
state.paused = false;
const defers = state.defers;
Expand Down
32 changes: 25 additions & 7 deletions dist/each.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@
if (!state.stack.length) {
return;
}
if (state.error && !options.relax) {
while (state.stack.length) {
const item = state.stack.shift();
item.reject.call(null, state.error);
}
return;
}
if (state.paused) {
return;
}
Expand All @@ -101,9 +94,21 @@
const item = state.stack.shift();
if (item.type === 'END') {
if(state.stack.length !== 0) console.error('INVALID_STATE');
if(state.error && !options.relax){
item.reject(state.error);
}else {
item.resolve();
}
return;
} else if (item.type === 'ERROR') {
state.error = item.value;
item.resolve();
return;
}
if (state.error && !options.relax) {
item.reject.call(null, state.error);
return;
}
state.running++;
try {
state.count++;
Expand Down Expand Up @@ -216,6 +221,19 @@
})
);
};
promise.error = function(error) {
return catcher(
new Promise(function(resolve, reject) {
state.stack.push({
type: 'ERROR',
resolve: resolve,
reject: reject,
value: error,
});
internal.pump();
})
);
};
promise.resume = function() {
state.paused = false;
const defers = state.defers;
Expand Down
32 changes: 25 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ export default function() {
if (!state.stack.length) {
return;
}
if (state.error && !options.relax) {
while (state.stack.length) {
const item = state.stack.shift();
item.reject.call(null, state.error);
}
return;
}
if (state.paused) {
return;
}
Expand All @@ -97,9 +90,21 @@ export default function() {
const item = state.stack.shift();
if (item.type === 'END') {
if(state.stack.length !== 0) console.error('INVALID_STATE');
if(state.error && !options.relax){
item.reject(state.error);
}else{
item.resolve();
}
return;
} else if (item.type === 'ERROR') {
state.error = item.value;
item.resolve();
return;
}
if (state.error && !options.relax) {
item.reject.call(null, state.error);
return;
}
state.running++;
try {
state.count++;
Expand Down Expand Up @@ -212,6 +217,19 @@ export default function() {
})
);
};
promise.error = function(error) {
return catcher(
new Promise(function(resolve, reject) {
state.stack.push({
type: 'ERROR',
resolve: resolve,
reject: reject,
value: error,
});
internal.pump();
})
);
};
promise.resume = function() {
state.paused = false;
const defers = state.defers;
Expand Down
25 changes: 25 additions & 0 deletions test/api.error.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import each from '../lib/index.js'

describe 'api.error', ->

it 'items scheduled after are rejected', ->
scheduler = each()
scheduler.error Error 'catchme'
scheduler
.call [1, 2]
.should.be.rejectedWith 'catchme'

it 'items scheduled before are executed', ->
stack = []
scheduler = each [1]
scheduler
.then (value) -> stack.push value
scheduler
.call 2
.then (value) -> stack.push value
await scheduler.error Error 'catchme'
stack.should.eql [ [1], 2]



0 comments on commit 3743472

Please sign in to comment.