Skip to content

Commit 0b92fa6

Browse files
committed
feat: add withLazyViewModel HOC
1 parent 05a2727 commit 0b92fa6

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"url": "git://github.com/js2me/mobx-vm-entities"
3131
},
3232
"dependencies": {
33-
"lodash-es": "^4.17.21"
33+
"lodash-es": "^4.17.21",
34+
"react-simple-loadable": "^1.0.0"
3435
},
3536
"peerDependencies": {
3637
"disposer-util": "^1.0.9",

pnpm-lock.yaml

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

src/hoc/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './with-view-model';
2+
export * from './with-lazy-view-model';

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ComponentType } from 'react';
2+
import { loadable } from 'react-simple-loadable';
3+
4+
import { Class } from '../utils/types';
5+
6+
import { ViewModelHocConfig, withViewModel } from './with-view-model';
7+
8+
interface LazyViewAndModel {
9+
Model: Class<any>;
10+
View: ComponentType<any>;
11+
}
12+
13+
export function withLazyViewModel(
14+
loadFunction: () => Promise<LazyViewAndModel>,
15+
config?: ViewModelHocConfig<any>,
16+
) {
17+
return loadable(async () => {
18+
const { Model, View } = await loadFunction();
19+
return withViewModel(Model, config)(View);
20+
}, config?.fallback);
21+
}

0 commit comments

Comments
 (0)