mirror of
https://github.com/vector-im/element-call.git
synced 2026-02-08 04:19:11 +00:00
Previously we had a ViewModel class which was responsible for little more than creating an ObservableScope. However, since this ObservableScope would be created implicitly upon view model construction, it became a tad bit harder for callers to remember to eventually end the scope (as you wouldn't just have to remember to end ObservableScopes, but also to destroy ViewModels). Requiring the scope to be specified explicitly by the caller also makes it possible for the caller to reuse the scope for other purposes, reducing the number of scopes mentally in flight that need tending to, and for all state holders (not just view models) to be handled uniformly by helper functions such as generateKeyed$.
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
/*
|
|
Copyright 2025 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
import { test } from "vitest";
|
|
import { Subject } from "rxjs";
|
|
|
|
import { withTestScheduler } from "./test";
|
|
import { generateKeyed$, pauseWhen } from "./observable";
|
|
|
|
test("pauseWhen", () => {
|
|
withTestScheduler(({ behavior, expectObservable }) => {
|
|
const inputMarbles = " abcdefgh-i-jk-";
|
|
const pauseMarbles = " n-y--n-yn-y--n";
|
|
const outputMarbles = "abc--fgh-i---k";
|
|
expectObservable(
|
|
behavior(inputMarbles).pipe(
|
|
pauseWhen(behavior(pauseMarbles, { y: true, n: false })),
|
|
),
|
|
).toBe(outputMarbles);
|
|
});
|
|
});
|
|
|
|
test("generateKeyed$ has the right output and ends scopes at the right times", () => {
|
|
const scope1$ = new Subject<string>();
|
|
const scope2$ = new Subject<string>();
|
|
const scope3$ = new Subject<string>();
|
|
const scope4$ = new Subject<string>();
|
|
const scopeSubjects = [scope1$, scope2$, scope3$, scope4$];
|
|
|
|
withTestScheduler(({ hot, expectObservable }) => {
|
|
// Each scope should start when the input number reaches or surpasses their
|
|
// number and end when the input number drops back below their number.
|
|
// At the very end, unsubscribing should end all remaining scopes.
|
|
const inputMarbles = " 123242";
|
|
const outputMarbles = " abcbdb";
|
|
const subscriptionMarbles = "^-----!";
|
|
const scope1Marbles = " y-----n";
|
|
const scope2Marbles = " -y----n";
|
|
const scope3Marbles = " --ynyn";
|
|
const scope4Marbles = " ----yn";
|
|
|
|
expectObservable(
|
|
generateKeyed$(hot<string>(inputMarbles), (input, createOrGet) => {
|
|
for (let i = 1; i <= +input; i++) {
|
|
createOrGet(i.toString(), (scope) => {
|
|
scopeSubjects[i - 1].next("y");
|
|
scope.onEnd(() => scopeSubjects[i - 1].next("n"));
|
|
return i.toString();
|
|
});
|
|
}
|
|
return "abcd"[+input - 1];
|
|
}),
|
|
subscriptionMarbles,
|
|
).toBe(outputMarbles);
|
|
|
|
expectObservable(scope1$).toBe(scope1Marbles);
|
|
expectObservable(scope2$).toBe(scope2Marbles);
|
|
expectObservable(scope3$).toBe(scope3Marbles);
|
|
expectObservable(scope4$).toBe(scope4Marbles);
|
|
});
|
|
});
|