@ace-fetch/uni-app
Version:
uni-app adapter for @ace-fetch/core.
61 lines (60 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterceptorManager = void 0;
var utils_1 = require("../utils");
var InterceptorManager = /** @class */ (function () {
function InterceptorManager() {
this.handlers = [];
}
/**
* Add a new interceptor to the stack
* @param {Function} fulfilled The function to handle `then` for a `Promise`
* @param {Function} rejected The function to handle `reject` for a `Promise`
* @return {Number} An ID used to remove interceptor later
*/
InterceptorManager.prototype.use = function (fulfilled, rejected, options) {
var _a, _b;
this.handlers.push({
fulfilled: fulfilled,
rejected: rejected,
synchronous: (_a = options === null || options === void 0 ? void 0 : options.synchronous) !== null && _a !== void 0 ? _a : false,
runWhen: (_b = options === null || options === void 0 ? void 0 : options.runWhen) !== null && _b !== void 0 ? _b : null,
});
return this.handlers.length - 1;
};
/**
* Remove an interceptor from the stack
* @param {Number} id The ID that was returned by `use`
* @returns {Boolean} `true` if the interceptor was removed, `false` otherwise
*/
InterceptorManager.prototype.eject = function (id) {
if (this.handlers[id]) {
this.handlers[id] = null;
}
};
/**
* Clear all interceptors from the stack
* @returns {void}
*/
InterceptorManager.prototype.clear = function () {
if (this.handlers) {
this.handlers = [];
}
};
/**
* Iterate over all the registered interceptors
* This method is particularly useful for skipping over any
* interceptors that may have become `null` calling `eject`.
* @param {Function} fn The function to call for each interceptor
* @returns {void}
*/
InterceptorManager.prototype.forEach = function (fn) {
(0, utils_1.forEach)(this.handlers, function forEachHandler(h) {
if (h !== null) {
fn(h);
}
});
};
return InterceptorManager;
}());
exports.InterceptorManager = InterceptorManager;