Skip to content

Commit

Permalink
Fix option type, extract and reuse (#31)
Browse files Browse the repository at this point in the history
Not exported for the moment
  • Loading branch information
microbit-matt-hillsdon authored Oct 17, 2024
1 parent 4de7ea8 commit adf0c90
Showing 1 changed file with 11 additions and 13 deletions.
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

0 comments on commit adf0c90

Please sign in to comment.