Skip to content

Commit

Permalink
feat(connect): make transportReconnect implicitly true
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Nov 7, 2024
1 parent 0dfebc8 commit d523359
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/connect/e2e/common.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const initTrezorConnect = async (
debug: false,
popup: false,
pendingTransportEvent: true,
transportReconnect: false,
connectSrc: process.env.TREZOR_CONNECT_SRC, // custom source for karma tests
...options,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/connect/src/core/__tests__/Core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { initCoreState } from '../index';
const { createTestTransport } = global.JestMocks;

const getSettings = (partial: Partial<ConnectSettings> = {}) =>
parseConnectSettings({ transports: [createTestTransport()], ...partial });
parseConnectSettings({
transports: [createTestTransport()],
transportReconnect: false,
...partial,
});

describe('Core', () => {
beforeAll(async () => {});
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ export class Core extends EventEmitter {
this.deviceList.init({ pendingTransportEvent, transportReconnect });

// in auto core mode, we have to wait to check if transport is available
if (!transportReconnect || coreMode === 'auto') {
if (transportReconnect === false || coreMode === 'auto') {
await this.deviceList.pendingConnection();
}

Expand Down
9 changes: 5 additions & 4 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
.catch(error => {
this.cleanup();
this.emit(TRANSPORT.ERROR, error);
this.initPromise = initParams.transportReconnect
? this.createReconnectPromise(initParams)
: undefined;
this.initPromise =
initParams.transportReconnect !== false
? this.createReconnectPromise(initParams)
: undefined;
});
}

Expand Down Expand Up @@ -291,7 +292,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
transport.on(TRANSPORT.ERROR, error => {
this.cleanup();
this.emit(TRANSPORT.ERROR, error);
if (initParams.transportReconnect) {
if (initParams.transportReconnect !== false) {
this.initPromise = this.createReconnectPromise(initParams);
}
});
Expand Down
24 changes: 14 additions & 10 deletions packages/connect/src/device/__tests__/DeviceList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ describe('DeviceList', () => {
beforeAll(async () => {
// todo: I don't get it. If we pass empty messages: {} (see getDeviceListParams), tests behave differently.
await DataManager.load({
...parseConnectSettings({}),
...parseConnectSettings({
transportReconnect: false,
}),
});

// Todo: This is a hack, it should not be here at all. Transport shall return valid device (with valid features)
Expand All @@ -42,7 +44,9 @@ describe('DeviceList', () => {

beforeEach(() => {
list = new DeviceList({
...parseConnectSettings({}),
...parseConnectSettings({
transportReconnect: false,
}),
messages: DataManager.getProtobufMessages(),
});
eventsSpy = jest.fn();
Expand Down Expand Up @@ -99,7 +103,7 @@ describe('DeviceList', () => {
);

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
// transport-error is not emitted yet because list.init is not awaited
expect(eventsSpy).toHaveBeenCalledTimes(0);
await list.pendingConnection();
Expand All @@ -117,7 +121,7 @@ describe('DeviceList', () => {
);

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
// transport-error is not emitted yet because list.init is not awaited
expect(eventsSpy).toHaveBeenCalledTimes(0);
await list.pendingConnection();
Expand All @@ -131,7 +135,7 @@ describe('DeviceList', () => {
});

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
await list.pendingConnection();

const events = eventsSpy.mock.calls.map(call => call[0]);
Expand All @@ -144,7 +148,7 @@ describe('DeviceList', () => {
});

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
const transportFirstEvent = list.pendingConnection();

// NOTE: this behavior is wrong, if device creation fails DeviceList shouldn't wait 10 secs.
Expand Down Expand Up @@ -174,7 +178,7 @@ describe('DeviceList', () => {
});

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
await list.pendingConnection();

const events = eventsSpy.mock.calls.map(call => call[0]);
Expand All @@ -189,7 +193,7 @@ describe('DeviceList', () => {
});

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
await list.pendingConnection();

const events = eventsSpy.mock.calls.map(([event, { path }]) => [event, path]);
Expand Down Expand Up @@ -221,7 +225,7 @@ describe('DeviceList', () => {
// NOTE: this behavior is wrong
jest.useFakeTimers();
list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
const transportFirstEvent = list.pendingConnection();
await jest.advanceTimersByTimeAsync(6 * 1000); // TODO: this is wrong
await transportFirstEvent;
Expand Down Expand Up @@ -267,7 +271,7 @@ describe('DeviceList', () => {
});

list.setTransports([transport]);
list.init({ pendingTransportEvent: true });
list.init({ pendingTransportEvent: true, transportReconnect: false });
await list.pendingConnection();

// emit TRANSPORT.CHANGE 3 times
Expand Down

0 comments on commit d523359

Please sign in to comment.