我将 rxjs
与 Angular 2 和 Typescript 一起使用。我想在多个组件之间共享一个通用的 Web 资源(我的应用程序上下文中的“项目”,本质上是一个 JSON 文档)。为了实现这一目标,我引入了一个公开可观察对象的服务,该服务将由所有客户端共享:
/**
* Handed out to clients so they can subscribe to something.
*/
private _observable : Observable<Project>;
/**
* Used to emit events to clients.
*/
private _observer : Observer<Project>;
constructor(private _http: Http) {
// Create observable and observer once and for all. These instances
// are not allowed to changed as they are passed on to every subscriber.
this._observable = Observable.create( (obs : Observer<Project>) => {
this._observer = obs;
});
}
客户现在只需获取对该_observable
的引用并订阅它即可。
/**
* Retrieves an observable that always points to the active
* project.
*/
get ActiveProject() : Observable<Project> {
return (this._observable);
}
当某个组件决定实际加载项目时,它会调用以下方法:
/**
* @param id The id of the project to set for all subscribers
*/
setActiveProject(id : string) {
// Projects shouldn't change while other requests are in progress
if (this._httpRequest) {
throw { "err" : "HTTP request in progress" };
}
this._httpRequest = this._http.get('/api/project/' + id)
.catch(this.handleError)
.map(res => new Project(res.json()));
this._httpRequest.subscribe(res => {
// Cache the project
this._cachedProject = res;
// Show that there are no more requests
this._httpRequest = null;
// Inform subscribers
this._observer.next(this._cachedProject)
console.log("Got project");
});
}
它基本上执行一个 HTTP 请求,将 JSON 文档转换为“正确的”实例,并调用 this._observer.next()
通知所有订阅者有关更改的信息。
但是,如果在 HTTP 请求发生后进行了某些订阅,则在发出新的 HTTP 请求之前看不到任何内容。我发现 rxjs 中有某种缓存(或重播?)机制似乎可以解决这个问题,但我不知道如何使用它。
tl;dr:如何确保观察者上对 subscribe
的调用最初收到最新值?
额外问题:通过“将观察者从可观察对象中拉出来”(在构造函数中),我实际上创建了一个主题吗?
最佳答案
这就是BehaviorSubject
所做的
import { BehaviorSubject } from 'rxjs/subject/BehaviorSubject';
...
obs=new BehaviourSubject(4);
obs.subscribe(); //prints 4
obs.next(3); //prints 3
obs.subscribe(); //prints 3
关于javascript - 如何确保对观察者的订阅调用最初收到最新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36372724/