This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkermain.js
124 lines (99 loc) · 3.23 KB
/
workermain.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
+function () {
'use strict';
var
currentMessageId = 0,
usingFakeIDB,
stringifyAndPost,
toEmscripten,
commandQueue = [],
xxx;
function doNothing() {
}
function fromEmscripten(create, remove) {
stringifyAndPost(MSGS.COMMAND_FINISHED, {
create: create,
remove: remove
});
}
function toEmscriptenReciever(a) {
toEmscripten = a;
}
function messageHandler(e) { //set up the onmessage global function handler
e = JSON.parse(e.data);
switch(e.messageType) {
case MSGS.TEST_FOR_IDB: { //report whether IndexedDB is available in a web worker
stringifyAndPost(MSGS.IDB_STATUS, usingFakeIDB);
} break;
case MSGS.UPDATE_FAKE_IDB: { //update the fake indexedDB in the web worker with changes that have happened on the main thread
toEmscripten(false, e.data.create, e.data.remove);
Module.FS.syncfs(4, localPersisted); //worker to memfs
} break;
case MSGS.RUN_COMMAND: { //run the specified command line, may post back changed file contents if IndexedDB doesn't work in a web worker
runCommand(e.data);
}
}
}
function localPersisted() {
stringifyAndPost(MSGS.FAKE_IDB_UPDATED);
}
function runCommand(commandLine) {
var path = commandLine.substring(0, commandLine.lastIndexOf("/"));
stdout(path+"/>querycsv "+JSON.stringify(commandLine));
commandQueue.push([path, commandLine]);
if(!usingFakeIDB) {
Module.FS.syncfs(true, runCommand2); //indexeddb to memfs
}
else {
runCommand2();
}
}
function runCommand2() {
//do whatever emscripten wants us to do to run the program.
var e = Module.ccall(
'wrapmain',
'number',
['string','string'],
commandQueue.shift()
);
if(usingFakeIDB) {
//return the response code and contents of changed files
Module.FS.syncfs(3, doNothing); //memfs to worker
}
else {
//if indexDB is functional in a web worker, then resync it then send the response code.
Module.FS.syncfs(false, resynced); //local to indexeddb
}
}
function resynced () {
stringifyAndPost(MSGS.COMMAND_FINISHED);
}
//pseudo code thats called whenever stdout or strerr are flushed. the main thread will probably append this text to a pre tag or something like it
function stdout(text) {
stringifyAndPost(MSGS.OUTPUT_TEXT, {
text:text,
isStderr :false
});
}
function stderr(text) {
stringifyAndPost(MSGS.OUTPUT_TEXT, {
text:text,
isStderr :true
});
}
function ready() {
usingFakeIDB = !(self.indexeddb = self.indexedDB || self.mozIndexedDB || self.webkitIndexedDB || self.msIndexedDB),
stringifyAndPost = stringifyAndPostFactory(self, JSON);
self.onmessage = messageHandler;
Module.print = stdout;
Module.printErr = stderr;
Module.FS.mkdir('/Documents');
Module.FS.mount(Module.FS.filesystems.IDBWFS, {
fromEmscripten: fromEmscripten,
toEmscriptenReciever: toEmscriptenReciever
}, '/Documents');
// sync from persisted state into memory and then
// refresh the folder view
Module.FS.syncfs(usingFakeIDB?4:true, doNothing);
}
ready();
}();