Skip to content

Commit

Permalink
Merge pull request #2 from kamilkisiela/fix/initnital-chat-messages
Browse files Browse the repository at this point in the history
Fix/initnital chat messages
  • Loading branch information
eitanfr authored Aug 27, 2017
2 parents 202d125 + 7bf5247 commit ea0b023
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
18 changes: 11 additions & 7 deletions src/app/chat/chat-view/chat-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
36 changes: 22 additions & 14 deletions src/app/chat/services/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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 {
private cursor: any;
private noMoreToLoad = false;
private loadingMoreMessages = false;
private messagesQueryObservable: ApolloQueryObservable<MessagesQuery.Result>;
private messagesSubscriptionObservable;
private user;

constructor(private apollo: Apollo,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand All @@ -102,6 +103,12 @@ export class ChatService {
});
}

unsubscribeMessagesSubscription() {
if ( this.messagesSubscriptionObservable){
this.messagesSubscriptionObservable.unsubscribe();
}
}

loadMoreMessages(channelId: string, count: number): Promise<ApolloQueryResult<MessagesQuery.Result>> {
if (!this.messagesQueryObservable) {
return Promise.reject('call getMessages() first');
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

0 comments on commit ea0b023

Please sign in to comment.