mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
perf: improve startup media cache behavior
This commit is contained in:
1 parent
867e22de0b
commit
f7552a433d
4 files changed
+74
-11
No files matched your search
@@ -2,7 +2,11 @@ import { useEffect, useState } from 'react';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { activeSessionIdAtom } from '$state/sessions';
|
||||
import { fetchMediaBlob, getCurrentMediaSessionScope } from '$utils/mediaTransport';
|
||||
import {
|
||||
fetchMediaBlob,
|
||||
getCurrentMediaSessionScope,
|
||||
getStableMediaCacheKeyFragment,
|
||||
} from '$utils/mediaTransport';
|
||||
import { hasControllingServiceWorker, hasServiceWorker } from '$utils/platform';
|
||||
import { rewriteAuthenticatedMediaUrl } from '$utils/matrix';
|
||||
|
||||
@@ -22,7 +26,7 @@ const objectUrlCache = new Map<string, ObjectUrlEntry>();
|
||||
const inflightRequests = new Map<string, Promise<string>>();
|
||||
|
||||
function getObjectUrlCacheKey(sessionScope: string, url: string): string {
|
||||
return `${sessionScope}\x00${url}`;
|
||||
return `${sessionScope}\x00${getStableMediaCacheKeyFragment(url)}`;
|
||||
}
|
||||
|
||||
function normalizeRenderableMediaUrl(url: string | undefined): string | undefined {
|
||||
|
||||
@@ -10,6 +10,11 @@ import {
|
||||
|
||||
const log = createLogger('sessions');
|
||||
|
||||
const notifySessionChanged = (): void => {
|
||||
if (typeof window === 'undefined') return;
|
||||
window.dispatchEvent(new Event('sable-session-changed'));
|
||||
};
|
||||
|
||||
export type OidcSessionInfo = {
|
||||
issuer: string;
|
||||
clientId: string;
|
||||
@@ -55,12 +60,14 @@ export function setFallbackSession(
|
||||
localStorage.setItem('cinny_device_id', deviceId);
|
||||
localStorage.setItem('cinny_user_id', userId);
|
||||
localStorage.setItem('cinny_hs_base_url', baseUrl);
|
||||
notifySessionChanged();
|
||||
}
|
||||
export const removeFallbackSession = () => {
|
||||
localStorage.removeItem('cinny_hs_base_url');
|
||||
localStorage.removeItem('cinny_user_id');
|
||||
localStorage.removeItem('cinny_device_id');
|
||||
localStorage.removeItem('cinny_access_token');
|
||||
notifySessionChanged();
|
||||
};
|
||||
export const getFallbackSession = (): Session | undefined => {
|
||||
const baseUrl = localStorage.getItem('cinny_hs_base_url');
|
||||
@@ -167,6 +174,7 @@ export const sessionsAtom = atom<Sessions, [SessionsAction], void>(
|
||||
(session) => session.userId !== action.session.userId
|
||||
);
|
||||
set(baseSessionsAtom, sessions);
|
||||
notifySessionChanged();
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -188,6 +196,7 @@ export const updateSessionTokens = (
|
||||
};
|
||||
setLocalStorageItem(MATRIX_SESSIONS_KEY, sessions);
|
||||
window.dispatchEvent(new StorageEvent('storage', { key: MATRIX_SESSIONS_KEY }));
|
||||
notifySessionChanged();
|
||||
};
|
||||
|
||||
export const getStoredSessionRefreshToken = (userId: string): string | undefined =>
|
||||
@@ -206,7 +215,9 @@ const baseActiveSessionAtom = atomWithLocalStorage<string | undefined>(
|
||||
export const activeSessionIdAtom = atom<string | undefined, [string | undefined], void>(
|
||||
(get) => get(baseActiveSessionAtom),
|
||||
(_get, set, value) => {
|
||||
const previous = _get(baseActiveSessionAtom);
|
||||
set(baseActiveSessionAtom, value);
|
||||
if (previous !== value) notifySessionChanged();
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
@media (hover: none) {
|
||||
html body button:active,
|
||||
html body [role="button"]:active,
|
||||
html body [class*="Button"]:active,
|
||||
html body [class*="Chip"]:active {
|
||||
html body [role='button']:active,
|
||||
html body [class*='Button']:active,
|
||||
html body [class*='Chip']:active {
|
||||
transform: none !important;
|
||||
}
|
||||
|
||||
html body button:hover,
|
||||
html body [role="button"]:hover {
|
||||
html body [role='button']:hover {
|
||||
transform: none !important;
|
||||
}
|
||||
|
||||
|
||||
@@ -133,23 +133,38 @@ export function getActiveMediaSession(): { baseUrl: string; accessToken: string
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let cachedSessionScope: string | undefined;
|
||||
|
||||
export const invalidateMediaSessionScope = (): void => {
|
||||
cachedSessionScope = undefined;
|
||||
};
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('sable-session-changed', invalidateMediaSessionScope);
|
||||
window.addEventListener('storage', invalidateMediaSessionScope);
|
||||
}
|
||||
|
||||
export function getCurrentMediaSessionScope(): string {
|
||||
if (typeof localStorage === 'undefined') return 'anonymous';
|
||||
if (cachedSessionScope !== undefined) return cachedSessionScope;
|
||||
|
||||
const activeSession = getActiveStoredSession();
|
||||
|
||||
if (activeSession) {
|
||||
return activeSession.userId;
|
||||
cachedSessionScope = activeSession.userId;
|
||||
return cachedSessionScope;
|
||||
}
|
||||
|
||||
const fallbackBaseUrl = localStorage.getItem(FALLBACK_BASE_URL_KEY);
|
||||
const fallbackUserId = localStorage.getItem(FALLBACK_USER_ID_KEY);
|
||||
|
||||
if (fallbackBaseUrl && fallbackUserId) {
|
||||
return fallbackUserId;
|
||||
cachedSessionScope = fallbackUserId;
|
||||
return cachedSessionScope;
|
||||
}
|
||||
|
||||
return 'anonymous';
|
||||
cachedSessionScope = 'anonymous';
|
||||
return cachedSessionScope;
|
||||
}
|
||||
|
||||
function getFetchCacheMode(cacheMode: MediaFetchCacheMode): RequestCache {
|
||||
@@ -159,11 +174,44 @@ function getFetchCacheMode(cacheMode: MediaFetchCacheMode): RequestCache {
|
||||
}
|
||||
|
||||
function getRequestKey(url: string, cacheMode: MediaFetchCacheMode): string {
|
||||
return `${cacheMode}:${url}`;
|
||||
return `${cacheMode}:${getStableMediaCacheKeyFragment(url)}`;
|
||||
}
|
||||
|
||||
type MatrixMediaInfo = {
|
||||
mxcUrl: string;
|
||||
query: string;
|
||||
};
|
||||
|
||||
function getMatrixMediaInfo(url: string): MatrixMediaInfo | undefined {
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const match = parsed.pathname.match(
|
||||
/^\/_matrix\/(?:media\/[^/]+|client\/v1\/media)\/(?:download|thumbnail)\/([^/]+)\/([^/?#]+)/
|
||||
);
|
||||
if (!match) return undefined;
|
||||
|
||||
const [, serverName, mediaId] = match;
|
||||
return {
|
||||
mxcUrl: `mxc://${serverName}/${mediaId}`,
|
||||
query: parsed.search,
|
||||
};
|
||||
}
|
||||
|
||||
function getStableMediaCacheKeyFragment(url: string): string {
|
||||
const info = getMatrixMediaInfo(url);
|
||||
if (info) return `${info.mxcUrl}${info.query}`;
|
||||
return url;
|
||||
}
|
||||
|
||||
export { getStableMediaCacheKeyFragment };
|
||||
|
||||
function getScopedMediaCacheKey(url: string, sessionScope?: string): string {
|
||||
return `${sessionScope ?? getCurrentMediaSessionScope()}:${url}`;
|
||||
return `${sessionScope ?? getCurrentMediaSessionScope()}:${getStableMediaCacheKeyFragment(url)}`;
|
||||
}
|
||||
|
||||
function resolveAccessToken(url: string, options?: MediaTransportOptions): string | undefined {
|
||||
|
||||
Reference in new issue
Block a user