简体   繁体   English

如何在 RxJS 中完成 Observable

[英]How can I complete Observable in RxJS

Let's say we have an Observable :假设我们有一个Observable

var observable = Rx.Observable
    .fromEvent(document.getElementById('emitter'), 'click');

How can I make it Complete (what will trigger onComplete event for all subscribed Observers ) ?我怎样才能让它完成(什么会触发所有订阅的观察者的onComplete事件)?

In this present form, you cannot.在这种目前的形式中,你不能。 Your observable is derived from a source which does not complete so it cannot itself complete.您的 observable 源自一个不完整的源,因此它本身无法完成。 What you can do is extend this source with a completing condition.您可以做的是使用完成条件扩展此源。 This would work like :这会像这样工作:

var end$ = new Rx.Subject();
var observable = Rx.Observable
    .fromEvent(document.getElementById('emitter'), 'click')
    .takeUntil(end$);

When you want to end observable , you do end$.onNext("anything you want here");当你想结束observable ,你做end$.onNext("anything you want here"); . . That is in the case the ending event is generated by you.那就是在结束事件由您生成的情况下。 If this is another source generating that event (keypress, etc.) then you can directly put an observable derived from that source as an argument of takeUntil .如果这是生成该事件的另一个源(按键等),那么您可以直接将从该源派生的 observable 作为takeUntil的参数。

Documentation:文档:

What worked for me is using the take() operator.对我有用的是使用take()运算符。 It will fire the complete callback after x number of events.它将在 x 个事件后触发完整的回调。 So by passing 1, it will complete after the first event.因此,通过传递 1,它将在第一个事件之后完成。

Typescript:打字稿:

private preloadImage(url: string): Observable<Event> {
    let img = new Image();
    let imageSource = Observable.fromEvent(img, "load");

    img.src = url;

    return imageSource.take(1);
}

I think what you are looking for is the dispose() method.我认为您正在寻找的是dispose()方法。

from: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/creating.md#cold-vs-hot-observables来自: https : //github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/creating.md#cold-vs-hot-observables

Notice that the subscribe method returns a Disposable, so that you can unsubscribe to a sequence and dispose of it easily.请注意,subscribe 方法返回一个 Disposable,以便您可以取消订阅序列并轻松处理它。 When you invoke the dispose method on the observable sequence, the observer will stop listening to the observable for data.当您对 observable 序列调用 dispose 方法时,观察者将停止监听 observable 的数据。 Normally, you do not need to explicitly call dispose unless you need to unsubscribe early, or when the source observable sequence has a longer life span than the observer.通常,除非您需要提前取消订阅,或者当源可观察序列的生命周期比观察者更长时,否则您不需要显式调用 dispose。 Subscriptions in Rx are designed for fire-and-forget scenarios without the usage of a finalizer. Rx 中的订阅专为即发即忘的场景而设计,无需使用终结器。 Note that the default behavior of the Observable operators is to dispose of the subscription as soon as possible (ie, when an onCompleted or onError messages is published).请注意,Observable 运算符的默认行为是尽快处理订阅(即,当发布 onCompleted 或 onError 消息时)。 For example, the code will subscribe x to both sequences a and b.例如,代码将 x 订阅序列 a 和 b。 If a throws an error, x will immediately be unsubscribed from b.如果 a 抛出错误,x 将立即从 b 取消订阅。

The original answer is bit complicated, i found an easier way to do this, here is how:原来的答案有点复杂,我找到了一种更简单的方法,方法如下:

const subscription$ = interval(1000).pipe(
  finalize(() => console.log("End")),
).subscribe();

setTimeout(() => {
  subscription$.unsubscribe();
}, 4000);

The finalize is also triggered on unsubscribe.取消订阅时也会触发 finalize。

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

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