最后活跃于 1 month ago

Unsubs.ts 原始文件
1export type Unsub = (() => void) | Unsubs;
2
3export class Unsubs {
4 private fns: Unsub[] = [];
5
6 public add(fn: Unsub): this {
7 this.fns.push(fn);
8 return this;
9 }
10
11 public unsub(): this {
12 this.fns.forEach((unsub) => {
13 if (typeof unsub === "function") {
14 unsub();
15 } else {
16 unsub.unsub();
17 }
18 });
19 return this;
20 }
21
22 public clear(): this {
23 this.fns = [];
24 return this;
25 }
26}
unsubs.ts 原始文件
1/**
2 * https://github.com/Noah2610/i3-color-theme-editor/blob/master/src/util/unsubs.ts
3 */
4
5export interface Unsubs {
6 add(fn: () => void): void;
7 unsubAll(): void;
8 fns: (() => void)[];
9}
10
11export function createUnsubs(): Unsubs {
12 const fns: Unsubs["fns"] = [];
13
14 return {
15 add(fn) {
16 fns.push(fn);
17 },
18 unsubAll() {
19 fns.forEach((unsub) => unsub());
20 },
21 fns,
22 };
23}