11import { ComponentProps , ComponentType } from 'react' ;
2+ import { loadable , LoadableMixin , LoadableConfig } from 'react-simple-loadable' ;
23import { PackedAsyncModule , unpackAsyncModule } from 'yummies/imports' ;
34
45import { viewModelsConfig } from '../config/global-config.js' ;
5- import { loadable , LoadableMixin } from '../lib/react-simple-loadable.js' ;
6- import { Class , MaybePromise } from '../utils/types.js' ;
6+ import { Class , Maybe , MaybePromise } from '../utils/types.js' ;
77import { AnyViewModel } from '../view-model/index.js' ;
88
99import {
@@ -25,6 +25,10 @@ export type ComponentWithLazyViewModel<
2525 TView extends ComponentType < any > ,
2626> = ComponentWithViewModel < TViewModel , ComponentProps < TView > > & LoadableMixin ;
2727
28+ export interface LazyViewModelHocConfig < TViewModel extends AnyViewModel >
29+ extends ViewModelHocConfig < TViewModel > ,
30+ Pick < LoadableConfig , 'loading' | 'preload' | 'throwOnError' > { }
31+
2832/**
2933 * Lazy creates new instance of ViewModel
3034 *
@@ -35,9 +39,18 @@ export function withLazyViewModel<
3539 TView extends ComponentType < any > ,
3640> (
3741 loadFunction : ( ) => MaybePromise < LazyViewAndModel < TViewModel , TView > > ,
38- config ?: ViewModelHocConfig < any > ,
42+ configOrFallbackComponent ?:
43+ | LazyViewModelHocConfig < NoInfer < TViewModel > >
44+ | LoadableConfig [ 'loading' ] ,
3945) : ComponentWithLazyViewModel < TViewModel , TView > {
40- const patchedConfig : ViewModelHocConfig < any > = {
46+ const config : Maybe < LazyViewModelHocConfig < NoInfer < TViewModel > > > =
47+ typeof configOrFallbackComponent === 'function'
48+ ? {
49+ fallback : configOrFallbackComponent ,
50+ }
51+ : configOrFallbackComponent ;
52+
53+ const patchedConfig : LazyViewModelHocConfig < NoInfer < TViewModel > > = {
4154 ...config ,
4255 ctx : {
4356 ...config ?. ctx ,
@@ -48,15 +61,22 @@ export function withLazyViewModel<
4861 const fallbackComponent =
4962 patchedConfig ?. fallback ?? viewModelsConfig . fallbackComponent ;
5063
51- const lazyVM = loadable ( async ( ) => {
52- const { Model : ModelOrAsync , View : ViewOrAsync } = await loadFunction ( ) ;
53- const [ Model , View ] = await Promise . all ( [
54- unpackAsyncModule ( ModelOrAsync ) ,
55- unpackAsyncModule ( ViewOrAsync ) ,
56- ] ) ;
64+ const lazyVM = loadable (
65+ async ( ) => {
66+ const { Model : ModelOrAsync , View : ViewOrAsync } = await loadFunction ( ) ;
67+ const [ Model , View ] = await Promise . all ( [
68+ unpackAsyncModule ( ModelOrAsync ) ,
69+ unpackAsyncModule ( ViewOrAsync ) ,
70+ ] ) ;
5771
58- return withViewModel ( Model , patchedConfig ) ( View ) ;
59- } , fallbackComponent ) as ComponentWithLazyViewModel < TViewModel , TView > ;
72+ return withViewModel ( Model , patchedConfig ) ( View ) ;
73+ } ,
74+ {
75+ loading : patchedConfig ?. loading ?? fallbackComponent ,
76+ preload : patchedConfig ?. preload ,
77+ throwOnError : patchedConfig ?. throwOnError ,
78+ } ,
79+ ) as ComponentWithLazyViewModel < TViewModel , TView > ;
6080
6181 patchedConfig . ctx ! . externalComponent = lazyVM ;
6282
0 commit comments