我已添加到IStore
transaction
概念中。它直接表示提供一种方法来存储到我的IStore
哪些待处理操作保持挂起。完成后,它们将被删除:
export interface IStore {
user: IUser;
txs: ITxRedux;
}
我所有的减速机都像:
* reducer name: `'OPERATION'`
* success reducer name: `'OPERATION_SUCCESS'`
* failed reducer name: `'OPERATION_FAILED'`
使用@Effects
:
@Effect({ dispatch: true })
userLogin$: Observable<Action> = this._actions$
.ofType('USER_LOGIN')
.switchMap((action: Action) =>
{
....
});
目前,我的效果有这种模式:
return make_http_call
.map(_ => ({type: 'OPERATION_SUCCESS'}, payload: {...}))
.catch(_ => ({type: 'OPERATION_FAILED'}, payload: {...}));
因此,我想在每次调用或完成效果时,通过在"transaction"
中添加或删除IStore.txs
来获得一种方法。当我说"add a transaction into my IStore.txs"
时,我的意思是呼叫transaction
缩减器:
public static ADD_TX = `ADD_TX`;
private static addTx(txsRdx: ITxRedux, type, payload: ITx) {
const tx = payload;
return {
ids: [ ...txsRdx.ids, tx.id ],
entities: Object.assign({}, txsRdx.entities, {[tx.id]: tx}),
};
}
public static REMOVE_TX = `REMOVE_TX`;
private static removeTx(txsRdx: ITxRedux, type, payload) {
const tx: ITx = payload;
var entitiesTmp = {...txsRdx.entities};
delete entitiesTmp[tx.id];
return {
ids: txsRdx.ids.filter(id => tx.id != id),
entities: entitiesTmp
};
}
我听过一些关于元减速器的讨论,但我不知道他们是否能够实现我的目标。
有没有办法以优雅的方式获得它?
答案 0 :(得分:2)
延迟回复,但您可能会发现this post有用。经典示例(主要来自该帖子)是通过记录元减速器记录每个动作/状态变化:
Observable<Any>
在主应用程序模块中,使用通常的export function logging(reducer) {
return function loggingReducer(state, action) {
console.group(action.type);
// invoke following, "wrapped" reducer in the chain
const nextState = reducer(state, action);
console.log(`%c prev state`, `color: #9E9E9E`, state);
console.log(`%c action`, `color: #03A9F4`, action);
console.log(`%c next state`, `color: #4CAF50`, nextState);
console.groupEnd();
// return wrapped reducer output
return nextState;
};
}
减速机工厂撰写新的logging
减速机工厂:
combineReducers
请注意设置const appReducer = compose(logging, combineReducers)(reducers);
//...
StoreModule.provideStore(appReducer),
和全局应用减少器,因为自该博客帖子以来,该语法在最近的ngrx版本中已更改。
另外,如果您正在寻找实现meta-reducer来捕获和调用远程API调用的一些灵感,那么您可能希望了解一个等效的,已经制作的Redux中间件,为redux-api-middleware。 HTH