Skip to content

Instantly share code, notes, and snippets.

@michael-mckenna
Created June 4, 2021 18:39
Show Gist options
  • Save michael-mckenna/cae21c80099a9da7212036fe42b0d061 to your computer and use it in GitHub Desktop.
Save michael-mckenna/cae21c80099a9da7212036fe42b0d061 to your computer and use it in GitHub Desktop.
// maintains messages across channels
private messageDB = {1: [], 2: [], 3: []};
// Tracks messages for the currently selected channel
private messageSubject$ = new BehaviorSubject<string[]>([]);
public readonly messages$ = this.messageSubject$.asObservable();
private selectedChannelId: number;
constructor() {
}
// the getter will return the last value emitted in message subject
private get messages(): string[] {
return this.messageSubject$.getValue();
}
private set messages(val: string[]) {
this.messageSubject$.next(val);
this.messageDB[this.selectedChannelId] = val;
}
getInitialMessagesForChannel(channelId: number) {
this.selectedChannelId = channelId;
this.messages = this.messageDB[channelId];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment