自动初始化BehaviorSubject来缓存全局数据
简化的用例:
- 我有带有多个模块的 Angular 应用程序,大多数(不是全部)模块使用机场列表,
- 我想创建一个
global-cache.service.ts
并将机场列表缓存在BehaviorSubject
将作为 Observable 公开。我只想在用户登陆订阅该 Observable 的组件时初始化 BehaviorSubject (点击数据库)(而不是在服务控制器中初始化它)
这是起点,无法让它工作(请参阅 this.getAirpotsFromDB()
调用后的注释):
global-cache.service.ts
airports: BehaviorSubject<string[]> = new BehaviorSubject(null);
get airports$(): Observable<string[]> {
if (this.airports.getValue() == null) {
//get list from db, and initialize the subject
this.getAirportsFromDB();
//**PROBLEM:** how do I return `this.airports.asObservable()` after it's initialized with data from the call above or return it from inside the call?
}
else{
//list initialized, emit from subject (don't hit db)
return this.airports.asObservable();
}
}
getAirportsFromDB() {
this.http.get<string[]>('/api/airports').subscribe((_result) => {
this.airports.next(_result);
});
}
myComponent1.ts
//imports global-cache.service but never subscribes to airports$ (service has many other arrays that are used here)
//User lands here first, `getAirportsFromDB()` never gets called, user goes to myComponent2
myComponent2.ts
...
airports$: any = this.globalCacheService.airports$;
//subscribed in html `airports$ | async`
...
//user lands here, `getAirportsFromDB()` is called, `airports` BehaviorSubject is initialized
//user then goes to myComponent3
myComponent3.ts
...
airports$: any = this.globalCacheService.airports$;
//subscribed in html `airports$ | async`
...
//`airports` BehaviorSubject emits last value (initialized in Component2). `getAirportsFromDB()` does not get called again
Simplified use case:
- I have Angular app with multiple modules, most (not all) of the modules use a list of airports
- I want to create a
global-cache.service.ts
and cache the airport list inBehaviorSubject
which will be exposed as Observable. I only want to initialize theBehaviorSubject
(hit the DB) when user lands on component that subscribes to that Observable (vs initializing it in the service controller)
Here's the starting point, can't get it to work (see comment after this.getAirpotsFromDB()
call) :
global-cache.service.ts
airports: BehaviorSubject<string[]> = new BehaviorSubject(null);
get airports$(): Observable<string[]> {
if (this.airports.getValue() == null) {
//get list from db, and initialize the subject
this.getAirportsFromDB();
//**PROBLEM:** how do I return `this.airports.asObservable()` after it's initialized with data from the call above or return it from inside the call?
}
else{
//list initialized, emit from subject (don't hit db)
return this.airports.asObservable();
}
}
getAirportsFromDB() {
this.http.get<string[]>('/api/airports').subscribe((_result) => {
this.airports.next(_result);
});
}
myComponent1.ts
//imports global-cache.service but never subscribes to airports$ (service has many other arrays that are used here)
//User lands here first, `getAirportsFromDB()` never gets called, user goes to myComponent2
myComponent2.ts
...
airports$: any = this.globalCacheService.airports$;
//subscribed in html `airports$ | async`
...
//user lands here, `getAirportsFromDB()` is called, `airports` BehaviorSubject is initialized
//user then goes to myComponent3
myComponent3.ts
...
airports$: any = this.globalCacheService.airports$;
//subscribed in html `airports$ | async`
...
//`airports` BehaviorSubject emits last value (initialized in Component2). `getAirportsFromDB()` does not get called again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试如下所示,只需使用
tap
运算符并将 http 请求的值保存在BehaviorSubject
中,并从getAirportsFromDB 返回
Observable
小更新:
第一次调用时不会返回BehaviorSubject observable,因此对 getter 的后续调用不会更新视图,因此您只能在将其用作缓存并且不想接收的情况下使用上面的代码任何更新
You can try something like below, just use
tap
operator and save the value of http request in theBehaviorSubject
and returnObservable
fromgetAirportsFromDB
Minor update:
The BehaviorSubject observable isn't getting returned on the first call so subsequent calls to the getter wouldn't update the view, so you can use the code above only in case, that you use it as the cache and don't want to receive any updates
最终选择了
shareReplay(1)
;方法更简单,并且完全符合我的要求global-cache.service.ts
Ended up going with
shareReplay(1)
; way easier and does exactly what I wantedglobal-cache.service.ts