Repository navigation
Expand file tree
/
Copy pathtapable.js
More file actions
62 lines (50 loc) · 1.57 KB
/
Copy pathtapable.js
File metadata and controls
62 lines (50 loc) · 1.57 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
import { SyncHook, AsyncParallelHook } from 'tapable'
class List {
getRoutes () {
}
}
class Car {
constructor() {
this.hooks = {
accelerate: new SyncHook(["newSpeed"]),
brake: new SyncHook(),
calculateRoutes: new AsyncParallelHook(["source", "target", "routesList"])
};
}
setSpeed (newSpeed) {
// following call returns undefined even when you returned values
this.hooks.accelerate.call(newSpeed);
}
useNavigationSystemPromise (source, target) {
const routesList = new List();
return this.hooks.calculateRoutes.promise(source, target, routesList).then((res) => {
// res is undefined for AsyncParallelHook
console.log('我是回调')
return routesList.getRoutes();
});
}
useNavigationSystemAsync (source, target, callback) {
const routesList = new List();
this.hooks.calculateRoutes.callAsync(source, target, routesList, err => {
if (err) return callback(err);
callback(null, routesList.getRoutes());
});
}
}
const myCar = new Car()
// 1. 注册
myCar.hooks.accelerate.tap('test 1', (speed) => {
console.log('accelerate', speed);
})
myCar.hooks.calculateRoutes.tapPromise('test promise', (source, target) => {
return new Promise((resolve) => {
setTimeout(() => {
// 先执行完注册事件,在执行回调
console.log('calculateRoutes', source, 'calculateRoutes:', target);
resolve()
}, 10);
})
})
// 2. 触发
myCar.setSpeed(10)
myCar.useNavigationSystemPromise('i am source', 'i am target')