Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed Jul 12, 2024
1 parent e958276 commit 81d2421
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
18 changes: 9 additions & 9 deletions lib/accelerometer-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AccelerometerService {
constructor(
private accelerometerDataCharacteristic: BluetoothRemoteGATTCharacteristic,
private accelerometerPeriodCharacteristic: BluetoothRemoteGATTCharacteristic,
private dispatchTypedEvent: TypedServiceEventDispatcher
private dispatchTypedEvent: TypedServiceEventDispatcher,
) {
this.addDataEventListener();
if (AccelerometerService.isNotifying) {
Expand All @@ -23,26 +23,26 @@ export class AccelerometerService {

static async init(
gattServer: BluetoothRemoteGATTServer,
dispatcher: TypedServiceEventDispatcher
dispatcher: TypedServiceEventDispatcher,
): Promise<AccelerometerService> {
if (this.accelerometerInstance) {
return this.accelerometerInstance;
}
const accelerometerService = await gattServer.getPrimaryService(
profile.accelerometer.id
profile.accelerometer.id,
);
const accelerometerDataCharacteristic =
await accelerometerService.getCharacteristic(
profile.accelerometer.characteristics.data.id
profile.accelerometer.characteristics.data.id,
);
const accelerometerPeriodCharacteristic =
await accelerometerService.getCharacteristic(
profile.accelerometer.characteristics.period.id
profile.accelerometer.characteristics.period.id,
);
this.accelerometerInstance = new AccelerometerService(
accelerometerDataCharacteristic,
accelerometerPeriodCharacteristic,
dispatcher
dispatcher,
);
return this.accelerometerInstance;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export class AccelerometerService {
const dataView = new DataView(new ArrayBuffer(2));
dataView.setUint16(0, value, true);
await this.accelerometerPeriodCharacteristic.writeValueWithoutResponse(
dataView
dataView,
);
}

Expand All @@ -90,9 +90,9 @@ export class AccelerometerService {
const data = this.dataViewToData(target.value);
this.dispatchTypedEvent(
"accelerometerdatachanged",
new AccelerometerDataEvent(data)
new AccelerometerDataEvent(data),
);
}
},
);
this.eventListenerInitialized = true;
}
Expand Down
32 changes: 16 additions & 16 deletions lib/bluetooth-device-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class BluetoothDeviceWrapper {
constructor(
public readonly device: BluetoothDevice,
private logging: Logging = new NullLogging(),
private dispatchTypedEvent: TypedServiceEventDispatcher
private dispatchTypedEvent: TypedServiceEventDispatcher,
) {
device.addEventListener(
"gattserverdisconnected",
this.handleDisconnectEvent
this.handleDisconnectEvent,
);
}

Expand All @@ -72,7 +72,7 @@ export class BluetoothDeviceWrapper {
});
if (this.duringExplicitConnectDisconnect) {
this.logging.log(
"Skipping connect attempt when one is already in progress"
"Skipping connect attempt when one is already in progress",
);
// Wait for the gattConnectPromise while showing a "connecting" dialog.
// If the user clicks disconnect while the automatic reconnect is in progress,
Expand All @@ -83,7 +83,7 @@ export class BluetoothDeviceWrapper {
this.duringExplicitConnectDisconnect++;
if (this.device.gatt === undefined) {
throw new Error(
"BluetoothRemoteGATTServer for micro:bit device is undefined"
"BluetoothRemoteGATTServer for micro:bit device is undefined",
);
}
try {
Expand All @@ -102,15 +102,15 @@ export class BluetoothDeviceWrapper {
// Do we still want to be connected?
if (!this.connecting) {
this.logging.log(
"Bluetooth GATT server connect after timeout, triggering disconnect"
"Bluetooth GATT server connect after timeout, triggering disconnect",
);
this.disconnectPromise = (async () => {
await this.disconnectInternal(false);
this.disconnectPromise = undefined;
})();
} else {
this.logging.log(
"Bluetooth GATT server connected when connecting"
"Bluetooth GATT server connected when connecting",
);
}
})
Expand All @@ -121,7 +121,7 @@ export class BluetoothDeviceWrapper {
} else {
this.logging.error(
"Bluetooth GATT server connect error after our timeout",
e
e,
);
return undefined;
}
Expand All @@ -136,7 +136,7 @@ export class BluetoothDeviceWrapper {
const gattConnectResult = await Promise.race([
this.gattConnectPromise,
new Promise<"timeout">((resolve) =>
setTimeout(() => resolve("timeout"), connectTimeoutDuration)
setTimeout(() => resolve("timeout"), connectTimeoutDuration),
),
]);
if (gattConnectResult === "timeout") {
Expand Down Expand Up @@ -178,7 +178,7 @@ export class BluetoothDeviceWrapper {

private async disconnectInternal(userTriggered: boolean): Promise<void> {
this.logging.log(
`Bluetooth disconnect ${userTriggered ? "(user triggered)" : "(programmatic)"}`
`Bluetooth disconnect ${userTriggered ? "(user triggered)" : "(programmatic)"}`,
);
this.duringExplicitConnectDisconnect++;
try {
Expand All @@ -193,7 +193,7 @@ export class BluetoothDeviceWrapper {
this.duringExplicitConnectDisconnect--;
}
this.reconnectReadyPromise = new Promise((resolve) =>
setTimeout(resolve, 3_500)
setTimeout(resolve, 3_500),
);
}

Expand All @@ -216,20 +216,20 @@ export class BluetoothDeviceWrapper {
try {
if (!this.duringExplicitConnectDisconnect) {
this.logging.log(
"Bluetooth GATT disconnected... automatically trying reconnect"
"Bluetooth GATT disconnected... automatically trying reconnect",
);
// stateOnReconnectionAttempt();
this.disposeServices();
await this.reconnect();
} else {
this.logging.log(
"Bluetooth GATT disconnect ignored during explicit disconnect"
"Bluetooth GATT disconnect ignored during explicit disconnect",
);
}
} catch (e) {
this.logging.error(
"Bluetooth connect triggered by disconnect listener failed",
e
e,
);
}
};
Expand All @@ -246,10 +246,10 @@ export class BluetoothDeviceWrapper {
const serviceMeta = profile.deviceInformation;
try {
const deviceInfo = await this.assertGattServer().getPrimaryService(
serviceMeta.id
serviceMeta.id,
);
const characteristic = await deviceInfo.getCharacteristic(
serviceMeta.characteristics.modelNumber.id
serviceMeta.characteristics.modelNumber.id,
);
const modelNumberBytes = await characteristic.readValue();
const modelNumber = new TextDecoder().decode(modelNumberBytes);
Expand Down Expand Up @@ -281,7 +281,7 @@ export class BluetoothDeviceWrapper {
export const createBluetoothDeviceWrapper = async (
device: BluetoothDevice,
logging: Logging,
dispatchTypedEvent: TypedServiceEventDispatcher
dispatchTypedEvent: TypedServiceEventDispatcher,
): Promise<BluetoothDeviceWrapper | undefined> => {
try {
// Reuse our connection objects for the same device as they
Expand Down
8 changes: 4 additions & 4 deletions lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class MicrobitWebBluetoothConnection
* A progress callback. Called with undefined when the process is complete or has failed.
*/
progress: (percentage: number | undefined) => void;
}
},
): Promise<void> {
throw new Error("Unsupported");
}
Expand Down Expand Up @@ -177,7 +177,7 @@ export class MicrobitWebBluetoothConnection
this.connection = await createBluetoothDeviceWrapper(
device,
this.logging,
(type, event) => this.dispatchTypedEvent(type, event)
(type, event) => this.dispatchTypedEvent(type, event),
);
}
// TODO: timeout unification?
Expand All @@ -187,7 +187,7 @@ export class MicrobitWebBluetoothConnection
}

private async chooseDevice(
options: ConnectOptions
options: ConnectOptions,
): Promise<BluetoothDevice | undefined> {
if (this.device) {
return this.device;
Expand Down Expand Up @@ -219,7 +219,7 @@ export class MicrobitWebBluetoothConnection
],
}),
new Promise<"timeout">((resolve) =>
setTimeout(() => resolve("timeout"), requestDeviceTimeoutDuration)
setTimeout(() => resolve("timeout"), requestDeviceTimeoutDuration),
),
]);
if (result === "timeout") {
Expand Down
2 changes: 1 addition & 1 deletion lib/service-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export type TypedServiceEvent = keyof ServiceConnectionEventMap;

export type TypedServiceEventDispatcher = (
_type: TypedServiceEvent,
event: ServiceConnectionEventMap[TypedServiceEvent]
event: ServiceConnectionEventMap[TypedServiceEvent],
) => boolean;
30 changes: 15 additions & 15 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,32 @@ document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
`;

const transport = document.querySelector(
"#flash > .transport"
"#flash > .transport",
)! as HTMLSelectElement;
const connect = document.querySelector("#flash > .connect")!;
const disconnect = document.querySelector("#flash > .disconnect")!;
const flash = document.querySelector("#flash > .flash")!;
const fileInput = document.querySelector(
"#flash input[type=file]"
"#flash input[type=file]",
)! as HTMLInputElement;
const statusParagraph = document.querySelector("#flash > .status")!;
const accDataGet = document.querySelector(
"#flash > .acc-data-controls > .acc-data-get"
"#flash > .acc-data-controls > .acc-data-get",
)!;
const accDataListen = document.querySelector(
"#flash > .acc-data-controls > .acc-data-listen"
"#flash > .acc-data-controls > .acc-data-listen",
)!;
const accDataStop = document.querySelector(
"#flash > .acc-data-controls > .acc-data-stop"
"#flash > .acc-data-controls > .acc-data-stop",
)!;
const accPeriodGet = document.querySelector(
"#flash > .acc-period-controls > .acc-period-get"
"#flash > .acc-period-controls > .acc-period-get",
)!;
const accPeriodInput = document.querySelector(
"#flash > .acc-period-controls .acc-period-input"
"#flash > .acc-period-controls .acc-period-input",
)! as HTMLInputElement;
const accPeriodSet = document.querySelector(
"#flash > .acc-period-controls > .acc-period-set"
"#flash > .acc-period-controls > .acc-period-set",
)!;

const displayStatus = (status: ConnectionStatus) => {
Expand Down Expand Up @@ -135,7 +135,7 @@ accDataGet.addEventListener("click", async () => {
console.log("Get accelerometer data", data);
} else {
throw new Error(
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`"
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`",
);
}
});
Expand All @@ -148,11 +148,11 @@ accDataListen.addEventListener("click", async () => {
if (connection instanceof MicrobitWebBluetoothConnection) {
connection?.addEventListener(
"accelerometerdatachanged",
accChangedListener
accChangedListener,
);
} else {
throw new Error(
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`"
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`",
);
}
});
Expand All @@ -161,11 +161,11 @@ accDataStop.addEventListener("click", async () => {
if (connection instanceof MicrobitWebBluetoothConnection) {
connection?.removeEventListener(
"accelerometerdatachanged",
accChangedListener
accChangedListener,
);
} else {
throw new Error(
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`"
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`",
);
}
});
Expand All @@ -176,7 +176,7 @@ accPeriodGet.addEventListener("click", async () => {
console.log("Get accelerometer period", period);
} else {
throw new Error(
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`"
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`",
);
}
});
Expand All @@ -187,7 +187,7 @@ accPeriodSet.addEventListener("click", async () => {
await connection.setAccelerometerPeriod(period);
} else {
throw new Error(
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`"
"`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`",
);
}
});

0 comments on commit 81d2421

Please sign in to comment.