-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloWorldWorker.ts
40 lines (32 loc) · 1.07 KB
/
HelloWorldWorker.ts
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
import {
comRouting,
RawPayload,
WorkerTaskCommandResponse,
WorkerMessage,
WorkerTaskWorker
} from 'wtd-core';
export class HelloWorldWorker implements WorkerTaskWorker {
init(message: WorkerMessage) {
const initComplete = WorkerMessage.createFromExisting(message, {
overrideCmd: WorkerTaskCommandResponse.INIT_COMPLETE
});
self.postMessage(initComplete);
}
execute(message: WorkerMessage) {
// burn some time
for (let i = 0; i < 25000000; i++) {
i++;
}
const rawPayload = new RawPayload({
hello: 'Hello! I just incremented "i" 25 million times.'
});
const execComplete = WorkerMessage.createFromExisting(message, {
overrideCmd: WorkerTaskCommandResponse.EXECUTE_COMPLETE
});
execComplete.addPayload(rawPayload);
// no need to pack as there aren't any buffers used
self.postMessage(execComplete);
}
}
const worker = new HelloWorldWorker();
self.onmessage = message => comRouting(worker, message);