Jem*_*Jem 7 redux ngrx ngrx-effects angular ngrx-store
所以我通过构建以下沙箱来试验ngrx&ngrx/effects:
https://stackblitz.com/edit/ngrx-vanilla
快速介绍:
三页:
问题和背景:
收到更新后(在app/store/effects/meeting.effects.ts中说明),将调度新操作.
最后,问题是:让一个共同的服务了解商店是一种干净的做法吗?将一个监听器注册到websocket/firebase实时数据库以便在推送数据时调度操作的最佳位置在哪里?
在这里,我做了一个效果(meeting.effects)对meetingActions.START_MEETING操作类型做出反应,每当推送数据时,都会向商店发送更新订单,但由于我提出的一系列原因,这感觉不对:
这些案件通常如何处理?
假设websocket正在发出不同类型的事件,则将每个事件映射到websocket服务中的不同操作,例如
@Injectable()
class WebsocketService{
private socket$:Observable<Action>
getActions():Observable<Action>{
if(!this.socket$) {
this.socket$ = Observable.webSocket(url).pipe(
map(functionToMapActions)
,shareReplay(1)
);
}
return this.socket$;
}
}
Run Code Online (Sandbox Code Playgroud)
其中functionToMapActions将webSocket事件映射为动作,建议shareReplay在最后添加操作符,以便我们仅读取一次webSocket。
the Observable.webSocket connects to webSocket , and emits events as they arrive
webSocket connection will be established when you suscribe to webService.getActions()
You can subscribe to websocket actions in @Effects initialization see here
@Effect()
init$ = this.websocketService.getActions();
Run Code Online (Sandbox Code Playgroud)
this will emit all actions as soon as your app starts (if effect in in root module) or your module is loaded if it is in lazy loaded module;
Or if you are interrested in limited set of actions you can do like this
@Effect()
init$ = this.websocketService.getActions().pipe(filter(filterNeededActionsHere));
Run Code Online (Sandbox Code Playgroud)
you can also start listening to actions only after perticular event like this
@Effect()
init$ = this.actions$.pipe(
ofType('Event which marks start')
,swichMapTo(this.websocketService.getActions())
);
Run Code Online (Sandbox Code Playgroud)
like previous example you can also filter out action here sane as before
hope this answers your question
| 归档时间: |
|
| 查看次数: |
1752 次 |
| 最近记录: |