Skip to content

Commit

Permalink
Fixed cancellation of the allSettled method;
Browse files Browse the repository at this point in the history
Refactored `all` method logic;
  • Loading branch information
DigitalBrainJS committed Apr 19, 2021
1 parent 2e29e1f commit 6862b8f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
42 changes: 16 additions & 26 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,14 @@ class CPromise extends Promise {
return this;
}

[_setInnerChain](chain){
[_setInnerChain](chain, captureProgress){
const shadow= this[_shadow];

shadow.innerChain = chain;

chain.on('propagate', (type, scope, data) => {
if (type === TYPE_PROGRESS) {
if (!shadow.isCaptured) return;
if (!captureProgress || !shadow.isCaptured) return;
const progress = chain.progress();
this.progress(progress, data);
return;
Expand All @@ -670,9 +670,10 @@ class CPromise extends Promise {
this.emit('propagate', type, scope, data);
});

shadow.isCaptured && chain.captureProgress();

shadow.innerWeight = chain[_shadow].totalWeight;
if (captureProgress) {
shadow.isCaptured && chain.captureProgress();
shadow.innerWeight = chain[_shadow].totalWeight;
}
}

[_resolve](value, isRejected) {
Expand Down Expand Up @@ -734,7 +735,7 @@ class CPromise extends Promise {
}

if (value instanceof CPromise) {
this[_setInnerChain](value);
this[_setInnerChain](value, true);
}

super.then.call(
Expand Down Expand Up @@ -1354,7 +1355,12 @@ class CPromise extends Promise {
mapper: (value, i) => {
return this.resolve(mapper ? mapper(value, i) : value).then(
value => ({status: "fulfilled", value}),
err => ({status: "rejected", reason: err})
err => {
if(this.isCanceledError(err)) {
throw err;
}
return {status: "rejected", reason: err};
}
)
}
})
Expand Down Expand Up @@ -1451,7 +1457,7 @@ class CPromise extends Promise {
shadow.innerChain[_events]= null;
}

scope[_setInnerChain](promise);
scope[_setInnerChain](promise, true);

return promise;
}
Expand Down Expand Up @@ -1719,31 +1725,15 @@ class CPromise extends Promise {
}
}

scope.on('signal', (type, data) => {
switch (type) {
case SIGNAL_CANCEL:
if (!promise.cancel(data.err, data.force)) {
onRejected(data.err);
}
return true;
case SIGNAL_PAUSE:
promise.pause();
return true;
case SIGNAL_RESUME:
promise.resume();
return true;
}

return promise.emitSignal(type, data);
});

const next = (r) => {
if (r.done) {
return resolve(r.value);
}

promise = this.from(r.value, {resolveSignatures});

scope[_setInnerChain](promise, false);

sum += weight;
weight = promise.isChain ? 1 : promise.weight();

Expand Down
45 changes: 32 additions & 13 deletions test/tests/CPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,19 +642,38 @@ module.exports = {
}
},

'CPromise.allSettled': async function () {
const err = new Error('test1');
return CPromise.allSettled([
delay(100, 123),
CPromise.reject(err),
CPromise.resolve(456)
]).then(results => {
assert.deepStrictEqual(results, [
{status: 'fulfilled', value: 123},
{status: 'rejected', reason: err},
{status: 'fulfilled', value: 456}
]);
})
'CPromise.allSettled': {
'should aggregate promise results into array of status objects' : async function () {
const err = new Error('test1');
return CPromise.allSettled([
delay(100, 123),
CPromise.reject(err),
CPromise.resolve(456)
]).then(results => {
assert.deepStrictEqual(results, [
{status: 'fulfilled', value: 123},
{status: 'rejected', reason: err},
{status: 'fulfilled', value: 456}
]);
})
},

'should support cancellation': async()=>{
const chain= CPromise.allSettled([
delay(100, 123),
delay(500).then(()=> {
throw Error('break');
})
]).then(results => {
assert.fail('was not cancelled');
}, err=>{
if(!CPromise.isCanceledError(err)){
assert.fail(`rejected not with CancelledError`);
}
})

setTimeout(()=> chain.cancel(), 200);
}
},

'CPromise.on': {
Expand Down

0 comments on commit 6862b8f

Please sign in to comment.