|
1 | 1 | /* global initSqlJs */
|
2 | 2 | /* eslint-env worker */
|
| 3 | +/* eslint func-names: ["off"] */ |
3 | 4 | /* eslint no-restricted-globals: ["error"] */
|
4 |
| -var db; |
5 | 5 |
|
6 |
| -function onModuleReady(SQL) { |
| 6 | + |
| 7 | +// encapsulate web-worker code to run in any env |
| 8 | +(function () { |
7 | 9 | "use strict";
|
8 | 10 |
|
9 |
| - function createDb(data) { |
10 |
| - if (db != null) db.close(); |
11 |
| - db = new SQL.Database(data); |
12 |
| - return db; |
| 11 | + // isomorphism - do not run worker.js code if not in web-worker env |
| 12 | + if (!( |
| 13 | + typeof self === "object" |
| 14 | + && typeof importScripts === "function" |
| 15 | + && typeof self |
| 16 | + && self.importScripts === importScripts |
| 17 | + )) { |
| 18 | + return; |
13 | 19 | }
|
14 | 20 |
|
15 |
| - var buff; var data; var result; |
16 |
| - data = this["data"]; |
17 |
| - switch (data && data["action"]) { |
18 |
| - case "open": |
19 |
| - buff = data["buffer"]; |
20 |
| - createDb(buff && new Uint8Array(buff)); |
21 |
| - return postMessage({ |
22 |
| - id: data["id"], |
23 |
| - ready: true |
24 |
| - }); |
25 |
| - case "exec": |
26 |
| - if (db === null) { |
27 |
| - createDb(); |
28 |
| - } |
29 |
| - if (!data["sql"]) { |
30 |
| - throw "exec: Missing query string"; |
31 |
| - } |
32 |
| - return postMessage({ |
33 |
| - id: data["id"], |
34 |
| - results: db.exec(data["sql"], data["params"]) |
35 |
| - }); |
36 |
| - case "each": |
37 |
| - if (db === null) { |
38 |
| - createDb(); |
39 |
| - } |
40 |
| - var callback = function callback(row) { |
| 21 | + // Declare toplevel variables |
| 22 | + var db; |
| 23 | + var sqlModuleReady; |
| 24 | + |
| 25 | + function onModuleReady(SQL) { |
| 26 | + function createDb(data) { |
| 27 | + if (db != null) db.close(); |
| 28 | + db = new SQL.Database(data); |
| 29 | + return db; |
| 30 | + } |
| 31 | + |
| 32 | + var buff; var data; var result; |
| 33 | + data = this["data"]; |
| 34 | + switch (data && data["action"]) { |
| 35 | + case "open": |
| 36 | + buff = data["buffer"]; |
| 37 | + createDb(buff && new Uint8Array(buff)); |
41 | 38 | return postMessage({
|
42 | 39 | id: data["id"],
|
43 |
| - row: row, |
44 |
| - finished: false |
| 40 | + ready: true |
45 | 41 | });
|
46 |
| - }; |
47 |
| - var done = function done() { |
| 42 | + case "exec": |
| 43 | + if (db === null) { |
| 44 | + createDb(); |
| 45 | + } |
| 46 | + if (!data["sql"]) { |
| 47 | + throw "exec: Missing query string"; |
| 48 | + } |
48 | 49 | return postMessage({
|
49 | 50 | id: data["id"],
|
50 |
| - finished: true |
| 51 | + results: db.exec(data["sql"], data["params"]) |
51 | 52 | });
|
52 |
| - }; |
53 |
| - return db.each(data["sql"], data["params"], callback, done); |
54 |
| - case "export": |
55 |
| - buff = db["export"](); |
56 |
| - result = { |
57 |
| - id: data["id"], |
58 |
| - buffer: buff |
59 |
| - }; |
60 |
| - try { |
61 |
| - return postMessage(result, [result]); |
62 |
| - } catch (error) { |
63 |
| - return postMessage(result); |
64 |
| - } |
65 |
| - case "close": |
66 |
| - return db && db.close(); |
67 |
| - default: |
68 |
| - throw new Error("Invalid action : " + (data && data["action"])); |
| 53 | + case "each": |
| 54 | + if (db === null) { |
| 55 | + createDb(); |
| 56 | + } |
| 57 | + var callback = function callback(row) { |
| 58 | + return postMessage({ |
| 59 | + id: data["id"], |
| 60 | + row: row, |
| 61 | + finished: false |
| 62 | + }); |
| 63 | + }; |
| 64 | + var done = function done() { |
| 65 | + return postMessage({ |
| 66 | + id: data["id"], |
| 67 | + finished: true |
| 68 | + }); |
| 69 | + }; |
| 70 | + return db.each(data["sql"], data["params"], callback, done); |
| 71 | + case "export": |
| 72 | + buff = db["export"](); |
| 73 | + result = { |
| 74 | + id: data["id"], |
| 75 | + buffer: buff |
| 76 | + }; |
| 77 | + try { |
| 78 | + return postMessage(result, [result]); |
| 79 | + } catch (error) { |
| 80 | + return postMessage(result); |
| 81 | + } |
| 82 | + case "close": |
| 83 | + return db && db.close(); |
| 84 | + default: |
| 85 | + throw new Error("Invalid action : " + (data && data["action"])); |
| 86 | + } |
69 | 87 | }
|
70 |
| -} |
71 | 88 |
|
72 |
| -function onError(err) { |
73 |
| - "use strict"; |
74 |
| - |
75 |
| - return postMessage({ |
76 |
| - id: this["data"]["id"], |
77 |
| - error: err["message"] |
78 |
| - }); |
79 |
| -} |
| 89 | + function onError(err) { |
| 90 | + return postMessage({ |
| 91 | + id: this["data"]["id"], |
| 92 | + error: err["message"] |
| 93 | + }); |
| 94 | + } |
80 | 95 |
|
81 |
| -if (typeof importScripts === "function") { |
| 96 | + // init web-worker onmessage event-handling |
82 | 97 | db = null;
|
83 |
| - var sqlModuleReady = initSqlJs(); |
| 98 | + sqlModuleReady = initSqlJs(); |
84 | 99 | self.onmessage = function onmessage(event) {
|
85 |
| - "use strict"; |
86 |
| - |
87 | 100 | return sqlModuleReady
|
88 | 101 | .then(onModuleReady.bind(event))
|
89 | 102 | .catch(onError.bind(event));
|
90 | 103 | };
|
91 |
| -} |
| 104 | +}()); |
0 commit comments