Skip to content

Commit

Permalink
Added support for signal filter parameter in onSignal method;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Jan 31, 2022
1 parent 80c7427 commit 104654a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
13 changes: 13 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Creates a new CPromise instance
* [.onCapture(listener)](#module_CPromise..CPromise+onCapture) ⇒ <code>CPromise</code>
* [.onDone(listener)](#module_CPromise..CPromise+onDone)
* [.onSignal(listener)](#module_CPromise..CPromise+onSignal)
* [.onSignal(signal, listener)](#module_CPromise..CPromise+onSignal)
* [.totalWeight([weight])](#module_CPromise..CPromise+totalWeight) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.innerWeight([weight])](#module_CPromise..CPromise+innerWeight) ⇒ <code>Number</code> \| <code>CPromise</code>
* [.progress([value], [data], [scope])](#module_CPromise..CPromise+progress) ⇒ <code>Number</code> \| <code>CPromise</code>
Expand Down Expand Up @@ -309,6 +310,18 @@ registers the listener for done event
| --- | --- |
| listener | <code>CPSignalListener</code> |

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

#### cPromise.onSignal(signal, listener)
registers the listener for done event

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

| Param | Type |
| --- | --- |
| signal | <code>Signal</code> |
| listener | <code>CPSignalListener</code> |

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

#### cPromise.totalWeight([weight]) ⇒ <code>Number</code> \| <code>CPromise</code>
Expand Down
25 changes: 18 additions & 7 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,25 @@ const CPromise= class CPromise extends Promise {
*/

/**
* registers the listener for done event
* @param {CPSignalListener} listener
*//**
* registers the listener for done event
* @param {Signal} signal
* @param {CPSignalListener} listener
*/

onSignal(listener){

onSignal(signal, listener) {
if (arguments.length === 1) {
listener = signal;
signal = null;
}

return this.on('signal', function (_signal, data) {
if (signal && signal !== _signal) return;
return signal? listener.call(this, data) : listener.call(this, _signal, data);
});
}

/**
Expand Down Expand Up @@ -2298,15 +2312,12 @@ prototype.addEventListener = prototype.on;

prototype.removeEventListener = prototype.off;

lazyBindMethods(prototype, ['Cancel', 'Pause', 'Resume', 'Capture', 'Done', 'Signal'].map(type => {
lazyBindMethods(prototype, ['Cancel', 'Pause', 'Resume', 'Capture', 'Done'].map(type => {
const typeL = type.toLowerCase();

const methodName= 'on' + type;
const methodName = 'on' + type;

prototype[methodName]= function method(listener) {
if (!this[_shadow].isPending) {
throw Error(`Unable to subscribe to ${typeL} event since the promise has been already settled`);
}
prototype[methodName] = function method(listener) {
return this.on(typeL, listener);
}

Expand Down
18 changes: 18 additions & 0 deletions test/tests/CPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,5 +1071,23 @@ module.exports = {
}).then(resolve, reject);
});
}
},

'signals': {
'onSignal': async function () {
let signalEmitted = false;
const promise = CPromise.delay(1000).onSignal('test', (data) => {
assert.deepStrictEqual(data, {x: 1});
signalEmitted = true;
}).then(() => {
assert.ok(signalEmitted, 'signal was not emitted');
});

await CPromise.delay(500);

promise.emitSignal('test', {x: 1});

return promise;
}
}
};

0 comments on commit 104654a

Please sign in to comment.