-
Notifications
You must be signed in to change notification settings - Fork 0
/
01synchook.js
41 lines (33 loc) · 1.02 KB
/
01synchook.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
/*
* @Date: 2022-02-24 12:46:36
* @LastEditors: jimouspeng
* @Description: SyncHook-同步钩子
* @LastEditTime: 2022-02-24 13:01:19
* @FilePath: \tapable\SyncHook.js
*/
const { SyncHook } = require('tapable')
// All Hook constructors take one optional argument, which is a list of argument names as strings.
// 数组的长度对应参数的个数,参数只能为【非空】字符
const hook = new SyncHook(['jimous'])
// For sync hooks, tap is the only valid method to add a plugin
hook.tap('plugin1', (res) => {
console.log('plugin1', res)
})
hook.tap('plugin2', (res) => {
console.log('plugin2', res)
})
hook.tap('plugin3', (res) => {
console.log('plugin3', res)
})
hook.call('jimous~~')
class Car {
constructor() {
this.startHook = new SyncHook(['tips'])
}
start(tips) {
this.startHook.call(tips)
}
}
const car = new Car()
car.startHook.tap('driver', (res) => console.log(res, '确认安全带是否系好'))
car.start('出发!') // 出发! 确认安全带是否系好