main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
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 | *
|
---|
16 | * @return {Number} An ID used to remove interceptor later
|
---|
17 | */
|
---|
18 | use(fulfilled, rejected, options) {
|
---|
19 | this.handlers.push({
|
---|
20 | fulfilled,
|
---|
21 | rejected,
|
---|
22 | synchronous: options ? options.synchronous : false,
|
---|
23 | runWhen: options ? options.runWhen : null
|
---|
24 | });
|
---|
25 | return this.handlers.length - 1;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Remove an interceptor from the stack
|
---|
30 | *
|
---|
31 | * @param {Number} id The ID that was returned by `use`
|
---|
32 | *
|
---|
33 | * @returns {Boolean} `true` if the interceptor was removed, `false` otherwise
|
---|
34 | */
|
---|
35 | eject(id) {
|
---|
36 | if (this.handlers[id]) {
|
---|
37 | this.handlers[id] = null;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Clear all interceptors from the stack
|
---|
43 | *
|
---|
44 | * @returns {void}
|
---|
45 | */
|
---|
46 | clear() {
|
---|
47 | if (this.handlers) {
|
---|
48 | this.handlers = [];
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Iterate over all the registered interceptors
|
---|
54 | *
|
---|
55 | * This method is particularly useful for skipping over any
|
---|
56 | * interceptors that may have become `null` calling `eject`.
|
---|
57 | *
|
---|
58 | * @param {Function} fn The function to call for each interceptor
|
---|
59 | *
|
---|
60 | * @returns {void}
|
---|
61 | */
|
---|
62 | forEach(fn) {
|
---|
63 | utils.forEach(this.handlers, function forEachHandler(h) {
|
---|
64 | if (h !== null) {
|
---|
65 | fn(h);
|
---|
66 | }
|
---|
67 | });
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | export default InterceptorManager;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.