|
| 1 | +import { |
| 2 | + createSignal, |
| 3 | + createResource, |
| 4 | + createMemo, |
| 5 | + untrack, |
| 6 | + Component, |
| 7 | + JSX, |
| 8 | + sharedConfig, |
| 9 | +} from 'solid-js'; |
| 10 | + |
| 11 | +// lazy load a function component asynchronously |
| 12 | +export function lazy<T extends Component<any>>( |
| 13 | + fn: () => Promise<{ default: T }>, |
| 14 | +): T & { preload: () => Promise<{ default: T }> } { |
| 15 | + let comp: () => T | undefined; |
| 16 | + let p: Promise<{ default: T }> | undefined; |
| 17 | + const wrap: T & { preload?: () => void } = ((props: any) => { |
| 18 | + const ctx = sharedConfig.context; |
| 19 | + if (ctx) { |
| 20 | + const [s, set] = createSignal<T>(); |
| 21 | + sharedConfig.count || (sharedConfig.count = 0); |
| 22 | + sharedConfig.count++; |
| 23 | + (p || (p = fn())).then((mod) => { |
| 24 | + !sharedConfig.done && (sharedConfig.context = ctx); |
| 25 | + sharedConfig.count!--; |
| 26 | + set(() => mod.default); |
| 27 | + sharedConfig.context = undefined; |
| 28 | + }); |
| 29 | + comp = s; |
| 30 | + } else if (!comp) { |
| 31 | + const [s] = createResource<T>(() => |
| 32 | + (p || (p = fn())).then((mod) => mod.default), |
| 33 | + ); |
| 34 | + comp = s; |
| 35 | + } |
| 36 | + let Comp: T | undefined; |
| 37 | + return createMemo(() => |
| 38 | + (Comp = comp()) |
| 39 | + ? untrack(() => { |
| 40 | + if (!ctx || sharedConfig.done) return Comp!(props); |
| 41 | + const c = sharedConfig.context; |
| 42 | + sharedConfig.context = ctx; |
| 43 | + const r = Comp!(props); |
| 44 | + sharedConfig.context = c; |
| 45 | + return r; |
| 46 | + }) |
| 47 | + : null, |
| 48 | + ) as unknown as JSX.Element; |
| 49 | + }) as T; |
| 50 | + wrap.preload = () => |
| 51 | + p || ((p = fn()).then((mod) => (comp = () => mod.default)), p); |
| 52 | + return wrap as T & { preload: () => Promise<{ default: T }> }; |
| 53 | +} |
0 commit comments