| 3 | import utils from '../utils.js'; |
| 4 | |
| 5 | class InterceptorManager { |
| 6 | constructor() { |
| 7 | this.handlers = []; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Add a new interceptor to the stack |
| 12 | * |
| 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 runWhen |
| 16 | * |
| 17 | * @return {Number} An ID used to remove interceptor later |
| 18 | */ |
| 19 | use(fulfilled, rejected, options) { |
| 20 | this.handlers.push({ |
| 21 | fulfilled, |
| 22 | rejected, |
| 23 | synchronous: options ? options.synchronous : false, |
| 24 | runWhen: options ? options.runWhen : null, |
| 25 | }); |
| 26 | return this.handlers.length - 1; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Remove an interceptor from the stack |
| 31 | * |
| 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 stack |
| 44 | * |
| 45 | * @returns {void} |
| 46 | */ |
| 47 | clear() { |
| 48 | if (this.handlers) { |
| 49 | this.handlers = []; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Iterate over all the registered interceptors |
| 55 | * |
| 56 | * This method is particularly useful for skipping over any |
| 57 | * interceptors that may have become `null` calling `eject`. |
| 58 | * |
| 59 | * @param {Function} fn The function to call for each interceptor |
| 60 | * |
| 61 | * @returns {void} |
| 62 | */ |
nothing calls this directly
no outgoing calls
no test coverage detected