我想使用NGRX创建消息队列,添加到队列的每个消息应显示5秒钟,然后从状态中删除,
currentMessageId == null
,则应立即从队列中获取消息>这是我的状态:
import { EntityState } from '@ngrx/entity'; export interface UiMessage { id: string; severity: 'info' | 'warn' | 'error' | 'success'; summary?: string; detail: string; } export interface MessagesState extends EntityState<UiMessage>{ currentMessageId: string; }
我已经尝试过此代码,如果商店中总是有一条消息,它会很好地工作:
takeMessageInterval$ = createEffect(() => { return timer(0, 5000).pipe( map(() => { return UiActions.takeNextMessage() }) ) });
但请考虑这种情况:
这也是reducer函数:
export const messagesReducer = createReducer<MessagesState>(
initialState,
on(UiActions.addStateMessage, (current, action) => {
return messagesAdapter.addOne(action.message, current);
}),
on(UiActions.removeStateMessage, (current, action) => {
return messagesAdapter.removeOne(action.id, current);
}),
on(UiActions.takeNextMessage, (current, action) => {
// remove current message
if (current.currentMessageId) {
current = messagesAdapter.removeOne(current.currentMessageId, current);
}
// take next message id
const nextId = current.ids[0] as string;
current.currentMessageId = nextId;
return current;
}),
);
我想使用NGRX创建消息队列,应将添加到队列中的每条消息显示5秒钟,然后从状态中删除,应将空值分配给currentMessageId ...
这里可能是一种方法,假设一次只能显示一条消息: