-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Suspense.svelte
58 lines (49 loc) · 1.36 KB
/
Suspense.svelte
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
47
48
49
50
51
52
53
54
55
56
57
58
<script lang="ts">
import { HierarchicalObject, T, createRawEventDispatcher, useParent, watch } from '@threlte/core'
import { Group } from 'three'
import { createSuspenseContext } from './context'
type $$Events = {
load: void
suspend: void
error: Error[]
}
export let final = false
const dispatch = createRawEventDispatcher<$$Events>()
const { suspended, errors, setFinal } = createSuspenseContext({ final })
$: setFinal(final)
$: if (!$suspended) dispatch('load')
$: if ($suspended) dispatch('suspend')
$: if ($errors.length) dispatch('error', $errors)
const group = new Group()
const parent = useParent()
watch([parent, suspended, errors], ([parent, suspended, errors]) => {
// we don't have a parent, so we can't add ourselves to it
if (!parent) return
// if the component is suspended or has errors, we remove ourselves from the parent
if (suspended || errors.length) {
parent.remove(group)
return
}
parent.add(group)
return () => {
parent.remove(group)
}
})
</script>
<!-- Block the graph from mounting to the parent -->
<HierarchicalObject>
<T is={group}>
<slot
suspended={$suspended}
errors={$errors}
/>
</T>
</HierarchicalObject>
{#if $errors.length}
<slot
name="error"
errors={$errors}
/>
{:else if $suspended}
<slot name="fallback" />
{/if}