1
1
import {
2
+ isArray ,
2
3
normalizeClass ,
3
4
normalizeStyle ,
4
- toDisplayString ,
5
- isArray
5
+ toDisplayString
6
6
} from '@vue/shared'
7
- import { effectScope } from '@vue/reactivity'
7
+
8
+ import { ComponentInternalInstance , createComponentInstance } from './component'
8
9
9
10
export type Block = Node | Fragment | Block [ ]
10
11
export type ParentBlock = ParentNode | Node [ ]
@@ -14,14 +15,10 @@ export type BlockFn = (props?: any) => Block
14
15
export function render (
15
16
comp : BlockFn ,
16
17
container : string | ParentNode
17
- ) : ( ) => void {
18
- const scope = effectScope ( )
19
- const block = scope . run ( ( ) => comp ( ) ) !
20
- insert ( block , ( container = normalizeContainer ( container ) ) )
21
- return ( ) => {
22
- scope . stop ( )
23
- remove ( block , container as ParentNode )
24
- }
18
+ ) : ComponentInternalInstance {
19
+ const instance = createComponentInstance ( comp )
20
+ mountComponent ( instance , ( container = normalizeContainer ( container ) ) )
21
+ return instance
25
22
}
26
23
27
24
export function normalizeContainer ( container : string | ParentNode ) : ParentNode {
@@ -30,6 +27,31 @@ export function normalizeContainer(container: string | ParentNode): ParentNode {
30
27
: container
31
28
}
32
29
30
+ export const mountComponent = (
31
+ instance : ComponentInternalInstance ,
32
+ container : ParentNode
33
+ ) => {
34
+ instance . container = container
35
+ const block = instance . scope . run (
36
+ ( ) => ( instance . block = instance . component ( ) )
37
+ ) !
38
+ insert ( block , instance . container )
39
+ instance . isMounted = true
40
+ // TODO: lifecycle hooks (mounted, ...)
41
+ // const { m } = instance
42
+ // m && invoke(m)
43
+ }
44
+
45
+ export const unmountComponent = ( instance : ComponentInternalInstance ) => {
46
+ const { container, block, scope } = instance
47
+ scope . stop ( )
48
+ block && remove ( block , container )
49
+ instance . isMounted = false
50
+ // TODO: lifecycle hooks (unmounted, ...)
51
+ // const { um } = instance
52
+ // um && invoke(um)
53
+ }
54
+
33
55
export function insert (
34
56
block : Block ,
35
57
parent : ParentNode ,
0 commit comments