-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
onReveal.ts
46 lines (41 loc) · 1.61 KB
/
onReveal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { watch } from '@threlte/core'
import { getContext, onDestroy, onMount } from 'svelte'
import { writable } from 'svelte/store'
import { suspenseContextIdentifier, type SuspenseContext } from './context'
/**
* ### `onReveal`
*
* This hook is used to trigger a callback when the component is revealed (i.e.,
* no longer suspended). It works within the boundaries of the <Suspense>
* component and mimics Svelte's lifecycle method `onMount` If there is no
* <Suspense> component, the callback will be executed immediately as the
* component will never suspend.
*
* Note: This hook triggers with Svelte's `onMount` and can be used in its place
* for triggering animations, etc., within the boundaries of a <Suspense>
* component. Just like `onMount` you may return a cleanup function to be
* executed when the component is unmounted or when the component is suspended
* again.
*
* @param {() => void} callback - The function to be executed when the component
* is revealed.
*/
export const onReveal = (callback: () => (() => void) | void): void => {
const ctx = getContext<SuspenseContext | undefined>(suspenseContextIdentifier)
let cleanup: (() => void) | void
const mounted = writable(false)
onMount(() => {
// If there is no context, we are not in a suspense context, so we can just call the callback.
if (!ctx) cleanup = callback()
mounted.set(true)
})
// Return if there is no context.
if (!ctx) {
onDestroy(() => cleanup?.())
return
}
watch([ctx.suspended, mounted], ([suspended, mounted]) => {
if (mounted && !suspended) cleanup = callback()
return () => cleanup?.()
})
}