Skip to content

Commit

Permalink
Added support for propagation pause and resume events;
Browse files Browse the repository at this point in the history
Added support for deep pause state checking by the `isPaused` getter;
  • Loading branch information
DigitalBrainJS committed May 21, 2021
1 parent 83a7e47 commit acc8995
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,8 @@ Creates a new CPromise instance
* [.label([label])](#module_CPromise..CPromise+label) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.resolve(value)](#module_CPromise..CPromise+resolve) ⇒ <code>CPromise</code>
* [.reject(err)](#module_CPromise..CPromise+reject) ⇒ <code>CPromise</code>
* [.pause()](#module_CPromise..CPromise+pause) ⇒ <code>Boolean</code>
* [.resume()](#module_CPromise..CPromise+resume) ⇒ <code>Boolean</code>
* [.pause(data)](#module_CPromise..CPromise+pause) ⇒ <code>Boolean</code>
* [.resume(data)](#module_CPromise..CPromise+resume) ⇒ <code>Boolean</code>
* [.atomic([type])](#module_CPromise..CPromise+atomic)
* [.cancel([reason], [forced])](#module_CPromise..CPromise+cancel)
* [.emitSignal(type, [data], [handler], [locator])](#module_CPromise..CPromise+emitSignal) ⇒ <code>Boolean</code>
Expand Down Expand Up @@ -1187,7 +1187,7 @@ indicates if the promise progress is captured
<a name="module_CPromise..CPromise+isPaused"></a>

#### cPromise.isPaused ⇒ <code>Boolean</code>
indicates if the promise is paused
indicates if the promise chain is paused

**Kind**: instance property of [<code>CPromise</code>](#module_CPromise..CPromise)
<a name="module_CPromise..CPromise+isRejected"></a>
Expand Down Expand Up @@ -1399,16 +1399,26 @@ Rejects the promise with given error

<a name="module_CPromise..CPromise+pause"></a>

#### cPromise.pause() ⇒ <code>Boolean</code>
#### cPromise.pause(data) ⇒ <code>Boolean</code>
Pause promise

**Kind**: instance method of [<code>CPromise</code>](#module_CPromise..CPromise)

| Param | Type |
| --- | --- |
| data | <code>\*</code> |

<a name="module_CPromise..CPromise+resume"></a>

#### cPromise.resume() ⇒ <code>Boolean</code>
#### cPromise.resume(data) ⇒ <code>Boolean</code>
Resume promise

**Kind**: instance method of [<code>CPromise</code>](#module_CPromise..CPromise)

| Param | Type |
| --- | --- |
| data | <code>\*</code> |

<a name="module_CPromise..CPromise+atomic"></a>

#### cPromise.atomic([type]) ⇒
Expand Down
21 changes: 15 additions & 6 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const _render = Symbol('render');
const {toStringTag}= Symbol;

const TYPE_PROGRESS = Symbol('TYPE_PROGRESS');
const TYPE_PAUSE = Symbol('TYPE_PAUSE');
const TYPE_RESUME = Symbol('TYPE_RESUME');
const SIGNAL_CANCEL = Symbol('SIGNAL_CANCEL');
const SIGNAL_PAUSE = Symbol('SIGNAL_PAUSE');
const SIGNAL_RESUME = Symbol('SIGNAL_RESUME');
Expand Down Expand Up @@ -191,6 +193,10 @@ const CPromise= class CPromise extends Promise {
if (type === TYPE_PROGRESS) {
shadow.computedProgress = -1;
shadow.isListening && this.emit('progress', this.progress(), scope, data);
} else if (type === TYPE_PAUSE) {
this.emit('pause', data, scope);
} else if (type === TYPE_RESUME) {
this.emit('resume', data, scope);
}
});

Expand Down Expand Up @@ -632,12 +638,12 @@ const CPromise= class CPromise extends Promise {
}

/**
* indicates if the promise is paused
* indicates if the promise chain is paused
* @returns {Boolean}
*/

get isPaused() {
return this[_shadow].paused;
return this.scopes(true).some(scope=> scope[_shadow].paused)
}

/**
Expand Down Expand Up @@ -776,10 +782,11 @@ const CPromise= class CPromise extends Promise {

/**
* Pause promise
* @param {*} data
* @returns {Boolean}
*/

pause() {
pause(data) {
return this.emitSignal(SIGNAL_PAUSE, null, function () {
const shadow = this[_shadow];

Expand All @@ -791,18 +798,19 @@ const CPromise= class CPromise extends Promise {
shadow.timestamp !== -1 && (shadow.timeLeft = now() - shadow.timestamp);
}

this.emit('pause');
this.propagate(TYPE_PAUSE, data);
}
return true;
});
}

/**
* Resume promise
* @param {*} data
* @returns {Boolean}
*/

resume() {
resume(data) {
return this.emitSignal(SIGNAL_RESUME, null, function () {
const shadow = this[_shadow];

Expand All @@ -812,7 +820,8 @@ const CPromise= class CPromise extends Promise {
this.timeout(shadow.timeLeft);
shadow.timeLeft = 0;
}
this.emit('resume');

this.propagate(TYPE_RESUME, data);
}
return true;
});
Expand Down
38 changes: 38 additions & 0 deletions test/tests/CPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,44 @@ module.exports = {
}, 300);

return chain;
},

'should propagate pause&resume events': async function(){
let pauseEvents= [], resumeEvents= [];

const chain = new CPromise((resolve, reject, {onPause, onResume}) => {
setTimeout(resolve, 500);
onPause(() => {
pauseEvents[0] = true;
});

onResume(() => {
resumeEvents[0] = true;
});
}).then((v, {onPause, onResume}) => {
return delay(100);
});

chain.onPause(() => {
pauseEvents[1] = true;
});

chain.onResume(() => {
resumeEvents[1] = true;
});

chain.pause();

assert.strictEqual(chain.isPaused, true);

setTimeout(()=>chain.resume(), 100);

return chain.then(()=>{
assert.ok(pauseEvents[0], 'pause event [0] has not been emitted');
assert.ok(resumeEvents[0], 'resume event [0] has not been emitted');
assert.ok(pauseEvents[1], 'pause event [1] has not been emitted');
assert.ok(resumeEvents[1], 'resume event [1] has not been emitted');
});
}
},

Expand Down

0 comments on commit acc8995

Please sign in to comment.