Skip to content

Commit

Permalink
feat(connect): upgrade to bridge when it starts
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrjpolak committed Feb 5, 2025
1 parent 98af487 commit 013f14a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
return lock.promise as Promise<T>;
}

private readonly scheduledUpgradeChecks: ApiTypeMap<ReturnType<typeof setTimeout>> = {};

constructor({
messages,
priority,
Expand Down Expand Up @@ -378,6 +380,10 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
this.emit(TRANSPORT.START, getTransportInfo(transport));
}
}
if (transport) {
// new transport started successfully or present transport kept, (re)plan check
this.scheduleUpgradeCheck(apiType, initParams);
}
} catch (error) {
this.emit(TRANSPORT.ERROR, { apiType, error: error?.message });
if (initParams.transportReconnect && !abortSignal.aborted) {
Expand All @@ -396,6 +402,26 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
return promise.finally(() => clearTimeout(timeout));
}

private scheduleUpgradeCheck(apiType: TransportApiType, initParams: InitParams) {
clearTimeout(this.scheduledUpgradeChecks[apiType]);
this.scheduledUpgradeChecks[apiType] = setTimeout(async () => {
const transport = this.transport[apiType];
if (!transport) return;
const transports = this.transports.filter(t => t.apiType === apiType);
for (const t of transports) {
if (t === transport) break;
if (await t.ping()) {
this.transportLock(apiType, 'Upgrading', signal =>
this.createInitPromise(apiType, initParams, signal),
).catch(() => {});

return;
}
}
this.scheduleUpgradeCheck(apiType, initParams);
}, 1000);
}

private async selectTransport(
[transport, ...rest]: Transport[],
signal: AbortSignal,
Expand Down Expand Up @@ -542,6 +568,8 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
}

private async stopTransport(transport: Transport) {
clearTimeout(this.scheduledUpgradeChecks[transport.apiType]);

const devices = this.devices.clear(transport);

// disconnect devices
Expand Down

0 comments on commit 013f14a

Please sign in to comment.