-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathlifecycle.js
46 lines (40 loc) · 942 Bytes
/
lifecycle.js
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
import { noop } from '../util/core';
export function initLifecycle(vm) {
const hooks = [
'init',
'mounted',
'beforeEach',
'afterEach',
'doneEach',
'ready',
];
vm._hooks = {};
vm._lifecycle = {};
hooks.forEach(hook => {
const arr = (vm._hooks[hook] = []);
vm._lifecycle[hook] = fn => arr.push(fn);
});
}
export function callHook(vm, hookName, data, next = noop) {
const queue = vm._hooks[hookName];
const step = function(index) {
const hookFn = queue[index];
if (index >= queue.length) {
next(data);
} else if (typeof hookFn === 'function') {
if (hookFn.length === 2) {
hookFn(data, result => {
data = result;
step(index + 1);
});
} else {
const result = hookFn(data);
data = result === undefined ? data : result;
step(index + 1);
}
} else {
step(index + 1);
}
};
step(0);
}