feat: correction Subscription
This commit is contained in:
parent
8fed6df571
commit
964afddc90
@ -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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user