我想使用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()
})
)
});
但是考虑这种情况:
这也是化简函数:
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;
}),
);
答案 0 :(得分:0)
这里可能是一种方法,假设一次只能显示一条消息:
takeMessageInterval$ = createEffect(
() => this.actions$.pipe(
// Intercept when a message has been added
ofType(UiActions.addStateMessage),
concatMapTo( // Queue up the messages
timer(5000).pipe( // Display it for 5 seconds
endWith(UiActions.takeNextMessage()) // Display the next message and remove the current one
)
)
)
);