RxJS Observable - 每次满足条件时订阅一次
RxJS Observable - subscribe once every time a condition met
Observables 对我来说是新的,所以我什至不确定它是否可能(但我猜它是)。我想要实现的是有一个可观察的(Angular 2 http.get
调用),并在变量更改时订阅它 一次。示例流程如下所示:
let x = false;
// ... somewhere later I change "x" to true
subscribe gets called once, x => false
// stuffs happening, 10 minutes later x => true again
subscribe gets called once, x => false
更大的概念是我会有一个 UserService
并且在构造函数中我会订阅一次 /api/user/me
,只要 localStorage 中有令牌更改。这是一个有效的用例吗?如果是,我该如何使用可观察对象来实现?
像这样应该可以解决问题:
// Observable that on subscription will result in an api call. It would
// naturaly have another type in an acaual use case.
let apiCallObservable : Rx.Observable< { someResult: any }>;
let localStorageChangedObservable = new Rx.Subject<boolean>()
localStorageChangedObservable
// only care about local storage change if not null
// This is just an example the type of localStorageChangedObservable
// could be anything you want, and you could check anything you want in the
// "where"
.where(v => v != null)
.selectMany(() =>
// Take one is to make sure we terminate apiCallObservable after recieving
// one result from it.
apiCallObservable.take(1))
.subscribe(
(result: { someResult: any}) => {
// here we would have the result from the call to apiCallObservable
});
// To execute the chain abouve we would pass values to the localStorageChangedObservable
// subject like this:
localStorageChangedObservable.onNext(true); // results in api call and result in subscribe
localStorageChangedObservable.onNext(null); // results in nothing.
localStorageChangedObservable.onNext(false); // results in api call and result in subscribe
因此,当您想触发 api 调用时,基本上将某些内容传递给 localStorageChangedObservable.onNext(...)
。
Observables 对我来说是新的,所以我什至不确定它是否可能(但我猜它是)。我想要实现的是有一个可观察的(Angular 2 http.get
调用),并在变量更改时订阅它 一次。示例流程如下所示:
let x = false;
// ... somewhere later I change "x" to true
subscribe gets called once, x => false
// stuffs happening, 10 minutes later x => true again
subscribe gets called once, x => false
更大的概念是我会有一个 UserService
并且在构造函数中我会订阅一次 /api/user/me
,只要 localStorage 中有令牌更改。这是一个有效的用例吗?如果是,我该如何使用可观察对象来实现?
像这样应该可以解决问题:
// Observable that on subscription will result in an api call. It would
// naturaly have another type in an acaual use case.
let apiCallObservable : Rx.Observable< { someResult: any }>;
let localStorageChangedObservable = new Rx.Subject<boolean>()
localStorageChangedObservable
// only care about local storage change if not null
// This is just an example the type of localStorageChangedObservable
// could be anything you want, and you could check anything you want in the
// "where"
.where(v => v != null)
.selectMany(() =>
// Take one is to make sure we terminate apiCallObservable after recieving
// one result from it.
apiCallObservable.take(1))
.subscribe(
(result: { someResult: any}) => {
// here we would have the result from the call to apiCallObservable
});
// To execute the chain abouve we would pass values to the localStorageChangedObservable
// subject like this:
localStorageChangedObservable.onNext(true); // results in api call and result in subscribe
localStorageChangedObservable.onNext(null); // results in nothing.
localStorageChangedObservable.onNext(false); // results in api call and result in subscribe
因此,当您想触发 api 调用时,基本上将某些内容传递给 localStorageChangedObservable.onNext(...)
。