Skip to content

Fix option type, extract and reuse #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface MicrobitWebUSBConnectionOptions {
logging: Logging;
}

export interface FlashOptions {
partial: boolean;
progress: (percentage: number | undefined) => void;
minimumProgressIncrement?: number;
}

/**
* A WebUSB connection to a micro:bit device.
*/
Expand Down Expand Up @@ -201,11 +207,7 @@ export class MicrobitWebUSBConnection

async flash(
dataSource: FlashDataSource,
options: {
partial: boolean;
progress: (percentage: number | undefined) => void;
miniumProgressIncrement: number;
},
options: FlashOptions,
): Promise<void> {
this.flashing = true;
try {
Expand All @@ -230,11 +232,7 @@ export class MicrobitWebUSBConnection

private async flashInternal(
dataSource: FlashDataSource,
options: {
partial: boolean;
progress: (percentage: number | undefined, partial: boolean) => void;
miniumProgressIncrement: number;
},
options: FlashOptions,
): Promise<void> {
this.log("Stopping serial before flash");
await this.stopSerialInternal();
Expand All @@ -246,7 +244,7 @@ export class MicrobitWebUSBConnection

const partial = options.partial;
const progress = rateLimitProgress(
options.miniumProgressIncrement ?? 0.0025,
options.minimumProgressIncrement ?? 0.0025,
options.progress || (() => {}),
);

Expand Down Expand Up @@ -523,7 +521,7 @@ const enrichedError = (err: any): DeviceError => {
};

const rateLimitProgress = (
miniumProgressIncrement: number,
minimumProgressIncrement: number,
callback: (value: number | undefined, partial: boolean) => void,
) => {
let lastCallValue = -1;
Expand All @@ -532,7 +530,7 @@ const rateLimitProgress = (
value === undefined ||
value === 0 ||
value === 1 ||
value >= lastCallValue + miniumProgressIncrement
value >= lastCallValue + minimumProgressIncrement
) {
lastCallValue = value ?? -1;
callback(value, partial);
Expand Down