Skip to content

Commit

Permalink
Fixed progress capturing support for CPromise.run method;
Browse files Browse the repository at this point in the history
Added `CPromise#hasListener` method;
  • Loading branch information
DigitalBrainJS committed Feb 1, 2022
1 parent d568957 commit d5fead1
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 82 deletions.
13 changes: 13 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Creates a new CPromise instance
* [.canceled([onCanceled])](#module_CPromise..CPromise+canceled) ⇒ <code>CPromise</code>
* [.listen(signal)](#module_CPromise..CPromise+listen) ⇒ <code>CPromise</code>
* [.on(type, listener, [prepend])](#module_CPromise..CPromise+on) ⇒ <code>CPromise</code>
* [.hasListener(event, listener)](#module_CPromise..CPromise+hasListener) ⇒ <code>boolean</code>
* [.off(type, listener)](#module_CPromise..CPromise+off) ⇒ <code>CPromise</code>
* [.listenersCount(type)](#module_CPromise..CPromise+listenersCount) ⇒ <code>Number</code>
* [.hasListeners(type)](#module_CPromise..CPromise+hasListeners) ⇒ <code>Boolean</code>
Expand Down Expand Up @@ -614,6 +615,18 @@ adds a new listener
| listener | <code>function</code> | |
| [prepend] | <code>Boolean</code> | <code>false</code> |

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

#### cPromise.hasListener(event, listener) ⇒ <code>boolean</code>
Check whether the listener is already registered to the specific event

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

| Param | Type |
| --- | --- |
| event | <code>EventType</code> |
| listener | <code>function</code> |

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

#### cPromise.off(type, listener) ⇒ <code>CPromise</code>
Expand Down
71 changes: 46 additions & 25 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const {
lazyBindMethods,
isContextDefined,
getTag,
throttle
throttle,
toFixed
} = require('./utils');
const {classDecorator, propertyDecorator, bindDecorators}= require('./decorators');
const {versionNumber, _versionNumber, _version, version, warnVersionInteraction}= require('./env');
Expand Down Expand Up @@ -191,12 +192,20 @@ const CPromise= class CPromise extends Promise {
label !== undefined && this.label(label);

this.on('propagate', (type, scope, data) => {
if (type === TYPE_PROGRESS) {
switch(type){
case TYPE_PROGRESS:
shadow.computedProgress = -1;
shadow.isListening && this.emit('progress', this.progress(), scope, data);
} else if (type === TYPE_PAUSE) {
shadow.isListening && this.emit(
'progress',
this.progress(),
scope,
data
);
return;
case TYPE_PAUSE:
this.emit('pause', data, scope);
} else if (type === TYPE_RESUME) {
return;
case TYPE_RESUME:
this.emit('resume', data, scope);
}
});
Expand Down Expand Up @@ -401,16 +410,16 @@ const CPromise= class CPromise extends Promise {
throw TypeError('value must be a number [0, 1]');
}

if (!shadow.isCaptured) return this;

if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}

if (!shadow.isCaptured) return this;

if (value !== 0 && value !== 1) {
value = value.toFixed(10) * 1;
value = +value.toFixed(10);
}

if (shadow.progress !== value) {
Expand Down Expand Up @@ -446,11 +455,7 @@ const CPromise= class CPromise extends Promise {

let total = shadow.totalWeight;

if (total > 0) {
return (shadow.computedProgress = (progress + (total - sum)) / total);
}

shadow.computedProgress = progress / sum;
shadow.computedProgress = total > 0 ? (progress + (total - sum)) / total : progress / sum;
}

return shadow.computedProgress;
Expand Down Expand Up @@ -486,7 +491,7 @@ const CPromise= class CPromise extends Promise {
})
}

const {throttle: ms = 200, innerWeight} = options || {};
const {throttle: ms = 250, innerWeight} = options || {};

shadow.throttler && shadow.throttler.cancel();

Expand Down Expand Up @@ -1898,9 +1903,10 @@ const CPromise= class CPromise extends Promise {

const setProgress = (value, _scope, data) => {
const innerWeight = scope.innerWeight();
if (!innerWeight) return;
progress = (value * weight + sum) / innerWeight;
scope.progress(progress, _scope, data);
if (innerWeight) {
progress = (value * weight + sum) / innerWeight
scope.progress(progress, _scope, data);
}
}

const onFulfilled = (result) => {
Expand All @@ -1921,11 +1927,8 @@ const CPromise= class CPromise extends Promise {
}
}

const captureProgress= ()=>{
if(promise.isCaptured) return;
promise.progress((value, scope, data) => {
setProgress(value, scope, data);
});
const captureProgress = () => {
promise.progress(setProgress);
}

!scope.isCaptured && scope.onCapture(()=>{
Expand All @@ -1944,7 +1947,7 @@ const CPromise= class CPromise extends Promise {
sum += weight;
weight = promise.isChain ? 1 : promise.weight();

scope.isCaptured && captureProgress(promise);
scope.isCaptured && captureProgress();

return promise.then(onFulfilled, onRejected);
}
Expand Down Expand Up @@ -2112,23 +2115,41 @@ const CPromise= class CPromise extends Promise {
const events = this[_events];
if (!events) return this;
const listeners = events[type];

events['newListener'] && this.emit('newListener', type, listener);
const {newListener} = events;

if (!listeners) {
newListener && this.emit('newListener', type, listener);
events[type] = listener;
return this;
}

if (typeof listeners === 'function') {
if (listeners === listener) return this;
newListener && this.emit('newListener', type, listener);
events[type] = prepend ? [listener, listeners] : [listeners, listener];
return this;
}

if (listeners.indexOf(listener) !== -1) return this;
newListener && this.emit('newListener', type, listener);
prepend ? listeners.unshift(listener) : listeners.push(listener);
return this;
}

/**
* Check whether the listener is already registered to the specific event
* @param {EventType} event
* @param {Function} listener
* @returns {boolean}
*/

hasListener(event, listener) {
const events = this[_events];
if (!events) return false;
const listeners = events[event];
return !!listeners && (typeof listeners === 'function' || listeners.indexOf(listener) !== -1);
}

/**
* removes the listener
* @param {EventType} type
Expand Down
8 changes: 4 additions & 4 deletions test/src/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ module.exports = {
@innerWeight(4)
@async
*generator() {
yield delay(100);
yield delay(100);
yield delay(100);
yield delay(100);
yield delay(300);
yield delay(300);
yield delay(300);
yield delay(300);
}
}

Expand Down
Loading

0 comments on commit d5fead1

Please sign in to comment.