Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+103
@@ -0,0 +1,103 @@
|
||||
'use strict';
|
||||
|
||||
class CancelError extends Error {
|
||||
constructor(reason) {
|
||||
super(reason || 'Promise was canceled');
|
||||
this.name = 'CancelError';
|
||||
}
|
||||
|
||||
get isCanceled() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class PCancelable {
|
||||
static fn(userFn) {
|
||||
return (...args) => {
|
||||
return new PCancelable((resolve, reject, onCancel) => {
|
||||
args.push(onCancel);
|
||||
userFn(...args).then(resolve, reject);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
constructor(executor) {
|
||||
this._cancelHandlers = [];
|
||||
this._isPending = true;
|
||||
this._isCanceled = false;
|
||||
this._rejectOnCancel = true;
|
||||
|
||||
this._promise = new Promise((resolve, reject) => {
|
||||
this._reject = reject;
|
||||
|
||||
const onResolve = value => {
|
||||
this._isPending = false;
|
||||
resolve(value);
|
||||
};
|
||||
|
||||
const onReject = error => {
|
||||
this._isPending = false;
|
||||
reject(error);
|
||||
};
|
||||
|
||||
const onCancel = handler => {
|
||||
this._cancelHandlers.push(handler);
|
||||
};
|
||||
|
||||
Object.defineProperties(onCancel, {
|
||||
shouldReject: {
|
||||
get: () => this._rejectOnCancel,
|
||||
set: bool => {
|
||||
this._rejectOnCancel = bool;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return executor(onResolve, onReject, onCancel);
|
||||
});
|
||||
}
|
||||
|
||||
then(onFulfilled, onRejected) {
|
||||
return this._promise.then(onFulfilled, onRejected);
|
||||
}
|
||||
|
||||
catch(onRejected) {
|
||||
return this._promise.catch(onRejected);
|
||||
}
|
||||
|
||||
finally(onFinally) {
|
||||
return this._promise.finally(onFinally);
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
if (!this._isPending || this._isCanceled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._cancelHandlers.length > 0) {
|
||||
try {
|
||||
for (const handler of this._cancelHandlers) {
|
||||
handler();
|
||||
}
|
||||
} catch (error) {
|
||||
this._reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
this._isCanceled = true;
|
||||
if (this._rejectOnCancel) {
|
||||
this._reject(new CancelError(reason));
|
||||
}
|
||||
}
|
||||
|
||||
get isCanceled() {
|
||||
return this._isCanceled;
|
||||
}
|
||||
}
|
||||
|
||||
Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);
|
||||
|
||||
module.exports = PCancelable;
|
||||
module.exports.default = PCancelable;
|
||||
|
||||
module.exports.CancelError = CancelError;
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"name": "p-cancelable",
|
||||
"version": "1.1.0",
|
||||
"description": "Create a promise that can be canceled",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/p-cancelable",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd-check"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"promise",
|
||||
"cancelable",
|
||||
"cancel",
|
||||
"canceled",
|
||||
"canceling",
|
||||
"cancellable",
|
||||
"cancellation",
|
||||
"abort",
|
||||
"abortable",
|
||||
"aborting",
|
||||
"cleanup",
|
||||
"task",
|
||||
"token",
|
||||
"async",
|
||||
"function",
|
||||
"await",
|
||||
"promises",
|
||||
"bluebird"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "^1.3.1",
|
||||
"delay": "^4.1.0",
|
||||
"promise.prototype.finally": "^3.1.0",
|
||||
"tsd-check": "^0.3.0",
|
||||
"xo": "^0.24.0"
|
||||
}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz"
|
||||
,"_integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw=="
|
||||
,"_from": "p-cancelable@^1.0.0"
|
||||
}
|
||||
Reference in new issue
Block a user