Skip to content

Commit

Permalink
Added the ability for .delay to notify intermediate progress values;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Apr 16, 2021
1 parent 926567e commit eb32ab5
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 47 deletions.
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,14 +1041,19 @@ progress decorator
**Kind**: static property of [<code>CPromise</code>](#module_CPromise)
<a name="module_CPromise..CPromise"></a>

### CPromise~CPromise <code>Promise</code>
CPromise class
### CPromise~CPromise : <code>object</code>
Creates a new CPromise instance

**Kind**: inner class of [<code>CPromise</code>](#module_CPromise)
**Kind**: inner namespace of [<code>CPromise</code>](#module_CPromise)
**Extends**: <code>Promise</code>

* [~CPromise](#module_CPromise..CPromise) ⇐ <code>Promise</code>
* [new CPromise([executor], [options])](#new_module_CPromise..CPromise_new)
| Param | Type | Description |
| --- | --- | --- |
| [executor] | <code>CPromiseExecutorFn</code> | promise executor function that will be invoked in the context of the new CPromise instance |
| [options] | <code>CPromiseOptions</code> | |


* [~CPromise](#module_CPromise..CPromise) : <code>object</code>
* _instance_
* [.signal](#module_CPromise..CPromise+signal) : <code>AbortSignal</code>
* [.isPending](#module_CPromise..CPromise+isPending) ⇒ <code>Boolean</code>
Expand Down Expand Up @@ -1092,7 +1097,7 @@ CPromise class
* [.toString([entireChain])](#module_CPromise..CPromise+toString) ⇒ <code>string</code>
* _static_
* [.isCanceledError(thing)](#module_CPromise..CPromise.isCanceledError) ⇒ <code>boolean</code>
* [.delay(ms, value)](#module_CPromise..CPromise.delay) ⇒ <code>CPromise</code>
* [.delay(ms, value, [options])](#module_CPromise..CPromise.delay) ⇒ <code>CPromise</code>
* [.all(iterable, [options])](#module_CPromise..CPromise.all) ⇒ <code>CPromise</code>
* [.race(thenables)](#module_CPromise..CPromise.race) ⇒ <code>CPromise</code>
* [.allSettled(iterable, options)](#module_CPromise..CPromise.allSettled) ⇒ <code>CPromise</code>
Expand All @@ -1101,17 +1106,6 @@ CPromise class
* [.promisify(originalFn, [options])](#module_CPromise..CPromise.promisify) ⇒ <code>function</code>
* [.run(generatorFn, [options])](#module_CPromise..CPromise.run) ⇒ <code>CPromise</code>

<a name="new_module_CPromise..CPromise_new"></a>

#### new CPromise([executor], [options])
Creates a new CPromise instance


| Param | Type | Description |
| --- | --- | --- |
| [executor] | <code>CPromiseExecutorFn</code> | promise executor function that will be invoked in the context of the new CPromise instance |
| [options] | <code>CPromiseOptions</code> | |

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

#### cPromise.signal : <code>AbortSignal</code>
Expand Down Expand Up @@ -1541,15 +1535,17 @@ Checks if thing is an CanceledError instance

<a name="module_CPromise..CPromise.delay"></a>

#### CPromise.delay(ms, value) ⇒ <code>CPromise</code>
#### CPromise.delay(ms, value, [options]) ⇒ <code>CPromise</code>
Returns a CPromise that will be resolved after specified timeout

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

| Param | Type | Description |
| --- | --- | --- |
| ms | <code>Number</code> | delay before resolve the promise with specified value |
| value | | |
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| ms | <code>Number</code> | | delay before resolve the promise with specified value |
| value | | | |
| [options] | <code>object</code> | | |
| [options.progressTick] | <code>number</code> | <code>1000</code> | progress timer tick, must be >= 100ms |

<a name="module_CPromise..CPromise.all"></a>

Expand Down
Empty file added README2.md
Empty file.
32 changes: 31 additions & 1 deletion lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,16 +1014,46 @@ class CPromise extends Promise {
* Returns a CPromise that will be resolved after specified timeout
* @param {Number} ms - delay before resolve the promise with specified value
* @param value
* @param {object} [options]
* @param {number} [options.progressTick= 1000] progress timer tick, must be >= 100ms
* @returns {CPromise}
*/

static delay(ms, value) {
static delay(ms, value, options) {
return new this((resolve, reject, scope) => {
if (!Number.isFinite(ms)) {
throw TypeError('timeout must be a finite number');
}

const timer = setTimeout(() => resolve(value), ms);
scope.onCancel(() => clearTimeout(timer));

const {progressTick= 1000}= options!==undefined ? validateOptions(options, {
progressTick: numberFinitePositive
}) : {};

if (progressTick) {
if (progressTick < 100) {
throw Error('progressTick must be grater than 100ms to avoid performance impact');
}

const captureProgress= ()=>{
if (ms > progressTick * 1.5) {
let timestamp = Date.now();
const progressTimer = setInterval(() => {
scope.progress((Date.now() - timestamp) / ms);
}, progressTick);

scope.on('done', () => clearTimeout(progressTimer));
}
}

if(scope.isCaptured){
captureProgress();
}else{
scope.onCapture(captureProgress);
}
}
})
}

Expand Down
60 changes: 37 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion test/tests/CPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ module.exports = {
}
},
},

'CPromise.retry': {
'should reject if all retries failed': async () => {
let counter = 0;
Expand Down Expand Up @@ -898,6 +899,30 @@ module.exports = {
assert.strictEqual(value, 123);
})
}
}
},

'CPromise.delay': {
'progress capturing' : async()=>{
let index= 0;
const time= 1800;
const tick= 500;
const calc= (index)=> (index * tick) / time;
const arr= [calc(1), calc(2), calc(3), 1];

return new Promise((resolve, reject)=>{
CPromise.run(function*(){
yield CPromise.delay(time, undefined, {progressTick: tick});
}).progress(v=> {
try{
assert.ok(Math.abs(v- arr[index])<0.1, `${v}!=${arr[index]} (${arr})`);
index++;
}catch(err){
reject(err);
}
}).then(()=>{
assert.strictEqual(index, 4);
}).then(resolve, reject);
});
}
}
};

0 comments on commit eb32ab5

Please sign in to comment.