From fb82b53e550d374aa6f6c6fea6d4364ef07f8c1c Mon Sep 17 00:00:00 2001 From: ncunningham Date: Sun, 11 Mar 2018 13:29:50 -0400 Subject: [PATCH] Fixes TSLint for observables --- src/app/message/messages.service.ts | 38 +++++++------- src/app/thread/threads.service.ts | 79 +++++++++++++++-------------- 2 files changed, 57 insertions(+), 60 deletions(-) diff --git a/src/app/message/messages.service.ts b/src/app/message/messages.service.ts index 7024fd1..708072b 100644 --- a/src/app/message/messages.service.ts +++ b/src/app/message/messages.service.ts @@ -30,11 +30,9 @@ export class MessagesService { constructor() { this.messages = this.updates // watch the updates and accumulate operations on the messages - .scan((messages: Message[], - operation: IMessagesOperation) => { - return operation(messages); - }, - initialMessages) + .scan((messages: Message[], operation: IMessagesOperation) => { + return operation(messages); + }, initialMessages) // make sure we can share the most recent list of messages across anyone // who's interested in subscribing and cache the last known list of // messages @@ -56,22 +54,22 @@ export class MessagesService { // entirely. The pros are that it is potentially clearer. The cons are that // the stream is no longer composable. this.create - .map( function(message: Message): IMessagesOperation { + .asObservable() + .map(function(message: Message): IMessagesOperation { return (messages: Message[]) => { return messages.concat(message); }; }) .subscribe(this.updates); - this.newMessages - .subscribe(this.create); + this.newMessages.subscribe(this.create); // similarly, `markThreadAsRead` takes a Thread and then puts an operation // on the `updates` stream to mark the Messages as read this.markThreadAsRead - .map( (thread: Thread) => { + .map((thread: Thread) => { return (messages: Message[]) => { - return messages.map( (message: Message) => { + return messages.map((message: Message) => { // note that we're manipulating `message` directly here. Mutability // can be confusing and there are lots of reasons why you might want // to, say, copy the Message object or some other 'immutable' here @@ -83,7 +81,6 @@ export class MessagesService { }; }) .subscribe(this.updates); - } // an imperative function call to this action stream @@ -92,16 +89,15 @@ export class MessagesService { } messagesForThreadUser(thread: Thread, user: User): Observable { - return this.newMessages - .filter((message: Message) => { - // belongs to this thread - return (message.thread.id === thread.id) && - // and isn't authored by this user - (message.author.id !== user.id); - }); + return this.newMessages.asObservable().filter((message: Message) => { + // belongs to this thread + return ( + message.thread.id === thread.id && + // and isn't authored by this user + message.author.id !== user.id + ); + }); } } -export const messagesServiceInjectables: Array = [ - MessagesService -]; +export const messagesServiceInjectables: Array = [MessagesService]; diff --git a/src/app/thread/threads.service.ts b/src/app/thread/threads.service.ts index 6884cf1..3254fc8 100644 --- a/src/app/thread/threads.service.ts +++ b/src/app/thread/threads.service.ts @@ -7,7 +7,6 @@ import * as _ from 'lodash'; @Injectable() export class ThreadsService { - // `threads` is a observable that contains the most up to date list of threads threads: Observable<{ [key: string]: Thread }>; @@ -15,54 +14,59 @@ export class ThreadsService { orderedThreads: Observable; // `currentThread` contains the currently selected thread - currentThread: Subject = - new BehaviorSubject(new Thread()); + currentThread: Subject = new BehaviorSubject(new Thread()); // `currentThreadMessages` contains the set of messages for the currently // selected thread currentThreadMessages: Observable; constructor(public messagesService: MessagesService) { + this.threads = messagesService.messages.map((messages: Message[]) => { + const threads: { [key: string]: Thread } = {}; + // Store the message's thread in our accumulator `threads` + messages.map((message: Message) => { + threads[message.thread.id] = + threads[message.thread.id] || message.thread; - this.threads = messagesService.messages - .map( (messages: Message[]) => { - const threads: {[key: string]: Thread} = {}; - // Store the message's thread in our accumulator `threads` - messages.map((message: Message) => { - threads[message.thread.id] = threads[message.thread.id] || - message.thread; - - // Cache the most recent message for each thread - const messagesThread: Thread = threads[message.thread.id]; - if (!messagesThread.lastMessage || - messagesThread.lastMessage.sentAt < message.sentAt) { - messagesThread.lastMessage = message; - } - }); - return threads; + // Cache the most recent message for each thread + const messagesThread: Thread = threads[message.thread.id]; + if ( + !messagesThread.lastMessage || + messagesThread.lastMessage.sentAt < message.sentAt + ) { + messagesThread.lastMessage = message; + } }); + return threads; + }); - this.orderedThreads = this.threads - .map((threadGroups: { [key: string]: Thread }) => { + this.orderedThreads = this.threads.map( + (threadGroups: { [key: string]: Thread }) => { const threads: Thread[] = _.values(threadGroups); return _.sortBy(threads, (t: Thread) => t.lastMessage.sentAt).reverse(); - }); + } + ); this.currentThreadMessages = this.currentThread - .combineLatest(messagesService.messages, - (currentThread: Thread, messages: Message[]) => { - if (currentThread && messages.length > 0) { - return _.chain(messages) - .filter((message: Message) => - (message.thread.id === currentThread.id)) - .map((message: Message) => { - message.isRead = true; - return message; }) - .value(); - } else { - return []; + .asObservable() + .combineLatest( + messagesService.messages, + (currentThread: Thread, messages: Message[]) => { + if (currentThread && messages.length > 0) { + return _.chain(messages) + .filter( + (message: Message) => message.thread.id === currentThread.id + ) + .map((message: Message) => { + message.isRead = true; + return message; + }) + .value(); + } else { + return []; + } } - }); + ); this.currentThread.subscribe(this.messagesService.markThreadAsRead); } @@ -70,9 +74,6 @@ export class ThreadsService { setCurrentThread(newThread: Thread): void { this.currentThread.next(newThread); } - } -export const threadsServiceInjectables: Array = [ - ThreadsService -]; +export const threadsServiceInjectables: Array = [ThreadsService];