fragile-promise
Version:
A modern Promise implementation with timeout and cancellation using AbortController.
147 lines (136 loc) • 6.52 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FragilePromise = {}));
})(this, (function (exports) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
function __classPrivateFieldGet(receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
}
function __classPrivateFieldSet(receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var _FragilePromise_timeout;
class TimeoutError extends Error {
constructor(message) {
super(message !== null && message !== void 0 ? message : 'timeout error');
this.name = 'TimeoutError';
}
}
class CancellationError extends Error {
constructor(message) {
super(message !== null && message !== void 0 ? message : 'cancellation error');
this.name = 'CancellationError';
}
}
class FragilePromise extends Promise {
constructor(executor) {
const abortController = new AbortController();
super((resolve, reject) => {
const handleAbort = () => {
if (__classPrivateFieldGet(this, _FragilePromise_timeout, "f")) {
clearTimeout(__classPrivateFieldGet(this, _FragilePromise_timeout, "f"));
}
reject(abortController.signal.reason);
abortController.signal.removeEventListener('abort', handleAbort);
};
abortController.signal.addEventListener('abort', handleAbort);
executor(resolve, reject);
});
_FragilePromise_timeout.set(this, void 0);
this.abortController = abortController;
this.abortSignal = abortController.signal;
}
then(onfulfilled, onrejected) {
return Promise.prototype.then.call(this, onfulfilled, onrejected);
}
catch(onrejected) {
return Promise.prototype.catch.call(this, onrejected);
}
finally(onfinally) {
return Promise.prototype.finally.call(this, onfinally);
}
cancel() {
this.abortController.abort(CancellationError);
}
timeout(milliseconds) {
if (__classPrivateFieldGet(this, _FragilePromise_timeout, "f")) {
clearTimeout(__classPrivateFieldGet(this, _FragilePromise_timeout, "f"));
}
__classPrivateFieldSet(this, _FragilePromise_timeout, setTimeout(() => {
this.abortController.abort(TimeoutError);
}, milliseconds), "f");
return this;
}
delay(milliseconds) {
return new FragilePromise(resolve => {
setTimeout((() => {
resolve(this);
}).bind(this), milliseconds);
});
}
isCancelled() {
return (this.abortSignal.aborted &&
this.abortSignal.reason.name === 'CancellationError');
}
isTimedOut() {
return (this.abortSignal.aborted &&
this.abortSignal.reason.name === 'TimeoutError');
}
static isCancellationError(error) {
return error.name === 'CancellationError';
}
static isTimeoutError(error) {
return error.name === 'TimeoutError';
}
static withResolvers() {
let resolve = () => { };
let reject = () => { };
const promise = new FragilePromise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
reject,
resolve,
};
}
static all(values) {
return new FragilePromise((resolve, reject) => {
Promise.all(values).then(resolve).catch(reject);
});
}
static reject(reason) {
return new FragilePromise((_, reject) => reject(reason));
}
static resolve(value) {
return new FragilePromise(resolve => resolve(Promise.resolve(value)));
}
}
_FragilePromise_timeout = new WeakMap();
exports.CancellationError = CancellationError;
exports.FragilePromise = FragilePromise;
exports.TimeoutError = TimeoutError;
}));