简体   繁体   English

理解观察者和子纤维的角度2

[英]understanding observers and subscibers angular2

long time lurker - first time poster. 长时间潜伏 - 第一次海报。 This is a lot of theory, so bear with me. 这是很多理论,所以请耐心等待。

I am working to mentally model the idea of an observable, observer and a subscriber (rxjs and angular2) and wanted to make sure I am on the right track. 我正在努力为可观察,观察者和订阅者(​​rxjs和angular2)的想法进行精神建模,并希望确保我走在正确的轨道上。

I currently understand observables as "streams of events that can be watched and acted upon". 我目前将可观察的事物理解为“可以观察并采取行动的事件流”。 arrays and requests can be cast as observable so that an observer can subscribe to them and when an event is fired can act on that event or the data in the observable. 数组和请求可以转换为可观察的,以便观察者可以订阅它们,并且在触发事件时可以对该事件或可观察数据中的数据进行操作。

I see that a subscriber is a type of observer that can watch an observable with 3 possible handlers: onNext, onComplete and onError. 我看到订阅者是一种观察者,可以使用3种可能的处理程序来观察可观察者:onNext,onComplete和onError。

I understand these can be in separate classes so long as the subscriber can gain access to the observable to subscribe. 我知道这些可以在不同的类中,只要订阅者可以访问observable进行订阅。

Please let me know if this is an accurate description. 如果这是一个准确的描述,请告诉我。

If this is true, I am curious as how to make an array observable as in: when new items are added to the array, how do I subscribe to that event and act upon it? 如果这是真的,我很好奇如何创建一个数组observable,如:在将新项添加到数组时,如何订阅该事件并对其进行操作?

I have tried working through this by creating an array, filling it with mouse events and attempting to cast this array as Observable. 我已经尝试通过创建一个数组,用鼠标事件填充它并尝试将此数组转换为Observable来解决这个问题。

This failed, but I was able to create an observable subject, pushing new items in with next() and subscribing to those next events with a subscriber (which I have no idea what type it is, so I just left it untyped as I could not get the compiler to accept anything I put in). 这失败了,但是我能够创建一个可观察的主题,用next()推送新项目并订阅那些订阅者的下一个事件(我不知道它是什么类型,所以我只是把它保持为无法输入不让编译器接受我放入的任何东西)。

my questions are: 我的问题是:

1) Is my above understanding of observable & observer/subscriber correct? 1)我对可观察和观察者/订阅者的理解是否正确?

2) can I create a standard array, make it observable and listen to 'added' or 'modified' events? 2)我可以创建一个标准数组,使其可观察并听取“添加”或“修改”事件吗?

3) what is the type of the subscriber that I mention above? 3)我上面提到的用户类型是什么?

this.mouseLocationSubscriber = this.mLocs$.subscribe(
        X => this.handleNext(X),
        err => this.handleSubscriberError(err),
        () => console.log('subscriber recd complete')
);

Please: 请:

  • if this post is in the wrong place, help me put it in the right spot 如果这篇文章在错误的地方,请帮我把它放在正确的位置

  • if this post is incorrectly formatted, help me understand how to format it for the best possibility of finding an answer 如果这篇文章格式不正确,请帮助我理解如何格式化它以获得找到答案的最佳可能性

  • if this post is a dupe of some similar lost code theorist, help me find that response 如果这篇文章是一些类似的丢失代码理论家的骗局,请帮助我找到答案

  • if there is any thing else I should be mobbed for, I have prepared you a pitchfork: ----E 如果还有其他东西我应该被围攻,我已经为你准备了一个干草叉: ---- E.

1) Is my above understanding of observable & observer/subscriber correct? 1)我对可观察和观察者/订阅者的理解是否正确?

You are on the right track, there's nothing that I could consider a mistake on that definition. 你走在正确的轨道上,没有什么我可以认为这个定义上的错误。 Maybe what I would just change is that I would merge observer/subscribers into one entity, because they are quite the same. 也许我会改变的是,我会将观察者/订阅者合并为一个实体,因为它们完全相同。

Also, keep in mind that Observables take a functional-style programming approach: When you create a stream, nothing gets executed until an observer calls .subscribe() on it. 另外,请记住Observable采用函数式编程方法:当您创建流时,在观察者调用.subscribe()之前,不执行任何操作。

Another thing that people often get confused on is the use of onError . 人们经常感到困惑的另一件事是使用onError When a stream yields onError it means that an unrecoverable crash has happened: The stream gets closed and onNext won't be called again. 当流产生onError ,意味着发生了不可恢复的崩溃:流关闭, onNext将不再被调用。 When people wants to get some kind of soft-errors what they do is tweak the Observable type to include that, such as isSuccesfull:boolean or errors:Error[] 当人们想要获得某种软错误时,他们所做的就是调整Observable类型以包含它,例如isSuccesfull:booleanerrors:Error[]

2) can I create a standard array, make it observable and listen to 'added' or 'modified' events? 2)我可以创建一个标准数组,使其可观察并听取“添加”或“修改”事件吗?

No... Observables can't do magic. 不......观察者不能做魔法。 From Rx.Observable you get some helper methods to transform arrays into Observable sequences, but they are not observable arrays. Rx.Observable可以获得一些辅助方法来将数组转换为Observable序列,但它们不是可观察的数组。 Think of it that you can make an observable of anything that you can do with Rx.Observable.create , for instance, a possible implementation of Rx.Observable.fromArray could be: Rx.Observable.create ,你可以用Rx.Observable.create做一个可以观察的事情,例如, Rx.Observable.fromArray一个可能的实现可能是:

function observableFromArray(array){
    return Rx.Observable.create((observer) => {
        array.forEach((elm) => {
            observer.onNext(elm);
        });
        observer.onCompleted();
    });
}

But as you can see, when an item is pushed on the array, there's no way to notify the observer. 但正如您所看到的,当一个项目被推到数组上时,无法通知观察者。

Something that could be used to do something as such are Proxies , which allow you to intercept operations done in a object/array... That's a bit edgy, so don't expert too much support in IE. 可以用来执行某些操作的东西是Proxies ,它允许你拦截在一个对象/数组中完成的操作......这有点尖锐,所以不要在IE中提供过多的专业支持。

I think your best bet is to go through Subjects , as you said. 正如你所说,我认为你最好的选择就是通过Subjects You could create your custom "ObservableArray" (or try googling that, maybe someone already did) where observers can subscribe to it, and producers can push/delete values. 您可以创建自定义的“ObservableArray”(或尝试使用Google搜索,也许有人已经这样做了)观察者可以订阅它,生产者可以推送/删除值。

3) what is the type of the subscriber that I mention above? 3)我上面提到的用户类型是什么?

Types are always kept. 始终保持类型。 If you have a Observable<MouseEvent> , the observer will be of type Observer<MouseEvent> . 如果你有一个Observable<MouseEvent> ,观察者将是Observer<MouseEvent>类型。 Think of it as an observable that emits MouseEvents, and an observer that accepts MouseEvents. 可以将其视为发出MouseEvents的可观察对象,以及接受MouseEvents的观察者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM