Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RxJS 5和Angular 2:根据之前的结果按计划重播订阅_Angular_Typescript_Rxjs_Observable - Fatal编程技术网

RxJS 5和Angular 2:根据之前的结果按计划重播订阅

RxJS 5和Angular 2:根据之前的结果按计划重播订阅,angular,typescript,rxjs,observable,Angular,Typescript,Rxjs,Observable,在我的Angular 2typescript 2应用程序中,我向服务器查询需要定期更新的值。更新之间的延迟是可变的(服务器发送过期日期和值) 当当前值过期时,我很难创建一个可观察的流来自动重播(启动对服务器的新调用)。到目前为止,我所拥有的根本不具有可伸缩性: price = 5; //initial value is known expires = ...;//initial expiration is known getData(){ // server returns {expi

在我的
Angular 2
typescript 2
应用程序中,我向服务器查询需要定期更新的值。更新之间的延迟是可变的(服务器发送过期日期和值)

当当前值过期时,我很难创建一个可观察的流来自动重播(启动对服务器的新调用)。到目前为止,我所拥有的根本不具有可伸缩性:

price = 5; //initial value is known
expires = ...;//initial expiration is known

getData(){
    // server returns {expires:number, price:number}
    this.http.get('...').map(res => res.json())
}

Observable.timer(expires-Date.now()) // when initial price expires
    .switchMap(()=>this.getData()) // fetch new price and expiration
    .subscribe( data =>
        {
            this.price = data.price;
            Observable.timer(data.expires-Date.now()) //redo when price expires
                .switchMap(()=>getData())
                .subscribe(...) //callback hell (endless inner blocks)
        }
    );

必须有更好的方法来安排跟进电话

必要时,先打电话给DoobservableStup

getData(){
    // server returns {expires:number, price:number}
    this.http.get('...').map(res => res.json())
    .subscribe( data =>
        {
            this.price = data.price;
            doObservableStuff(data.expires-Date.now())
        });
}

doObservableStuff(time){
    Observable.timer(time)
    .switchMap(() => this.getData())
}

必要时,首先调用DoobservableStup

getData(){
    // server returns {expires:number, price:number}
    this.http.get('...').map(res => res.json())
    .subscribe( data =>
        {
            this.price = data.price;
            doObservableStuff(data.expires-Date.now())
        });
}

doObservableStuff(time){
    Observable.timer(time)
    .switchMap(() => this.getData())
}

我的方法是使用
switchMap()
timer()
来延迟可观察链的开始,并且
repeat()
在上一个价格到期后继续寻找新数据:

price = 0;    //initial price
_exp  = 0; //initial delay before fetching data

/** Observable will emit after the expiration delay */
wait() {return Observable.timer(this._exp*1000)}

stream$ = Observable.of(null)
    .switchMap(()=>this.wait()) // wait for the expiration delay                
    .switchMap(()=>this.getServerData()) // get fresh data
    .do(e=>{this._exp = e.expires}) //update expiration             
    .repeat() // repeat until the calling code unsubscribes
当我订阅时,会立即获取第一个价格,并无限期地重复该序列,每个周期延迟
到期
秒。我可以在价格到达时更新模型:

ngOnInit(){
    this.stream$.subscribe( e=>this.price = e.price);
}

我的方法是使用
switchMap()
timer()
来延迟可观察链的开始,并且
repeat()
在上一个价格到期后继续寻找新数据:

price = 0;    //initial price
_exp  = 0; //initial delay before fetching data

/** Observable will emit after the expiration delay */
wait() {return Observable.timer(this._exp*1000)}

stream$ = Observable.of(null)
    .switchMap(()=>this.wait()) // wait for the expiration delay                
    .switchMap(()=>this.getServerData()) // get fresh data
    .do(e=>{this._exp = e.expires}) //update expiration             
    .repeat() // repeat until the calling code unsubscribes
当我订阅时,会立即获取第一个价格,并无限期地重复该序列,每个周期延迟
到期
秒。我可以在价格到达时更新模型:

ngOnInit(){
    this.stream$.subscribe( e=>this.price = e.price);
}

感谢您提供的创造性解决方案。
getData()
实际上位于当前模块所依赖的另一个服务(
DataService
)中,而其他模块所依赖的是相同的
getData()
,因此我不想从该方法调用
doObservableStuff()
,该方法与
this.price
不在同一范围内;这两种方法应该在独立的上下文中使用。感谢您提供的创造性解决方案。
getData()
实际上位于当前模块所依赖的另一个服务(
DataService
)中,而其他模块所依赖的是相同的
getData()
,因此我不想从该方法调用
doObservableStuff()
,该方法与
this.price
不在同一范围内;这两种方法应该在独立的上下文中使用。