Skip to content

Commit

Permalink
Added debounce option for scope.progress;
Browse files Browse the repository at this point in the history
Update README.hbs.md;
  • Loading branch information
DigitalBrainJS committed Sep 11, 2020
1 parent b41a296 commit 295d433
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
![Travis (.com)](https://img.shields.io/travis/com/DigitalBrainJS/c-promise)
[![Coverage Status](https://coveralls.io/repos/github/DigitalBrainJS/c-promise/badge.svg?branch=master)](https://coveralls.io/github/DigitalBrainJS/c-promise?branch=master)
![npm](https://img.shields.io/npm/dm/c-promise2)
![npm bundle size](https://img.shields.io/bundlephobia/min/c-promise2)
![David](https://img.shields.io/david/DigitalBrainJS/c-promise)

## SYNOPSIS :sparkles:
Expand Down Expand Up @@ -47,13 +48,14 @@ If cancellation failed (the chain has been already fulfilled) it will return `fa

This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like
````javascript
function fetchWithTimeout(url, timeout) {
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {signal}).then(resolve, reject)
}).timeout(timeout)
function fetchWithTimeout(url, options) {
const {timeout, ...fetchOptions}= options;
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {...fetchOptions, signal}).then(resolve, reject)
}, timeout)
}

const chain= fetchWithTimeout('http://localhost/', 5000);
const chain= fetchWithTimeout('http://localhost/', {timeout: 5000});
// chain.cancel();
````

Expand Down Expand Up @@ -104,20 +106,6 @@ CPromise.delay(1000, 'It works!').then(str => console.log('Done', str));
- Run this file using npm run playground or npm run playground:watch command to see the result

## Usage example
A font-end example of wrapping fetch to the CPromise and handling cancellation using signals (AbortController)
````javascript
function cancelableFetch(url) {
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {signal}).then(resolve, reject);
})
}
// URL with 5 seconds delay to respond
const chain= cancelableFetch('https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s')
.then(console.log, console.warn);

setTimeout(()=> chain.cancel(), 1000);
````

Handling cancellation with `onCancel` listeners (see the [live demo](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2)):
````javascript
import CPromise from "c-promise";
Expand Down Expand Up @@ -210,7 +198,7 @@ Cancellable Promise with extra features
* [.isPending](#module_CPromise..CPromiseScope+isPending) ⇒ <code>Boolean</code>
* [.isCanceled](#module_CPromise..CPromiseScope+isCanceled) ⇒ <code>Boolean</code>
* [.onCancel(listener)](#module_CPromise..CPromiseScope+onCancel)
* [.progress([value], [data])](#module_CPromise..CPromiseScope+progress)
* [.progress([value], [data], options)](#module_CPromise..CPromiseScope+progress)
* [.propagate(type, data)](#module_CPromise..CPromiseScope+propagate) ⇒ <code>CPromiseScope</code>
* [.captureProgress()](#module_CPromise..CPromiseScope+captureProgress) ⇒ <code>CPromiseScope</code>
* [.scopes()](#module_CPromise..CPromiseScope+scopes) ⇒ <code>Array.&lt;CPromiseScope&gt;</code>
Expand Down Expand Up @@ -262,7 +250,7 @@ Scope for CPromises instances
* [.isPending](#module_CPromise..CPromiseScope+isPending) ⇒ <code>Boolean</code>
* [.isCanceled](#module_CPromise..CPromiseScope+isCanceled) ⇒ <code>Boolean</code>
* [.onCancel(listener)](#module_CPromise..CPromiseScope+onCancel)
* [.progress([value], [data])](#module_CPromise..CPromiseScope+progress)
* [.progress([value], [data], options)](#module_CPromise..CPromiseScope+progress)
* [.propagate(type, data)](#module_CPromise..CPromiseScope+propagate) ⇒ <code>CPromiseScope</code>
* [.captureProgress()](#module_CPromise..CPromiseScope+captureProgress) ⇒ <code>CPromiseScope</code>
* [.scopes()](#module_CPromise..CPromiseScope+scopes) ⇒ <code>Array.&lt;CPromiseScope&gt;</code>
Expand Down Expand Up @@ -319,7 +307,7 @@ registers the listener for cancel event

<a name="module_CPromise..CPromiseScope+progress"></a>

#### cPromiseScope.progress([value], [data])
#### cPromiseScope.progress([value], [data], options)
Set promise progress

**Kind**: instance method of [<code>CPromiseScope</code>](#module_CPromise..CPromiseScope)
Expand All @@ -328,6 +316,8 @@ Set promise progress
| --- | --- | --- |
| [value] | <code>Number</code> | a number between [0, 1] |
| [data] | <code>\*</code> | any data to send for progress event listeners |
| options | <code>Object</code> | |
| options.debounce | <code>Number</code> | min interval in ms to emit the progress |

<a name="module_CPromise..CPromiseScope+propagate"></a>

Expand Down
26 changes: 7 additions & 19 deletions jsdoc2md/README.hbs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
![Travis (.com)](https://img.shields.io/travis/com/DigitalBrainJS/c-promise)
[![Coverage Status](https://coveralls.io/repos/github/DigitalBrainJS/c-promise/badge.svg?branch=master)](https://coveralls.io/github/DigitalBrainJS/c-promise?branch=master)
![npm](https://img.shields.io/npm/dm/c-promise2)
![npm bundle size](https://img.shields.io/bundlephobia/min/c-promise2)
![David](https://img.shields.io/david/DigitalBrainJS/c-promise)

## SYNOPSIS :sparkles:
Expand Down Expand Up @@ -47,13 +48,14 @@ If cancellation failed (the chain has been already fulfilled) it will return `fa

This is how an abortable fetch ([live example](https://jsfiddle.net/DigitalBrain/c6njyrt9/10/)) with a timeout might look like
````javascript
function fetchWithTimeout(url, timeout) {
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {signal}).then(resolve, reject)
}).timeout(timeout)
function fetchWithTimeout(url, options) {
const {timeout, ...fetchOptions}= options;
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {...fetchOptions, signal}).then(resolve, reject)
}, timeout)
}

const chain= fetchWithTimeout('http://localhost/', 5000);
const chain= fetchWithTimeout('http://localhost/', {timeout: 5000});
// chain.cancel();
````

Expand Down Expand Up @@ -104,20 +106,6 @@ CPromise.delay(1000, 'It works!').then(str => console.log('Done', str));
- Run this file using npm run playground or npm run playground:watch command to see the result

## Usage example
A font-end example of wrapping fetch to the CPromise and handling cancellation using signals (AbortController)
````javascript
function cancelableFetch(url) {
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {signal}).then(resolve, reject);
})
}
// URL with 5 seconds delay to respond
const chain= cancelableFetch('https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s')
.then(console.log, console.warn);

