Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve reconnect callback fidelity #52

Merged
merged 1 commit into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/promisedwebsocket/ReconnectingPromisedWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ export default class ReconnectingPromisedWebSocket implements PromisedWebSocket
this.dispatchEvent(event);
});

this.webSocket.open(timeoutMs).then((event: Event) => {
this.webSocket.addEventListener('open', (event: Event) => {
this.didOpenWebSocket();
resolve(event);
this.dispatchEvent(event);
});

return this.webSocket.open(timeoutMs).then(event => resolve(event));
});
}

Expand Down
13 changes: 8 additions & 5 deletions src/screensharingsession/DefaultScreenSharingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,31 @@ export default class DefaultScreenSharingSession implements ScreenSharingSession
});

this.webSocket.addEventListener('close', (event: CloseEvent) => {
this.logger.warn('screen sharing connection closed');
this.stop().catch(() => {});
this.observerQueue.forEach((observer: ScreenSharingSessionObserver) => {
Maybe.of(observer.didClose).map(f => f.bind(observer)(event));
});
});

this.webSocket.addEventListener('reconnect', (event: Event) => {
this.logger.warn('WebSocket reconnecting');
this.logger.warn('screen sharing connection reconnecting');
this.stop().catch(() => {});
this.observerQueue.forEach((observer: ScreenSharingSessionObserver) => {
Maybe.of(observer.willReconnect).map(f => f.bind(observer)(event));
});
});

this.logger.info(`opening screen sharing connection to ${this.webSocket.url}`);

return this.webSocket.open(timeoutMs).then((event: Event) => {
this.webSocket.addEventListener('open', (event: Event) => {
this.logger.warn('screen sharing connection opened');
this.observerQueue.forEach((observer: ScreenSharingSessionObserver) => {
Maybe.of(observer.didOpen).map(f => f.bind(observer)(event));
});
return event;
});

this.logger.info(`opening screen sharing connection to ${this.webSocket.url}`);

return this.webSocket.open(timeoutMs);
}

close(timeoutMs: number): Promise<Event> {
Expand Down
8 changes: 6 additions & 2 deletions src/screenviewing/session/DefaultScreenViewingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ export default class DefaultScreenViewingSession implements ScreenViewingSession
);

this.webSocket.addEventListener('message', (event: MessageEvent) => {
Maybe.of(this.observer.didReceiveWebSocketMessage).map(f => f.bind(this.observer)(event));
Maybe.of(this.observer).map(observer => {
Maybe.of(observer.didReceiveWebSocketMessage).map(f => f.bind(this.observer)(event));
});
});

this.webSocket.addEventListener('close', (event: CloseEvent) => {
Maybe.of(this.observer.didCloseWebSocket).map(f => f.bind(this.observer)(event));
Maybe.of(this.observer).map(observer => {
Maybe.of(observer.didCloseWebSocket).map(f => f.bind(this.observer)(event));
});
});

return this.webSocket.open(request.timeoutMs);
Expand Down
20 changes: 20 additions & 0 deletions test/promisedwebsocket/ReconnectingPromisedWebSocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ describe('ReconnectingPromisedWebSocket', () => {
});

describe('#open', () => {
describe('with error', () => {
it('is rejected', (done: Mocha.Done) => {
const webSocket = {
...Substitute.for<PromisedWebSocket>(),
open(_timeoutMs: number): Promise<Event> {
throw new Error();
},
};
const webSocketFactory = Substitute.for<PromisedWebSocketFactory>();
const subject = new ReconnectingPromisedWebSocket(
url,
protocols,
binaryType,
webSocketFactory,
backoff
);
webSocketFactory.create(Arg.all()).returns(webSocket);
chai.expect(subject.open(timeoutMs)).to.eventually.be.rejected.and.notify(done);
});
});
describe('without open', () => {
it('is fulfilled', (done: Mocha.Done) => {
const webSocketFactory = Substitute.for<PromisedWebSocketFactory>();
Expand Down
10 changes: 8 additions & 2 deletions test/promisedwebsocketmock/PromisedWebSocketMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ export default class PromisedWebSocketMock implements PromisedWebSocket {
}

open(_timeoutMs: number): Promise<Event> {
return Promise.resolve(Substitute.for<Event>());
const event = Substitute.for<Event>();
event.type.returns('open');
this.dispatchEvent(event);
return Promise.resolve(event);
}

close(_timeoutMs: number): Promise<Event> {
return Promise.resolve(Substitute.for<CloseEvent>());
const event = Substitute.for<CloseEvent>();
event.type.returns('close');
this.dispatchEvent(event);
return Promise.resolve(event);
}

send(_data: string | ArrayBufferLike | Blob | ArrayBufferView): Promise<void> {
Expand Down
17 changes: 4 additions & 13 deletions test/screensharingsession/DefaultScreenSharingSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('DefaultScreenSharingSession', function() {
describe('#open', () => {
describe('with open', () => {
it('is fulfilled', (done: Mocha.Done) => {
const promisedWebSocket = Substitute.for<PromisedWebSocket>();
const promisedWebSocket = new PromisedWebSocketMock();
const subject = new DefaultScreenSharingSession(
promisedWebSocket,
constraintsProvider,
Expand All @@ -50,17 +50,12 @@ describe('DefaultScreenSharingSession', function() {
mediaRecordingFactory,
logging
);
const event = Substitute.for<Event>();
event.type.returns('open');
promisedWebSocket.open(Arg.any()).returns(Promise.resolve(event));
promisedWebSocket.url.returns('');
subject.open(1000).should.eventually.be.fulfilled.and.notify(done);
promisedWebSocket.dispatchEvent(event);
});
});

it('nofifies', (done: Mocha.Done) => {
const promisedWebSocket = Substitute.for<PromisedWebSocket>();
it('notifies', (done: Mocha.Done) => {
const promisedWebSocket = new PromisedWebSocketMock();
const subject = new DefaultScreenSharingSession(
promisedWebSocket,
constraintsProvider,
Expand All @@ -71,15 +66,11 @@ describe('DefaultScreenSharingSession', function() {
mediaRecordingFactory,
logging
);
const event = Substitute.for<Event>();
const observer: ScreenSharingSessionObserver = {
didOpen(event: Event): void {
chai.expect(event).to.eq(event);
didOpen(_event: Event): void {
done();
},
};
promisedWebSocket.open(Arg.any()).returns(Promise.resolve(event));
promisedWebSocket.url.returns('');
subject.registerObserver(observer);
subject.open(1000).should.eventually.be.fulfilled;
});
Expand Down