UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

42 lines (41 loc) 1.01 kB
import { useEffect as o, useState as c } from "react"; import { useQuery as u } from "@tanstack/react-query"; async function n(e) { return await (await fetch(e)).blob(); } function a(e) { URL.revokeObjectURL(e); } function b(e) { const { data: t, error: r } = u({ queryKey: ["usePreFetchResource", e], queryFn: () => n(e), enabled: !!e, // The URL may expire, so the fetched item should never be marked as 'stale'; a refetch may not work. staleTime: 1 / 0 }); return o(() => { r && console.error( `Failed to fetch object with presigned URL ${e}. See network log for details` ); }, [r, e]), f(t); } function f(e) { const [t, r] = c(void 0); return o(() => { if (e) { const s = URL.createObjectURL(e); r(s); } else r(void 0); }, [e]), o(() => () => { t && a(t); }, [t]), t; } export { b as default, n as fetchBlob, a as releaseResourceUrl, f as useCreateUrlForData }; //# sourceMappingURL=usePreFetchResource.js.map