Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
Messages from webview are made constant
Browse files Browse the repository at this point in the history
  • Loading branch information
xnkevinnguyen committed Feb 7, 2020
1 parent dc73efb commit d859420
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
9 changes: 0 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,6 @@ export enum TelemetryEventName {
}
export const DEFAULT_DEVICE = CONSTANTS.DEVICE_NAME.CPX;

export enum WebviewMessages {
BUTTON_PRESS = "button-press",
PLAY_SIMULATOR = "play-simulator",
SENSOR_CHANGED = "sensor-changed",
REFRESH_SIMULATOR = "refresh-simulator",
SLIDER_TELEMETRY = "slider-telemetry",
SWITCH_DEVICE = "switch-device",
}

// tslint:disable-next-line: no-namespace
export namespace DialogResponses {
export const ACCEPT_AND_RUN: MessageItem = {
Expand Down
14 changes: 7 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
DialogResponses,
SERVER_INFO,
TelemetryEventName,
WebviewMessages,
} from "./constants";
import { CPXWorkspace } from "./cpxWorkspace";
import { DebuggerCommunicationServer } from "./debuggerCommunicationServer";
Expand All @@ -23,6 +22,7 @@ import { SerialMonitor } from "./serialMonitor";
import { SimulatorDebugConfigurationProvider } from "./simulatorDebugConfigurationProvider";
import TelemetryAI from "./telemetry/telemetryAI";
import { UsbDetector } from "./usbDetector";
import { WEBVIEW_MESSAGES } from "./view/constants";

let currentFileAbsPath: string = "";
let currentTextDocument: vscode.TextDocument;
Expand Down Expand Up @@ -156,7 +156,7 @@ export async function activate(context: vscode.ExtensionContext) {
message => {
const messageJson = JSON.stringify(message.text);
switch (message.command) {
case WebviewMessages.BUTTON_PRESS:
case WEBVIEW_MESSAGES.BUTTON_PRESS:
// Send input to the Python process
handleButtonPressTelemetry(message.text);
console.log(`About to write ${messageJson} \n`);
Expand All @@ -173,7 +173,7 @@ export async function activate(context: vscode.ExtensionContext) {
);
}
break;
case WebviewMessages.PLAY_SIMULATOR:
case WEBVIEW_MESSAGES.TOGGLE_PLAY_STOP:
console.log(`Play button ${messageJson} \n`);
if (message.text.state as boolean) {
setPathAndSendMessage(
Expand Down Expand Up @@ -204,7 +204,7 @@ export async function activate(context: vscode.ExtensionContext) {

break;

case WebviewMessages.SENSOR_CHANGED:
case WEBVIEW_MESSAGES.SENSOR_CHANGED:
checkForTelemetry(message.text);
console.log(`Sensor changed ${messageJson} \n`);
if (
Expand All @@ -220,14 +220,14 @@ export async function activate(context: vscode.ExtensionContext) {
);
}
break;
case WebviewMessages.REFRESH_SIMULATOR:
case WEBVIEW_MESSAGES.REFRESH_SIMULATOR:
console.log("Refresh button");
runSimulatorCommand();
break;
case WebviewMessages.SLIDER_TELEMETRY:
case WEBVIEW_MESSAGES.SLIDER_TELEMETRY:
handleSensorTelemetry(message.text);
break;
case WebviewMessages.SWITCH_DEVICE:
case WEBVIEW_MESSAGES.SWITCH_DEVICE:
switchDevice(message.text.active_device);
killProcessIfRunning();
break;
Expand Down
4 changes: 2 additions & 2 deletions src/view/components/cpx/CpxSimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import * as React from "react";
import { CONSTANTS } from "../../constants";
import { CONSTANTS, WEBVIEW_MESSAGES } from "../../constants";
import { sendMessage } from "../../utils/MessageUtils";

import "../../styles/Simulator.css";
Expand Down Expand Up @@ -183,7 +183,7 @@ class Simulator extends React.Component<{}, IState> {
}

protected refreshSimulatorClick() {
sendMessage("refresh-simulator", true);
sendMessage(WEBVIEW_MESSAGES.REFRESH_SIMULATOR, true);
const button = window.document.getElementById(
CONSTANTS.ID_NAME.REFRESH_BUTTON
);
Expand Down
6 changes: 3 additions & 3 deletions src/view/components/microbit/MicrobitSimulator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import CONSTANTS from "../../constants";
import CONSTANTS, { WEBVIEW_MESSAGES } from "../../constants";
import PlayLogo from "../../svgs/play_svg";
import StopLogo from "../../svgs/stop_svg";
import { sendMessage } from "../../utils/MessageUtils";
Expand Down Expand Up @@ -110,7 +110,7 @@ export class MicrobitSimulator extends React.Component<any, IState> {
);
}
protected togglePlayClick = () => {
sendMessage("play-simulator", {
sendMessage(WEBVIEW_MESSAGES.TOGGLE_PLAY_STOP, {
active_device: CONSTANTS.DEVICE_NAME.MICROBIT,
selected_file: this.state.selected_file,
state: !this.state.play_button,
Expand All @@ -122,7 +122,7 @@ export class MicrobitSimulator extends React.Component<any, IState> {
});
}
protected refreshSimulatorClick = () => {
sendMessage("refresh-simulator", true);
sendMessage(WEBVIEW_MESSAGES.REFRESH_SIMULATOR, true);
};
protected onMouseUp(button: HTMLElement, event: Event, key: string) {
event.preventDefault();
Expand Down
13 changes: 10 additions & 3 deletions src/view/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ export enum DEVICE_LIST_KEY {
CPX = "CPX",
MICROBIT = "micro:bit",
}
export const WEBVIEW_MESSAGES = {
SWITCH_DEVICE: "switch-device",
};

//
export enum WEBVIEW_MESSAGES {
SWITCH_DEVICE = "switch-device",
REFRESH_SIMULATOR = "refresh-simulator",
TOGGLE_PLAY_STOP = "toggle-play-stop",
BUTTON_PRESS = "button-press",
SENSOR_CHANGED = "sensor-changed",
SLIDER_TELEMETRY = "slider-telemetry",
}

export default CONSTANTS;

0 comments on commit d859420

Please sign in to comment.