-
-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathHook.js
More file actions
229 lines (202 loc) · 5.69 KB
/
Hook.js
File metadata and controls
229 lines (202 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const util = require("util");
const deprecateContext = util.deprecate(
() => {},
"Hook.context is deprecated and will be removed"
);
function CALL_DELEGATE(...args) {
this.call = this._createCall("sync");
return this.call(...args);
}
function CALL_ASYNC_DELEGATE(...args) {
this.callAsync = this._createCall("async");
return this.callAsync(...args);
}
function PROMISE_DELEGATE(...args) {
this.promise = this._createCall("promise");
return this.promise(...args);
}
class Hook {
constructor(args = [], name = undefined) {
this._args = args;
this.name = name;
this.taps = [];
this.interceptors = [];
this._call = CALL_DELEGATE;
this.call = CALL_DELEGATE;
this._callAsync = CALL_ASYNC_DELEGATE;
this.callAsync = CALL_ASYNC_DELEGATE;
this._promise = PROMISE_DELEGATE;
this.promise = PROMISE_DELEGATE;
this._x = undefined;
// eslint-disable-next-line no-self-assign
this.compile = this.compile;
// eslint-disable-next-line no-self-assign
this.tap = this.tap;
// eslint-disable-next-line no-self-assign
this.tapAsync = this.tapAsync;
// eslint-disable-next-line no-self-assign
this.tapPromise = this.tapPromise;
}
compile(_options) {
throw new Error("Abstract: should be overridden");
}
_createCall(type) {
return this.compile({
taps: this.taps,
interceptors: this.interceptors,
args: this._args,
type
});
}
_tap(type, options, fn) {
if (typeof options === "string") {
// Fast path: a string options ("name") is by far the most common
// case. Build the final descriptor in a single allocation instead
// of creating `{ name }` and then `Object.assign`ing it.
const name = options.trim();
if (name === "") {
throw new Error("Missing name for tap");
}
options = { type, fn, name };
} else {
if (typeof options !== "object" || options === null) {
throw new Error("Invalid tap options");
}
let { name } = options;
if (typeof name === "string") {
name = name.trim();
}
if (typeof name !== "string" || name === "") {
throw new Error("Missing name for tap");
}
if (typeof options.context !== "undefined") {
deprecateContext();
}
// Fast path: only `name` is set. Build the descriptor as a literal
// so `_insert` and downstream consumers see the same hidden class
// as the string-options path, avoiding a polymorphic call site.
if (
options.before === undefined &&
options.stage === undefined &&
options.context === undefined &&
options.type === undefined &&
options.fn === undefined
) {
options = { type, fn, name };
} else {
options.name = name;
// Preserve previous precedence: user-provided keys win over the internal `type`/`fn`.
options = Object.assign({ type, fn }, options);
}
}
options = this._runRegisterInterceptors(options);
this._insert(options);
}
tap(options, fn) {
this._tap("sync", options, fn);
}
tapAsync(options, fn) {
this._tap("async", options, fn);
}
tapPromise(options, fn) {
this._tap("promise", options, fn);
}
_runRegisterInterceptors(options) {
const { interceptors } = this;
const { length } = interceptors;
// Common case: no interceptors.
if (length === 0) return options;
for (let i = 0; i < length; i++) {
const interceptor = interceptors[i];
if (interceptor.register) {
const newOptions = interceptor.register(options);
if (newOptions !== undefined) {
options = newOptions;
}
}
}
return options;
}
withOptions(options) {
const mergeOptions = (opt) =>
Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt);
return {
name: this.name,
tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
intercept: (interceptor) => this.intercept(interceptor),
isUsed: () => this.isUsed(),
withOptions: (opt) => this.withOptions(mergeOptions(opt))
};
}
isUsed() {
return this.taps.length > 0 || this.interceptors.length > 0;
}
intercept(interceptor) {
this._resetCompilation();
this.interceptors.push(Object.assign({}, interceptor));
if (interceptor.register) {
for (let i = 0; i < this.taps.length; i++) {
this.taps[i] = interceptor.register(this.taps[i]);
}
}
}
_resetCompilation() {
this.call = this._call;
this.callAsync = this._callAsync;
this.promise = this._promise;
}
_insert(item) {
this._resetCompilation();
const { taps } = this;
const stage = typeof item.stage === "number" ? item.stage : 0;
// Fast path: the overwhelmingly common `hook.tap("name", fn)` case
// has no `before` and default stage 0. If the list is empty or the
// last tap's stage is <= the new item's stage the item belongs at
// the end - append in O(1), skipping the Set allocation and the
// shift loop.
if (!(typeof item.before === "string" || Array.isArray(item.before))) {
const n = taps.length;
if (n === 0 || (taps[n - 1].stage || 0) <= stage) {
taps[n] = item;
return;
}
}
let before;
if (typeof item.before === "string") {
before = new Set([item.before]);
} else if (Array.isArray(item.before)) {
before = new Set(item.before);
}
let i = taps.length;
while (i > 0) {
i--;
const tap = taps[i];
taps[i + 1] = tap;
const xStage = tap.stage || 0;
if (before) {
if (before.has(tap.name)) {
before.delete(tap.name);
continue;
}
if (before.size > 0) {
continue;
}
}
if (xStage > stage) {
continue;
}
i++;
break;
}
taps[i] = item;
}
}
Object.setPrototypeOf(Hook.prototype, null);
module.exports = Hook;