Skip to content

Commit

Permalink
fix(channels): fix crashing after subscribing to the new channel
Browse files Browse the repository at this point in the history
  • Loading branch information
KutsenkoA authored and veger committed Oct 4, 2019
1 parent ae7f113 commit 8d14d35
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api/realtime/channel.spec.ts
Expand Up @@ -21,7 +21,7 @@ function createSubject({
injectorMock.get.mockImplementation(() => requestExecuterMock);
websocket.start(websocketMock, () => Promise.resolve("token"));

const subject = new Channel(injectorMock, websocketMock, name);
const subject = new Channel(injectorMock, () => websocketMock, name);

return {
subject,
Expand Down
10 changes: 5 additions & 5 deletions src/api/realtime/channel.ts
Expand Up @@ -32,24 +32,24 @@ export class Channel<T = any> extends Observable<RealTimeEventMessage<T>> {
/**
* @internal
* @param injector
* @param websocket
* @param websocketFactory
* @param name
*/
constructor(
readonly injector: ReflectiveInjector,
readonly websocket: IWebSocket,
readonly websocketFactory: () => IWebSocket,
readonly name: string
) {
super((observer) => {
wsReadyDefer.promise
.then(() => subscribeEventMessage(
this.websocket, ["published"], name, ResourceType.Channel, observer)
this.websocketFactory(), ["published"], name, ResourceType.Channel, observer)
)
.catch((error: any) => observer.error(error));

return () => wsReadyDefer.promise
.then(() => unsubscribeEventMessage(
this.websocket, ["published"], name, ResourceType.Channel, observer
this.websocketFactory(), ["published"], name, ResourceType.Channel, observer
)
.catch(() => undefined)
);
Expand All @@ -63,7 +63,7 @@ export class Channel<T = any> extends Observable<RealTimeEventMessage<T>> {
public publish(data: any): Observable<RealTimeCommandResponse> {
return from(
wsReadyDefer.promise.then(() => {
return realTimeCommand(this.websocket, {
return realTimeCommand(this.websocketFactory(), {
command: RealTimeCommandTypes.Publish,
arguments: { channel: this.name, data },
correlation_id: Math.random().toString(),
Expand Down
2 changes: 1 addition & 1 deletion src/api/realtime/realTimeModule.ts
Expand Up @@ -117,7 +117,7 @@ export class RealTimeModule implements IModule {
* @param {string} name Name of the channel
*/
public channel<T = any>(name: string): Channel<T> {
return new Channel<T>(this.injector, this.websocket, name);
return new Channel<T>(this.injector, () => this.websocket, name);
}

/**
Expand Down

0 comments on commit 8d14d35

Please sign in to comment.