-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.svelte.js
54 lines (44 loc) · 1.49 KB
/
index.svelte.js
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
import { mount, unmount } from 'svelte'
export default function SvelteStateRendererFactory({ props: defaultProps, ...defaultOptions } = {}) {
return function makeRenderer(stateRouter) {
const asr = {
makePath: stateRouter.makePath,
stateIsActive: stateRouter.stateIsActive,
go: stateRouter.go,
getActiveState: stateRouter.getActiveState,
}
async function render(context) {
const { template, element: target, content = {} } = context
const options = template.options || {}
const props = $state({ ...content, ...defaultProps, asr })
const svelte = mount(typeof template === 'function' ? template : template.component, { ...defaultOptions, target, props, ...options })
function onRouteChange() {
props.asr = asr
}
stateRouter.on(`stateChangeEnd`, onRouteChange)
svelte.asrOnDestroy = () => stateRouter.removeListener(`stateChangeEnd`, onRouteChange)
svelte.mountedToTarget = target
return svelte
}
return {
render,
reset: async function reset(context) {
const svelte = context.domApi
const element = svelte.mountedToTarget
svelte.asrOnDestroy()
unmount(svelte)
const renderContext = { element, ...context }
return render(renderContext)
},
destroy: async function destroy(svelte) {
svelte.asrOnDestroy()
return unmount(svelte)
},
getChildElement: async function getChildElement(svelte) {
const element = svelte.mountedToTarget
const child = element.querySelector(`uiView`)
return child
},
}
}
}