UNPKG

wpilib-riolog

Version:
180 lines 6.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); // tslint:disable-next-line:max-line-length const interfaces_1 = require("./interfaces"); class RioLogWindow { constructor(windowProv, rioConProivder) { this.webview = undefined; this.rioConsole = undefined; this.running = false; this.disposables = []; this.pausedArray = []; this.paused = false; this.hiddenArray = []; this.windowProvider = windowProv; this.rioConsoleProvider = rioConProivder; } start(teamNumber) { if (this.running) { return; } this.running = true; this.createWebView(); this.createRioConsole(); if (this.webview === undefined || this.rioConsole === undefined) { return; } this.webview.on('didDispose', () => { if (this.rioConsole !== undefined) { this.rioConsole.stop(); this.rioConsole.removeAllListeners(); } this.rioConsole = undefined; this.webview = undefined; this.running = false; }); this.webview.on('didReceiveMessage', async (data) => { await this.onMessageReceived(data); }); this.rioConsole.on('connectionChanged', async (c) => { await this.onConnectionChanged(c); }); this.rioConsole.on('message', async (message) => { await this.onNewMessageToSend(message); }); this.rioConsole.setTeamNumber(teamNumber); this.rioConsole.startListening(); } stop() { if (this.webview !== undefined) { this.webview.dispose(); } } dispose() { this.stop(); for (const d of this.disposables) { d.dispose(); } } createWebView() { this.webview = this.windowProvider.createWindowView(); this.webview.on('windowActive', async () => { if (this.webview === undefined) { return; } // Window goes active. await this.webview.postMessage({ message: this.hiddenArray, type: interfaces_1.SendTypes.Batch, }); if (this.rioConsole !== undefined) { if (this.rioConsole.connected === true) { await this.webview.postMessage({ message: true, type: interfaces_1.SendTypes.ConnectionChanged, }); } else { await this.webview.postMessage({ message: false, type: interfaces_1.SendTypes.ConnectionChanged, }); } } }); } createRioConsole() { this.rioConsole = this.rioConsoleProvider.getRioConsole(); } async sendPaused() { if (this.webview === undefined) { return; } const success = await this.webview.postMessage({ message: this.pausedArray, type: interfaces_1.SendTypes.Batch, }); if (!success) { this.hiddenArray.push(...this.pausedArray); } this.pausedArray = []; } async onConnectionChanged(connected) { if (this.webview === undefined) { return; } if (connected) { await this.webview.postMessage({ message: true, type: interfaces_1.SendTypes.ConnectionChanged, }); } else { await this.webview.postMessage({ message: false, type: interfaces_1.SendTypes.ConnectionChanged, }); } } async onNewMessageToSend(message) { if (this.webview === undefined) { return; } if (this.paused === true) { this.pausedArray.push(message); await this.webview.postMessage({ message: this.pausedArray.length, type: interfaces_1.SendTypes.PauseUpdate, }); } else { const success = await this.webview.postMessage({ message, type: interfaces_1.SendTypes.New, }); if (!success) { this.hiddenArray.push(message); } } } async onMessageReceived(data) { if (this.rioConsole === undefined) { return; } if (data.type === interfaces_1.ReceiveTypes.Discard) { this.rioConsole.discard = data.message; } else if (data.type === interfaces_1.ReceiveTypes.Pause) { const old = this.paused; this.paused = data.message; if (old === true && this.paused === false) { await this.sendPaused(); } } else if (data.type === interfaces_1.ReceiveTypes.Save) { if (this.webview === undefined) { return; } const deserializedLogs = []; for (const d of data.message) { const parsed = JSON.parse(d); deserializedLogs.push(parsed); } await this.webview.handleSave(deserializedLogs); } else if (data.type === interfaces_1.ReceiveTypes.Reconnect) { const newValue = data.message; this.rioConsole.setAutoReconnect(newValue); if (newValue === false) { this.rioConsole.disconnect(); } } else if (data.type === interfaces_1.ReceiveTypes.ChangeNumber) { const number = data.message; console.log('setting team number'); this.rioConsole.setTeamNumber(number); } } } exports.RioLogWindow = RioLogWindow; //# sourceMappingURL=riologwindow.js.map