2016-11-19 107 views
0

我有很多Reducer查询,我需要链接到行为主题。目前我正在为此做好每一件!@ngrx - 将商店查询链接到BehaviorSubject

有没有更简洁的方式来表达这一点?

this._higherCurveObservable = this.store.let(getCurveSummaryForInstrumentTimeframe(this._instrument, this._timeframe.Code)); 
this._higherCurveSubject = <BehaviorSubject<CurveSummary>>new BehaviorSubject(null).distinctUntilChanged(); 
this._higherCurveSubscription = this._higherCurveObservable.subscribe(x => this._higherCurveSubject.next(x)); 
this._higherCurveSubject.subscribe(x => this.higherCurveChange(x)); 

回答

1

应该没有必要创建单独的BehaviourSubjects,你可能只是你的基础上需要储存流Observables

(唯一一次当你需要这样的东西 - 而我并不假定你这样做 - 当你试图滥用BehaviourSubject以便能够从商店获取数据时,情况就是如此从侧面,在这种情况下,你应该重新考虑架构调度数据,因为这将违背概念的ngrx具有单中心店)

所以不存在要与较少的开销写这篇多种方式。


实施例1:一个永久的订阅,从未取消订阅(例如,在服务)

this.store 
    .let(getCurveSummaryForInstrumentTimeframe(this._instrument, this._timeframe.Code)) 
    .distinctUntilChanged() 
    .do(x => this.higherCurveChange(x)) 
    .subscribe(); 

实施例2:临时订阅,应该在一个退订特定的时间点(例如,在组件中,当组件被销毁时它应该取消订阅)

const higherCurve$: Observable<CurveSummary> = this.store 
    .let(getCurveSummaryForInstrumentTimeframe(this._instrument, this._timeframe.Code)) 
    .distinctUntilChanged(); 
this._higherCurveSubscription = higherCurve$.subscribe(x => this.higherCurveChange(x)); 

// ... some time later 
this._higherCurveSubscription.unsubscribe(); 
+0

非常有帮助!谢谢!你说得对,我滥用行为主题。我现在有一个单一的中央数据存储区,它的工作非常漂亮! – reach4thelasers