【发布时间】:2021-10-10 21:51:15
【问题描述】:
我有很多电话。它们有不同的关键字,并且 call 的 id 是 call_id。数组开始时为空。如果数组为空或没有带有来电ID的呼叫,则必须将呼叫添加到状态中的呼叫数组。否则,当 state 中的数组有一个带有传入 id 的调用时,它将更新匹配的调用。现在 initialState.calls[] 还是空的
export interface Call {
timestamp: number;
call_id: string;
keywords: Keyword[];
}
export const initialState: KeywordsModel = {
version: '',
calls: [],
isOpen: false,
lastUpdatedOn: Date.now()
};
export const reducer = createReducer(
initialState,
on(KeywordsWebSocketActions.messageReceived, (state, { message, timeReceived }) => ({
...state,
...message,
lastUpdatedOn: timeReceived
})),
on(KeywordsWebSocketActions.keywordsPageOpened, state => ({ ...state, isOpen: true })),
on(KeywordsWebSocketActions.messageCallReceived, (state, { call }) => ({ ...state, calls: (state.calls.map((existCall: Call) => (existCall.call_id === call.call_id) ? state.calls[existCall.call_id] : [...state.calls, call])),
}),
))
export function reducerFactory(state: KeywordsModel | null, actions: Action) {
return reducer(state, actions);
}
使用 map() 的 messageCallReceived 完成后,STATE 保持为空 {}。
请指教。
【问题讨论】:
标签: angular typescript ngrx store