-
Notifications
You must be signed in to change notification settings - Fork 37
/
device-hooks.tsx
264 lines (243 loc) · 7.13 KB
/
device-hooks.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* (c) 2022, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import React, {
Dispatch,
ReactNode,
SetStateAction,
useContext,
useEffect,
useState,
} from "react";
import { useFileSystem } from "../fs/fs-hooks";
import { useLogging } from "../logging/logging-hooks";
import {
ConnectionStatus,
DeviceConnection,
SerialDataEvent,
ConnectionStatusEvent,
} from "./device";
import { SimulatorDeviceConnection } from "./simulator";
const DeviceContext = React.createContext<undefined | DeviceConnection>(
undefined
);
/**
* Hook to access the device from UI code.
*
* @returns The device.
*/
export const useDevice = () => {
const device = useContext(DeviceContext);
if (!device) {
throw new Error("Missing provider.");
}
return device;
};
/**
* Hook to access the simulator from UI code.
*/
export const useSimulator = (): SimulatorDeviceConnection => {
const device = useDevice();
if (!(device instanceof SimulatorDeviceConnection)) {
throw new Error("Simulator not in scope");
}
return device;
};
/**
* State that tracks the device connections status.
*/
export const useConnectionStatus = () => {
const device = useDevice();
const [status, setStatus] = useState<ConnectionStatus>(device.status);
useEffect(() => {
const statusListener = (event: ConnectionStatusEvent) => {
setStatus(event.status);
};
device.addEventListener("status", statusListener);
return () => {
device.removeEventListener("status", statusListener);
};
}, [device, setStatus]);
return status;
};
interface TraceLineParts {
file: string | undefined;
line: number | undefined;
}
const unknownParts: TraceLineParts = { line: undefined, file: undefined };
/**
* Parse a line from the trace of a traceback.
*
* Trims leading/trailing space and returns undefined files
* if it does not match the expected format.
*/
export const parseTraceLine = (line: string): TraceLineParts => {
// E.g.
// File "main.py", line 5, in foo
// File "<stdin>", line 1, in <module>
const match = /^File [<"]([^>"]+)[">], line (\d+)/.exec(line.trim());
if (match) {
const file: string | undefined =
match[1] === "__main__" ? "main.py" : match[1];
let line: number | undefined;
const number = match[2];
if (number) {
line = parseInt(number, 10);
}
return { line, file };
}
return unknownParts;
};
export class Traceback {
private parsed?: TraceLineParts;
constructor(public error: string, public trace: string[]) {}
private parse(): TraceLineParts {
if (this.parsed) {
return this.parsed;
}
const trace = this.trace[this.trace.length - 1];
if (trace) {
this.parsed = parseTraceLine(trace);
} else {
this.parsed = unknownParts;
}
return this.parsed;
}
get line(): number | undefined {
return this.parse().line;
}
get file(): string | undefined {
const file = this.parse().file;
switch (file) {
case "stdin":
return undefined;
default:
return file;
}
}
}
export class TracebackScrollback {
private scrollback: string = "";
push(data: string) {
this.scrollback = this.scrollback + data;
const limit = 4096;
if (this.scrollback.length > limit) {
this.scrollback = this.scrollback.slice(data.length - limit);
}
const lines = this.scrollback.split("\r\n");
for (let i = lines.length - 1; i >= 0; --i) {
if (lines[i].startsWith("Traceback (most recent call last):")) {
// Start of last traceback
// Skip all following lines with an indent and grab the first one without, which is the error message.
let endOfIndent = i + 1;
while (
endOfIndent < lines.length &&
lines[endOfIndent].startsWith(" ")
) {
endOfIndent++;
}
if (endOfIndent < lines.length) {
const trace = lines
.slice(i + 1, endOfIndent)
.map((line) => line.trim());
if (trace[0] && trace[0].startsWith('File "<stdin>"')) {
// User entered code at the REPL, discard.
return undefined;
}
const error = lines[endOfIndent];
if (error.startsWith("KeyboardInterrupt")) {
// User interrupted the program (we assume), discard.
return undefined;
}
return new Traceback(error, trace);
}
return undefined;
}
}
return undefined;
}
clear() {
this.scrollback = "";
}
}
export const useDeviceTraceback = () => {
const device = useDevice();
const [runtimeError, setRuntimeError] = useState<Traceback | undefined>(
undefined
);
const logging = useLogging();
useEffect(() => {
const buffer = new TracebackScrollback();
const dataListener = (event: SerialDataEvent) => {
const latest = buffer.push(event.data);
setRuntimeError((current) => {
if (!current && latest) {
logging.event({
type: "serial-traceback",
});
}
return latest;
});
};
const clearListener = () => {
buffer.clear();
setRuntimeError(undefined);
};
device.addEventListener("serial_data", dataListener);
device.addEventListener("serial_reset", clearListener);
device.addEventListener("serial_error", clearListener);
return () => {
device.removeEventListener("serial_error", clearListener);
device.removeEventListener("serial_reset", clearListener);
device.removeEventListener("serial_data", dataListener);
};
}, [device, setRuntimeError, logging]);
return runtimeError;
};
export enum SyncStatus {
OUT_OF_SYNC = "OUT_OF_SYNC",
IN_SYNC = "IN_SYNC",
}
type UseSyncStatus = [SyncStatus, Dispatch<SetStateAction<SyncStatus>>];
const SyncContext = React.createContext<undefined | UseSyncStatus>(undefined);
export const useSyncStatus = (): SyncStatus => {
const value = useContext(SyncContext);
if (!value) {
throw new Error("Missing provider!");
}
return value[0];
};
export const DeviceContextProvider = ({
value: device,
children,
}: {
value: DeviceConnection;
children: ReactNode;
}) => {
const syncStatusState = useState<SyncStatus>(SyncStatus.OUT_OF_SYNC);
const [, setSyncStatus] = syncStatusState;
const fs = useFileSystem();
useEffect(() => {
const moveToOutOfSync = () => setSyncStatus(SyncStatus.OUT_OF_SYNC);
const moveToInSync = () => setSyncStatus(SyncStatus.IN_SYNC);
fs.addEventListener("file_text_updated", moveToOutOfSync);
fs.addEventListener("project_updated", moveToOutOfSync);
device.addEventListener("flash", moveToInSync);
device.addEventListener("status", moveToOutOfSync);
return () => {
fs.removeEventListener("file_text_updated", moveToOutOfSync);
fs.removeEventListener("project_updated", moveToOutOfSync);
device.removeEventListener("status", moveToOutOfSync);
device.removeEventListener("flash", moveToInSync);
};
}, [fs, device, setSyncStatus]);
return (
<DeviceContext.Provider value={device}>
<SyncContext.Provider value={syncStatusState}>
{children}
</SyncContext.Provider>
</DeviceContext.Provider>
);
};