@twilio/mcs-client
Version:
Twilio Media Content Service client library
99 lines (90 loc) • 3.4 kB
JavaScript
/*
@license
Copyright (c) 2018, Twilio, Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
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.
*/
;
var global =
typeof global !== "undefined"
? global
: typeof self !== "undefined"
? self
: typeof window !== "undefined"
? window
: {};
Object.defineProperty(exports, '__esModule', { value: true });
var index = require('./node_modules/uuid/index.js');
/**
* Cancellable promise. Extends the functionality of the native Promise to include the cancel method.
*
* Example:
*
* ```ts
*
* const cancellableFetchPromise = new CancellablePromise(async (resolve, reject, onCancel) => {
* const request = fetch("https://example.com/");
*
* onCancel(() => request.cancel());
*
* try {
* const response = await request;
* resolve(response);
* } catch (err) {
* reject(err);
* }
* });
*
* cancellableFetchPromise.cancel();
* ```
*/
class CancellablePromise extends Promise {
/**
* Creates a new CancellablePromise.
* @param executor A callback used to initialize the promise. This callback is passed three arguments:
* a resolve callback used to resolve the promise with a value or the result of another promise,
* a reject callback used to reject the promise with a provided reason or error,
* and an onCancel callback used to define behavior of cancellation.
*/
constructor(executor) {
const outerId = index["default"].v4();
let outerRejectPromise;
super((resolve, reject) => {
outerRejectPromise = reject;
return executor((value) => {
CancellablePromise.cancellationMap.delete(outerId);
resolve(value);
}, (reason) => {
CancellablePromise.cancellationMap.delete(outerId);
reject(reason);
}, (cancellationFunction) => {
CancellablePromise.cancellationMap.set(outerId, cancellationFunction);
});
});
this.id = outerId;
this.rejectPromise = outerRejectPromise;
}
/**
* Cancels the promise and invokes the cancellation callback if it was defined during instantiation. Cancellation will result in the promise being rejected.
*/
cancel() {
const onCancel = CancellablePromise.cancellationMap.get(this.id);
onCancel === null || onCancel === void 0 ? void 0 : onCancel();
if (this.rejectPromise) {
this.catch(() => void 0);
this.rejectPromise(new Error("Promise was cancelled"));
}
return this;
}
}
CancellablePromise.cancellationMap = new Map();
exports.CancellablePromise = CancellablePromise;
//# sourceMappingURL=cancellable-promise.js.map