Skip to content

Commit

Permalink
chore(connect): remove DataManager from DeviceList
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed May 16, 2024
1 parent f093d5e commit 2d3a470
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 2 additions & 0 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
16 changes: 13 additions & 3 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,16 +61,26 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> {

transportFirstEventPromise: Promise<void> | undefined;

private pendingTransportEvent: ConnectSettings['pendingTransportEvent'];
private priority: ConnectSettings['priority'];

constructor({
transports,
debug,
messages,
pendingTransportEvent,
priority,
}: {
transports?: ConnectSettings['transports'];
debug?: ConnectSettings['debug'];
messages: Record<string, any>;
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'
Expand Down Expand Up @@ -192,7 +202,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> {
const path = descriptor.path.toString();
this.creatingDevicesDescriptors[path] = descriptor;

const priority = DataManager.getSettings('priority');
const { priority } = this;
const penalty = this.getAuthPenalty();

if (priority || penalty) {
Expand Down Expand Up @@ -300,7 +310,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> {

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));
Expand Down
40 changes: 15 additions & 25 deletions packages/connect/src/device/__tests__/DeviceList.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parseConnectSettings } from '../../data/connectSettings';
import { DataManager } from '../../data/DataManager';
import { ConnectSettings } from '../../exports';
import { DeviceList } from '../DeviceList';
import {
AbstractApiTransport,
Expand Down Expand Up @@ -62,33 +61,25 @@ const createTestTransport = (apiMethods = {}) => {
return transport;
};

const loadDataManager = (settings: Partial<ConnectSettings> = {}) => {
return DataManager.load(
parseConnectSettings({
...settings,
}),
);
};
const getDeviceListParams = (
params: Partial<ConstructorParameters<typeof DeviceList>[0]>,
): ConstructorParameters<typeof DeviceList>[0] => {
const { debug, transports, priority, pendingTransportEvent } = parseConnectSettings({});

const getDeviceListParams = (params: Partial<ConstructorParameters<typeof DeviceList>[0]>) => {
const { debug, transports } = DataManager.getSettings();
const messages = DataManager.getProtobufMessages();

return {
debug,
messages,
transports,
priority,
pendingTransportEvent,
messages,
...params,
};
};

const createDeviceList = (
deviceListParams: ConstructorParameters<typeof DeviceList>[0] = { messages: {} },
) => {
const list = new DeviceList({
...parseConnectSettings(),
...deviceListParams,
});
const createDeviceList = (deviceListParams: ConstructorParameters<typeof DeviceList>[0]) => {
const list = new DeviceList(deviceListParams);
const eventsSpy = jest.fn();
(
[
Expand All @@ -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
Expand Down Expand Up @@ -276,7 +269,6 @@ describe('DeviceList', () => {
return { success: true, payload: [{ path: '1' }, { path: '2' }, { path: '3' }] };
},
});

const { list, eventsSpy } = createDeviceList(
getDeviceListParams({ transports: [transport] }),
);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2d3a470

Please sign in to comment.