fusee-framework 1.6.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -123
- package/app/benchmark/benchmark.js +633 -0
- package/app/benchmark/index.html +102 -0
- package/app/benchmark/styles.css +249 -0
- package/app/components/Counter.js +17 -7
- package/app/components/CounterSection.js +3 -4
- package/app/main.js +50 -62
- package/app/main.ts +78 -63
- package/app/pages/404.js +16 -0
- package/app/pages/[...all].js +35 -0
- package/app/pages/[...all].ts +35 -0
- package/app/pages/_layout.js +53 -0
- package/app/pages/_layout.ts +26 -0
- package/app/pages/about.js +23 -0
- package/app/pages/about.ts +29 -0
- package/app/pages/async-work.js +65 -0
- package/app/pages/composables.js +158 -0
- package/app/pages/composables.ts +158 -0
- package/app/pages/dynamic.js +40 -0
- package/app/pages/dynamic.ts +40 -0
- package/app/pages/index.js +137 -0
- package/app/pages/index.ts +87 -0
- package/app/pages/login.ts +21 -0
- package/app/pages/objects/[id].js +56 -0
- package/app/pages/objects/[id].ts +56 -0
- package/app/pages/posts/[category]/[slug].js +56 -0
- package/app/pages/posts/[category]/[slug].ts +56 -0
- package/app/pages/router-example.js +79 -0
- package/app/pages/router-example.ts +56 -0
- package/app/pages/scroll-demo.ts +102 -0
- package/app/pages/test-for.js +65 -0
- package/app/pages/test-for.ts +80 -0
- package/app/pages/users/[id].js +24 -0
- package/app/pages/users/[id].ts +46 -0
- package/app/pages/users/index.js +22 -0
- package/app/pages/users/index.ts +29 -0
- package/app/pages/users.js +43 -0
- package/app/pages/users.ts +78 -0
- package/app/providers/ChildComponent.js +8 -2
- package/app/providers/ChildComponent.ts +47 -47
- package/app/providers/GrandchildComponent.js +19 -0
- package/app/providers/ParentComponent.js +5 -1
- package/app/routes/About.js +1 -1
- package/app/routes/About.ts +1 -1
- package/app/routes/AuthRoutes.ts +41 -0
- package/app/routes/Home.js +9 -0
- package/app/routes/NotFound.js +16 -0
- package/app/routes/NotFound.ts +16 -0
- package/app/routes/RouterExample.js +78 -0
- package/app/routes/RouterExample.ts +56 -0
- package/app/routes/TestResource.js +47 -0
- package/app/routes/WildcardDemo.js +35 -0
- package/app/routes/WildcardDemo.ts +35 -0
- package/app/services.js +15 -0
- package/auto-imports.d.ts +9 -0
- package/bin/create-fusee-linux +0 -0
- package/bin/create-fusee-mac-arm64 +0 -0
- package/bin/create-fusee-mac-intel +0 -0
- package/bin/create-fusee-win.exe +0 -0
- package/bin/create-fusee.exe +0 -0
- package/bin/index.js +38 -0
- package/create-fusee-go/cmd/generate.go +72 -0
- package/create-fusee-go/cmd/init.go +38 -0
- package/create-fusee-go/cmd/root.go +121 -0
- package/create-fusee-go/go.mod +10 -0
- package/create-fusee-go/go.sum +10 -0
- package/create-fusee-go/internal/assets/assets.go +78 -0
- package/create-fusee-go/internal/assets/embed/framework/auto-import-preset.js +9 -0
- package/create-fusee-go/internal/assets/embed/framework/core/compiler.js +253 -0
- package/create-fusee-go/internal/assets/embed/framework/core/component.js +266 -0
- package/create-fusee-go/internal/assets/embed/framework/core/composable.js +27 -0
- package/create-fusee-go/internal/assets/embed/framework/core/directives.js +875 -0
- package/create-fusee-go/internal/assets/embed/framework/core/evaluator.js +153 -0
- package/create-fusee-go/internal/assets/embed/framework/core/event-delegation.js +191 -0
- package/create-fusee-go/internal/assets/embed/framework/core/signal.js +412 -0
- package/create-fusee-go/internal/assets/embed/framework/core/store.js +81 -0
- package/create-fusee-go/internal/assets/embed/framework/index.d.ts +3 -0
- package/create-fusee-go/internal/assets/embed/framework/index.js +10 -0
- package/create-fusee-go/internal/assets/embed/framework/router/file-router.js +177 -0
- package/create-fusee-go/internal/assets/embed/framework/router/router.js +462 -0
- package/create-fusee-go/internal/assets/embed/framework/types/compiler.d.ts +15 -0
- package/create-fusee-go/internal/assets/embed/framework/types/component.d.ts +88 -0
- package/create-fusee-go/internal/assets/embed/framework/types/composable.d.ts +14 -0
- package/create-fusee-go/internal/assets/embed/framework/types/directives.d.ts +55 -0
- package/create-fusee-go/internal/assets/embed/framework/types/evaluator.d.ts +13 -0
- package/create-fusee-go/internal/assets/embed/framework/types/event-delegation.d.ts +32 -0
- package/create-fusee-go/internal/assets/embed/framework/types/index.d.ts +22 -0
- package/create-fusee-go/internal/assets/embed/framework/types/router.d.ts +32 -0
- package/create-fusee-go/internal/assets/embed/framework/types/signal.d.ts +79 -0
- package/create-fusee-go/internal/assets/embed/framework/types/store.d.ts +8 -0
- package/create-fusee-go/internal/assets/templates/Counter.tmpl +26 -0
- package/create-fusee-go/internal/assets/templates/component.tmpl +11 -0
- package/create-fusee-go/internal/assets/templates/components/Loading.tmpl +24 -0
- package/create-fusee-go/internal/assets/templates/index.html.tmpl +15 -0
- package/create-fusee-go/internal/assets/templates/main.tmpl +14 -0
- package/create-fusee-go/internal/assets/templates/package.json.tmpl +14 -0
- package/create-fusee-go/internal/assets/templates/page.tmpl +12 -0
- package/create-fusee-go/internal/assets/templates/pages/about.tmpl +20 -0
- package/create-fusee-go/internal/assets/templates/pages/index.tmpl +22 -0
- package/create-fusee-go/internal/assets/templates/pages/layout.tmpl +18 -0
- package/create-fusee-go/internal/assets/templates/tsconfig.json.tmpl +14 -0
- package/create-fusee-go/internal/assets/templates/vite.config.js.tmpl +18 -0
- package/create-fusee-go/main.go +7 -0
- package/framework/auto-import-preset.js +13 -9
- package/framework/core/compiler.js +252 -249
- package/framework/core/component.js +279 -260
- package/framework/core/di.js +187 -0
- package/framework/core/directives.js +819 -786
- package/framework/core/evaluator.js +153 -70
- package/framework/core/event-delegation.js +33 -14
- package/framework/core/reconcile.js +151 -0
- package/framework/core/signal.js +679 -403
- package/framework/core/store.js +322 -81
- package/framework/index.js +12 -11
- package/framework/router/file-router.js +177 -0
- package/framework/router/router.js +554 -80
- package/framework/types/component.d.ts +89 -88
- package/framework/types/di.d.ts +43 -0
- package/framework/types/event-delegation.d.ts +0 -58
- package/framework/types/index.d.ts +32 -22
- package/framework/types/reconcile.d.ts +7 -0
- package/framework/types/router.d.ts +42 -15
- package/framework/types/signal.d.ts +147 -78
- package/framework/types/store.d.ts +66 -8
- package/index.html +638 -626
- package/package.json +5 -3
- package/tsconfig.json +1 -0
- package/vite.config.js +7 -1
- /package/{framework → create-fusee-go/internal/assets/embed/framework}/bin/create-fusee.js +0 -0
|
@@ -1,261 +1,280 @@
|
|
|
1
|
-
import { effect, setEffectHook, batch } from './signal.js'
|
|
2
|
-
import { mountTemplate } from './compiler.js'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
instance.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
for (const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
1
|
+
import { effect, setEffectHook, batch } from './signal.js'
|
|
2
|
+
import { mountTemplate } from './compiler.js'
|
|
3
|
+
import { rootInjector, runInContext, replaceActiveInjector, EnvironmentInjector } from './di.js'
|
|
4
|
+
export { inject } from './di.js'
|
|
5
|
+
|
|
6
|
+
let currentInstance = null
|
|
7
|
+
|
|
8
|
+
setEffectHook(eff => {
|
|
9
|
+
if (currentInstance) currentInstance._effects.push(eff)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export function getCurrentInstance() {
|
|
13
|
+
return currentInstance
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function setCurrentInstance(instance) {
|
|
17
|
+
currentInstance = instance
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function resolveProps(schema, received) {
|
|
21
|
+
const isArray = Array.isArray(schema)
|
|
22
|
+
const resolved = {}
|
|
23
|
+
|
|
24
|
+
const normalizedSchema = {}
|
|
25
|
+
if (isArray) {
|
|
26
|
+
schema.forEach(k => normalizedSchema[k.toLowerCase()] = k)
|
|
27
|
+
} else {
|
|
28
|
+
for (const key of Object.keys(schema)) {
|
|
29
|
+
normalizedSchema[key.toLowerCase()] = key
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
for (const receivedKey of Object.keys(received)) {
|
|
34
|
+
const schemaKey = isArray
|
|
35
|
+
? schema.find(k => k.toLowerCase() === receivedKey.toLowerCase())
|
|
36
|
+
: normalizedSchema[receivedKey.toLowerCase()]
|
|
37
|
+
|
|
38
|
+
if (!schemaKey) {
|
|
39
|
+
console.warn(`[framework] Unknown prop "${receivedKey}"`)
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const descriptor = Object.getOwnPropertyDescriptor(received, receivedKey)
|
|
44
|
+
if (descriptor && descriptor.get) {
|
|
45
|
+
Object.defineProperty(resolved, schemaKey, {
|
|
46
|
+
get: descriptor.get,
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true
|
|
49
|
+
})
|
|
50
|
+
} else {
|
|
51
|
+
resolved[schemaKey] = received[receivedKey]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!isArray) {
|
|
56
|
+
for (const [key, config] of Object.entries(schema)) {
|
|
57
|
+
if (!(key in resolved)) {
|
|
58
|
+
if (config.required) {
|
|
59
|
+
console.error(`[framework] Required prop "${key}" is missing`)
|
|
60
|
+
}
|
|
61
|
+
if (config.default !== undefined) {
|
|
62
|
+
resolved[key] = typeof config.default === 'function'
|
|
63
|
+
? config.default()
|
|
64
|
+
: config.default
|
|
65
|
+
}
|
|
66
|
+
} else if (config.type) {
|
|
67
|
+
const value = resolved[key]
|
|
68
|
+
const expectedType = config.type.name
|
|
69
|
+
const actualType = typeof value
|
|
70
|
+
const typeMap = { String: 'string', Number: 'number', Boolean: 'boolean' }
|
|
71
|
+
if (typeMap[expectedType] && actualType !== typeMap[expectedType]) {
|
|
72
|
+
console.warn(`[framework] Prop "${key}" expected ${expectedType} but got ${actualType}`)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return resolved
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function createEmit(listeners) {
|
|
82
|
+
return function emit(eventName, ...args) {
|
|
83
|
+
const handler = listeners[eventName]
|
|
84
|
+
if (typeof handler === 'function') {
|
|
85
|
+
batch(() => handler(...args))
|
|
86
|
+
} else if (handler !== undefined) {
|
|
87
|
+
console.warn(`[framework] emit("${eventName}"): listener is not a function`)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function parseSlots(slotHTML) {
|
|
93
|
+
const slots = { default: '' }
|
|
94
|
+
if (!slotHTML || !slotHTML.trim()) return slots
|
|
95
|
+
|
|
96
|
+
const namedSlotRe = /<template\s+slot="([^"]+)"[^>]*>([\s\S]*?)<\/template>/gi
|
|
97
|
+
let remaining = slotHTML
|
|
98
|
+
let match
|
|
99
|
+
|
|
100
|
+
while ((match = namedSlotRe.exec(slotHTML)) !== null) {
|
|
101
|
+
slots[match[1]] = match[2].trim()
|
|
102
|
+
remaining = remaining.replace(match[0], '')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const defaultContent = remaining.trim()
|
|
106
|
+
if (defaultContent) slots.default = defaultContent
|
|
107
|
+
|
|
108
|
+
return slots
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function resolveSlots(template, slots) {
|
|
112
|
+
let result = template.replace(/<slot\s+name="([^"]+)"\s*>[\s\S]*?<\/slot>/gi, (match, name) => {
|
|
113
|
+
return slots[name] ?? ''
|
|
114
|
+
})
|
|
115
|
+
result = result.replace(/<slot\s*>[\s\S]*?<\/slot>/gi, () => {
|
|
116
|
+
return slots.default ?? ''
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
result = result.replace(/<slot\s+name="([^"]+)"\s*\/>/gi, (_, name) => slots[name] ?? '')
|
|
120
|
+
result = result.replace(/<slot\s*\/>/gi, () => slots.default ?? '')
|
|
121
|
+
return result
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function onMount(fn) {
|
|
125
|
+
if (currentInstance) currentInstance._mountHooks.push(fn)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function onUnmount(fn) {
|
|
129
|
+
if (currentInstance) currentInstance._unmountHooks.push(fn)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function defineComponent(options) {
|
|
133
|
+
return function ComponentFactory(props = {}, { listeners = {}, slots = {}, parent = null } = {}) {
|
|
134
|
+
const parentInjector = (parent && parent._injector) || rootInjector;
|
|
135
|
+
|
|
136
|
+
const instance = {
|
|
137
|
+
props: options.props ? resolveProps(options.props, props) : props,
|
|
138
|
+
_mountHooks: [],
|
|
139
|
+
_unmountHooks: [],
|
|
140
|
+
_effects: [],
|
|
141
|
+
_element: null,
|
|
142
|
+
_parent: parent,
|
|
143
|
+
_injector: parentInjector,
|
|
144
|
+
_ownsInjector: false
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const emit = createEmit(listeners)
|
|
148
|
+
|
|
149
|
+
currentInstance = instance
|
|
150
|
+
const result = runInContext(instance._injector, () => {
|
|
151
|
+
return options.setup(instance.props, { emit, slots });
|
|
152
|
+
});
|
|
153
|
+
instance.state = result
|
|
154
|
+
result._instance = instance
|
|
155
|
+
currentInstance = null
|
|
156
|
+
|
|
157
|
+
function render(container) {
|
|
158
|
+
instance._element = container
|
|
159
|
+
|
|
160
|
+
const resolvedTemplate = resolveSlots(result.template, slots)
|
|
161
|
+
|
|
162
|
+
const { effects } = mountTemplate(
|
|
163
|
+
resolvedTemplate,
|
|
164
|
+
container,
|
|
165
|
+
result,
|
|
166
|
+
options.components || {}
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
instance._effects.push(...effects)
|
|
170
|
+
for (const hook of instance._mountHooks) hook()
|
|
171
|
+
|
|
172
|
+
return instance
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function unmount() {
|
|
176
|
+
for (const hook of instance._unmountHooks) hook()
|
|
177
|
+
const effects = instance._effects
|
|
178
|
+
for (let i = effects.length - 1; i >= 0; i--) {
|
|
179
|
+
const cleanup = effects[i]
|
|
180
|
+
if (typeof cleanup === 'function') cleanup()
|
|
181
|
+
}
|
|
182
|
+
instance._effects = []
|
|
183
|
+
if (instance._element) {
|
|
184
|
+
instance._element.innerHTML = ''
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return { render, unmount, instance }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function provide(key, value) {
|
|
193
|
+
if (!currentInstance) return;
|
|
194
|
+
|
|
195
|
+
if (!currentInstance._ownsInjector) {
|
|
196
|
+
currentInstance._injector = new EnvironmentInjector([], currentInstance._injector);
|
|
197
|
+
currentInstance._ownsInjector = true;
|
|
198
|
+
replaceActiveInjector(currentInstance._injector);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (value === undefined && key && key.provide) {
|
|
202
|
+
currentInstance._injector.provide(key);
|
|
203
|
+
} else if (value && typeof value === 'object' && (value.useClass || value.useFactory || value.useExisting)) {
|
|
204
|
+
currentInstance._injector.provide({ provide: key, ...value });
|
|
205
|
+
} else {
|
|
206
|
+
currentInstance._injector.provide({ provide: key, useValue: value });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function defineAsyncComponent(loaderOrOptions) {
|
|
211
|
+
const options = typeof loaderOrOptions === 'function'
|
|
212
|
+
? { loader: loaderOrOptions }
|
|
213
|
+
: loaderOrOptions
|
|
214
|
+
|
|
215
|
+
return function AsyncComponentFactory(props = {}, { listeners = {}, slots = {}, parent = null } = {}) {
|
|
216
|
+
const parentInjector = (parent && parent._injector) || rootInjector;
|
|
217
|
+
|
|
218
|
+
const instance = {
|
|
219
|
+
props,
|
|
220
|
+
_mountHooks: [],
|
|
221
|
+
_unmountHooks: [],
|
|
222
|
+
_effects: [],
|
|
223
|
+
_element: null,
|
|
224
|
+
_parent: parent,
|
|
225
|
+
_injector: parentInjector,
|
|
226
|
+
_ownsInjector: false
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
let childApi = null
|
|
230
|
+
let loadingApi = null
|
|
231
|
+
let isUnmounted = false
|
|
232
|
+
|
|
233
|
+
function render(container) {
|
|
234
|
+
instance._element = container
|
|
235
|
+
|
|
236
|
+
if (options.loadingComponent) {
|
|
237
|
+
loadingApi = options.loadingComponent({}, { parent })
|
|
238
|
+
loadingApi.render(container)
|
|
239
|
+
} else {
|
|
240
|
+
container.innerHTML = '<!-- async component boundary -->'
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
Promise.resolve(options.loader())
|
|
244
|
+
.then(comp => {
|
|
245
|
+
if (isUnmounted) return
|
|
246
|
+
let ComponentFn = comp.default || comp
|
|
247
|
+
|
|
248
|
+
if (typeof ComponentFn === 'object' && ComponentFn !== null) {
|
|
249
|
+
ComponentFn = Object.values(ComponentFn).find(v => typeof v === 'function') || ComponentFn
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (loadingApi) {
|
|
253
|
+
loadingApi.unmount()
|
|
254
|
+
loadingApi = null
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
childApi = ComponentFn(props, { listeners, slots, parent })
|
|
258
|
+
|
|
259
|
+
container.innerHTML = ''
|
|
260
|
+
childApi.render(container)
|
|
261
|
+
})
|
|
262
|
+
.catch(err => {
|
|
263
|
+
console.error('[framework] Failed to load async component:', err)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
return instance
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function unmount() {
|
|
270
|
+
isUnmounted = true
|
|
271
|
+
if (loadingApi) loadingApi.unmount()
|
|
272
|
+
if (childApi) childApi.unmount()
|
|
273
|
+
if (instance._element && !childApi && !loadingApi) {
|
|
274
|
+
instance._element.innerHTML = ''
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return { render, unmount, instance }
|
|
279
|
+
}
|
|
261
280
|
}
|