40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
const broadcast = new BroadcastChannel('immich');
|
|
|
|
let isLoadedReplyListeners: ((url: string, isUrlCached: boolean) => void)[] = [];
|
|
broadcast.addEventListener('message', (event) => {
|
|
if (event.data.type == 'isImageUrlCachedReply') {
|
|
for (const listener of isLoadedReplyListeners) {
|
|
listener(event.data.url, event.data.isImageUrlCached);
|
|
}
|
|
}
|
|
});
|
|
|
|
export function cancelImageUrl(url: string) {
|
|
broadcast.postMessage({ type: 'cancel', url });
|
|
}
|
|
|
|
export function preloadImageUrl(url: string) {
|
|
broadcast.postMessage({ type: 'preload', url });
|
|
}
|
|
|
|
export function isImageUrlCached(url: string) {
|
|
if (!globalThis.isSecureContext) {
|
|
return Promise.resolve(false);
|
|
}
|
|
return new Promise((resolve) => {
|
|
const listener = (urlReply: string, isUrlCached: boolean) => {
|
|
if (urlReply === url) {
|
|
cleanup(isUrlCached);
|
|
}
|
|
};
|
|
const cleanup = (isUrlCached: boolean) => {
|
|
isLoadedReplyListeners = isLoadedReplyListeners.filter((element) => element !== listener);
|
|
resolve(isUrlCached);
|
|
};
|
|
isLoadedReplyListeners.push(listener);
|
|
broadcast.postMessage({ type: 'isImageUrlCached', url });
|
|
|
|
setTimeout(() => cleanup(false), 5000);
|
|
});
|
|
}
|