获取所有当前(活动)订阅 [英] Get all current (active) subscriptions

查看:74
本文介绍了获取所有当前(活动)订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不手动存储的情况下获得所有活动"订阅?

Is it possible to get all the "active" subscriptions without storing them manually?

我想unsubscribe所有活动"订阅,并且不想在数组或变量中引用每个订阅.

I'd like to unsubscribe all of the "active" subscriptions and don't want to reference each of them in an array or a variable.

推荐答案

我取决于您使用的是Subject还是Observable,但可能无法自动"执行此操作.

I depends on whether you're using a Subject or an Observable but there's probably no way to do this "automatically".

我不认为您可以拥有订阅的可观察的"之类的东西,因为您存储了一个观察的"或订阅":

I don't think you can have such thing as "subscribed Observable" because you either store an Observable or Subscription:

const source = Observable.of(...)
  .map(...);

const subscription = source
  .subscribe();

此处source表示可观察,而subscription表示单个订阅.

Here source represents an Observable and subscription represents a single subscription.

请注意,您可以拥有一个Subscription实例,该实例存储多个其他订阅:

Note that you can have a Subscription instance that stores multiple other subscriptions:

const subscriptions = new Subscription();

const sub1 = Observable...subscribe();
const sub2 = Observable...subscribe();
const sub3 = Observable...subscribe();

subscriptions.add(sub1).add(sub2).add(sub3);

// Then unsubscribe all of them with a single 
subscriptions.unsubscribe();

主题

如果您正在使用主题,它们本身也具有unsubscribe方法,请参见 https://github.com/ReactiveX/rxjs/blob/master/src/Subject.ts#L96 .

Subjects

If you're using Subjects they do have the unsubscribe method themselves, see https://github.com/ReactiveX/rxjs/blob/master/src/Subject.ts#L96.

但是请注意,这会使主题停止",有关更多信息,请参见

However be aware that this makes the Subject "stopped", for more info see https://medium.com/@martin.sikora/rxjs-subjects-and-their-internal-state-7cfdee905156

这篇关于获取所有当前(活动)订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