跳到主要内容

SubscriptionManager

class SubscriptionManager<S extends SubscriptionConstructable> implements Manager

统一协调所有订阅,确保数据新鲜且不会过度获取。

实现

SubscriptionManager 实现了 Manager

constructor(Subscription: S)​

Subscription 是用于处理每个 endpoint 订阅的类。每个实例代表对某个特定唯一 endpoint 的一个订阅。

消费的 Action​

  • 'rdc/subscribe'
  • 'rdc/unsubscribe'

Subscription​

Subscription 是一个实现了 SubscriptionConstructable 的类。Subscription 实例负责处理实际的订阅。

/** Interface handling a single resource subscription */
interface Subscription {
add(frequency?: number): void;
remove(frequency?: number): boolean;
cleanup(): void;
}

/** The static class that constructs Subscription */
export interface SubscriptionConstructable {
new (
action: Omit<SubscribeAction, 'type'>,
controller: Controller,
): Subscription;
}

add(frequency?: number): void​

以给定频率为该资源添加一个新订阅。

remove(frequency?: number): boolean​

移除给定频率的订阅。如果移除后不再有任何订阅,则返回 true。这用于清理不再使用的 Subscription。

cleanup(): void​

在 Subscription 不再使用后,清理所有残留的资源。

内置实现​

备注

要实现自己的 Subscription 来处理 websocket,可以用收到的数据 dispatch rdc/set action 来进行更新。务必在构造函数中建立连接,并在 cleanup() 中关闭连接。