Skip to content

Commit 2ce2215

Browse files
Create init for asynchronous firmware initialization
1 parent 0f970ae commit 2ce2215

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

kernel/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const kernelPlugin: JupyterLiteServerPlugin<void> = {
4141
create: async (options: IKernel.IOptions): Promise<IKernel> => {
4242

4343
const serviceContainer = new ServiceContainer()
44+
await serviceContainer.init();
4445

4546
const welcomePanel = new WelcomePanel(serviceContainer);
4647
document.body.appendChild(welcomePanel.getElement());

kernel/src/services/FirmwareService.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FirmwareOption } from '../constants'
2+
import { defaultFirmwareOptions } from '../constants'
23
import { DeviceService } from './DeviceService';
34

45
export class FirmwareService {
@@ -14,16 +15,19 @@ export class FirmwareService {
1415
// Get the firmware options from GitHub (or local files).
1516
// Maybe we'll add an explicit button for updating the firmware options in the future...
1617
// We'll initialize our private firmwareOptions here with the reqFirmwareOptionsGitHub() method.
17-
console.log("In the constructor about to fetch firmware options from GitHub...");
18-
this.reqFirmwareOptionsGitHub().then((res) => {
19-
// log the res
20-
console.log('Firmware options response:', res);
21-
this.firmwareOptions = res;
22-
})
18+
// console.log("In the constructor about to fetch firmware options from GitHub...");
19+
// this.reqFirmwareOptionsGitHub().then((res) => {
20+
// // log the res
21+
// console.log('Firmware options response:', res);
22+
// this.firmwareOptions = res;
23+
// })
2324

2425
// Log the firmware options to the console for debugging.
25-
console.log('Firmware options:', this.firmwareOptions);
26-
26+
// console.log('Firmware options:', this.firmwareOptions);
27+
28+
// Initialize firmware options with defaultFirmwareOptions.
29+
this.firmwareOptions = defaultFirmwareOptions;
30+
2731
if (savedSelection) {
2832
this.selectedFirmwareId = savedSelection;
2933
} else {
@@ -34,16 +38,10 @@ export class FirmwareService {
3438
// We'll use this method to fetch the firmware options from GitHub.
3539
// We could alternatively use local files in ../binaries/ so we aren't sending a request to GitHub every time.
3640
private async reqFirmwareOptionsGitHub(): Promise<Record<string, FirmwareOption>> {
37-
// Log that we're in the reqFirmwareOptionsGitHub method.
38-
console.log('Fetching firmware options from GitHub...');
39-
4041
const releases = await fetch('https://api.github.com/repos/sparkfun/micropython/releases', {
4142
method: 'GET'
4243
});
4344

44-
// Log the response for debugging.
45-
console.log('Releases response:', releases);
46-
4745
if (!releases.ok) {
4846
throw new Error(`Failed to fetch releases: ${releases.status} ${releases.statusText}`);
4947
}
@@ -59,8 +57,6 @@ export class FirmwareService {
5957
const latestRelease = releasesData[0];
6058
if (latestRelease && latestRelease.assets) {
6159
latestRelease.assets.forEach((asset: any) => {
62-
// Log the asset for debugging.
63-
console.log('Asset:', asset);
6460

6561
// We should make the name the full name of the asset but the key the firmwareId (the board name in lowercase with dashes and an optional "m" prefix for minimal).
6662
const firmwareName = asset.name;
@@ -71,20 +67,12 @@ export class FirmwareService {
7167
}
7268
const firmwareUrl = asset.browser_download_url;
7369

74-
// Log the firmwareId, firmwareName, and firmwareUrl for debugging.
75-
console.log('Firmware ID:', firmwareId);
76-
console.log('Firmware Name:', firmwareName);
77-
console.log('Firmware URL:', firmwareUrl);
78-
7970
const firmwareOption: FirmwareOption = {
8071
name: firmwareName,
8172
url: firmwareUrl
8273
};
8374

8475
firmwareOptions[firmwareId] = firmwareOption;
85-
86-
// Log the firmware option for debugging.
87-
console.log('Firmware option:', firmwareOptions[firmwareId]);
8876
});
8977
}
9078

@@ -100,6 +88,20 @@ export class FirmwareService {
10088
return firmwareOptions;
10189
}
10290

91+
// Create an init function that performs any asynchronous initialization for the firmware service. For now that will just be fetching the firmware options from GitHub.
92+
async init(): Promise<void> {
93+
// Log that we're in the init method.
94+
console.log('Initializing firmware service...');
95+
96+
// Fetch the firmware options from GitHub.
97+
try {
98+
this.firmwareOptions = await this.reqFirmwareOptionsGitHub();
99+
console.log('Firmware options initialized:', this.firmwareOptions);
100+
} catch (error) {
101+
console.error('Error fetching firmware options:', error);
102+
}
103+
}
104+
103105
// This is where we can add our logic of fetching from GitHub.
104106
// We could alternatively use local files in ../binaries/ so we aren't sending a request to GitHub every time.
105107
getFirmwareOptions(){

kernel/src/services/ServiceContainer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export class ServiceContainer {
2222
);
2323
}
2424

25+
// Perform any asynchronous initialization for any of the services here.
26+
public async init(): Promise<void> {
27+
// We have to wait for the asynchronous init of the firmware service to complete before we can use it in the flash service.
28+
await this._firmwareService.init();
29+
}
30+
2531
public get deviceService(): DeviceService {
2632
return this._deviceService;
2733
}

0 commit comments

Comments
 (0)