Skip to content

Commit

Permalink
Added @canceled decorator;
Browse files Browse the repository at this point in the history
Removed playground/dist/ from the repo;
  • Loading branch information
DigitalBrainJS committed Dec 6, 2020
1 parent 9163e1e commit 7b53ca3
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 574 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.idea/
/test/tests/decorators.js
/test/tests/decorators.legacy.js
/playground/dist/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

For changes before version 0.4.0, please see the commit history

## [0.10.2] - 2020-12-08

### Added
- @canceled decorator

## [0.10.1] - 2020-12-07

### Updated
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [@async](#asynctimeout-number)
- [@listen](#listensignal-abortsignalstringsymbol)
- [@cancel](#cancelreason-string-signal-abortsignalstringsymbol)
- [@canceled](#canceledonrejectederr-scope-context-function)
- [@timeout](#timeoutms-number)
- [@innerWeight](#innerweightweight-number)
- [@label](#labellabel-string)
Expand Down Expand Up @@ -520,6 +521,9 @@ If this argument not specified or null, the internal default AbortController wil
### @cancel([reason: String], [signal: AbortSignal|String|Symbol])
Emits the cancel signal before the target function invoking.

### @canceled([onRejected(err, scope, context): Function])
Catches rejections with CanceledError errors

````javascript
import cpFetch from "cpFetch";

Expand Down Expand Up @@ -634,6 +638,12 @@ innerWeight decorator
### CPromise.label : <code>function</code>
label decorator

**Kind**: static property of [<code>CPromise</code>](#module_CPromise)
<a name="module_CPromise.canceled"></a>

### CPromise.canceled : <code>function</code>
label decorator

**Kind**: static property of [<code>CPromise</code>](#module_CPromise)
<a name="module_CPromise..CPromise"></a>

Expand Down
4 changes: 4 additions & 0 deletions jsdoc2md/README.hbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [@async](#asynctimeout-number)
- [@listen](#listensignal-abortsignalstringsymbol)
- [@cancel](#cancelreason-string-signal-abortsignalstringsymbol)
- [@canceled](#canceledonrejectederr-scope-context-function)
- [@timeout](#timeoutms-number)
- [@innerWeight](#innerweightweight-number)
- [@label](#labellabel-string)
Expand Down Expand Up @@ -520,6 +521,9 @@ If this argument not specified or null, the internal default AbortController wil
### @cancel([reason: String], [signal: AbortSignal|String|Symbol])
Emits the cancel signal before the target function invoking.

### @canceled([onRejected(err, scope, context): Function])
Catches rejections with CanceledError errors

````javascript
import cpFetch from "cpFetch";

Expand Down
66 changes: 46 additions & 20 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @typedef {String|Symbol} EventType
*/
const {CanceledError} = require('./canceled-error');
const {E_REASON_CANCELED, E_REASON_TIMEOUT, E_REASON_DISPOSED}= CanceledError;
const {E_REASON_CANCELED, E_REASON_TIMEOUT, E_REASON_DISPOSED} = CanceledError;
const {AbortController, AbortControllerEx, isAbortSignal, isAbortController} = require('./abort-controller');
const {validateOptions, validators} = require('./validator');
const {
Expand Down Expand Up @@ -100,7 +100,7 @@ class CPromise extends Promise {
}
}

let {label, weight, timeout, signal, nativeController= false} = options || {};
let {label, weight, timeout, signal, nativeController = false} = options || {};

let resolve, reject;

Expand Down Expand Up @@ -469,10 +469,10 @@ class CPromise extends Promise {
*/

get signal() {
const shadow= this[_shadow];
const shadow = this[_shadow];
if (this[_shadow].controller) return this[_shadow].controller.signal;

return (this[_shadow].controller = new (shadow.nativeController? AbortController : AbortControllerEx)()).signal;
return (this[_shadow].controller = new (shadow.nativeController ? AbortController : AbortControllerEx)()).signal;
}

/**
Expand Down Expand Up @@ -588,7 +588,8 @@ class CPromise extends Promise {
const resolve = () => {
if (isRejected) {
shadow.value = value;
shadow.isCanceled && !shadow.leafsCount && super.then(null, () => {});
shadow.isCanceled && !shadow.leafsCount && super.then(null, () => {
});
this.emit('done', value);
shadow.reject(value);
} else {
Expand Down Expand Up @@ -1621,8 +1622,8 @@ const decorators = {

decorator.descriptor = {
value: function (...args) {
let promise = CPromise.resolveGenerator(originalFn, {
context,
let promise = context.resolveGenerator(originalFn, {
context: this,
args
});

Expand Down Expand Up @@ -1701,6 +1702,26 @@ const decorators = {
return fn.apply(this, arguments);
}

return decorator;
},

canceled: (decorator, [arg0]) => {
if (arg0 != null && typeof arg0 !== 'function') {
throw TypeError(`@canceled decorator expects a function as the first argument`);
}

const fn = decorator.descriptor.value;

decorator.descriptor.value = function () {
const result = fn.apply(this, arguments);

if (!(result instanceof CPromise)) {
throw TypeError(`@canceled decorator can only be used for async function only, that returns CPromise instance`);
}

return result.canceled((err, scope)=> arg0.call(this, err, scope, this));
}

return decorator;
}
};
Expand Down Expand Up @@ -1752,63 +1773,68 @@ Object.defineProperties(CPromise, {
CPromise: {value: CPromise}
})

exports.CPromise= CPromise;
exports.CPromise = CPromise;
/**
* CanceledError class
* @type {CanceledError}
*/
exports.CanceledError= CanceledError;
exports.CanceledError = CanceledError;
/**
* Refers to the AbortController class (native if available)
* @type {AbortController|AbortControllerEx}
*/
exports.AbortController= AbortController;
exports.AbortController = AbortController;

/**
* AbortControllerEx class
* @type {AbortControllerEx}
*/
exports.AbortControllerEx= AbortControllerEx;
exports.AbortControllerEx = AbortControllerEx;

/**
* Generic cancellation reason
*/
exports.E_REASON_CANCELED= E_REASON_CANCELED;
exports.E_REASON_CANCELED = E_REASON_CANCELED;
/**
* Cancellation reason for the case when the instance will be disposed
*/
exports.E_REASON_DISPOSED= E_REASON_DISPOSED;
exports.E_REASON_DISPOSED = E_REASON_DISPOSED;
/**
* Timeout cancellation reason
*/
exports.E_REASON_TIMEOUT= E_REASON_TIMEOUT;
exports.E_REASON_TIMEOUT = E_REASON_TIMEOUT;
/**
* async decorator
* @type {Function}
*/
exports.async= CPromise.async;
exports.async = CPromise.async;
/**
* listen decorator
* @type {Function}
*/
exports.listen= CPromise.listen;
exports.listen = CPromise.listen;
/**
* cancel decorator
* @type {Function}
*/
exports.cancel= CPromise.cancel;
exports.cancel = CPromise.cancel;
/**
* timeout decorator
* @type {Function}
*/
exports.timeout= CPromise.timeout;
exports.timeout = CPromise.timeout;
/**
* innerWeight decorator
* @type {Function}
*/
exports.innerWeight= CPromise.innerWeight;
exports.innerWeight = CPromise.innerWeight;
/**
* label decorator
* @type {Function}
*/
exports.label = CPromise.label;
/**
* label decorator
* @type {Function}
*/
exports.label= CPromise.label;
exports.canceled = CPromise.canceled;
Loading

0 comments on commit 7b53ca3

Please sign in to comment.