mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 21:45:31 +09:00
perf(app): load embed author and footer icons through the image cache (#1756)
This commit is contained in:
1 parent
2083eaddd6
commit
ce41960fcd
3 files changed
+63
-2
No files matched your search
@@ -0,0 +1,26 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {useEffect, useState} from 'react';
|
||||
|
||||
export function useCachedImageLoaded(src: string | null | undefined): boolean {
|
||||
const resolved = src ?? null;
|
||||
const [readySrc, setReadySrc] = useState<string | null>(() =>
|
||||
resolved !== null && ImageCacheUtils.hasImage(resolved) ? resolved : null,
|
||||
);
|
||||
useEffect(() => {
|
||||
if (resolved === null) {
|
||||
return;
|
||||
}
|
||||
let active = true;
|
||||
const markReady = (): void => {
|
||||
if (active) setReadySrc(resolved);
|
||||
};
|
||||
const cleanup = ImageCacheUtils.loadImage(resolved, markReady, markReady);
|
||||
return () => {
|
||||
active = false;
|
||||
cleanup();
|
||||
};
|
||||
}, [resolved]);
|
||||
return resolved !== null && readySrc === resolved;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// @vitest-environment happy-dom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {afterEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
afterEach(() => {
|
||||
ImageCacheUtils._clearForTests();
|
||||
});
|
||||
|
||||
describe('loadImage rejection path', () => {
|
||||
it('reports an unusable source through onError so callers can fall back', () => {
|
||||
const onLoad = vi.fn();
|
||||
const onError = vi.fn();
|
||||
ImageCacheUtils.loadImage(null, onLoad, onError);
|
||||
expect(onLoad).not.toHaveBeenCalled();
|
||||
expect(onError).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('reports an oversized source through onError rather than queueing it', () => {
|
||||
const onLoad = vi.fn();
|
||||
const onError = vi.fn();
|
||||
ImageCacheUtils.loadImage(`https://cdn.test/${'a'.repeat(32 * 1024)}.png`, onLoad, onError);
|
||||
expect(onLoad).not.toHaveBeenCalled();
|
||||
expect(onError).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns a no-op disposer when it rejects, so cleanup stays safe', () => {
|
||||
const dispose = ImageCacheUtils.loadImage(null, vi.fn(), vi.fn());
|
||||
expect(() => dispose()).not.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user