File size: 963 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export type IMessageMonitorClient = {
  request: (method: string, params?: Record<string, unknown>) => Promise<unknown>;
  stop: () => Promise<void>;
};

export function attachIMessageMonitorAbortHandler(params: {
  abortSignal?: AbortSignal;
  client: IMessageMonitorClient;
  getSubscriptionId: () => number | null;
}): () => void {
  const abort = params.abortSignal;
  if (!abort) {
    return () => {};
  }

  const onAbort = () => {
    const subscriptionId = params.getSubscriptionId();
    if (subscriptionId) {
      void params.client
        .request("watch.unsubscribe", {
          subscription: subscriptionId,
        })
        .catch(() => {
          // Ignore disconnect errors during shutdown.
        });
    }
    void params.client.stop().catch(() => {
      // Ignore disconnect errors during shutdown.
    });
  };

  abort.addEventListener("abort", onAbort, { once: true });
  return () => abort.removeEventListener("abort", onAbort);
}