|
1 | 1 | /* @flow */
|
2 | 2 |
|
3 |
| -// () => ({ |
4 |
| -// component: import('./xxx.vue'), |
5 |
| -// delay: 200, |
6 |
| -// loading: LoadingComponent, |
7 |
| -// error: ErrorComponent |
8 |
| -// }) |
9 |
| - |
10 | 3 | import {
|
11 | 4 | warn,
|
| 5 | + once, |
| 6 | + isDef, |
| 7 | + isUndef, |
| 8 | + isTrue, |
12 | 9 | isObject
|
13 | 10 | } from 'core/util/index'
|
14 | 11 |
|
| 12 | +function ensureCtor (comp, base) { |
| 13 | + return isObject(comp) |
| 14 | + ? base.extend(comp) |
| 15 | + : comp |
| 16 | +} |
| 17 | + |
15 | 18 | export function resolveAsyncComponent (
|
16 | 19 | factory: Function,
|
17 | 20 | baseCtor: Class<Component>,
|
18 | 21 | context: Component
|
19 | 22 | ): Class<Component> | void {
|
20 |
| - if (factory.resolved) { |
| 23 | + if (isTrue(factory.error) && isDef(factory.errorComp)) { |
| 24 | + return factory.errorComp |
| 25 | + } |
| 26 | + |
| 27 | + if (isDef(factory.resolved)) { |
21 | 28 | return factory.resolved
|
22 | 29 | }
|
23 | 30 |
|
24 |
| - const cb = () => context.$forceUpdate() |
25 |
| - if (factory.requested) { |
26 |
| - // pool callbacks |
27 |
| - factory.pendingCallbacks.push(cb) |
| 31 | + if (isTrue(factory.loading) && isDef(factory.loadingComp)) { |
| 32 | + return factory.loadingComp |
| 33 | + } |
| 34 | + |
| 35 | + if (isDef(factory.contexts)) { |
| 36 | + // already pending |
| 37 | + factory.contexts.push(context) |
28 | 38 | } else {
|
29 |
| - factory.requested = true |
30 |
| - const cbs = factory.pendingCallbacks = [cb] |
| 39 | + const contexts = factory.contexts = [context] |
31 | 40 | let sync = true
|
32 | 41 |
|
33 |
| - const resolve = (res: Object | Class<Component>) => { |
34 |
| - if (isObject(res)) { |
35 |
| - res = baseCtor.extend(res) |
| 42 | + const forceRender = () => { |
| 43 | + for (let i = 0, l = contexts.length; i < l; i++) { |
| 44 | + contexts[i].$forceUpdate() |
36 | 45 | }
|
| 46 | + } |
| 47 | + |
| 48 | + const resolve = once((res: Object | Class<Component>) => { |
37 | 49 | // cache resolved
|
38 |
| - factory.resolved = res |
| 50 | + factory.resolved = ensureCtor(res, baseCtor) |
39 | 51 | // invoke callbacks only if this is not a synchronous resolve
|
40 | 52 | // (async resolves are shimmed as synchronous during SSR)
|
41 | 53 | if (!sync) {
|
42 |
| - for (let i = 0, l = cbs.length; i < l; i++) { |
43 |
| - cbs[i](res) |
44 |
| - } |
| 54 | + forceRender() |
45 | 55 | }
|
46 |
| - } |
| 56 | + }) |
47 | 57 |
|
48 |
| - const reject = reason => { |
| 58 | + const reject = once(reason => { |
49 | 59 | process.env.NODE_ENV !== 'production' && warn(
|
50 | 60 | `Failed to resolve async component: ${String(factory)}` +
|
51 | 61 | (reason ? `\nReason: ${reason}` : '')
|
52 | 62 | )
|
53 |
| - } |
| 63 | + if (isDef(factory.errorComp)) { |
| 64 | + factory.error = true |
| 65 | + forceRender() |
| 66 | + } |
| 67 | + }) |
54 | 68 |
|
55 | 69 | const res = factory(resolve, reject)
|
56 | 70 |
|
57 |
| - // handle promise |
58 |
| - if (res && typeof res.then === 'function' && !factory.resolved) { |
59 |
| - res.then(resolve, reject) |
| 71 | + if (isObject(res)) { |
| 72 | + if (typeof res.then === 'function') { |
| 73 | + // () => Promise |
| 74 | + if (isUndef(factory.resolved)) { |
| 75 | + res.then(resolve, reject) |
| 76 | + } |
| 77 | + } else if (isDef(res.component) && typeof res.component.then === 'function') { |
| 78 | + if (isDef(res.error)) { |
| 79 | + factory.errorComp = ensureCtor(res.error, baseCtor) |
| 80 | + } |
| 81 | + |
| 82 | + if (isDef(res.loading)) { |
| 83 | + factory.loadingComp = ensureCtor(res.loading, baseCtor) |
| 84 | + setTimeout(() => { |
| 85 | + if (isUndef(factory.resolved) && isUndef(factory.error)) { |
| 86 | + factory.loading = true |
| 87 | + forceRender() |
| 88 | + } |
| 89 | + }, res.delay || 200) |
| 90 | + } |
| 91 | + |
| 92 | + if (isDef(res.timeout)) { |
| 93 | + setTimeout(reject, res.timeout) |
| 94 | + } |
| 95 | + |
| 96 | + res.component.then(resolve, reject) |
| 97 | + } |
60 | 98 | }
|
61 | 99 |
|
62 | 100 | sync = false
|
|
0 commit comments