@threlte/extras
Version:
Utilities, abstractions and plugins for your Threlte apps
27 lines (26 loc) • 892 B
JavaScript
import { getContext } from 'svelte';
import { suspenseContextIdentifier } from './context.js';
import { fromStore } from 'svelte/store';
/**
* ### `onSuspend`
*
* This hook is used to trigger a callback when the component is suspended. It
* works within the boundaries of the <Suspense> component. If there is no
* <Suspense> component, the callback will not be executed, as the component
* will never suspend.
*
* @param {() => void} callback - The function to be executed when the component
* is suspended.
*/
export const onSuspend = (callback) => {
const ctx = getContext(suspenseContextIdentifier);
// If there's no Suspense boundary, we can just return, the component will never suspend.
if (!ctx)
return;
const suspended = fromStore(ctx.suspended);
$effect.pre(() => {
if (suspended.current) {
callback();
}
});
};