我有一个通知模块(如下所示),根据来自其他模块的传入操作显示snackbar。例如'[Product] item added','[Product] item removed'等。这些actionTypes使用动作'AddTrigger'动态添加到NotificationState'触发器'。在Notification Effects下,我订阅了'触发'状态,并通过将更新的allowedTypes作为参数传递给'ofType'函数来动态修改'show $'效果。
代码
@Injectable()
export class NotificationEffect
{
@Effect() public show$: Observable<Action>;
constructor (
private actions$: Actions,
private store: Store<NotificationModuleState>,
private matSnackBar: MatSnackBar
)
{
this.store.select( state => state.notificationState.triggers )
.subscribe( triggers =>
{
this.show$ = this.actions$.ofType( ...triggers )
.map( action => this.matSnackBar.open( 'Dynamic notification', 'Ok', { duration: 5000 } ) )
.switchMap( snackBar => snackBar.afterDismissed() )
.map( closed => new NotificationAction.Closed() );
} )
}
}