fix: scope by account

This commit is contained in:
7w1 committed 2026-08-05 21:23:49 -05:00
1 parent 04973d67db
commit 7f9d042bbe
4 files changed
+56 -12

No files matched your search

@@ -144,8 +144,8 @@ describe('ScheduledMessagesList cancellation', () => {
testState.cancelDelayedEvent.mockReset();
testState.coordinatorRun.mockReset();
testState.invalidateQueries.mockReset();
testState.coordinatorRun.mockImplementation((_roomId: string, operation: () => unknown) =>
operation()
testState.coordinatorRun.mockImplementation(
(_mx: unknown, _roomId: string, operation: () => unknown) => operation()
);
});
@@ -157,7 +157,11 @@ describe('ScheduledMessagesList cancellation', () => {
fireEvent.click(cancel);
fireEvent.click(cancel);
expect(testState.coordinatorRun).toHaveBeenCalledWith(room.roomId, expect.any(Function));
expect(testState.coordinatorRun).toHaveBeenCalledWith(
testState.matrix,
room.roomId,
expect.any(Function)
);
expect(testState.cancelDelayedEvent).toHaveBeenCalledOnce();
expect(cancel).toBeDisabled();
expect(cancel).toHaveAttribute('aria-busy', 'true');
@@ -199,7 +199,7 @@ export function ScheduledMessagesList({ room, onEditMessage }: ScheduledMessages
}));
try {
await roomScheduleCoordinator.run(room.roomId, () => cancelDelayedEvent(mx, delayId));
await roomScheduleCoordinator.run(mx, room.roomId, () => cancelDelayedEvent(mx, delayId));
invalidateEvents();
setCancellationStates((states) => {
const next = { ...states };
@@ -1,6 +1,9 @@
import { describe, expect, it, vi } from 'vitest';
import { roomScheduleCoordinator } from './roomScheduleCoordinator';
const account = (userId: string) => ({ getSafeUserId: () => userId });
const alice = account('@alice:example.org');
function deferred() {
let resolve!: () => void;
const promise = new Promise<void>((resolvePromise) => {
@@ -14,12 +17,12 @@ describe('roomScheduleCoordinator', () => {
const first = deferred();
const order: string[] = [];
const firstOperation = roomScheduleCoordinator.run('!room:example.org', async () => {
const firstOperation = roomScheduleCoordinator.run(alice, '!room:example.org', async () => {
order.push('first-start');
await first.promise;
order.push('first-end');
});
const secondOperation = roomScheduleCoordinator.run('!room:example.org', async () => {
const secondOperation = roomScheduleCoordinator.run(alice, '!room:example.org', async () => {
order.push('second');
});
@@ -35,11 +38,11 @@ describe('roomScheduleCoordinator', () => {
const second = vi.fn<() => Promise<string>>(async () => 'completed');
await expect(
roomScheduleCoordinator.run('!room:example.org', async () => {
roomScheduleCoordinator.run(alice, '!room:example.org', async () => {
throw new Error('cancel failed');
})
).rejects.toThrow('cancel failed');
await expect(roomScheduleCoordinator.run('!room:example.org', second)).resolves.toBe(
await expect(roomScheduleCoordinator.run(alice, '!room:example.org', second)).resolves.toBe(
'completed'
);
@@ -50,8 +53,34 @@ describe('roomScheduleCoordinator', () => {
const first = deferred();
const second = vi.fn<() => Promise<string>>(async () => 'completed');
const firstOperation = roomScheduleCoordinator.run('!first:example.org', () => first.promise);
const secondOperation = roomScheduleCoordinator.run('!second:example.org', second);
const firstOperation = roomScheduleCoordinator.run(
alice,
'!first:example.org',
() => first.promise
);
const secondOperation = roomScheduleCoordinator.run(alice, '!second:example.org', second);
await expect(secondOperation).resolves.toBe('completed');
expect(second).toHaveBeenCalledOnce();
first.resolve();
await firstOperation;
});
it('does not serialize the same room ID across accounts', async () => {
const first = deferred();
const second = vi.fn<() => Promise<string>>(async () => 'completed');
const firstOperation = roomScheduleCoordinator.run(
alice,
'!room:example.org',
() => first.promise
);
const secondOperation = roomScheduleCoordinator.run(
account('@bob:example.org'),
'!room:example.org',
second
);
await expect(secondOperation).resolves.toBe('completed');
expect(second).toHaveBeenCalledOnce();
+13 -2
View File
@@ -1,4 +1,15 @@
import { createKeyedQueue } from '$utils/keyedQueue';
import type { MatrixClient } from '$types/matrix-sdk';
/** Serializes delayed-event mutations for each room without blocking other rooms. */
export const roomScheduleCoordinator = { run: createKeyedQueue() };
const runQueued = createKeyedQueue();
/** Serializes delayed-event mutations per account and room. */
export const roomScheduleCoordinator = {
run<T>(
mx: Pick<MatrixClient, 'getSafeUserId'>,
roomId: string,
operation: () => T | PromiseLike<T>
): Promise<T> {
return runQueued(`${mx.getSafeUserId()}\0${roomId}`, operation);
},
};