-
Notifications
You must be signed in to change notification settings - Fork 8
/
worker.ts
98 lines (84 loc) · 2.57 KB
/
worker.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import createXeusModule from './xeus_lua';
// We alias self to ctx and give it our newly created type
const ctx: Worker = self as any;
let raw_xkernel: any;
let raw_xserver: any;
let _parentHeader: any;
async function async_get_input_function(prompt: string) {
prompt = typeof prompt === 'undefined' ? '' : prompt;
await sendInputRequest({ prompt: prompt, password: false });
const replyPromise = new Promise(resolve => {
resolveInputReply = resolve;
});
const result: any = await replyPromise;
return result['value'];
}
async function sendInputRequest(content: any) {
ctx.postMessage({
parentHeader: _parentHeader,
content,
type: 'special_input_request'
});
}
// eslint-disable-next-line
// @ts-ignore: breaks typedoc
ctx.async_get_input_function = async_get_input_function;
// eslint-disable-next-line
// @ts-ignore: breaks typedoc
let resolveInputReply: any;
function postMessageToMain(message: any, channel: string) {
message.channel = channel;
message.type = message.header.msg_type;
message.parent_header = _parentHeader;
ctx.postMessage(message);
}
async function loadCppModule(moduleFactory: any): Promise<any> {
const options: any = {};
return moduleFactory(options).then((Module: any) => {
raw_xkernel = new Module.xkernel();
raw_xserver = raw_xkernel.get_server();
raw_xserver!.register_js_callback(
(type: string, channel: number, data: any) => {
data = JSON.parse(data);
switch (type) {
case 'shell': {
postMessageToMain(data, 'shell');
break;
}
case 'control': {
throw new Error('send_control is not yet implemented');
break;
}
case 'stdin': {
postMessageToMain(data, 'stdin');
break;
}
case 'publish': {
// TODO ask what to do with channel
postMessageToMain(data, 'iopub');
break;
}
}
}
);
raw_xkernel!.start();
});
}
const loadCppModulePromise = loadCppModule(createXeusModule);
ctx.onmessage = async (event: MessageEvent): Promise<void> => {
await loadCppModulePromise;
const data = event.data;
const msg = data.msg;
const msg_type = msg.header.msg_type;
if (msg_type !== 'input_reply') {
_parentHeader = data.parent['header'];
}
if (msg_type === 'input_reply') {
resolveInputReply(msg.content);
} else {
const str_msg = JSON.stringify(msg);
raw_xserver!.notify_listener(str_msg, msg.channel);
}
};