|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +let messageHandler; |
| 4 | +let schematic; |
| 5 | + |
| 6 | +// Obtain a handle to the VS Code API. |
| 7 | +let vscode = acquireVsCodeApi(); |
| 8 | + |
| 9 | +// Check whether we're actually restoring panel state instead of loading a |
| 10 | +// completely new preview panel. |
| 11 | +let state = vscode.getState(); |
| 12 | +if (state) { |
| 13 | + // We've got a state from a previous run. |
| 14 | + // Restore it and signal we're ready. |
| 15 | + start(state); |
| 16 | +} |
| 17 | + |
| 18 | +onmessage = async function(e) { |
| 19 | + if (e.data.type === 'start') { |
| 20 | + // This is a fresh new webview, not one restored from an existing state. |
| 21 | + // Therefore, save the state to start with. |
| 22 | + state = e.data.state; |
| 23 | + saveState(); |
| 24 | + start(e.data.state); |
| 25 | + } else if (e.data.type === 'compiling') { |
| 26 | + document.querySelector('#schematic').classList.add('compiling'); |
| 27 | + terminal.clear('Compiling...'); |
| 28 | + } else if (e.data.type === 'loading') { |
| 29 | + // Compiled, loading the program now. |
| 30 | + terminal.clear('Loading...'); |
| 31 | + } else if (e.data.type === 'started') { |
| 32 | + // Message is sent right before actually starting the program. |
| 33 | + document.querySelector('#schematic').classList.remove('compiling'); |
| 34 | + terminal.clear('Running...'); |
| 35 | + } else if (e.data.type === 'notifyUpdate') { |
| 36 | + // Worker notifies us that there are pending updates. |
| 37 | + // Wait for the browser to tell us to update the screen. |
| 38 | + requestAnimationFrame(() => { |
| 39 | + vscode.postMessage({ |
| 40 | + type: 'getUpdate', |
| 41 | + }); |
| 42 | + }); |
| 43 | + } else if (e.data.type === 'properties') { |
| 44 | + // Set properties in the properties panel at the bottom. |
| 45 | + schematic.setProperties(e.data.properties); |
| 46 | + } else if (e.data.type === 'update') { |
| 47 | + // Received updates. Apply them to the webview. |
| 48 | + schematic.update(e.data.updates); |
| 49 | + } else if (e.data.type === 'error') { |
| 50 | + terminal.showError(e.data.message); |
| 51 | + } else { |
| 52 | + console.log('unknown message:', e.data); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +async function start(state) { |
| 57 | + schematic = new Schematic(state); |
| 58 | + await schematic.refresh(); |
| 59 | + vscode.postMessage({ |
| 60 | + type: 'ready', |
| 61 | + workerConfig: schematic.configForWorker(), |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +function workerPostMessage(message) { |
| 66 | + vscode.postMessage(message); |
| 67 | +} |
| 68 | + |
| 69 | +function saveState() { |
| 70 | + vscode.setState(state); |
| 71 | +} |
0 commit comments