Skip to content

Commit 5b8ef2c

Browse files
committed
feat: add withLazyViewModel extra overload for better DX
1 parent a1fc843 commit 5b8ef2c

File tree

7 files changed

+68
-37
lines changed

7 files changed

+68
-37
lines changed

.changeset/cruel-poems-greet.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+
new overload for withLazyViewModel HOC for better DX (+ add docs for this)

docs/react/api/use-create-view-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Using in [`withViewModel()`](/react/api/with-view-model) HOC.
55
## API Signature
66
```tsx
77
function useCreateViewModel<VM extends AnyViewModel>(
8-
ViewModelClass: Constructor<VM>,
8+
ViewModelClass: Class<VM>,
99
payload?: ViewModelPayload<VM>,
1010
config?: UseCreateViewModelConfig<VM>
1111
): VM;

docs/react/api/with-lazy-view-model.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ This HOC is using [react-simple-loadable](https://www.npmjs.com/package/react-si
88

99
## API Signature
1010
```tsx
11-
function withLazyViewModel<VM extends AnyViewModel>(
11+
function withLazyViewModel<TViewModel extends AnyViewModel>(
1212
loadFn: () => Promise<{
13-
Model: Constructor<VM>,
14-
Component: ComponentType<ComponentProps & ViewModelProps<VM>>
13+
Model: Class<TViewModel> | Promised<Class<TViewModel>>,
14+
Component: TView | Promised<TView>
1515
}>,
16-
config?: ViewModelHocConfig<VM>
16+
config?: ViewModelHocConfig<TViewModel>
1717
): ComponentWithLazyViewModel
1818
```
1919

@@ -41,7 +41,9 @@ import { ViewModelProps } from "mobx-view-model";
4141
import { observer } from "mobx-react-lite";
4242
import { FruitButtonHugeVM } from "./model";
4343
44-
export const FruitButtonView = observer(({ model }: ViewModelProps<FruitButtonHugeVM>) => {
44+
export const FruitButtonView = observer(({
45+
model
46+
}: ViewModelProps<FruitButtonHugeVM>) => {
4547
return (
4648
<div>
4749
<button onClick={model.handleButtonClick}>click me</button>
@@ -55,17 +57,27 @@ And connect them together with lazy loading this modules using this HOC
5557
```tsx title="index.ts"
5658
import { withLazyViewModel } from "mobx-view-model";
5759
58-
export const FruitButton = withLazyViewModel(
59-
async () => {
60-
const [{ FruitButtonHugeVM }, { FruitButtonView }] = await Promise.all([
61-
import("./model"),
62-
import("./view"),
63-
])
64-
65-
return {
66-
Model: FruitButtonHugeVM,
67-
View: FruitButtonView,
68-
}
69-
},
70-
)
60+
export const FruitButton = withLazyViewModel(() => {
61+
return {
62+
Model: import("./model").then(m => m.FruitButtonHugeVM),
63+
View: import("./view").then(m => m.FruitButtonView),
64+
}
65+
})
7166
```
67+
68+
Also you can use `"default"` exports if you need
69+
70+
```tsx
71+
... // model.ts
72+
export default class MyVM extends ViewModelBase {}
73+
... // view.tsx
74+
export default function MyView() { return <div>1</div> }
75+
... // index.ts
76+
export const MyComponent = withLazyeViewModel(() =>{
77+
return {
78+
Model: import("./model"),
79+
View: import("./view"),
80+
}
81+
})
82+
```
83+

docs/react/api/with-view-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A Higher-Order Component that connects React components to their [ViewModels](/a
55
## API Signature
66
```tsx
77
function withViewModel<VM extends AnyViewModel>(
8-
ViewModelClass: Constructor<VM>,
8+
ViewModelClass: Class<VM>,
99
config?: ViewModelHocConfig<VM>
1010
):
1111
(Component: ComponentType<ComponentProps & ViewModelProps<VM>>) =>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"url": "git://github.com/js2me/mobx-view-model"
4949
},
5050
"dependencies": {
51-
"react-simple-loadable": "^2.3.7",
52-
"yummies": "^4.0.0"
51+
"react-simple-loadable": "^2.3.8",
52+
"yummies": "^4.2.6"
5353
},
5454
"peerDependencies": {
5555
"mobx": "^6.12.4",

pnpm-lock.yaml

Lines changed: 18 additions & 10 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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ComponentProps, ComponentType } from 'react';
2+
import { PackedAsyncModule, unpackAsyncModule } from 'yummies/imports';
23

34
import { viewModelsConfig } from '../config/global-config.js';
45
import { loadable, LoadableMixin } from '../lib/react-simple-loadable.js';
5-
import { Class } from '../utils/types.js';
6+
import { Class, MaybePromise } from '../utils/types.js';
67
import { AnyViewModel } from '../view-model/index.js';
78

89
import {
@@ -15,8 +16,8 @@ export interface LazyViewAndModel<
1516
TViewModel extends AnyViewModel,
1617
TView extends ComponentType<any>,
1718
> {
18-
Model: Class<TViewModel>;
19-
View?: TView;
19+
Model: Class<TViewModel> | PackedAsyncModule<Class<TViewModel>>;
20+
View?: TView | PackedAsyncModule<TView>;
2021
}
2122

2223
export type ComponentWithLazyViewModel<
@@ -33,7 +34,7 @@ export function withLazyViewModel<
3334
TViewModel extends AnyViewModel,
3435
TView extends ComponentType<any>,
3536
>(
36-
loadFunction: () => Promise<LazyViewAndModel<TViewModel, TView>>,
37+
loadFunction: () => MaybePromise<LazyViewAndModel<TViewModel, TView>>,
3738
config?: ViewModelHocConfig<any>,
3839
): ComponentWithLazyViewModel<TViewModel, TView> {
3940
const patchedConfig: ViewModelHocConfig<any> = {
@@ -48,7 +49,12 @@ export function withLazyViewModel<
4849
patchedConfig?.fallback ?? viewModelsConfig.fallbackComponent;
4950

5051
const lazyVM = loadable(async () => {
51-
const { Model, View } = await loadFunction();
52+
const { Model: ModelOrAsync, View: ViewOrAsync } = await loadFunction();
53+
const [Model, View] = await Promise.all([
54+
unpackAsyncModule(ModelOrAsync),
55+
unpackAsyncModule(ViewOrAsync),
56+
]);
57+
5258
return withViewModel(Model, patchedConfig)(View);
5359
}, fallbackComponent) as ComponentWithLazyViewModel<TViewModel, TView>;
5460

0 commit comments

Comments
 (0)