blob: 754088bdcce724d3aafc3b508bcfd42776a45e9f [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import { WebviewApi } from 'vscode-webview';
import { ExternalActionRequestEvent, LoggingView } from './components/view';
import { PersistedState, State, VSCodeStore } from './src/state';
import { ffxLogToLogRowData } from './src/ffxLogToLogRow';
//@ts-check
// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
(function () {
const vscode = acquireVsCodeApi() as WebviewApi<PersistedState>;
const view = new LoggingView(new State(new VSCodeStore(vscode)), document.body);
// TODO(fxbug.dev/109719): move this to the persisted state.
let playing = true;
// Handle messages sent from the extension to the webview
window.addEventListener('message', event => {
const message = event.data;
switch (message.type) {
case 'addLog':
if (playing) {
const converted = ffxLogToLogRowData(message.log, true);
if (converted) {
// *1 forces conversion to double
converted.timestamp = parseFloat(message.log.timestamp);
view.addLog(converted);
}
}
break;
case 'monotonicTimeUpdate':
view.updateMonotonicTime(message.value);
break;
case 'reset':
view.reset();
}
});
// Forward webview messages to the extension.
view.addEventListener(LoggingView.externalActionRequestEvent, (e) => {
const event = (e as CustomEvent).detail as ExternalActionRequestEvent;
switch (event.type) {
case 'pause-log-streaming':
playing = false;
vscode.postMessage({
command: 'pauseLogStreaming'
});
break;
case 'resume-log-streaming':
playing = true;
vscode.postMessage({
command: 'resumeLogStreaming'
});
break;
case 'clear-logs':
vscode.postMessage({ command: 'clearLogs', timestamp: event.untilTimestamp });
break;
case 'fetch-clock-monotonic':
vscode.postMessage({ command: 'getMonotonicTime' });
break;
}
});
}());