forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (45 loc) 路 1.19 KB
/
Copy pathindex.js
File metadata and controls
50 lines (45 loc) 路 1.19 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
import { noop } from '../util/core';
/** This class sets of lifecycle hooks that plugins can hook into. */
export function lifecycleMixin(Base = class {}) {
return class Lifecycle extends Base {
initLifecycle() {
const hooks = [
'init',
'mounted',
'beforeEach',
'afterEach',
'doneEach',
'ready',
];
this._hooks = {};
this._lifecycle = {};
hooks.forEach(hook => {
const arr = (this._hooks[hook] = []);
this._lifecycle[hook] = fn => arr.push(fn);
});
}
callHook(hook, data, next = noop) {
const queue = this._hooks[hook];
const step = function(index) {
const hook = queue[index];
if (index >= queue.length) {
next(data);
} else if (typeof hook === 'function') {
if (hook.length === 2) {
hook(data, result => {
data = result;
step(index + 1);
});
} else {
const result = hook(data);
data = result === undefined ? data : result;
step(index + 1);
}
} else {
step(index + 1);
}
};
step(0);
}
};
}