diff --git a/packages/connect/src/core/index.ts b/packages/connect/src/core/index.ts index 632bd04ec9d3..6c7230315fa2 100644 --- a/packages/connect/src/core/index.ts +++ b/packages/connect/src/core/index.ts @@ -965,6 +965,8 @@ const initDeviceList = async (transportReconnect?: boolean) => { debug: DataManager.getSettings('debug'), transports: DataManager.getSettings('transports'), messages: DataManager.getProtobufMessages(), + pendingTransportEvent: DataManager.getSettings('pendingTransportEvent'), + priority: DataManager.getSettings('priority'), }); _deviceList.on(DEVICE.CONNECT, device => { diff --git a/packages/connect/src/device/DeviceList.ts b/packages/connect/src/device/DeviceList.ts index 8c8d8a693139..ec6186a4e6b0 100644 --- a/packages/connect/src/device/DeviceList.ts +++ b/packages/connect/src/device/DeviceList.ts @@ -16,7 +16,7 @@ import { ERRORS } from '../constants'; import { DEVICE, TransportInfo } from '../events'; import { Device } from './Device'; import type { ConnectSettings, Device as DeviceTyped } from '../types'; -import { DataManager } from '../data/DataManager'; + import { getBridgeInfo } from '../data/transportInfo'; import { initLog } from '../utils/debug'; import { resolveAfter } from '../utils/promiseUtils'; @@ -61,16 +61,26 @@ export class DeviceList extends TypedEmitter { transportFirstEventPromise: Promise | undefined; + private pendingTransportEvent: ConnectSettings['pendingTransportEvent']; + private priority: ConnectSettings['priority']; + constructor({ transports, debug, messages, + pendingTransportEvent, + priority, }: { transports?: ConnectSettings['transports']; debug?: ConnectSettings['debug']; messages: Record; + pendingTransportEvent?: ConnectSettings['pendingTransportEvent']; + priority: ConnectSettings['priority']; }) { super(); + + this.pendingTransportEvent = pendingTransportEvent; + this.priority = priority; // we fill in `transports` with a reasonable fallback in src/index. // since web index is released into npm, we can not rely // on that that transports will be always set here. We need to provide a 'fallback of the last resort' @@ -192,7 +202,7 @@ export class DeviceList extends TypedEmitter { const path = descriptor.path.toString(); this.creatingDevicesDescriptors[path] = descriptor; - const priority = DataManager.getSettings('priority'); + const { priority } = this; const penalty = this.getAuthPenalty(); if (priority || penalty) { @@ -300,7 +310,7 @@ export class DeviceList extends TypedEmitter { const descriptors = enumerateResult.payload; - if (descriptors.length > 0 && DataManager.getSettings('pendingTransportEvent')) { + if (descriptors.length > 0 && this.pendingTransportEvent) { this.transportStartPending = descriptors.length; // listen for self emitted events and resolve pending transport event if needed this.on(DEVICE.CONNECT, this.resolveTransportEvent.bind(this)); diff --git a/packages/connect/src/device/__tests__/DeviceList.test.ts b/packages/connect/src/device/__tests__/DeviceList.test.ts index 7a0569b009d7..9ea34f165df7 100644 --- a/packages/connect/src/device/__tests__/DeviceList.test.ts +++ b/packages/connect/src/device/__tests__/DeviceList.test.ts @@ -1,6 +1,5 @@ import { parseConnectSettings } from '../../data/connectSettings'; import { DataManager } from '../../data/DataManager'; -import { ConnectSettings } from '../../exports'; import { DeviceList } from '../DeviceList'; import { AbstractApiTransport, @@ -62,33 +61,25 @@ const createTestTransport = (apiMethods = {}) => { return transport; }; -const loadDataManager = (settings: Partial = {}) => { - return DataManager.load( - parseConnectSettings({ - ...settings, - }), - ); -}; +const getDeviceListParams = ( + params: Partial[0]>, +): ConstructorParameters[0] => { + const { debug, transports, priority, pendingTransportEvent } = parseConnectSettings({}); -const getDeviceListParams = (params: Partial[0]>) => { - const { debug, transports } = DataManager.getSettings(); const messages = DataManager.getProtobufMessages(); return { debug, - messages, transports, + priority, + pendingTransportEvent, + messages, ...params, }; }; -const createDeviceList = ( - deviceListParams: ConstructorParameters[0] = { messages: {} }, -) => { - const list = new DeviceList({ - ...parseConnectSettings(), - ...deviceListParams, - }); +const createDeviceList = (deviceListParams: ConstructorParameters[0]) => { + const list = new DeviceList(deviceListParams); const eventsSpy = jest.fn(); ( [ @@ -112,10 +103,12 @@ const createDeviceList = ( }; describe('DeviceList', () => { - beforeEach(async () => { - await loadDataManager(); + beforeAll(async () => { + // todo: I don't get it. If we pass empty messages: {} (see getDeviceListParams), tests behave differently. + await DataManager.load({ + ...parseConnectSettings({}), + }); }); - it('constructor throws error on unknown transport (string)', () => { const params = getDeviceListParams({ // @ts-expect-error @@ -276,7 +269,6 @@ describe('DeviceList', () => { return { success: true, payload: [{ path: '1' }, { path: '2' }, { path: '3' }] }; }, }); - const { list, eventsSpy } = createDeviceList( getDeviceListParams({ transports: [transport] }), ); @@ -343,12 +335,10 @@ describe('DeviceList', () => { }); it('.init() without pendingTransportEvent (device connected after start)', async () => { - await loadDataManager({ pendingTransportEvent: false }); - const transport = createTestTransport(); const { list, eventsSpy } = createDeviceList( - getDeviceListParams({ transports: [transport] }), + getDeviceListParams({ transports: [transport], pendingTransportEvent: false }), ); list.init();