热水和从EventEmitter共享观测 [英] Hot and shared Observable from an EventEmitter
问题描述
有没有办法让从 EventEmitter
(或等效可用在角2阿尔法46 / RxJS 5阿尔法)的热点观察到的?即如果我们订阅的价值得到解决后,它与$ P $触发pviously解析值。类似于我们总是时返回相同的承诺。
Is there a way to have a hot observable from an EventEmitter
(or equivalent available in Angular 2 alpha 46 / RxJS 5 alpha)? i.e. if we subscribe after the value is resolved, it triggers with the previously resolved value. Similar to what we have when always returning the same promise.
在理想情况下,仅使用2角对象(我读的地方光RxJS将在晚些时候嵌入删除的依赖),否则进口RxJS是罚款。 AsyncSubject似乎符合我的需要,但它不提供RxJS 5阿尔法。
Ideally, only using Angular 2 objects (I read somewhere a light RxJS would be embedded later to remove the dependency), otherwise importing RxJS is fine. AsyncSubject seems to match my need, but it is not available in RxJS 5 alpha.
我尝试以下,没有成功(从不触发)。有关如何使用它的主意?
I tried the following, without success (never triggers). Any idea about how to use it?
let emitter = new EventEmitter<MyObj>();
setTimeout(() => {emitter.next(new MyObj());});
this.observable = emitter;
return this.observable.share();
USECASE :达成一些异步对象只有一次(例如一系列HTTP调用的合并/包在新 EventEmitter
),但提供解决异步对象的任何服务/组件订阅它,即使他们订阅它解决后(HTTP响应收到)。
Usecase: reach some async objects only once (for example a series of HTTP calls merged/wrapped in a new EventEmitter
), but provide the resolved async object to any service/component subscribing to it, even if they subscribe after it is resolved (the HTTP responses are received).
的编辑:问题不是如何从EventEmitter或任何等效可用角2阿尔法46 / RxJS 5阿尔法允许后,认购合并HTTP响应,但如何获得(热?)可观察到的异步结果检索/解析(HTTP是只是异步原点的一个例子)。 myEventEmitter.share()不工作(CF plunker以上),虽然它的工作原理与观测通过HTTP返回(CF的从 @Eric马丁内斯plunker)。而作为角2阿尔法46,.toRx()方法不存在任何更多的EventEmitter是可观察并受本身。的
the question is not about how to merge HTTP responses, but how to get a (hot?) observable from EventEmitter or any equivalent available with Angular 2 alpha 46 / RxJS 5 alpha that allows to subscribe after the async result is retrieved/resolved (HTTP is just an example of async origin). myEventEmitter.share() does not work (cf plunker above), although it works with the Observable returned by HTTP (cf plunker from @Eric Martinez). And as of Angular 2 alpha 46, .toRx() method does not exist any more, the EventEmitter is the observable and subject itself.
这是什么,只要我们总是返回相同的承诺对象,承诺运作良好。既然我们有HTTP角2服务引入观察员,我想,以避免混合承诺和观察员(和观察员被说成是比承诺更强大,所以它应该允许做的是容易的承诺)。
This is something working well with promises as long as we always return the same promise object. Since we have observers introduced with HTTP Angular 2 services, I would like to avoid mixing promises and observers (and observers are said to be more powerful than promises, so it should allow to do what is easy with promises).
约每股规格()(我还没有发现文档的版本5阿尔法 - 版本所采用角2) - 工作的观测
由角2 HTTP服务返回,不工作EventEmitter。
Specs about share() (I haven't found doc for version 5 alpha - version used by Angular 2) - working on the Observable
returned by the Angular 2 HTTP service, not working on EventEmitter.
的编辑:澄清为什么不使用观测通过HTTP返回并补充说,不使用RxJS直接就更好了的
的编辑:修改说明:该担心的是多个订阅,而不是合并HTTP结果的
谢谢!
推荐答案
ReplaySubject
正在做什么,我一直在寻找。 @robwormald提供小胶质我略作修改,以更好地展示工作的例子。
ReplaySubject
is doing what I was looking for. @robwormald provided a working example on gitter I slightly modified to better demonstrate.
的揭露HTTP响应:的
import {Injectable} from 'angular2/angular2';
import {Http} from 'angular2/http';
import {ReplaySubject} from '@reactivex/rxjs/dist/cjs/Rx'
@Injectable()
export class PeopleService {
constructor(http:Http) {
this.people = new ReplaySubject(1);
http.get('api/people.json')
.map(res => res.json())
.subscribe(this.people);
}
}
的订阅多次:的
// ... annotations
export class App {
constructor(peopleService:PeopleService) {
people.subscribe(v => {
console.log(v)
});
//some time later
setTimeout(() => {
people.subscribe(v => {
console.log(v)
});
people.subscribe(v => {
console.log(v)
});
},2000)
}
}
编辑: BehaviorSubject
是一个另类。在此用例,所不同的是初始值,例如,如果我们想用HTTP响应更新前显示来自高速缓存的内容。
the BehaviorSubject
is an alternative. In this usecase, the difference is the initial value, for example if we want to display content from cache before updating with the HTTP response.
这篇关于热水和从EventEmitter共享观测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!