Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Nov 20, 2022
1 parent 64cc631 commit 246dfa8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 16 deletions.
1 change: 1 addition & 0 deletions ci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ transport manual:
COMPOSE_PROJECT_NAME: $CI_JOB_ID
COMPOSE_FILE: ./docker/docker-compose.connect-popup-ci.yml
URL: ${DEV_SERVER_URL}/connect/${CI_BUILD_REF_NAME}/
TEST_FILE: $TEST_FILE
script:
- yarn install --immutable
- docker-compose pull
Expand Down
12 changes: 12 additions & 0 deletions packages/connect/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,23 @@ export class Device extends EventEmitter {

dispose() {
this.removeAllListeners();
console.log(
'DEVICE.DISPOSE',
'this.isUsedHere()',
this.isUsedHere(),
'this.activitySessionID',
this.activitySessionID,
);
if (this.isUsedHere() && this.activitySessionID) {
try {
if (this.commands) {
this.commands.cancel();
}
console.log('==============================');
console.log('==============================');
console.log('==============================');
console.log('==============================');

this.transport.release(this.activitySessionID, true);
} catch (err) {
// empty
Expand Down
15 changes: 9 additions & 6 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class DeviceList extends EventEmitter {
new BridgeTransport({
messages: this.messages,
latestVersion: getBridgeInfo().version.join('.'),
debug: true,
}),
);
return;
Expand Down Expand Up @@ -190,8 +191,6 @@ export class DeviceList extends EventEmitter {
* transport reported some change of descriptors
*/
this.transport.on(TRANSPORT.UPDATE, diff => {
// new DiffHandler(this, diff).handle();

diff.connected.forEach(async descriptor => {
const path = descriptor.path.toString();
const priority = DataManager.getSettings('priority');
Expand Down Expand Up @@ -321,7 +320,7 @@ export class DeviceList extends EventEmitter {
this.on(DEVICE.CONNECT, this.resolveTransportEvent.bind(this));
this.on(DEVICE.CONNECT_UNACQUIRED, this.resolveTransportEvent.bind(this));

this.transport.enumerate();
await this.transport.enumerate();

this.transport.listen();

Expand Down Expand Up @@ -411,11 +410,15 @@ export class DeviceList extends EventEmitter {
dispose() {
this.removeAllListeners();

this.allDevices().forEach(device => device.dispose());

if (this.transport) {
this.transport.stop();
// while keeping this method (dispose) sync, we need to to wait for final release call
// but also cancel other pending calls.
setTimeout(() => {
this.transport.stop();
}, 100);
}

this.allDevices().forEach(device => device.dispose());
}

disconnectDevices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Onboarding - analytics consent', () => {
cy.viewport(1080, 1440).resetDb();
});

it('shows analytics consent when going to settings and back on non-initialized T1 device', () => {
it.only('shows analytics consent when going to settings and back on non-initialized T1 device', () => {
cy.task('startEmu', { version: '1-latest', wipe: true });
cy.prefixedVisit('/');

Expand Down
2 changes: 1 addition & 1 deletion packages/suite-web/e2e/tests/onboarding/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Onboarding - transport webusb/bridge', () => {
.its('store')
.invoke('getState')
.should(state => {
expect(state?.suite.transport?.type).to.deep.eq('WebusbTransport');
expect(state?.suite.transport?.type).to.deep.eq('WebUsbTransport');
});

cy.getTestElement('@onboarding/expand-troubleshooting-tips').click();
Expand Down
4 changes: 2 additions & 2 deletions packages/suite/src/utils/suite/__tests__/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const fixtures = [
{
description: `Transport is webusb`,
transport: {
type: 'WebusbTransport',
type: 'WebUsbTransport',
},
result: true,
},
{
description: `Transport is not webusb (bridge)`,
transport: {
type: 'bridge',
type: 'BridgeTransport',
},
result: false,
},
Expand Down
1 change: 0 additions & 1 deletion packages/transport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export { getAvailableTransport } from './utils/getAvailableTransport';
/**
* Set fetch for this lib globally
*/
export { setFetch } from './utils/http';

export { SessionsBackend } from './sessions/backend';
export { SessionsClient } from './sessions/client';
5 changes: 5 additions & 0 deletions packages/transport/src/transports/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,17 @@ export abstract class Transport extends TypedEmitter<{
const diff = this._getDiff(nextDescriptors);

this.log('handleDescriptorsChange:diff', diff);
this.log('this.acquiring', this.acquiring);

this.descriptors = nextDescriptors;

if (diff.didUpdate) {
this.emit(TRANSPORT.UPDATE, diff);
}

if (this.acquiring) {
delete this.acquiring;
}
}

// todo: consider accepting logger class from outside
Expand Down
24 changes: 21 additions & 3 deletions packages/transport/src/transports/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,20 @@ export class BridgeTransport extends Transport {
expectedSessionIds: { [path: string]: string } = {};
listenPromises: { [path: string]: Deferred<any> } = {};

constructor({ messages, url = DEFAULT_URL, latestVersion }: BridgeConstructorParameters) {
super({ messages });
constructor({
messages,
url = DEFAULT_URL,
latestVersion,
debug,
}: BridgeConstructorParameters) {
super({ messages, debug });
this.name = 'BridgeTransport';
this.url = url;
this.latestVersion = latestVersion;
// @ts-expect-error
this.abortController = getAbortController();

setFetch(fetch, true); // todo
setFetch(fetch, typeof window === 'undefined'); // todo
}

async init() {
Expand Down Expand Up @@ -159,6 +164,9 @@ export class BridgeTransport extends Transport {
const reportedNextSession = descriptor.session;
const expectedNextSession = this.expectedSessionIds[path];

this.log('reportedNextSession', reportedNextSession);
this.log('expectedNextSession', expectedNextSession);

if (reportedNextSession == expectedNextSession) {
listenPromise.resolve(expectedNextSession);
} else {
Expand Down Expand Up @@ -214,15 +222,18 @@ export class BridgeTransport extends Transport {
this.listenPromises[input.path] = createDeferred();

const acquireS = await this._post({ url });
this.log('acquire response', acquireS);

// @ts-ignore
this.releasePromise = createDeferred();

const expectedSessionId = check.acquire(acquireS);
this.acquiring = expectedSessionId;
this.expectedSessionIds[input.path] = expectedSessionId;

this.log('acquire: settings expectedSessionIds', JSON.stringify(this.expectedSessionIds));
return this.listenPromises[input.path].promise.then(() => {
this.log('acquire listenPromise resolved');
delete this.expectedSessionIds[input.path];
delete this.listenPromises[input.path];

Expand All @@ -234,6 +245,12 @@ export class BridgeTransport extends Transport {
async release(session: string, onclose?: boolean) {
this.releasing = session;

// todo: maybe navigator.sendBeacon should have its use case here but bridge does not react to it
// if (onclose) {
// const res = navigator.sendBeacon(`/release/${session}`);
// return;
// }

this._post({
url: `/release/${session}`,
});
Expand Down Expand Up @@ -315,6 +332,7 @@ export class BridgeTransport extends Transport {

stop() {
this.stopped = true;
console.log('TRIGGER ABORT');
this.abortController.abort();
}
}
10 changes: 8 additions & 2 deletions packages/transport/src/transports/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ export class UsbTransport extends Transport {
expectedSessionIds: { [path: string]: string } = {};
listenPromises: { [path: string]: Deferred<any> } = {};

constructor({ messages, usbInterface, sessionsClient, listen }: UsbTransportConstructorParams) {
super({ messages });
constructor({
messages,
usbInterface,
sessionsClient,
listen,
debug,
}: UsbTransportConstructorParams) {
super({ messages, debug });
this.usbInterface = usbInterface;
this.sessionsClient = sessionsClient;

Expand Down

0 comments on commit 246dfa8

Please sign in to comment.