Skip to content

Commit

Permalink
fix(transport-bridge): fix concurrent enumerate on node-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 authored and szymonlesisz committed Aug 13, 2024
1 parent f0a0601 commit 94d1a26
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/transport-bridge/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const createCore = (apiArg: 'usb' | 'udp' | AbstractApi, logger?: Log) =>
};

const enumerate = async ({ signal }: { signal: AbortSignal }) => {
await sessionsClient.enumerateIntent();
const enumerateResult = await api.enumerate(signal);

if (!enumerateResult.success) {
Expand Down
11 changes: 11 additions & 0 deletions packages/transport/src/sessions/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export class SessionsBackground extends TypedEmitter<{
case 'handshake':
result = this.handshake();
break;
case 'enumerateIntent':
await this.enumerateIntent();
break;
case 'enumerateDone':
result = await this.enumerateDone(message.payload);
break;
Expand Down Expand Up @@ -119,11 +122,19 @@ export class SessionsBackground extends TypedEmitter<{
return this.success(undefined);
}

private async enumerateIntent() {
await this.waitInQueue();

return this.success(undefined);
}

/**
* enumerate done
* - caller informs about current descriptors
*/
private enumerateDone(payload: EnumerateDoneRequest) {
this.clearLock();

const disconnectedDevices = this.filterDisconnectedDevices(
Object.values(this.descriptors),
payload.descriptors.map(d => d.path), // which paths are occupied paths after last interface read
Expand Down
5 changes: 4 additions & 1 deletion packages/transport/src/sessions/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export class SessionsClient extends TypedEmitter<{
handshake() {
return this.request({ type: 'handshake' });
}

enumerateIntent() {
return this.request({ type: 'enumerateIntent' });
}
enumerateDone(payload: EnumerateDoneRequest) {
return this.request({ type: 'enumerateDone', payload });
}
Expand All @@ -78,6 +80,7 @@ export class SessionsClient extends TypedEmitter<{
}
dispose() {
this.removeAllListeners('descriptors');

return this.request({ type: 'dispose' });
}
}
1 change: 1 addition & 0 deletions packages/transport/src/sessions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export type Params = {

export interface HandleMessageApi {
handshake: () => HandshakeResponse;
enumerateIntent: () => BackgroundResponse<void>;
enumerateDone: (payload: EnumerateDoneRequest) => EnumerateDoneResponse;
acquireIntent: (payload: AcquireIntentRequest) => AcquireIntentResponse;
acquireDone: (payload: AcquireDoneRequest) => AcquireDoneResponse;
Expand Down
3 changes: 3 additions & 0 deletions packages/transport/src/transports/abstractApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export abstract class AbstractApiTransport extends AbstractTransport {

public enumerate() {
return this.scheduleAction(async signal => {
// todo: consider doing await this.sessionsClient.enumerateIntent() here
// it looks like Webusb does not need it is not needed at least on macos.

// enumerate usb api
const enumerateResult = await this.api.enumerate(signal);

Expand Down

0 comments on commit 94d1a26

Please sign in to comment.