SolidJSS
SolidJS11mo ago
69 replies
webstrand

How to onCleanup an async resource from createResource?

createResource(url, async (url) => {
    var resource;
    tryOnCleanup(() => resource?.close());
    return resource = await connectToUrl(url);
});

or
createResource(url, async (url, info) => {
    tryOnCleanup(() => info.value?.close()); // not sure if this reference gets updated?
    return await connectToUrl(url);
});

or
const [conn] = createResource(url, async (url) => await connectToUrl(url));
createComputed(() => { const c = conn(); tryOnCleanup(() => c?.close()) })

or something else entirely?

I need to cleanup the connection when the resource re-fetches, and when the whole subtree is disposed of. If I don't the browser leaks the resource and eventually crashes.
Was this page helpful?