Skip to content

Commit bffb14d

Browse files
committed
refactor: hook useCreateViewModel split onto 2 nested hooks
1 parent 7ff9325 commit bffb14d

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

src/hooks/use-create-view-model.ts

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-use-before-define */
12
/* eslint-disable react-hooks/rules-of-hooks */
23
import { useContext, useLayoutEffect } from 'react';
34
import { Class, AllPropertiesOptional, Maybe } from 'yummies/utils/types';
@@ -37,42 +38,6 @@ export interface UseCreateViewModelConfig<TViewModel extends AnyViewModel>
3738
factory?: CreateViewModelFactoryFn<TViewModel>;
3839
}
3940

40-
const useCreateViewModelSimple = (
41-
VM: Class<ViewModelSimple>,
42-
payload?: any,
43-
) => {
44-
const viewModels = useContext(ViewModelsContext);
45-
const instance = useValue(() => {
46-
const instance = new VM();
47-
48-
viewModels?.markToBeAttached(instance);
49-
50-
return instance;
51-
});
52-
53-
if ('setPayload' in instance) {
54-
useLayoutEffect(() => {
55-
instance.setPayload!(payload);
56-
}, [payload]);
57-
}
58-
59-
useIsomorphicLayoutEffect(() => {
60-
if (viewModels) {
61-
viewModels.attach(instance);
62-
return () => {
63-
viewModels.detach(instance.id);
64-
};
65-
} else {
66-
instance.mount?.();
67-
return () => {
68-
instance.unmount?.();
69-
};
70-
}
71-
}, [instance]);
72-
73-
return instance;
74-
};
75-
7641
/**
7742
* Creates new instance of ViewModel
7843
*
@@ -110,12 +75,25 @@ export function useCreateViewModel<TViewModelSimple extends ViewModelSimple>(
11075
*
11176
* [**Documentation**](https://js2me.github.io/mobx-view-model/react/api/use-create-view-model.html)
11277
*/
113-
export function useCreateViewModel(VM: Class<any>, ...args: any[]) {
114-
const [payload, config] = args as unknown as [
115-
any,
116-
Maybe<UseCreateViewModelConfig<AnyViewModel>>,
117-
];
78+
export function useCreateViewModel(
79+
VM: Class<any>,
80+
payload?: any,
81+
config?: any,
82+
) {
83+
if ('payloadChanged' in VM.prototype) {
84+
// scenario for ViewModelBase
85+
return useCreateViewModelBase(VM, payload, config);
86+
}
87+
88+
// scenario for ViewModelSimple
89+
return useCreateViewModelSimple(VM, payload);
90+
}
11891

92+
const useCreateViewModelBase = (
93+
VM: Class<any>,
94+
payload?: any,
95+
config?: Maybe<UseCreateViewModelConfig<AnyViewModel>>,
96+
) => {
11997
if (
12098
!('willMount' in VM.prototype) &&
12199
!('payloadChanged' in VM.prototype) &&
@@ -191,4 +169,40 @@ export function useCreateViewModel(VM: Class<any>, ...args: any[]) {
191169
instance.setPayload(payload ?? {});
192170

193171
return instance;
194-
}
172+
};
173+
174+
const useCreateViewModelSimple = (
175+
VM: Class<ViewModelSimple>,
176+
payload?: any,
177+
) => {
178+
const viewModels = useContext(ViewModelsContext);
179+
const instance = useValue(() => {
180+
const instance = new VM();
181+
182+
viewModels?.markToBeAttached(instance);
183+
184+
return instance;
185+
});
186+
187+
if ('setPayload' in instance) {
188+
useLayoutEffect(() => {
189+
instance.setPayload!(payload);
190+
}, [payload]);
191+
}
192+
193+
useIsomorphicLayoutEffect(() => {
194+
if (viewModels) {
195+
viewModels.attach(instance);
196+
return () => {
197+
viewModels.detach(instance.id);
198+
};
199+
} else {
200+
instance.mount?.();
201+
return () => {
202+
instance.unmount?.();
203+
};
204+
}
205+
}, [instance]);
206+
207+
return instance;
208+
};

0 commit comments

Comments
 (0)