File size: 907 Bytes
9b4caaa
f36471e
9b4caaa
 
 
 
 
 
 
 
f36471e
9b4caaa
 
f36471e
 
 
 
 
 
 
 
9b4caaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
import { onDestroy } from "svelte";
import { createInit } from "./create-init.svelte";

/**
 * Manages abort controllers, and aborts them when the component unmounts.
 */
export class AbortManager {
	private controllers: AbortController[] = [];

	constructor() {
		this.init();
	}

	init = createInit(() => {
		try {
			onDestroy(() => this.abortAll());
		} catch {
			// no-op
		}
	});

	/**
	 * Creates a new abort controller and adds it to the manager.
	 */
	public createController(): AbortController {
		const controller = new AbortController();
		this.controllers.push(controller);
		return controller;
	}

	/**
	 * Aborts all controllers and clears the manager.
	 */
	public abortAll(): void {
		this.controllers.forEach(controller => controller.abort());
		this.controllers = [];
	}

	/** Clears the manager without aborting the controllers. */
	public clear(): void {
		this.controllers = [];
	}
}