Skip to content

Commit b1d1507

Browse files
Fix error scenario handling. (#57)
- Clarify meaning of dispose on components with rename. - Fix missing dispose/boardStopped call for data logging.
1 parent b25113a commit b1d1507

File tree

13 files changed

+26
-24
lines changed

13 files changed

+26
-24
lines changed

src/board/accelerometer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ export class Accelerometer {
7575
this.gestureCallback = gestureCallback;
7676
}
7777

78-
dispose() {}
78+
boardStopped() {}
7979
}

src/board/audio/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class Audio {
138138
}
139139
}
140140

141-
dispose() {
141+
boardStopped() {
142142
if (this.context) {
143143
this.context.close();
144144
this.context = undefined;

src/board/buttons.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class Button {
115115
return result;
116116
}
117117

118-
dispose() {
118+
boardStopped() {
119119
this._presses = 0;
120120
}
121121
}

src/board/compass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export class Compass {
2727
return Math.sqrt(x * x + y * y + z * z);
2828
}
2929

30-
dispose() {}
30+
boardStopped() {}
3131
}

src/board/data-logging.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe("DataLogging", () => {
233233

234234
it("dispose resets timestamp if nothing written", () => {
235235
logging.setTimestamp(MICROBIT_HAL_LOG_TIMESTAMP_SECONDS);
236-
logging.dispose();
236+
logging.boardStopped();
237237
logging.initialize();
238238

239239
logging.beginRow();
@@ -253,7 +253,7 @@ describe("DataLogging", () => {
253253
logging.logData("b", "");
254254
logging.endRow();
255255

256-
logging.dispose();
256+
logging.boardStopped();
257257
logging.initialize();
258258

259259
logging.beginRow();

src/board/data-logging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class DataLogging {
133133

134134
initialize() {}
135135

136-
dispose() {
136+
boardStopped() {
137137
// We don't delete the log here as it's on flash, but we do reset in-memory state.
138138
this.resetNonFlashStateExceptTimestamp();
139139
// Keep the timestamp if we could restore it from a persisted log.

src/board/display.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Display {
7373
}
7474
}
7575

76-
dispose() {
76+
boardStopped() {
7777
this.clear();
7878
}
7979
}

src/board/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,11 @@ export class Board {
331331
module.forceStop();
332332
} catch (e: any) {
333333
if (e.name !== "ExitStatus") {
334-
console.error(e);
335-
throw new Error("Expected status message");
334+
this.notifications.onInternalError(e);
336335
}
337336
}
337+
// Called by the HAL for normal shutdown but not in error scenarios.
338+
this.stopComponents();
338339
this.modulePromise = undefined;
339340

340341
if (panicCode !== undefined) {
@@ -530,15 +531,16 @@ export class Board {
530531
this.serialInputBuffer.length = 0;
531532
}
532533

533-
dispose() {
534-
this.audio.dispose();
535-
this.buttons.forEach((b) => b.dispose());
536-
this.pins.forEach((p) => p.dispose());
537-
this.display.dispose();
538-
this.accelerometer.dispose();
539-
this.compass.dispose();
540-
this.microphone.dispose();
541-
this.radio.dispose();
534+
stopComponents() {
535+
this.audio.boardStopped();
536+
this.buttons.forEach((b) => b.boardStopped());
537+
this.pins.forEach((p) => p.boardStopped());
538+
this.display.boardStopped();
539+
this.accelerometer.boardStopped();
540+
this.compass.boardStopped();
541+
this.microphone.boardStopped();
542+
this.radio.boardStopped();
543+
this.dataLogging.boardStopped();
542544
this.serialInputBuffer.length = 0;
543545

544546
// Nofify of the state resets.

src/board/microphone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Microphone {
6363
this.soundLevelCallback = soundLevelCallback;
6464
}
6565

66-
dispose() {
66+
boardStopped() {
6767
this.microphoneOff();
6868
}
6969
}

src/board/pins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ export class Pin {
113113
}
114114
}
115115

116-
dispose() {}
116+
boardStopped() {}
117117
}

src/board/radio.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Radio", () => {
5555
});
5656

5757
it("disables the radio", () => {
58-
radio.dispose();
58+
radio.boardStopped();
5959
expect(radio.state).toEqual({
6060
type: "radio",
6161
enabled: false,

src/board/radio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class Radio {
107107
}
108108
}
109109

110-
dispose() {
110+
boardStopped() {
111111
this.rxQueue = undefined;
112112
this.config = undefined;
113113
this.state = {

src/jshal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mergeInto(LibraryManager.library, {
3131
},
3232

3333
mp_js_hal_deinit: function () {
34-
Module.board.dispose();
34+
Module.board.stopComponents();
3535
},
3636

3737
mp_js_hal_ticks_ms: function () {

0 commit comments

Comments
 (0)