Rxjs - 无法将冷的可观察到的转化为热的
Rxjs - Can't convert cold observable into a hot one
所以我正在使用 Angular 2 并努力使 HTTP 请求可观察以在多个观察者之间共享。更具体地说,我想分享所有响应类型,包括错误。
我试过这样的:
return this._http.request(new Request(options))
.map((res: Response) => this.refreshToken(res))
.share()
然后
this.data.request()
.map((response: Response) => (new financeiro.FetchCompletedAction(response)))
.catch((error: any) => {
console.log('lancamento-effects:');
return Observable.of(new feedback.HttpRequestFailedAction(["Erro ao inserir lançamento"]))
})
上面的代码应该发送错误操作,最终在用户屏幕上显示错误消息。
实际上这种情况发生了两次..
它还在控制台上打印了两次。
这是一些代码 (Plunkr):
@Component({
selector: 'my-app',
template: `
<button (click)="subscribe()">Subscribe</button>
`
})
export class AppComponent {
obs: Observable<any>;
constructor(private http: Http) {
// Create the observable ONCE + Share.
this.obs = this.http.get('https://httpbin.org/get')
.do(() => console.log('***SIDE EFFECT***'))
.mapTo('***RESULT***')
.share();
}
subscribe() {
// Subscribe to the same observable TWICE.
this.obs.subscribe(val => console.log('subs1', val));
this.obs.subscribe(val => console.log('subs2', val));
}
}
这是控制台显示的内容:
***SIDE EFFECT***
subs1 ***RESULT***
subs2 ***RESULT***
一个 side-effect(即 HTTP 请求),两个订阅。
这就是你要找的吗?
确保每个订阅者都订阅相同的 热观察。每次调用:
return this._http.request(new Request(options))
.map((res: Response) => this.refreshToken(res))
.share()
您正在从一个冷可观察对象创建一个新的热可观察对象。
示例假设上面的代码包装在一个名为 foo() 的函数中:如果您执行以下操作,您将创建 2 个热可观察对象和 2 个 http 请求:
foo().subscribe(...)
foo().subscribe(...)
而以下只会创建一个热可观察对象(和一个 http 请求):
let hotObs = foo();
hotObs.subscribe(...)
hotObs.subscribe(...);
所以我正在使用 Angular 2 并努力使 HTTP 请求可观察以在多个观察者之间共享。更具体地说,我想分享所有响应类型,包括错误。
我试过这样的:
return this._http.request(new Request(options))
.map((res: Response) => this.refreshToken(res))
.share()
然后
this.data.request()
.map((response: Response) => (new financeiro.FetchCompletedAction(response)))
.catch((error: any) => {
console.log('lancamento-effects:');
return Observable.of(new feedback.HttpRequestFailedAction(["Erro ao inserir lançamento"]))
})
上面的代码应该发送错误操作,最终在用户屏幕上显示错误消息。
实际上这种情况发生了两次..
它还在控制台上打印了两次。
这是一些代码 (Plunkr):
@Component({
selector: 'my-app',
template: `
<button (click)="subscribe()">Subscribe</button>
`
})
export class AppComponent {
obs: Observable<any>;
constructor(private http: Http) {
// Create the observable ONCE + Share.
this.obs = this.http.get('https://httpbin.org/get')
.do(() => console.log('***SIDE EFFECT***'))
.mapTo('***RESULT***')
.share();
}
subscribe() {
// Subscribe to the same observable TWICE.
this.obs.subscribe(val => console.log('subs1', val));
this.obs.subscribe(val => console.log('subs2', val));
}
}
这是控制台显示的内容:
***SIDE EFFECT***
subs1 ***RESULT***
subs2 ***RESULT***
一个 side-effect(即 HTTP 请求),两个订阅。
这就是你要找的吗?
确保每个订阅者都订阅相同的 热观察。每次调用:
return this._http.request(new Request(options))
.map((res: Response) => this.refreshToken(res))
.share()
您正在从一个冷可观察对象创建一个新的热可观察对象。
示例假设上面的代码包装在一个名为 foo() 的函数中:如果您执行以下操作,您将创建 2 个热可观察对象和 2 个 http 请求:
foo().subscribe(...)
foo().subscribe(...)
而以下只会创建一个热可观察对象(和一个 http 请求):
let hotObs = foo();
hotObs.subscribe(...)
hotObs.subscribe(...);