once is an async event wrapper that ensures an async initialization is only ran once.
Any subsequent calls after the first call will return a promise that resolves/rejects
with the result of the first call.
Some initialization might require clean up, such as unregister
event handlers and/or timers. In this case, a production build might
work fine but a HMR (Hot Module Reload) development server might not
do this for you automatically.
One way to work around this during development is to store the cleanup
as a global object
constgetResourceThatNeedsCleanup = once({ fn:async () => { if (__DEV__) { // Configure your bundler to inject this // await if you need async clean up await (windowasany).cleanupMyResource?.(); }
An alternative solution is to not use once but instead tie the initialization
of the resource to some other lifecycle event that gets cleaned up during HMR.
For example, A framework that supports HMR for React components might unmount
the component before reloading, which gives you a chance to clean up the resource.
This is not an issue if the resource doesn't leak other resources,
since it will eventually be GC'd.
Args for constructing a
onceonceis an async event wrapper that ensures an async initialization is only ran once. Any subsequent calls after the first call will return a promise that resolves/rejects with the result of the first call.Example
Caveat with HMR
Some initialization might require clean up, such as unregister event handlers and/or timers. In this case, a production build might work fine but a HMR (Hot Module Reload) development server might not do this for you automatically.
One way to work around this during development is to store the cleanup as a global object
An alternative solution is to not use
oncebut instead tie the initialization of the resource to some other lifecycle event that gets cleaned up during HMR. For example, A framework that supports HMR for React components might unmount the component before reloading, which gives you a chance to clean up the resource.This is not an issue if the resource doesn't leak other resources, since it will eventually be GC'd.