Skip to content

Commit 69b796f

Browse files
committed
feat: improve withLazyViewModel
1 parent 9fa5182 commit 69b796f

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

.changeset/warm-lands-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-view-model": minor
3+
---
4+
5+
`withLazyViewModel` HOC lazy load parameters + better overload for second arg

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"url": "git://github.com/js2me/mobx-view-model"
4949
},
5050
"dependencies": {
51-
"react-simple-loadable": "^2.3.8",
51+
"react-simple-loadable": "^2.3.9",
5252
"yummies": "^4.2.6"
5353
},
5454
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hoc/with-lazy-view-model.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ComponentProps, ComponentType } from 'react';
2+
import { loadable, LoadableMixin, LoadableConfig } from 'react-simple-loadable';
23
import { PackedAsyncModule, unpackAsyncModule } from 'yummies/imports';
34

45
import { 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';
77
import { AnyViewModel } from '../view-model/index.js';
88

99
import {
@@ -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

src/lib/react-simple-loadable.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)