|
1 | | -import {polyfill} from './polyfill'; |
2 | 1 | import {$} from "./util"; |
3 | 2 | import {updateDownloadLink} from "./download"; |
4 | 3 | import {restoreParams, storeParams} from "./localstorage"; |
| 4 | +import {WorkerMessage} from "./WorkerMessage"; |
5 | 5 |
|
6 | 6 | restoreParams(); |
7 | 7 |
|
8 | | -let largeInput; |
| 8 | +const worker = new Worker(new URL('./worker.js', import.meta.url)); |
| 9 | +let workerReady = false; |
| 10 | +let queued; |
9 | 11 |
|
10 | | -polyfill().then(() => import("../../json_typegen_wasm/pkg")).then(typegen_wasm => { |
11 | | - const render = () => { |
12 | | - const typename = $('typename').value; |
13 | | - let input = largeInput || $('input').value; |
14 | | - const options = ({ |
15 | | - output_mode: $('outputmode').value, |
16 | | - property_name_format: $('propertynameformat').value, |
17 | | - unwrap: $('unwrap').value, |
18 | | - }); |
| 12 | +const render = () => { |
| 13 | + const typename = $('typename').value; |
| 14 | + let input = $('input').value; |
| 15 | + const options = ({ |
| 16 | + output_mode: $('outputmode').value, |
| 17 | + property_name_format: $('propertynameformat').value, |
| 18 | + unwrap: $('unwrap').value, |
| 19 | + }); |
19 | 20 |
|
20 | | - const extraoptions_elem = $('extraoptions'); |
21 | | - const extraoptions_json = extraoptions_elem.value; |
22 | | - let extraoptions; |
23 | | - try { |
24 | | - extraoptions = extraoptions_json && JSON.parse(extraoptions_json); |
25 | | - extraoptions_elem.classList.remove("is-danger") |
26 | | - } catch (e) { |
27 | | - extraoptions_elem.classList.add("is-danger") |
28 | | - } |
| 21 | + const extraoptions_elem = $('extraoptions'); |
| 22 | + const extraoptions_json = extraoptions_elem.value; |
| 23 | + let extraoptions; |
| 24 | + try { |
| 25 | + extraoptions = extraoptions_json && JSON.parse(extraoptions_json); |
| 26 | + extraoptions_elem.classList.remove('is-danger') |
| 27 | + } catch (e) { |
| 28 | + extraoptions_elem.classList.add('is-danger') |
| 29 | + } |
| 30 | + |
| 31 | + storeParams({ |
| 32 | + typename, |
| 33 | + input: (input.length < 1000000) ? input : "", |
| 34 | + options, |
| 35 | + extraoptions: extraoptions ? extraoptions_json : undefined |
| 36 | + }) |
29 | 37 |
|
30 | | - storeParams({ |
31 | | - typename, |
32 | | - input: (input.length < 1000000) ? input : "", |
33 | | - options, |
34 | | - extraoptions: extraoptions ? extraoptions_json : undefined |
35 | | - }) |
| 38 | + const combinedOptions = Object.assign({}, options, extraoptions || {}); |
36 | 39 |
|
37 | | - const combinedOptions = Object.assign({}, options, extraoptions || {}); |
| 40 | + const message = { |
| 41 | + type: WorkerMessage.CODEGEN, |
| 42 | + typename, |
| 43 | + input: input || "{}", |
| 44 | + options: combinedOptions, |
| 45 | + }; |
| 46 | + requestCodegen(message); |
| 47 | +}; |
38 | 48 |
|
39 | | - const result = typegen_wasm.run(typename, input || "{}", JSON.stringify(combinedOptions)); |
| 49 | +function requestCodegen(message) { |
| 50 | + if (workerReady) { |
| 51 | + worker.postMessage(message); |
| 52 | + workerReady = false; |
| 53 | + $('target-wrapper').classList.add('is-loading'); |
| 54 | + } else { |
| 55 | + queued = message; |
| 56 | + } |
| 57 | +} |
40 | 58 |
|
| 59 | +worker.onmessage = messageEvent => { |
| 60 | + const message = messageEvent.data; |
| 61 | + if (message.type === WorkerMessage.CODEGEN_COMPLETE) { |
| 62 | + $('target-wrapper').classList.remove("is-loading"); |
41 | 63 | const target = $('target'); |
42 | | - target.value = result.trim(); |
| 64 | + target.value = message.result.trim(); |
43 | 65 | target.style.height = "10px"; |
44 | 66 | target.style.height = (target.scrollHeight + 5) + "px"; |
45 | 67 |
|
46 | | - updateDownloadLink(result, typename, options); |
47 | | - }; |
| 68 | + updateDownloadLink(message.result, message.typename, message.options); |
| 69 | + } else if (message.type === WorkerMessage.LOAD_FILE_COMPLETE) { |
| 70 | + $('large-file-spinner').classList.add('is-invisible'); |
| 71 | + $('clear-input-button').classList.remove('is-invisible'); |
| 72 | + } else if (message.type === WorkerMessage.WASM_READY) { |
| 73 | + // no action needed |
| 74 | + } else { |
| 75 | + console.warn("Unknown worker message ", messageEvent); |
| 76 | + } |
| 77 | + |
| 78 | + workerReady = true; |
| 79 | + if (queued) { |
| 80 | + requestCodegen(queued); |
| 81 | + queued = undefined; |
| 82 | + } |
| 83 | +} |
48 | 84 |
|
49 | | - $('typename').onkeyup = render; |
50 | | - $('input').onkeyup = render; |
51 | | - $('outputmode').onchange = render; |
52 | | - $('propertynameformat').onchange = render; |
53 | | - $('unwrap').onkeyup = render; |
54 | | - $('extraoptions').onkeyup = render; |
| 85 | +$('typename').onkeyup = render; |
| 86 | +$('input').onkeyup = render; |
| 87 | +$('outputmode').onchange = render; |
| 88 | +$('propertynameformat').onchange = render; |
| 89 | +$('unwrap').onkeyup = render; |
| 90 | +$('extraoptions').onkeyup = render; |
55 | 91 |
|
56 | | - $('loadfile').onchange = (event) => { |
57 | | - const file = event.target.files[0]; |
58 | | - if (file) { |
| 92 | +$('loadfile').onchange = (event) => { |
| 93 | + const file = event.target.files[0]; |
| 94 | + if (file) { |
| 95 | + if (file.size > 1000000) { |
| 96 | + $('input').value = ""; |
| 97 | + $('large-file-overlay').classList.remove('is-invisible'); |
| 98 | + $('large-file-spinner').classList.remove('is-invisible'); |
| 99 | + $('clear-input-button').classList.add('is-invisible'); |
| 100 | + const fileSizeMb = (file.size / 1000000).toFixed(2); |
| 101 | + $('large-file-message').textContent = `"${file.name}" (${fileSizeMb} MB)`; |
| 102 | + worker.postMessage({ |
| 103 | + type: WorkerMessage.LOAD_FILE, |
| 104 | + file |
| 105 | + }); |
| 106 | + workerReady = false; |
| 107 | + render(); |
| 108 | + } else { |
59 | 109 | const reader = new FileReader(); |
60 | 110 | reader.onload = (fileEvent) => { |
61 | | - const contents = fileEvent.target.result; |
62 | | - if (file.size > 1000000) { |
63 | | - largeInput = contents; |
64 | | - $('input').value = ""; |
65 | | - $('large-file-overlay').classList.remove("is-invisible"); |
66 | | - const fileSizeMb = (file.size / 1000000).toFixed(2); |
67 | | - $('large-file-message').textContent = `"${file.name}" (${fileSizeMb} MB)` |
68 | | - } else { |
69 | | - $('input').value = contents; |
70 | | - } |
| 111 | + $('input').value = fileEvent.target.result; |
71 | 112 | render(); |
72 | 113 | } |
73 | 114 | reader.readAsText(file); |
74 | 115 | } |
75 | 116 | } |
| 117 | +} |
76 | 118 |
|
77 | | - $('clear-input-button').onclick = () => { |
78 | | - largeInput = undefined; |
79 | | - $('large-file-overlay').classList.add("is-invisible"); |
80 | | - $('input').value = ""; |
81 | | - render(); |
82 | | - } |
83 | | - |
| 119 | +$('clear-input-button').onclick = () => { |
| 120 | + worker.postMessage({ |
| 121 | + type: WorkerMessage.CLEAR_FILE |
| 122 | + }); |
| 123 | + $('large-file-overlay').classList.add('is-invisible'); |
| 124 | + $('input').value = ""; |
84 | 125 | render(); |
85 | | -}); |
| 126 | +} |
| 127 | + |
| 128 | +render(); |
0 commit comments