Javascript @ngrx-将存储查询链接到BehaviorSubject
我有很多问题需要与行为主题联系起来。此刻,我正在为每一个人做这件事 有没有更简洁的方式来表达这一点Javascript @ngrx-将存储查询链接到BehaviorSubject,javascript,redux,observable,ngrx,Javascript,Redux,Observable,Ngrx,我有很多问题需要与行为主题联系起来。此刻,我正在为每一个人做这件事 有没有更简洁的方式来表达这一点 this._higherCurveObservable = this.store.let(getCurveSummaryForInstrumentTimeframe(this._instrument, this._timeframe.Code)); this._higherCurveSubject = <BehaviorSubject<CurveSummary>>new B
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));
this.\u higherCurveObservable=this.store.let(getCurveSummaryForInstrumentTimeframe(this.\u instrument,this.\u timeframe.code));
this.\u higherCurveSubject=new BehaviorSubject(null).distinctUntilChanged();
this.\u higherCurveSubscription=this.\u higherCurveObservable.subscribe(x=>this.\u higherCurveSubject.next(x));
this._higherCurveSubject.subscribe(x=>this.higherCurveChange(x));
不需要创建单独的行为主题
,您只需根据存储流创建所需的观察对象即可
(当您试图滥用Behavior Subject
从存储区获取数据以及从侧面发送数据时,您唯一需要这样做的时候——我并不是假设您这样做了——是这样的情况,在这种情况下,您应该重新考虑体系结构,因为这与ngrx
拥有一个中央存储。)
因此,有多种方法可以以较少的开销编写此文件
示例1:永不退订的永久订阅(例如在服务中)
示例2:应在特定时间点取消订阅的临时订阅(例如,在组件中,应在组件被销毁时取消订阅)
const higherCurve$:Observable=this.store
.let(GetCurveSummary for instrument timeframe(此.\u instrument,此.\u timeframe.Code))
.distinctUntilChanged();
这个.u higherCurveSubscription=higherCurve$.subscribe(x=>this.higherCurveChange(x));
//…一段时间后
此._higherCurveSubscription.unsubscribe();
非常有用!谢谢!你说得对,我滥用了行为主题。我现在有一个单一的中央数据存储,它工作得非常好!
this.store
.let(getCurveSummaryForInstrumentTimeframe(this._instrument, this._timeframe.Code))
.distinctUntilChanged()
.do(x => this.higherCurveChange(x))
.subscribe();
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();