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

Fixes TSLint for observables #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 17 additions & 21 deletions src/app/message/messages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -83,7 +81,6 @@ export class MessagesService {
};
})
.subscribe(this.updates);

}

// an imperative function call to this action stream
Expand All @@ -92,16 +89,15 @@ export class MessagesService {
}

messagesForThreadUser(thread: Thread, user: User): Observable<Message> {
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<any> = [
MessagesService
];
export const messagesServiceInjectables: Array<any> = [MessagesService];
79 changes: 40 additions & 39 deletions src/app/thread/threads.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,73 @@ 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 }>;

// `orderedThreads` contains a newest-first chronological list of threads
orderedThreads: Observable<Thread[]>;

// `currentThread` contains the currently selected thread
currentThread: Subject<Thread> =
new BehaviorSubject<Thread>(new Thread());
currentThread: Subject<Thread> = new BehaviorSubject<Thread>(new Thread());

// `currentThreadMessages` contains the set of messages for the currently
// selected thread
currentThreadMessages: Observable<Message[]>;

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);
}

setCurrentThread(newThread: Thread): void {
this.currentThread.next(newThread);
}

}

export const threadsServiceInjectables: Array<any> = [
ThreadsService
];
export const threadsServiceInjectables: Array<any> = [ThreadsService];