Compare commits

..

1 Commits

Author SHA1 Message Date
Timo K
2a19f06e4a reword docstring 2025-11-17 19:45:14 +01:00
3 changed files with 2 additions and 131 deletions

View File

@@ -173,7 +173,7 @@ type AudioLivekitItem = {
/**
* The return of createCallViewModel$
* This interface represents the root snapshot for the call view. Snapshots in EC with rxjs behave like snapshot trees.
* this interface represents the root source of data for the call view.
* They are a list of observables and objects containing observables to allow for a very granular update mechanism.
*
* This allows to have one huge call view model that represents the entire view without a unnecessary amount of updates.

View File

@@ -1,124 +0,0 @@
/*
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 { render } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { catchError, of, tap, Subject, map, BehaviorSubject } from "rxjs";
import { type Behavior } from "./state/Behavior";
import { useBehavior } from "./useBehavior";
import { ObservableScope } from "./state/ObservableScope";
function TestComponent({
behavior$,
shouldThrow = false,
children,
}: {
behavior$: Behavior<string>;
shouldThrow?: boolean;
children: React.ReactNode;
}): React.ReactNode {
if (shouldThrow) throw Error("Test error");
const value = useBehavior(behavior$);
return (
<div>
value: {value}
{children}
</div>
);
}
describe("useBehavior", () => {
let scope: ObservableScope;
beforeEach(() => (scope = new ObservableScope()));
afterEach(() => scope.end());
it("reference test", () => {
expect(() =>
render(
<TestComponent behavior$={scope.behavior(of("test"))}>
Test
</TestComponent>,
),
).not.toThrow();
});
it("should return the correct behavior", () => {
expect(() =>
render(
<TestComponent
shouldThrow={true}
behavior$={scope.behavior(of("test"))}
>
Test
</TestComponent>,
),
).toThrow("Test error");
});
it("useBehavior forwards the throw in obs computation", () => {
const throwingOperation = (): string => {
throw new Error("Test error");
};
expect(() => {
const s$ = new Subject<string>();
const b$ = scope.behavior(s$, "");
s$.next("test");
s$.next(throwingOperation());
render(<TestComponent behavior$={b$}>Test</TestComponent>);
}).toThrow("Test error");
});
function errorOnE(s: string): string {
if (s === "E") throw new Error("Test error");
return s + "CheckedForE";
}
it("useBehavior does not throw in caught obs computation", () => {
console.log("hello");
const s$ = new Subject<string>();
const b$ = scope.behavior(s$.pipe(map(errorOnE)), "START");
const inheritForThrowCatch$ = scope.behavior(
b$.pipe(
tap((v) => console.log("tap-" + v)),
catchError((err) => {
console.log("caught error" + err);
return of("I caught an error and converted it to sth nicer");
}),
),
);
s$.next("test");
s$.next("E");
s$.next("after error");
expect(() => {
render(
<TestComponent behavior$={inheritForThrowCatch$}>Test</TestComponent>,
);
}).not.toThrow();
expect(() => {
render(<TestComponent behavior$={b$}>Test</TestComponent>);
}).toThrow();
});
it("a playground to test around a little bit", () => {
const s$ = new BehaviorSubject("initial");
const e = new Error("Test error");
console.log("hello");
s$.error(e);
// does not help to set the value afterwards if it errord once it cannot recover
s$.next("nextval");
const s2$ = scope.behavior(s$.pipe(catchError((e) => e.message)));
expect(() => s$.value).toThrow(e);
expect(() => s2$.value).not.toThrow();
});
});

View File

@@ -15,12 +15,7 @@ import { type Behavior } from "./state/Behavior";
export function useBehavior<T>(behavior: Behavior<T>): T {
const subscribe = useCallback(
(onChange: () => void) => {
const s = behavior.subscribe({
next: onChange,
error: (e) => {
throw e;
},
});
const s = behavior.subscribe(onChange);
return (): void => s.unsubscribe();
},
[behavior],