diff --git a/src/app/chat/chat-view/chat-view.component.ts b/src/app/chat/chat-view/chat-view.component.ts index b43290e..68825e0 100644 --- a/src/app/chat/chat-view/chat-view.component.ts +++ b/src/app/chat/chat-view/chat-view.component.ts @@ -61,9 +61,7 @@ export class ChatViewComponent implements OnInit, OnDestroy { ngOnInit() { this.routeParamsSub = this.route.params.subscribe(params => { - if (this.messagesSub) { - this.messagesSub.unsubscribe(); - } + this.unsubscribeChannel(); this.isFirstLoad = true; this.messages = undefined; @@ -172,16 +170,22 @@ export class ChatViewComponent implements OnInit, OnDestroy { } } - ngOnDestroy() { - if (this.chatContentScrollSubscription) { - this.chatContentScrollSubscription.unsubscribe(); - } + + unsubscribeChannel() { + this.chatService.unsubscribeMessagesSubscription(); if (this.messagesSub) { this.messagesSub.unsubscribe(); } if (this.channelSub) { this.channelSub.unsubscribe(); } + } + + ngOnDestroy() { + if (this.chatContentScrollSubscription) { + this.chatContentScrollSubscription.unsubscribe(); + } + this.unsubscribeChannel(); if (this.routeParamsSub) { this.routeParamsSub.unsubscribe(); } diff --git a/src/app/chat/services/chat/chat.service.ts b/src/app/chat/services/chat/chat.service.ts index 9a4c1bf..3b35177 100644 --- a/src/app/chat/services/chat/chat.service.ts +++ b/src/app/chat/services/chat/chat.service.ts @@ -1,14 +1,14 @@ import 'rxjs/add/operator/do'; -import { Injectable } from '@angular/core'; -import { Apollo, ApolloQueryObservable } from 'apollo-angular'; -import { AuthenticationService } from '../../../shared/services/authentication.service'; -import { ApolloQueryResult } from 'apollo-client'; -import { sendMessageMutation } from '../../../graphql/queries/send-message.mutation'; -import { messagesQuery } from '../../../graphql/queries/messages.query'; -import { chatMessageAddedSubscription } from '../../../graphql/queries/chat-message-added.subscription'; -import { ChannelByNameQuery, MessagesQuery } from '../../../graphql/types/types'; -import { channelByNameQuery } from '../../../graphql/queries/channel-by-name.query'; -import { Observable } from 'rxjs/Observable'; +import {Injectable} from '@angular/core'; +import {Apollo, ApolloQueryObservable} from 'apollo-angular'; +import {AuthenticationService} from '../../../shared/services/authentication.service'; +import {ApolloQueryResult} from 'apollo-client'; +import {sendMessageMutation} from '../../../graphql/queries/send-message.mutation'; +import {messagesQuery} from '../../../graphql/queries/messages.query'; +import {chatMessageAddedSubscription} from '../../../graphql/queries/chat-message-added.subscription'; +import {ChannelByNameQuery, MessagesQuery} from '../../../graphql/types/types'; +import {channelByNameQuery} from '../../../graphql/queries/channel-by-name.query'; +import {Observable} from 'rxjs/Observable'; @Injectable() export class ChatService { @@ -16,6 +16,7 @@ export class ChatService { private noMoreToLoad = false; private loadingMoreMessages = false; private messagesQueryObservable: ApolloQueryObservable; + private messagesSubscriptionObservable; private user; constructor(private apollo: Apollo, @@ -55,7 +56,7 @@ export class ChatService { }, optimisticResponse: this.optimisticSendMessage(content), updateQueries: { - messages: (previousResult: any, { mutationResult }: any) => { + messages: (previousResult: any, {mutationResult}: any) => { const message = mutationResult.data.sendMessage; return this.pushNewMessage(previousResult, message); } @@ -88,7 +89,7 @@ export class ChatService { throw new Error('call getMessages() first'); } - this.apollo.subscribe({ + this.messagesSubscriptionObservable = this.apollo.subscribe({ query: chatMessageAddedSubscription, variables: { channelId, @@ -102,6 +103,12 @@ export class ChatService { }); } + unsubscribeMessagesSubscription() { + if ( this.messagesSubscriptionObservable){ + this.messagesSubscriptionObservable.unsubscribe(); + } + } + loadMoreMessages(channelId: string, count: number): Promise> { if (!this.messagesQueryObservable) { return Promise.reject('call getMessages() first'); @@ -118,7 +125,7 @@ export class ChatService { cursor: this.cursor, count, }, - updateQuery: (prev, { fetchMoreResult }) => { + updateQuery: (prev, {fetchMoreResult}) => { this.cursor = fetchMoreResult.messages.cursor; this.loadingMoreMessages = false; @@ -148,7 +155,8 @@ export class ChatService { private pushNewMessage(prev, newMessage) { let result; - if (prev.messages.messagesArray[prev.messages.messagesArray.length - 1].id === newMessage.id) { + const prevMessagesLen = prev.messages.messagesArray.length; + if (prevMessagesLen && prev.messages.messagesArray[prevMessagesLen - 1].id === newMessage.id) { result = prev; } else { diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts index c77d085..907d70b 100644 --- a/src/app/shared/shared.module.ts +++ b/src/app/shared/shared.module.ts @@ -29,7 +29,7 @@ import { DefaultAvatarPipe } from './pipes/default-avatar/default-avatar.pipe'; DefaultAvatarPipe ], declarations: [UnixTimeToStringPipe, DefaultAvatarPipe], - providers: [PushNotificationsService, LoginPageService], + providers: [PushNotificationsService], }) export class SharedModule { }