-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataProcessorController.js
74 lines (66 loc) · 2.04 KB
/
dataProcessorController.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
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
import webworkify from 'webworkify-webpack'
import EventsConfig from './config/EventsConfig'
import BaseClass from "./base/BaseClass";
export default class DataProcessorController extends BaseClass {
constructor(options) {
super(options);
this.libPath = window.location.origin + "/lib/"
this.processors = []
}
init() {
this.initWorkers()
this.bindEvent()
this.loadjs()
}
bindEvent() {
this.events.on(EventsConfig.DateIn, (data) => {
this.processors[data.index].postMessage({
type: 'in',
data: data.data,
pts: data.pts
})
})
this.events.on(EventsConfig.DateClose, () => {
for (let i = 0; i < this.processors; i++) {
this.processors[i].postMessage({
type: 'close',
})
}
})
}
initWorkers() {
// 创建worker
for (let i = 0; i < this.options.worker; i++) {
let processor = webworkify(require.resolve('./dataProcessor.js'))
processor.id = i
processor.onmessage = (event) => {
let workerData = event.data
let type = workerData.type
let data = workerData.data
switch (type) {
case 'dataProcessorReady':
this.onDataProcessorReady()
break
case 'decoded':
this.onDecoded(data)
break
}
}
this.processors.push(processor)
}
}
loadjs() {
for (let i = 0; i < this.processors.length; i++) {
this.processors[i].postMessage({
type: 'loadwasm',
libPath: this.libPath
})
}
}
onDecoded(data) {
this.events.emit(EventsConfig.DecodeDecoded, data)
}
onDataProcessorReady() {
this.events.emit(EventsConfig.DataProcessorReady)
}
}