opusdev/vector-similarity-api
1
1'use strict';2 3import utils from '../utils.js';4 5class InterceptorManager {6 constructor() {7 this.handlers = [];8 }9 10 /**11 * Add a new interceptor to the stack12 *13 * @param {Function} fulfilled The function to handle `then` for a `Promise`14 * @param {Function} rejected The function to handle `reject` for a `Promise`15 * @param {Object} options The options for the interceptor, synchronous and runWhen16 *17 * @return {Number} An ID used to remove interceptor later18 */19 use(fulfilled, rejected, options) {20 this.handlers.push({21 fulfilled,22 rejected,23 synchronous: options ? options.synchronous : false,24 runWhen: options ? options.runWhen : null25 });26 return this.handlers.length - 1;27 }28 29 /**30 * Remove an interceptor from the stack31 *32 * @param {Number} id The ID that was returned by `use`33 *34 * @returns {void}35 */36 eject(id) {37 if (this.handlers[id]) {38 this.handlers[id] = null;39 }40 }41 42 /**43 * Clear all interceptors from the stack44 *45 * @returns {void}46 */47 clear() {48 if (this.handlers) {49 this.handlers = [];50 }51 }52 53 /**54 * Iterate over all the registered interceptors55 *56 * This method is particularly useful for skipping over any57 * interceptors that may have become `null` calling `eject`.58 *59 * @param {Function} fn The function to call for each interceptor60 *61 * @returns {void}62 */63 forEach(fn) {64 utils.forEach(this.handlers, function forEachHandler(h) {65 if (h !== null) {66 fn(h);67 }68 });69 }70}71 72export default InterceptorManager;73 