Files
element-call/src/state/ObservableScope.ts
T

36 lines
855 B
TypeScript
Raw Normal View History

2024-01-20 20:39:12 -05:00
/*
Copyright 2024 New Vector Ltd.
2024-01-20 20:39:12 -05:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2024-01-20 20:39:12 -05:00
*/
import { type Observable, Subject, takeUntil } from "rxjs";
type MonoTypeOperator = <T>(o: Observable<T>) => Observable<T>;
2024-01-20 20:39:12 -05:00
/**
* A scope which limits the execution lifetime of its bound Observables.
*/
export class ObservableScope {
private readonly ended$ = new Subject<void>();
2024-01-20 20:39:12 -05:00
private readonly bindImpl: MonoTypeOperator = takeUntil(this.ended$);
2024-01-20 20:39:12 -05:00
/**
* Binds an Observable to this scope, so that it completes when the scope
* ends.
*/
public bind(): MonoTypeOperator {
return this.bindImpl;
}
2024-01-20 20:39:12 -05:00
/**
* Ends the scope, causing any bound Observables to complete.
*/
public end(): void {
this.ended$.next();
this.ended$.complete();
2024-01-20 20:39:12 -05:00
}
}