mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
fix sync when open room, and clean up sync sizes and data, other stuff i forgot
This commit is contained in:
1 parent
04a0649cdb
commit
b64cb8b333
11 files changed
+369
-56
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { getCanonicalAliasRoomId, isRoomAlias } from '$utils/matrix';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
|
||||
type AliasResolutionState = {
|
||||
alias: string;
|
||||
roomId?: string;
|
||||
};
|
||||
|
||||
export type ResolvedRoomId = {
|
||||
roomId?: string;
|
||||
resolving: boolean;
|
||||
};
|
||||
|
||||
/** Resolve aliases even when the room is not present in the local sync store yet. */
|
||||
export const useResolvedRoomIdOrAlias = (roomIdOrAlias?: string): ResolvedRoomId => {
|
||||
const mx = useMatrixClient();
|
||||
const localRoomId =
|
||||
roomIdOrAlias && isRoomAlias(roomIdOrAlias)
|
||||
? getCanonicalAliasRoomId(mx, roomIdOrAlias)
|
||||
: roomIdOrAlias;
|
||||
const [aliasResolution, setAliasResolution] = useState<AliasResolutionState>();
|
||||
|
||||
useEffect(() => {
|
||||
if (!roomIdOrAlias || !isRoomAlias(roomIdOrAlias) || localRoomId) return undefined;
|
||||
if (aliasResolution?.alias === roomIdOrAlias) return undefined;
|
||||
|
||||
let cancelled = false;
|
||||
void mx
|
||||
.getRoomIdForAlias(roomIdOrAlias)
|
||||
.then(({ room_id: resolvedRoomId }) => {
|
||||
if (!cancelled) setAliasResolution({ alias: roomIdOrAlias, roomId: resolvedRoomId });
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setAliasResolution({ alias: roomIdOrAlias });
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [aliasResolution?.alias, localRoomId, mx, roomIdOrAlias]);
|
||||
|
||||
if (localRoomId) return { roomId: localRoomId, resolving: false };
|
||||
if (!roomIdOrAlias || !isRoomAlias(roomIdOrAlias)) {
|
||||
return { roomId: roomIdOrAlias, resolving: false };
|
||||
}
|
||||
if (aliasResolution?.alias === roomIdOrAlias) {
|
||||
return { roomId: aliasResolution.roomId, resolving: false };
|
||||
}
|
||||
return { resolving: true };
|
||||
};
|
||||
|
||||
export const useResolvedSelectedRoom = (): ResolvedRoomId => {
|
||||
const { roomIdOrAlias: encodedRoomIdOrAlias } = useParams();
|
||||
const roomIdOrAlias = encodedRoomIdOrAlias && decodeURIComponent(encodedRoomIdOrAlias);
|
||||
return useResolvedRoomIdOrAlias(roomIdOrAlias);
|
||||
};
|
||||
|
||||
export const useResolvedSelectedSpace = (): ResolvedRoomId => {
|
||||
const { spaceIdOrAlias: encodedSpaceIdOrAlias } = useParams();
|
||||
const spaceIdOrAlias = encodedSpaceIdOrAlias && decodeURIComponent(encodedSpaceIdOrAlias);
|
||||
return useResolvedRoomIdOrAlias(spaceIdOrAlias);
|
||||
};
|
||||
@@ -1,23 +1,62 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { getSlidingSyncManager } from '$client/initMatrix';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { useSelectedSpace } from '$hooks/router/useSelectedSpace';
|
||||
import { useResolvedSelectedRoom, useResolvedSelectedSpace } from '$hooks/router/useResolvedRoomId';
|
||||
import { useSpaces } from '$state/hooks/roomList';
|
||||
import { allRoomsAtom } from '$state/room-list/roomList';
|
||||
import { ClientEvent } from '$types/matrix-sdk';
|
||||
|
||||
export const useSlidingSyncActiveRoom = (): void => {
|
||||
const useAvailableSlidingSyncManager = () => {
|
||||
const mx = useMatrixClient();
|
||||
const roomId = useSelectedRoom();
|
||||
const spaceId = useSelectedSpace();
|
||||
const [manager, setManager] = useState(() => getSlidingSyncManager(mx));
|
||||
|
||||
useEffect(() => {
|
||||
const manager = getSlidingSyncManager(mx);
|
||||
if (!manager) return undefined;
|
||||
if (manager) return undefined;
|
||||
|
||||
const checkForManager = () => {
|
||||
const nextManager = getSlidingSyncManager(mx);
|
||||
if (nextManager) setManager(nextManager);
|
||||
};
|
||||
|
||||
const retryId = globalThis.setTimeout(checkForManager, 0);
|
||||
mx.on(ClientEvent.Sync, checkForManager);
|
||||
return () => {
|
||||
globalThis.clearTimeout(retryId);
|
||||
mx.removeListener(ClientEvent.Sync, checkForManager);
|
||||
};
|
||||
}, [manager, mx]);
|
||||
|
||||
return manager;
|
||||
};
|
||||
|
||||
export const useSlidingSyncRouteRooms = (): void => {
|
||||
const manager = useAvailableSlidingSyncManager();
|
||||
const { roomId, resolving: resolvingRoom } = useResolvedSelectedRoom();
|
||||
const { roomId: spaceId, resolving: resolvingSpace } = useResolvedSelectedSpace();
|
||||
|
||||
useEffect(() => {
|
||||
if (!manager || resolvingRoom || resolvingSpace) return undefined;
|
||||
|
||||
const activeRoomIds = [...new Set([spaceId, roomId].filter(Boolean))] as string[];
|
||||
activeRoomIds.forEach((activeRoomId) => manager.subscribeToRoom(activeRoomId));
|
||||
|
||||
return () => {
|
||||
activeRoomIds.forEach((activeRoomId) => manager.unsubscribeFromRoom(activeRoomId));
|
||||
};
|
||||
}, [mx, roomId, spaceId]);
|
||||
return () => activeRoomIds.forEach((activeRoomId) => manager.unsubscribeFromRoom(activeRoomId));
|
||||
}, [manager, resolvingRoom, resolvingSpace, roomId, spaceId]);
|
||||
};
|
||||
|
||||
export const useSlidingSyncSpaceSubscriptions = (): void => {
|
||||
const manager = useAvailableSlidingSyncManager();
|
||||
const mx = useMatrixClient();
|
||||
const spaces = useSpaces(mx, allRoomsAtom);
|
||||
|
||||
useEffect(() => {
|
||||
if (!manager) return undefined;
|
||||
manager.setSpaceSubscriptions(spaces);
|
||||
return undefined;
|
||||
}, [manager, spaces]);
|
||||
};
|
||||
|
||||
export const useSlidingSyncActiveRoom = (): void => {
|
||||
useSlidingSyncRouteRooms();
|
||||
useSlidingSyncSpaceSubscriptions();
|
||||
};
|
||||
@@ -7,7 +7,13 @@ export const useSyncState = (
|
||||
onChange: ClientEventHandlerMap[ClientEvent.Sync]
|
||||
): void => {
|
||||
useEffect(() => {
|
||||
mx?.on(ClientEvent.Sync, onChange);
|
||||
if (!mx) return undefined;
|
||||
|
||||
mx.on(ClientEvent.Sync, onChange);
|
||||
|
||||
const currentState = mx.getSyncState();
|
||||
if (currentState) onChange(currentState, null);
|
||||
|
||||
return () => {
|
||||
mx?.removeListener(ClientEvent.Sync, onChange);
|
||||
};
|
||||
|
||||
@@ -430,24 +430,28 @@ export function ClientRoot({ children }: ClientRootProps) {
|
||||
</Box>
|
||||
</SplashScreen>
|
||||
)}
|
||||
{!mx || (!syncReadyClient && !isError) ? (
|
||||
{!mx ? (
|
||||
<ClientRootLoading />
|
||||
) : isError ? null : (
|
||||
<SpecVersions baseUrl={baseUrl ?? ''}>
|
||||
<MatrixClientProvider value={mx}>
|
||||
<ServerConfigsLoader>
|
||||
{(serverConfigs) => (
|
||||
<CapabilitiesProvider value={serverConfigs.capabilities ?? {}}>
|
||||
<MediaConfigProvider value={serverConfigs.mediaConfig ?? {}}>
|
||||
<AuthMetadataProvider value={serverConfigs.authMetadata}>
|
||||
{children}
|
||||
</AuthMetadataProvider>
|
||||
</MediaConfigProvider>
|
||||
</CapabilitiesProvider>
|
||||
)}
|
||||
</ServerConfigsLoader>
|
||||
</MatrixClientProvider>
|
||||
</SpecVersions>
|
||||
<MatrixClientProvider value={mx}>
|
||||
{!syncReadyClient ? (
|
||||
<ClientRootLoading />
|
||||
) : (
|
||||
<SpecVersions baseUrl={baseUrl ?? ''}>
|
||||
<ServerConfigsLoader>
|
||||
{(serverConfigs) => (
|
||||
<CapabilitiesProvider value={serverConfigs.capabilities ?? {}}>
|
||||
<MediaConfigProvider value={serverConfigs.mediaConfig ?? {}}>
|
||||
<AuthMetadataProvider value={serverConfigs.authMetadata}>
|
||||
{children}
|
||||
</AuthMetadataProvider>
|
||||
</MediaConfigProvider>
|
||||
</CapabilitiesProvider>
|
||||
)}
|
||||
</ServerConfigsLoader>
|
||||
</SpecVersions>
|
||||
)}
|
||||
</MatrixClientProvider>
|
||||
)}
|
||||
</AutoDiscovery>
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Spinner } from 'folds';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { useResolvedSelectedRoom } from '$hooks/router/useResolvedRoomId';
|
||||
import { IsDirectRoomProvider, RoomProvider } from '$hooks/useRoom';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { JoinBeforeNavigate } from '$features/join-before-navigate';
|
||||
@@ -13,9 +14,11 @@ export function DirectRouteRoomProvider({ children }: { children: ReactNode }) {
|
||||
const { roomIdOrAlias: encodedRoomIdOrAlias, eventId: encodedEventId } = useParams();
|
||||
const roomIdOrAlias = encodedRoomIdOrAlias && decodeURIComponent(encodedRoomIdOrAlias);
|
||||
const eventId = encodedEventId && decodeURIComponent(encodedEventId);
|
||||
const roomId = useSelectedRoom();
|
||||
const { roomId, resolving } = useResolvedSelectedRoom();
|
||||
const room = mx.getRoom(roomId);
|
||||
|
||||
if (resolving) return <Spinner variant="Secondary" size="600" />;
|
||||
|
||||
if (!room || !rooms.includes(room.roomId)) {
|
||||
return <JoinBeforeNavigate roomIdOrAlias={roomIdOrAlias!} eventId={eventId} />;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Spinner } from 'folds';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { useResolvedSelectedRoom } from '$hooks/router/useResolvedRoomId';
|
||||
import { IsDirectRoomProvider, RoomProvider } from '$hooks/useRoom';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { JoinBeforeNavigate } from '$features/join-before-navigate';
|
||||
@@ -18,9 +19,11 @@ export function HomeRouteRoomProvider({ children }: { children: ReactNode }) {
|
||||
const roomIdOrAlias = encodedRoomIdOrAlias && decodeURIComponent(encodedRoomIdOrAlias);
|
||||
const eventId = encodedEventId && decodeURIComponent(encodedEventId);
|
||||
const viaServers = useSearchParamsViaServers();
|
||||
const roomId = useSelectedRoom();
|
||||
const { roomId, resolving } = useResolvedSelectedRoom();
|
||||
const room = mx.getRoom(roomId);
|
||||
|
||||
if (resolving) return <Spinner variant="Secondary" size="600" />;
|
||||
|
||||
if (!room || (!isShowingAllRoomsInHome && !rooms.includes(room.roomId))) {
|
||||
return (
|
||||
<JoinBeforeNavigate
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Spinner } from 'folds';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { useResolvedSelectedRoom } from '$hooks/router/useResolvedRoomId';
|
||||
import { IsDirectRoomProvider, RoomProvider } from '$hooks/useRoom';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { JoinBeforeNavigate } from '$features/join-before-navigate';
|
||||
@@ -26,9 +27,11 @@ export function SpaceRouteRoomProvider({ children }: { children: ReactNode }) {
|
||||
const roomIdOrAlias = encodedRoomIdOrAlias && decodeURIComponent(encodedRoomIdOrAlias);
|
||||
const eventId = encodedEventId && decodeURIComponent(encodedEventId);
|
||||
const viaServers = useSearchParamsViaServers();
|
||||
const roomId = useSelectedRoom();
|
||||
const { roomId, resolving } = useResolvedSelectedRoom();
|
||||
const room = mx.getRoom(roomId);
|
||||
|
||||
if (resolving) return <Spinner variant="Secondary" size="600" />;
|
||||
|
||||
if (!room || !allRooms.includes(room.roomId)) {
|
||||
// room is not joined
|
||||
return (
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Spinner } from 'folds';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { useSpaces } from '$state/hooks/roomList';
|
||||
import { allRoomsAtom } from '$state/room-list/roomList';
|
||||
import { useSelectedSpace } from '$hooks/router/useSelectedSpace';
|
||||
import { useResolvedSelectedSpace } from '$hooks/router/useResolvedRoomId';
|
||||
import { SpaceProvider } from '$hooks/useSpace';
|
||||
import { JoinBeforeNavigate } from '$features/join-before-navigate';
|
||||
import { useSearchParamsViaServers } from '$hooks/router/useSearchParamsViaServers';
|
||||
@@ -19,9 +20,11 @@ export function RouteSpaceProvider({ children }: RouteSpaceProviderProps) {
|
||||
const spaceIdOrAlias = encodedSpaceIdOrAlias && decodeURIComponent(encodedSpaceIdOrAlias);
|
||||
const viaServers = useSearchParamsViaServers();
|
||||
|
||||
const selectedSpaceId = useSelectedSpace();
|
||||
const { roomId: selectedSpaceId, resolving } = useResolvedSelectedSpace();
|
||||
const space = mx.getRoom(selectedSpaceId);
|
||||
|
||||
if (resolving) return <Spinner variant="Secondary" size="600" />;
|
||||
|
||||
if (!space || !joinedSpaces.includes(space.roomId)) {
|
||||
return <JoinBeforeNavigate roomIdOrAlias={spaceIdOrAlias ?? ''} viaServers={viaServers} />;
|
||||
}
|
||||
|
||||
@@ -129,6 +129,7 @@ function installSlidingSyncConnId(mx: MatrixClient): void {
|
||||
if (req.conn_id === undefined) {
|
||||
req.conn_id = SLIDING_SYNC_CONN_ID;
|
||||
}
|
||||
|
||||
return original(reqBody, baseUrl, abortSignal);
|
||||
};
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ const mocks = vi.hoisted(() => ({
|
||||
getListData: vi.fn<(key: string) => null>(),
|
||||
getListParams: vi.fn<(key: string) => null>(),
|
||||
setList: vi.fn<() => void>(),
|
||||
setListRanges: vi.fn<() => void>(),
|
||||
setListRanges: vi.fn<(key: string, ranges: number[][]) => void>(),
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -74,9 +74,22 @@ function makeManager(mx: ReturnType<typeof makeMockMx>): SlidingSyncManager {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.slidingSyncInstance.getListData.mockReset().mockReturnValue(null);
|
||||
mocks.slidingSyncInstance.getListParams.mockReset().mockReturnValue(null);
|
||||
mocks.slidingSyncInstance.setListRanges.mockReset();
|
||||
mocks.slidingSyncConstructorArgs = undefined;
|
||||
});
|
||||
|
||||
function fireLifecycle(state: SlidingSyncState, response: unknown = {}) {
|
||||
const lifecycleCall = mocks.slidingSyncInstance.on.mock.calls.find(
|
||||
([event]) => event === SlidingSyncEvent.Lifecycle
|
||||
);
|
||||
const lifecycle = lifecycleCall?.[1] as
|
||||
| ((nextState: SlidingSyncState, nextResponse: unknown, error?: Error) => void)
|
||||
| undefined;
|
||||
lifecycle?.(state, response);
|
||||
}
|
||||
|
||||
describe('SlidingSyncManager initial request', () => {
|
||||
it('starts with a small room-list range and only row-level state', () => {
|
||||
makeManager(makeMockMx());
|
||||
@@ -85,9 +98,9 @@ describe('SlidingSyncManager initial request', () => {
|
||||
const joined = lists.get('joined');
|
||||
|
||||
expect(joined?.ranges).toEqual([[0, 29]]);
|
||||
expect(joined?.required_state).toHaveLength(9);
|
||||
expect(joined?.required_state).toHaveLength(8);
|
||||
expect(joined?.required_state).toContainEqual([EventType.RoomJoinRules, '']);
|
||||
expect(joined?.required_state).not.toContainEqual(['m.image_pack', '*']);
|
||||
expect(joined?.required_state).not.toContainEqual(['m.space.child', '*']);
|
||||
});
|
||||
|
||||
it('expands by one page after the first response instead of jumping to all rooms', () => {
|
||||
@@ -108,7 +121,60 @@ describe('SlidingSyncManager initial request', () => {
|
||||
| undefined;
|
||||
lifecycle?.(SlidingSyncState.Complete, {});
|
||||
|
||||
expect(mocks.slidingSyncInstance.setListRanges).toHaveBeenCalledWith('joined', [[0, 279]]);
|
||||
expect(mocks.slidingSyncInstance.setListRanges).toHaveBeenCalledWith('joined', [
|
||||
[0, 29],
|
||||
[30, 59],
|
||||
]);
|
||||
});
|
||||
|
||||
it('continues expanding lists when an active-room subscription has not returned data yet', () => {
|
||||
const manager = makeManager(makeMockMx());
|
||||
mocks.slidingSyncInstance.getListData.mockImplementation((key: string) =>
|
||||
key === 'joined' ? ({ joinedCount: 1000 } as never) : null
|
||||
);
|
||||
mocks.slidingSyncInstance.getListParams.mockReturnValue({
|
||||
ranges: [[0, 29]],
|
||||
} as never);
|
||||
manager.attach();
|
||||
manager.subscribeToRoom('!active:example.com');
|
||||
|
||||
const lifecycleCall = mocks.slidingSyncInstance.on.mock.calls.find(
|
||||
([event]) => event === SlidingSyncEvent.Lifecycle
|
||||
);
|
||||
const lifecycle = lifecycleCall?.[1] as
|
||||
| ((state: SlidingSyncState, response: unknown, error?: Error) => void)
|
||||
| undefined;
|
||||
lifecycle?.(SlidingSyncState.Complete, {});
|
||||
|
||||
expect(mocks.slidingSyncInstance.setListRanges).toHaveBeenCalledWith('joined', [
|
||||
[0, 29],
|
||||
[30, 59],
|
||||
]);
|
||||
});
|
||||
|
||||
it('defers active subscriptions while an expanded range is awaiting its response', () => {
|
||||
let joinedRange: [number, number][] = [[0, 29]];
|
||||
const manager = makeManager(makeMockMx());
|
||||
mocks.slidingSyncInstance.getListData.mockImplementation((key: string) =>
|
||||
key === 'joined' ? ({ joinedCount: 193 } as never) : null
|
||||
);
|
||||
mocks.slidingSyncInstance.getListParams.mockImplementation((key: string) =>
|
||||
key === 'joined' ? ({ ranges: joinedRange } as never) : ({ ranges: [[0, 29]] } as never)
|
||||
);
|
||||
mocks.slidingSyncInstance.setListRanges.mockImplementation((key, ranges) => {
|
||||
if (key === 'joined') joinedRange = ranges as [number, number][];
|
||||
});
|
||||
manager.attach();
|
||||
|
||||
fireLifecycle(SlidingSyncState.Complete);
|
||||
manager.subscribeToRoom('!active:example.com');
|
||||
|
||||
const diagnostics = manager.getDiagnostics();
|
||||
expect(manager.isRoomActive('!active:example.com')).toBe(true);
|
||||
expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).not.toHaveBeenCalled();
|
||||
expect(diagnostics.lists.find((list) => list.key === 'joined')).toMatchObject({
|
||||
rangeEnd: 59,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -147,6 +213,7 @@ describe('SlidingSyncManager — membership leave auto-unsubscribe', () => {
|
||||
const mx = makeMockMx();
|
||||
const manager = makeManager(mx);
|
||||
manager.attach();
|
||||
fireLifecycle(SlidingSyncState.Complete);
|
||||
manager.subscribeToRoom('!room:example.com');
|
||||
|
||||
fireMembershipEvent(mx, 'leave');
|
||||
@@ -159,6 +226,7 @@ describe('SlidingSyncManager — membership leave auto-unsubscribe', () => {
|
||||
const mx = makeMockMx();
|
||||
const manager = makeManager(mx);
|
||||
manager.attach();
|
||||
fireLifecycle(SlidingSyncState.Complete);
|
||||
manager.subscribeToRoom('!room:example.com');
|
||||
|
||||
fireMembershipEvent(mx, 'ban');
|
||||
@@ -170,6 +238,7 @@ describe('SlidingSyncManager — membership leave auto-unsubscribe', () => {
|
||||
const mx = makeMockMx();
|
||||
const manager = makeManager(mx);
|
||||
manager.attach();
|
||||
fireLifecycle(SlidingSyncState.Complete);
|
||||
manager.subscribeToRoom('!room:example.com');
|
||||
|
||||
fireMembershipEvent(mx, 'leave', '!room:example.com', '@other:example.com');
|
||||
@@ -182,6 +251,7 @@ describe('SlidingSyncManager — membership leave auto-unsubscribe', () => {
|
||||
const mx = makeMockMx();
|
||||
const manager = makeManager(mx);
|
||||
manager.attach();
|
||||
fireLifecycle(SlidingSyncState.Complete);
|
||||
manager.subscribeToRoom('!room:example.com');
|
||||
|
||||
fireMembershipEvent(mx, 'join');
|
||||
|
||||
+132
-15
@@ -29,14 +29,16 @@ export const LIST_INVITES = 'invites';
|
||||
export const LIST_SEARCH = 'search';
|
||||
export const LIST_ROOM_SEARCH = 'room_search';
|
||||
export const LIST_SPACE = 'space';
|
||||
const LIST_TIMELINE_LIMIT = 1;
|
||||
const LIST_TIMELINE_LIMIT = 0;
|
||||
const DEFAULT_LIST_PAGE_SIZE = 250;
|
||||
const INITIAL_LIST_SIZE = 30;
|
||||
const LIST_EXPANSION_PAGE_SIZE = INITIAL_LIST_SIZE;
|
||||
const DEFAULT_POLL_TIMEOUT_MS = 20000;
|
||||
|
||||
const LIST_SORT_ORDER = ['by_recency', 'by_name'];
|
||||
|
||||
const UNENCRYPTED_SUBSCRIPTION_KEY = 'unencrypted';
|
||||
const SPACE_SUBSCRIPTION_KEY = 'space';
|
||||
const IMAGE_PACK_SUBSCRIPTION_KEY = 'image_packs';
|
||||
const ACTIVE_ROOM_TIMELINE_LIMIT = 50;
|
||||
|
||||
@@ -80,7 +82,18 @@ const buildListRequiredState = (): MSC3575RoomSubscription['required_state'] =>
|
||||
[EventType.RoomCanonicalAlias, ''],
|
||||
[EventType.RoomJoinRules, ''],
|
||||
[EventType.RoomMember, MSC3575_STATE_KEY_ME],
|
||||
];
|
||||
|
||||
const SPACE_REQUIRED_STATE: MSC3575RoomSubscription['required_state'] = [
|
||||
[EventType.RoomCreate, ''],
|
||||
[EventType.RoomName, ''],
|
||||
[EventType.RoomAvatar, ''],
|
||||
[EventType.RoomCanonicalAlias, ''],
|
||||
[EventType.RoomJoinRules, ''],
|
||||
[EventType.RoomEncryption, ''],
|
||||
[EventType.RoomTombstone, ''],
|
||||
['m.space.child', MSC3575_WILDCARD],
|
||||
['m.space.parent', MSC3575_WILDCARD],
|
||||
];
|
||||
|
||||
const ACTIVE_ROOM_REQUIRED_STATE: MSC3575RoomSubscription['required_state'] = [
|
||||
@@ -110,7 +123,7 @@ const ACTIVE_ROOM_REQUIRED_STATE: MSC3575RoomSubscription['required_state'] = [
|
||||
|
||||
const buildEncryptedSubscription = (timelineLimit: number): MSC3575RoomSubscription => ({
|
||||
timeline_limit: timelineLimit,
|
||||
required_state: [[MSC3575_WILDCARD, MSC3575_WILDCARD]],
|
||||
required_state: ACTIVE_ROOM_REQUIRED_STATE,
|
||||
});
|
||||
|
||||
const buildUnencryptedSubscription = (timelineLimit: number): MSC3575RoomSubscription => ({
|
||||
@@ -126,6 +139,11 @@ const buildImagePackSubscription = (): MSC3575RoomSubscription => ({
|
||||
],
|
||||
});
|
||||
|
||||
const buildSpaceSubscription = (): MSC3575RoomSubscription => ({
|
||||
timeline_limit: 0,
|
||||
required_state: SPACE_REQUIRED_STATE,
|
||||
});
|
||||
|
||||
const buildLists = (pageSize: number): Map<string, MSC3575List> => {
|
||||
const lists = new Map<string, MSC3575List>();
|
||||
const listRequiredState = buildListRequiredState();
|
||||
@@ -163,6 +181,12 @@ export class SlidingSyncManager {
|
||||
|
||||
private readonly activeRoomSubscriptions = new Set<string>();
|
||||
|
||||
private readonly deferredActiveRoomSubscriptions = new Set<string>();
|
||||
|
||||
private readonly spaceSubscriptions = new Set<string>();
|
||||
|
||||
private deferredSpaceSubscriptions = new Set<string>();
|
||||
|
||||
private readonly imagePackRoomSubscriptions = new Set<string>();
|
||||
|
||||
private readonly listPageSize: number;
|
||||
@@ -184,12 +208,14 @@ export class SlidingSyncManager {
|
||||
|
||||
private initialSyncCompleted = false;
|
||||
|
||||
private activeRoomDataReceived = false;
|
||||
|
||||
private syncCount = 0;
|
||||
|
||||
private previousListCounts: Map<string, number> = new Map();
|
||||
|
||||
private readonly requestedListRangeEnds = new Map<string, number>();
|
||||
|
||||
private readonly confirmedListRangeEnds = new Map<string, number>();
|
||||
|
||||
/**
|
||||
* One-shot RoomData listeners keyed by roomId, used to measure the latency
|
||||
* between subscribeToRoom() and the first data arriving for that room.
|
||||
@@ -223,6 +249,9 @@ export class SlidingSyncManager {
|
||||
const defaultSubscription = buildEncryptedSubscription(roomTimelineLimit);
|
||||
const lists = buildLists(listPageSize);
|
||||
this.listKeys = Array.from(lists.keys());
|
||||
lists.forEach((list, key) => {
|
||||
this.requestedListRangeEnds.set(key, getListEndIndex(list));
|
||||
});
|
||||
this.slidingSync = new SlidingSync(baseUrl, lists, defaultSubscription, mx, pollTimeoutMs);
|
||||
|
||||
this.slidingSync.addCustomSubscription(
|
||||
@@ -233,6 +262,7 @@ export class SlidingSyncManager {
|
||||
IMAGE_PACK_SUBSCRIPTION_KEY,
|
||||
buildImagePackSubscription()
|
||||
);
|
||||
this.slidingSync.addCustomSubscription(SPACE_SUBSCRIPTION_KEY, buildSpaceSubscription());
|
||||
|
||||
this.onLifecycle = (state, resp, err) => {
|
||||
const syncStartTime = performance.now();
|
||||
@@ -267,6 +297,10 @@ export class SlidingSyncManager {
|
||||
|
||||
if (err || !resp || state !== SlidingSyncState.Complete) return;
|
||||
|
||||
this.requestedListRangeEnds.forEach((rangeEnd, key) => {
|
||||
this.confirmedListRangeEnds.set(key, rangeEnd);
|
||||
});
|
||||
|
||||
const changes: Record<string, { previous: number; current: number; delta: number }> = {};
|
||||
let totalRoomCount = 0;
|
||||
let hasChanges = false;
|
||||
@@ -406,7 +440,6 @@ export class SlidingSyncManager {
|
||||
|
||||
private expandListsByPage(): void {
|
||||
if (!this.initialSyncCompleted) return;
|
||||
if (this.activeRoomSubscriptions.size > 0 && !this.activeRoomDataReceived) return;
|
||||
|
||||
let allListsComplete = true;
|
||||
let expandedAny = false;
|
||||
@@ -434,10 +467,22 @@ export class SlidingSyncManager {
|
||||
}
|
||||
|
||||
const existing = this.slidingSync.getListParams(key);
|
||||
const currentEnd = getListEndIndex(existing);
|
||||
const requestedEnd = getListEndIndex(existing);
|
||||
const currentEnd = this.confirmedListRangeEnds.get(key) ?? -1;
|
||||
|
||||
const maxEnd = knownCount - 1;
|
||||
|
||||
if (requestedEnd > currentEnd) {
|
||||
allListsComplete = false;
|
||||
expansionDetails[key] = {
|
||||
status: 'awaiting-response',
|
||||
knownCount,
|
||||
currentEnd,
|
||||
desiredEnd: requestedEnd,
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentEnd >= maxEnd) {
|
||||
expansionDetails[key] = { status: 'complete', knownCount, currentEnd };
|
||||
return;
|
||||
@@ -445,7 +490,7 @@ export class SlidingSyncManager {
|
||||
|
||||
allListsComplete = false;
|
||||
|
||||
const desiredEnd = Math.min(maxEnd, currentEnd + this.listPageSize);
|
||||
const desiredEnd = Math.min(maxEnd, currentEnd + LIST_EXPANSION_PAGE_SIZE);
|
||||
|
||||
if (desiredEnd === currentEnd) {
|
||||
expansionDetails[key] = {
|
||||
@@ -457,7 +502,10 @@ export class SlidingSyncManager {
|
||||
return;
|
||||
}
|
||||
|
||||
this.slidingSync.setListRanges(key, [[0, desiredEnd]]);
|
||||
const existingRanges = existing?.ranges ?? [];
|
||||
const nextRangeStart = currentEnd + 1;
|
||||
this.slidingSync.setListRanges(key, [...existingRanges, [nextRangeStart, desiredEnd]]);
|
||||
this.requestedListRangeEnds.set(key, desiredEnd);
|
||||
expandedAny = true;
|
||||
|
||||
expansionDetails[key] = {
|
||||
@@ -479,10 +527,10 @@ export class SlidingSyncManager {
|
||||
|
||||
const expansionDuration = performance.now() - expansionStartTime;
|
||||
const hasExpansions = Object.values(expansionDetails).some((d) => d.status === 'expanding');
|
||||
|
||||
if (allListsComplete) {
|
||||
if (!this.listsFullyLoaded) {
|
||||
this.listsFullyLoaded = true;
|
||||
globalThis.setTimeout(() => this.flushDeferredSubscriptions(), 0);
|
||||
log.log(`Sliding Sync all lists fully loaded for ${this.mx.getUserId()}`);
|
||||
const totalRooms = this.listKeys.reduce(
|
||||
(sum, key) => sum + (this.slidingSync.getListData(key)?.joinedCount ?? 0),
|
||||
@@ -576,7 +624,21 @@ export class SlidingSyncManager {
|
||||
}
|
||||
|
||||
public isRoomActive(roomId: string): boolean {
|
||||
return this.activeRoomSubscriptions.has(roomId);
|
||||
return (
|
||||
this.activeRoomSubscriptions.has(roomId) || this.deferredActiveRoomSubscriptions.has(roomId)
|
||||
);
|
||||
}
|
||||
|
||||
private flushDeferredSubscriptions(): void {
|
||||
if (this.disposed || !this.listsFullyLoaded) return;
|
||||
|
||||
const spaces = this.deferredSpaceSubscriptions;
|
||||
this.deferredSpaceSubscriptions = new Set();
|
||||
if (spaces.size > 0) this.setSpaceSubscriptions(spaces);
|
||||
|
||||
const activeRooms = [...this.deferredActiveRoomSubscriptions];
|
||||
this.deferredActiveRoomSubscriptions.clear();
|
||||
activeRooms.forEach((roomId) => this.subscribeToRoom(roomId));
|
||||
}
|
||||
|
||||
public subscribeToImagePackRoom(roomId: string): void {
|
||||
@@ -584,7 +646,11 @@ export class SlidingSyncManager {
|
||||
this.slidingSync.useCustomSubscription(roomId, IMAGE_PACK_SUBSCRIPTION_KEY);
|
||||
this.imagePackRoomSubscriptions.add(roomId);
|
||||
this.slidingSync.modifyRoomSubscriptions(
|
||||
new Set([...this.activeRoomSubscriptions, ...this.imagePackRoomSubscriptions])
|
||||
new Set([
|
||||
...this.activeRoomSubscriptions,
|
||||
...this.spaceSubscriptions,
|
||||
...this.imagePackRoomSubscriptions,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -600,6 +666,15 @@ export class SlidingSyncManager {
|
||||
|
||||
public subscribeToRoom(roomId: string): void {
|
||||
if (this.disposed) return;
|
||||
if (!this.listsFullyLoaded) {
|
||||
this.deferredActiveRoomSubscriptions.add(roomId);
|
||||
debugLog.info('sync', 'Room subscription deferred until lists are hydrated', {
|
||||
deferredSubscriptions: this.deferredActiveRoomSubscriptions.size,
|
||||
syncCycle: this.syncCount,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (this.activeRoomSubscriptions.has(roomId)) return;
|
||||
const room = this.mx.getRoom(roomId);
|
||||
const isEncrypted = this.mx.isRoomEncrypted(roomId);
|
||||
if (room && !isEncrypted) {
|
||||
@@ -607,8 +682,14 @@ export class SlidingSyncManager {
|
||||
}
|
||||
this.activeRoomSubscriptions.add(roomId);
|
||||
this.slidingSync.modifyRoomSubscriptions(
|
||||
new Set([...this.activeRoomSubscriptions, ...this.imagePackRoomSubscriptions])
|
||||
new Set([
|
||||
...this.activeRoomSubscriptions,
|
||||
...this.spaceSubscriptions,
|
||||
...this.imagePackRoomSubscriptions,
|
||||
])
|
||||
);
|
||||
|
||||
this.expandListsByPage();
|
||||
Sentry.metrics.gauge('sable.sync.active_subscriptions', this.activeRoomSubscriptions.size, {
|
||||
attributes: { transport: 'sliding' },
|
||||
});
|
||||
@@ -657,8 +738,6 @@ export class SlidingSyncManager {
|
||||
});
|
||||
this.slidingSync.removeListener(SlidingSyncEvent.RoomData, onFirstRoomData);
|
||||
this.pendingRoomDataListeners.delete(roomId);
|
||||
this.activeRoomDataReceived = true;
|
||||
this.expandListsByPage();
|
||||
};
|
||||
this.pendingRoomDataListeners.set(roomId, onFirstRoomData);
|
||||
this.slidingSync.on(SlidingSyncEvent.RoomData, onFirstRoomData);
|
||||
@@ -666,14 +745,23 @@ export class SlidingSyncManager {
|
||||
|
||||
public unsubscribeFromRoom(roomId: string): void {
|
||||
if (this.disposed) return;
|
||||
this.deferredActiveRoomSubscriptions.delete(roomId);
|
||||
if (!this.activeRoomSubscriptions.has(roomId)) return;
|
||||
const pendingListener = this.pendingRoomDataListeners.get(roomId);
|
||||
if (pendingListener) {
|
||||
this.slidingSync.removeListener(SlidingSyncEvent.RoomData, pendingListener);
|
||||
this.pendingRoomDataListeners.delete(roomId);
|
||||
}
|
||||
this.activeRoomSubscriptions.delete(roomId);
|
||||
if (this.spaceSubscriptions.has(roomId)) {
|
||||
this.slidingSync.useCustomSubscription(roomId, SPACE_SUBSCRIPTION_KEY);
|
||||
}
|
||||
this.slidingSync.modifyRoomSubscriptions(
|
||||
new Set([...this.activeRoomSubscriptions, ...this.imagePackRoomSubscriptions])
|
||||
new Set([
|
||||
...this.activeRoomSubscriptions,
|
||||
...this.spaceSubscriptions,
|
||||
...this.imagePackRoomSubscriptions,
|
||||
])
|
||||
);
|
||||
Sentry.metrics.gauge('sable.sync.active_subscriptions', this.activeRoomSubscriptions.size, {
|
||||
attributes: { transport: 'sliding' },
|
||||
@@ -684,4 +772,33 @@ export class SlidingSyncManager {
|
||||
syncCycle: this.syncCount,
|
||||
});
|
||||
}
|
||||
|
||||
public setSpaceSubscriptions(roomIds: Iterable<string>): void {
|
||||
if (this.disposed) return;
|
||||
const next = new Set(roomIds);
|
||||
if (!this.listsFullyLoaded) {
|
||||
this.deferredSpaceSubscriptions = next;
|
||||
return;
|
||||
}
|
||||
const unchanged =
|
||||
next.size === this.spaceSubscriptions.size &&
|
||||
[...next].every((roomId) => this.spaceSubscriptions.has(roomId));
|
||||
if (unchanged) return;
|
||||
|
||||
this.spaceSubscriptions.clear();
|
||||
next.forEach((roomId) => {
|
||||
this.spaceSubscriptions.add(roomId);
|
||||
if (!this.activeRoomSubscriptions.has(roomId)) {
|
||||
this.slidingSync.useCustomSubscription(roomId, SPACE_SUBSCRIPTION_KEY);
|
||||
}
|
||||
});
|
||||
this.slidingSync.modifyRoomSubscriptions(
|
||||
new Set([
|
||||
...this.activeRoomSubscriptions,
|
||||
...this.spaceSubscriptions,
|
||||
...this.imagePackRoomSubscriptions,
|
||||
])
|
||||
);
|
||||
this.expandListsByPage();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user