Merge pull request 'fix(lotus): restart the shared mic sampler only when the LiveKit rooms change (#45)' (#51) from lotus-mic-sampler-churn into lotus
CI / Build embedded bundle (push) Canceled after 0s
CI / Publish to Gitea npm registry (push) Canceled after 0s

This commit was merged in pull request #51.
This commit is contained in:
2026-10-18 07:38:41 -04:00
2 changed files with 23 additions and 3 deletions
+12 -1
View File
@@ -7,7 +7,7 @@ Please see LICENSE in the repository root for full details.
import { describe, expect, it } from "vitest";
import { MicLevelQuantizer } from "./lotusMicLevel";
import { MicLevelQuantizer, sameRooms } from "./lotusMicLevel";
describe("MicLevelQuantizer", () => {
it("maps RMS to 0–3 bars", () => {
@@ -35,3 +35,14 @@ describe("MicLevelQuantizer", () => {
expect(q.push(0)).toBe(0);
});
});
describe("sameRooms (EC #45)", () => {
it("compares rooms by identity and order", () => {
const a = {};
const b = {};
expect(sameRooms([a, b], [a, b])).toBe(true);
expect(sameRooms([a], [a, b])).toBe(false);
expect(sameRooms([a, b], [b, a])).toBe(false);
expect(sameRooms([{}], [{}])).toBe(false);
});
});
+11 -2
View File
@@ -46,6 +46,10 @@ const micPublication = (
: null;
};
/** Pure: the same LiveKit rooms, in the same order (by identity). */
export const sameRooms = <T>(a: readonly T[], b: readonly T[]): boolean =>
a.length === b.length && a.every((r, i) => r === b[i]);
const samplers = new WeakMap<
CallViewModel,
Observable<LocalMicSample | null>
@@ -62,10 +66,15 @@ export function observeLocalMicSample$(
const cached = samplers.get(vm);
if (cached) return cached;
const sampler$ = vm.allConnections$.pipe(
map((data) => data.getConnections().map((c) => c.livekitRoom)),
// [EC #45] allConnections$ re-emits far more often than the set of
// LiveKit rooms changes; restarting the tap each time closed the
// AudioContext ~1×/s and emitted null, which reset the muted-speech
// gate so the "You're muted" nudge could never fire.
distinctUntilChanged(sameRooms),
switchMap(
(data) =>
(rooms) =>
new Observable<LocalMicSample | null>((subscriber) => {
const rooms = data.getConnections().map((c) => c.livekitRoom);
let ctx: AudioContext | null = null;
let clone: MediaStreamTrack | null = null;
let timer: ReturnType<typeof setInterval> | undefined;