setTimeout(()=> chain.cancel(), 1000);
````

Handling cancellation with `onCancel` listeners (see the [live demo](https://runkit.com/digitalbrainjs/runkit-npm-c-promise2)):
````javascript
import CPromise from "c-promise";
Expand Down
24 changes: 23 additions & 1 deletion lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const {CanceledError}= require('./canceled-error');
const {AbortController: _AbortController}= require('./abort-controller');
const {TinyEventEmitter}= require('./tiny-event-emitter');

const {now}= Date;

const _scope = Symbol('scope');
const _handleCancelRejection = Symbol('handleCancelRejection');
const _isPending = Symbol('isPending');
Expand Down Expand Up @@ -158,16 +160,36 @@ class CPromiseScope extends TinyEventEmitter {
* Set promise progress
* @param {Number} [value] a number between [0, 1]
* @param {*} [data] any data to send for progress event listeners
* @param {Object} options
* @param {Number} options.debounce - min interval in ms to emit the progress
*/

progress(value, data){
progress(value, data, options){
const shadow = this[_shadow];

if(arguments.length){
if (!Number.isFinite(value)) {
throw TypeError('value must be a number [0, 1]');
}

if(options){
if(typeof options!=='object'){
throw TypeError('options must be an object');
}

const {debounce}= options;

if (debounce) {
const ts = shadow.debounceTS;
const date = now();
if (!ts || date - ts > debounce) {
shadow.debounceTS = date;
} else {
return this;
}
}
}

if (value < 0) {
value = 0;
} else if (value > 1) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"aborting",
"close",
"closable",
"task"
"task",
"p-cancelable"
],
"license": "MIT",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion playground/fetch-timeout.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
function fetchWithTimeout(url, timeout) {
return new CPromise((resolve, reject, {signal}) => {
fetch(url, {signal}).then(resolve, reject)
}).timeout(timeout)
}, timeout)
}

function request(timeout) {
Expand Down

0 comments on commit 295d433

Please sign in to comment.