Skip to content

Instantly share code, notes, and snippets.

private channelSubject$ = new BehaviorSubject<Channel[]>([]);
private get channels(): Channel[] {
return this.channelSubject$.getValue();
}
private set channels(val: Channel[]) {
this.channelSubject$.next(val);
}
channels$: Observable<Channel[]>;
selectedChannel$: Observable<Channel>;
constructor(public channelService: ChannelService) {
this.channels$ = channelService.channelsChanged$;
this.selectedChannel$ = channelService.selectedChannel$;
}
const defaultChannels: Channel[] = [
{ name: "Rocket League", id: 1 },
{ name: "Aficionados", id: 2 },
{ name: "Work", id: 3 }
]
export interface ChannelsState {
channels: Channel[];
selectedChannelId: ChannelId | null;
}
export const selectChannelsState = createFeatureSelector<ChannelsState>('channels');
export const selectChannelList = createSelector(
selectChannelsState,
(state: ChannelsState) => state.channels
);
/**
* Updatess the corresponding channel's name. Triggers change detection by returning a new array.
* @param channel channel with updated name
*/
updateChannel(channel: Channel) {
this.channels = this.channels.map(c => c.id === channel.id ? {...c, name: channel.name} : {...c});
}
// 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() {
messages: Observable<string[]>;
constructor(public messageService: MessageService) {
this.messages = messageService.messages$;
}
export const messageAdapter: EntityAdapter<MessageContainer> = createEntityAdapter<MessageContainer>({
selectId: messageContainer => messageContainer.channelId
});
// gets the channel id of the currently selected channel
export const selectCurrentChannelId = createSelector(
selectMessagesState,
getSelectedChannelId
);
export const selectMessageEntities = createSelector(
selectMessagesState,
selectMessageContainerEntities
);
this.messages$ = store.pipe(select(selectCurrentChannelMessages));