1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
class HookMap {
|
8 |
|
|
constructor(factory) {
|
9 |
|
|
this._map = new Map();
|
10 |
|
|
this._factory = factory;
|
11 |
|
|
this._interceptors = [];
|
12 |
|
|
}
|
13 |
|
|
|
14 |
|
|
get(key) {
|
15 |
|
|
return this._map.get(key);
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
for(key) {
|
19 |
|
|
const hook = this.get(key);
|
20 |
|
|
if (hook !== undefined) {
|
21 |
|
|
return hook;
|
22 |
|
|
}
|
23 |
|
|
let newHook = this._factory(key);
|
24 |
|
|
const interceptors = this._interceptors;
|
25 |
|
|
for (let i = 0; i < interceptors.length; i++) {
|
26 |
|
|
newHook = interceptors[i].factory(key, newHook);
|
27 |
|
|
}
|
28 |
|
|
this._map.set(key, newHook);
|
29 |
|
|
return newHook;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
intercept(interceptor) {
|
33 |
|
|
this._interceptors.push(
|
34 |
|
|
Object.assign(
|
35 |
|
|
{
|
36 |
|
|
factory: (key, hook) => hook
|
37 |
|
|
},
|
38 |
|
|
interceptor
|
39 |
|
|
)
|
40 |
|
|
);
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
tap(key, options, fn) {
|
44 |
|
|
return this.for(key).tap(options, fn);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
tapAsync(key, options, fn) {
|
48 |
|
|
return this.for(key).tapAsync(options, fn);
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
tapPromise(key, options, fn) {
|
52 |
|
|
return this.for(key).tapPromise(options, fn);
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
module.exports = HookMap;
|