Zuletzt aktiv 1 month ago

nr hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

1 file changed, 11 insertions, 3 deletions

Unsubs.ts

@@ -1,13 +1,21 @@
1 + export type Unsub = (() => void) | Unsubs;
2 +
1 3 export class Unsubs {
2 - private fns: (() => void)[] = [];
4 + private fns: Unsub[] = [];
3 5
4 - public add(fn: () => void): this {
6 + public add(fn: Unsub): this {
5 7 this.fns.push(fn);
6 8 return this;
7 9 }
8 10
9 11 public unsub(): this {
10 - this.fns.forEach((unsub) => unsub());
12 + this.fns.forEach((unsub) => {
13 + if (typeof unsub === "function") {
14 + unsub();
15 + } else {
16 + unsub.unsub();
17 + }
18 + });
11 19 return this;
12 20 }
13 21

nr hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

1 file changed, 18 insertions

Unsubs.ts(Datei erstellt)

@@ -0,0 +1,18 @@
1 + export class Unsubs {
2 + private fns: (() => void)[] = [];
3 +
4 + public add(fn: () => void): this {
5 + this.fns.push(fn);
6 + return this;
7 + }
8 +
9 + public unsub(): this {
10 + this.fns.forEach((unsub) => unsub());
11 + return this;
12 + }
13 +
14 + public clear(): this {
15 + this.fns = [];
16 + return this;
17 + }
18 + }

nr hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

Keine Änderungen

nr hat die Gist bearbeitet 2 months ago. Zu Änderung gehen

1 file changed, 23 insertions

unsubs.ts(Datei erstellt)

@@ -0,0 +1,23 @@
1 + /**
2 + * https://github.com/Noah2610/i3-color-theme-editor/blob/master/src/util/unsubs.ts
3 + */
4 +
5 + export interface Unsubs {
6 + add(fn: () => void): void;
7 + unsubAll(): void;
8 + fns: (() => void)[];
9 + }
10 +
11 + export 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 + }
Neuer Älter