Matrix: split shared client stop and eviction

This commit is contained in:
Gustavo Madeira Santana 2026-03-13 15:06:29 +00:00
parent 34fd8bb0d5
commit 2ffeeed7a2
No known key found for this signature in database
6 changed files with 25 additions and 17 deletions

View File

@ -12,7 +12,7 @@ const {
getMatrixRuntimeMock,
getActiveMatrixClientMock,
resolveSharedMatrixClientMock,
stopSharedClientInstanceMock,
removeSharedClientInstanceMock,
isBunRuntimeMock,
resolveMatrixAuthContextMock,
} = matrixClientResolverMocks;
@ -32,7 +32,7 @@ vi.mock("../client.js", () => ({
}));
vi.mock("../client/shared.js", () => ({
stopSharedClientInstance: (...args: unknown[]) => stopSharedClientInstanceMock(...args),
removeSharedClientInstance: (...args: unknown[]) => removeSharedClientInstanceMock(...args),
}));
vi.mock("../send.js", () => ({
@ -74,7 +74,7 @@ describe("action client helpers", () => {
const sharedClient = await resolveSharedMatrixClientMock.mock.results[0]?.value;
expect(sharedClient.prepareForOneOff).toHaveBeenCalledTimes(1);
expect(sharedClient.stop).toHaveBeenCalledTimes(1);
expect(stopSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(removeSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(result).toBe("ok");
});
@ -95,7 +95,7 @@ describe("action client helpers", () => {
expect(sharedClient.prepareForOneOff).not.toHaveBeenCalled();
expect(sharedClient.stop).not.toHaveBeenCalled();
expect(sharedClient.stopAndPersist).toHaveBeenCalledTimes(1);
expect(stopSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(removeSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
});
it("reuses active monitor client when available", async () => {
@ -197,7 +197,7 @@ describe("action client helpers", () => {
expect(result).toBe("ok");
expect(sharedClient.stop).toHaveBeenCalledTimes(1);
expect(sharedClient.stopAndPersist).not.toHaveBeenCalled();
expect(stopSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(removeSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
});
it("stops shared action clients when the wrapped call throws", async () => {

View File

@ -2,7 +2,7 @@ import { getMatrixRuntime } from "../runtime.js";
import type { CoreConfig } from "../types.js";
import { getActiveMatrixClient } from "./active-client.js";
import { isBunRuntime, resolveMatrixAuthContext, resolveSharedMatrixClient } from "./client.js";
import { stopSharedClientInstance } from "./client/shared.js";
import { removeSharedClientInstance } from "./client/shared.js";
import type { MatrixClient } from "./sdk.js";
type ResolvedRuntimeMatrixClient = {
@ -78,7 +78,7 @@ async function resolveRuntimeMatrixClient(opts: {
} else {
client.stop();
}
stopSharedClientInstance(client);
removeSharedClientInstance(client);
},
};
}

View File

@ -6,7 +6,7 @@ type MatrixClientResolverMocks = {
getMatrixRuntimeMock: Mock<() => unknown>;
getActiveMatrixClientMock: Mock<(...args: unknown[]) => MatrixClient | null>;
resolveSharedMatrixClientMock: Mock<(...args: unknown[]) => Promise<MatrixClient>>;
stopSharedClientInstanceMock: Mock<(...args: unknown[]) => void>;
removeSharedClientInstanceMock: Mock<(...args: unknown[]) => void>;
isBunRuntimeMock: Mock<() => boolean>;
resolveMatrixAuthContextMock: Mock<
(params: { cfg: unknown; accountId?: string | null }) => unknown
@ -18,7 +18,7 @@ export const matrixClientResolverMocks: MatrixClientResolverMocks = {
getMatrixRuntimeMock: vi.fn(),
getActiveMatrixClientMock: vi.fn(),
resolveSharedMatrixClientMock: vi.fn(),
stopSharedClientInstanceMock: vi.fn(),
removeSharedClientInstanceMock: vi.fn(),
isBunRuntimeMock: vi.fn(() => false),
resolveMatrixAuthContextMock: vi.fn(),
};
@ -44,7 +44,7 @@ export function primeMatrixClientResolverMocks(params?: {
getMatrixRuntimeMock,
getActiveMatrixClientMock,
resolveSharedMatrixClientMock,
stopSharedClientInstanceMock,
removeSharedClientInstanceMock,
isBunRuntimeMock,
resolveMatrixAuthContextMock,
} = matrixClientResolverMocks;
@ -70,7 +70,7 @@ export function primeMatrixClientResolverMocks(params?: {
});
getActiveMatrixClientMock.mockReturnValue(null);
isBunRuntimeMock.mockReturnValue(false);
stopSharedClientInstanceMock.mockReset();
removeSharedClientInstanceMock.mockReset();
resolveMatrixAuthContextMock.mockImplementation(
({
cfg: explicitCfg,

View File

@ -11,6 +11,7 @@ export {
} from "./client/config.js";
export { createMatrixClient } from "./client/create-client.js";
export {
removeSharedClientInstance,
resolveSharedMatrixClient,
stopSharedClientForAccount,
stopSharedClientInstance,

View File

@ -194,14 +194,21 @@ export function stopSharedClientForAccount(auth: MatrixAuth): void {
sharedClientPromises.delete(key);
}
export function stopSharedClientInstance(client: MatrixClient): void {
export function removeSharedClientInstance(client: MatrixClient): boolean {
for (const [key, state] of sharedClientStates.entries()) {
if (state.client !== client) {
continue;
}
state.client.stop();
sharedClientStates.delete(key);
sharedClientPromises.delete(key);
return true;
}
return false;
}
export function stopSharedClientInstance(client: MatrixClient): void {
if (!removeSharedClientInstance(client)) {
return;
}
client.stop();
}

View File

@ -9,7 +9,7 @@ const {
getMatrixRuntimeMock,
getActiveMatrixClientMock,
resolveSharedMatrixClientMock,
stopSharedClientInstanceMock,
removeSharedClientInstanceMock,
isBunRuntimeMock,
resolveMatrixAuthContextMock,
} = matrixClientResolverMocks;
@ -25,7 +25,7 @@ vi.mock("../client.js", () => ({
}));
vi.mock("../client/shared.js", () => ({
stopSharedClientInstance: (...args: unknown[]) => stopSharedClientInstanceMock(...args),
removeSharedClientInstance: (...args: unknown[]) => removeSharedClientInstanceMock(...args),
}));
vi.mock("../../runtime.js", () => ({
@ -63,7 +63,7 @@ describe("withResolvedMatrixClient", () => {
const sharedClient = await resolveSharedMatrixClientMock.mock.results[0]?.value;
expect(sharedClient.prepareForOneOff).toHaveBeenCalledTimes(1);
expect(sharedClient.stop).toHaveBeenCalledTimes(1);
expect(stopSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(removeSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(result).toBe("ok");
});
@ -132,6 +132,6 @@ describe("withResolvedMatrixClient", () => {
).rejects.toThrow("boom");
expect(sharedClient.stop).toHaveBeenCalledTimes(1);
expect(stopSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
expect(removeSharedClientInstanceMock).toHaveBeenCalledWith(sharedClient);
});
});