Skip to content

Instantly share code, notes, and snippets.

case EMessageActions.AddMessage:
const channelId = action.payload[0];
const newMsg = action.payload[1];
const entity = state.entities[channelId];
const newMsgArray = [
...entity.messages,
newMsg
];
addMessage(msg: string) {
this.messages = [
...this.messages,
msg
];
}
this.messages$ = store.pipe(select(selectCurrentChannelMessages));
// gets the channel id of the currently selected channel
export const selectCurrentChannelId = createSelector(
selectMessagesState,
getSelectedChannelId
);
export const selectMessageEntities = createSelector(
selectMessagesState,
selectMessageContainerEntities
);
export const messageAdapter: EntityAdapter<MessageContainer> = createEntityAdapter<MessageContainer>({
selectId: messageContainer => messageContainer.channelId
});
messages: Observable<string[]>;
constructor(public messageService: MessageService) {
this.messages = messageService.messages$;
}
// 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() {
/**
* 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});
}
export const selectChannelsState = createFeatureSelector<ChannelsState>('channels');
export const selectChannelList = createSelector(
selectChannelsState,
(state: ChannelsState) => state.channels
);
const defaultChannels: Channel[] = [
{ name: "Rocket League", id: 1 },
{ name: "Aficionados", id: 2 },
{ name: "Work", id: 3 }
]
export interface ChannelsState {
channels: Channel[];
selectedChannelId: ChannelId | null;
}