feat: correction Subscription

This commit is contained in:
Guillaume ARM 2023-07-05 15:23:29 +02:00
parent 8fed6df571
commit 964afddc90

View File

@ -17,29 +17,47 @@ export interface SubscriptionConstructor {
export type TeardownLogic = Subscription | Unsubscribable | Teardown | void; export type TeardownLogic = Subscription | Unsubscribable | Teardown | void;
const noop = () => {};
const executeTeardownLogic = (tl: TeardownLogic): void => {
if (typeof tl === 'function') {
tl();
return;
}
if (tl) {
tl.unsubscribe();
}
};
/** /**
* Implementation * Implementation
*/ */
export class Subscription implements ISubscription { export class Subscription implements ISubscription {
private teardowns: TeardownLogic[] = [];
public closed = false; public closed = false;
public constructor(action?: Teardown) { public constructor(private action: Teardown = noop) {}
void action;
throw new Error('TODO');
}
public unsubscribe(): void { public unsubscribe(): void {
throw new Error('TODO'); if (this.closed) return;
this.closed = true;
executeTeardownLogic(this.action);
this.teardowns.forEach(executeTeardownLogic);
} }
public add(tl: TeardownLogic): void { public add(tl: TeardownLogic): void {
void tl; if (this.closed) {
throw new Error('TODO'); executeTeardownLogic(tl);
} else {
this.teardowns.push(tl);
}
} }
public remove(tl: TeardownLogic): void { public remove(tl: TeardownLogic): void {
void tl; this.teardowns = this.teardowns.filter(t => t !== tl);
throw new Error('TODO');
} }
} }