Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/observer.ts

28 lines
653 B
TypeScript
Raw Normal View History

2023-01-02 11:09:47 +01:00
import { Observable, Subscription } from 'rxjs';
2023-01-04 09:31:16 +01:00
export class UmbObserver<T> {
#source!: Observable<T>;
#callback!: (_value: T) => void;
2023-01-02 11:09:47 +01:00
#subscription!: Subscription;
constructor(source: Observable<T>, callback: (_value: T) => void) {
this.#source = source;
this.#subscription = source.subscribe(callback);
2023-01-02 11:09:47 +01:00
}
hostConnected() {
if (this.#subscription.closed) {
this.#subscription = this.#source.subscribe(this.#callback);
}
}
2023-01-02 11:19:09 +01:00
2023-01-02 11:09:47 +01:00
hostDisconnected() {
2023-03-15 14:02:11 +01:00
// No cause then it cant re-connect, if the same element just was moved in DOM.
//this.#subscription.unsubscribe();
2023-01-02 11:09:47 +01:00
}
2023-01-02 11:38:01 +01:00
destroy(): void {
this.#subscription.unsubscribe();
}
}