-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
29 lines (23 loc) · 960 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Observable, ReplaySubject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
type CustomElement = { disconnectedCallback(): void };
type Decorated = CustomElement & { __elementDiconnected$?: Observable<true> };
export function disconnected(element: CustomElement): Observable<true> {
const decorated = element as Decorated;
if (decorated.__elementDiconnected$) {
return decorated.__elementDiconnected$;
}
const previousCallback = element.disconnectedCallback;
const stop$ = new ReplaySubject<true>();
decorated.disconnectedCallback = () => {
if (previousCallback) {
previousCallback.apply(element);
}
stop$.next(true);
stop$.complete();
};
return decorated.__elementDiconnected$ = stop$.asObservable();
}
export function untilDisconnected<T>(element: CustomElement): (source: Observable<T>) => Observable<T> {
return (source: Observable<T>) => source.pipe(takeUntil(disconnected(element)));
}