Skip to content

Commit

Permalink
chore(connect): DeviceList now has two params - full settings object …
Browse files Browse the repository at this point in the history
…and messages
  • Loading branch information
mroz22 committed May 16, 2024
1 parent afc8760 commit b616726
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
5 changes: 1 addition & 4 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,8 @@ const handleDeviceSelectionChanges = (interruptDevice?: DeviceTyped) => {
const initDeviceList = async (transportReconnect?: boolean) => {
try {
_deviceList = new DeviceList({
debug: DataManager.getSettings('debug'),
transports: DataManager.getSettings('transports'),
settings: DataManager.getSettings(),
messages: DataManager.getProtobufMessages(),
pendingTransportEvent: DataManager.getSettings('pendingTransportEvent'),
priority: DataManager.getSettings('priority'),
});

_deviceList.on(DEVICE.CONNECT, device => {
Expand Down
29 changes: 11 additions & 18 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,27 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> {

transportFirstEventPromise: Promise<void> | undefined;

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

constructor({
transports,
debug,
settings,
messages,
pendingTransportEvent,
priority,
}: {
transports?: ConnectSettings['transports'];
debug?: ConnectSettings['debug'];
settings: ConnectSettings;
messages: Record<string, any>;
pendingTransportEvent?: ConnectSettings['pendingTransportEvent'];
priority: ConnectSettings['priority'];
}) {
super();

this.pendingTransportEvent = pendingTransportEvent;
this.priority = priority;
this.settings = settings;
// 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'
if (!transports?.length) {
transports = ['BridgeTransport'];

const transports: ConnectSettings['transports'] = [...(settings.transports || [])];
if (!transports.length) {
transports.push('BridgeTransport');
}

const transportLogger = initLog('@trezor/transport', debug);
const transportLogger = initLog('@trezor/transport', this.settings.debug);

// todo: this should be passed from above
const abortController = new AbortController();
Expand Down Expand Up @@ -202,7 +195,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> {
const path = descriptor.path.toString();
this.creatingDevicesDescriptors[path] = descriptor;

const { priority } = this;
const { priority } = this.settings;
const penalty = this.getAuthPenalty();

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

const descriptors = enumerateResult.payload;

if (descriptors.length > 0 && this.pendingTransportEvent) {
if (descriptors.length > 0 && this.settings.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
17 changes: 6 additions & 11 deletions packages/connect/src/device/__tests__/DeviceList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ const createTestTransport = (apiMethods = {}) => {
};

const getDeviceListParams = (
params: Partial<ConstructorParameters<typeof DeviceList>[0]>,
partialSettings: Partial<ConstructorParameters<typeof DeviceList>[0]['settings']>,
): ConstructorParameters<typeof DeviceList>[0] => {
const { debug, transports, priority, pendingTransportEvent } = parseConnectSettings({});

const messages = DataManager.getProtobufMessages();

return {
debug,
transports,
priority,
pendingTransportEvent,
messages,
...params,
settings: {
...parseConnectSettings({}),
...partialSettings,
},
messages: DataManager.getProtobufMessages(),
};
};

Expand Down

0 comments on commit b616726

Please sign in to comment.