Skip to content

Commit da8313a

Browse files
committed
Refactoring
1 parent 06503c0 commit da8313a

File tree

1 file changed

+37
-61
lines changed

1 file changed

+37
-61
lines changed

src/UseStateLink.tsx

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -548,21 +548,45 @@ export function useState<S>(
548548
export function useState<S>(
549549
source: SetInitialStateAction<S> | State<S>
550550
): State<S> {
551-
let sourceIsInitialValue = true
552-
if (typeof source === 'object' && source !== null) {
553-
const statemethodsSource = source[self]
554-
if (statemethodsSource) {
555-
// it is already state object
556-
source = statemethodsSource; // get underlying StateMethods
557-
sourceIsInitialValue = false
551+
const parentMethods = typeof source === 'object' && source !== null ?
552+
source[self] as StateMethodsImpl<S> | undefined :
553+
undefined;
554+
if (parentMethods) {
555+
if (parentMethods.isMounted) {
556+
// Scoped state mount
557+
// eslint-disable-next-line react-hooks/rules-of-hooks
558+
const [, setValue] = React.useState({});
559+
return useSubscribedStateMethods<S>(
560+
parentMethods.state,
561+
parentMethods.path,
562+
() => setValue({}),
563+
parentMethods)[self];
564+
} else {
565+
// Global state mount or destroyed link
566+
// eslint-disable-next-line react-hooks/rules-of-hooks
567+
const [value, setValue] = React.useState({ state: parentMethods.state });
568+
return useSubscribedStateMethods<S>(
569+
value.state,
570+
parentMethods.path,
571+
() => setValue({ state: value.state }),
572+
value.state)[self];
558573
}
574+
} else {
575+
// Local state mount
576+
// eslint-disable-next-line react-hooks/rules-of-hooks
577+
const [value, setValue] = React.useState(() => ({ state: createStore(source) }));
578+
const result = useSubscribedStateMethods<S>(
579+
value.state,
580+
RootPath,
581+
() => setValue({ state: value.state }),
582+
value.state);
583+
React.useEffect(() => () => value.state.destroy(), []);
584+
const devtools = useState[DevToolsID]
585+
if (devtools) {
586+
result.attach(devtools)
587+
}
588+
return result[self];
559589
}
560-
const statemethods = useStateMethods(source as SetInitialStateAction<S>);
561-
const devtools = useState[DevToolsID]
562-
if (sourceIsInitialValue && devtools) {
563-
statemethods.attach(devtools)
564-
}
565-
return statemethods[self];
566590
}
567591

568592
/**
@@ -670,54 +694,6 @@ export function DevTools<S>(state: State<S>): DevToolsExtensions {
670694
/// INTERNAL SYMBOLS (LIBRARY IMPLEMENTATION)
671695
///
672696

673-
function useStateMethods<S>(
674-
source: StateMethods<S>
675-
): StateMethodsImpl<S>;
676-
function useStateMethods<S>(
677-
source: SetInitialStateAction<S>
678-
): StateMethodsImpl<S>;
679-
function useStateMethods<S>(
680-
source: SetInitialStateAction<S> | StateMethods<S>
681-
): StateMethodsImpl<S> {
682-
const parentLink = source instanceof StateMethodsImpl ?
683-
source as StateMethodsImpl<S> :
684-
undefined
685-
if (parentLink) {
686-
if (parentLink.isMounted) {
687-
// Scoped state mount
688-
// eslint-disable-next-line react-hooks/rules-of-hooks
689-
const [, setValue] = React.useState({});
690-
const link = useSubscribedStateMethods<S>(
691-
parentLink.state,
692-
parentLink.path,
693-
() => setValue({}),
694-
parentLink);
695-
return link;
696-
} else {
697-
// Global state mount or destroyed link
698-
// eslint-disable-next-line react-hooks/rules-of-hooks
699-
const [value, setValue] = React.useState({ state: parentLink.state });
700-
const link = useSubscribedStateMethods<S>(
701-
value.state,
702-
parentLink.path,
703-
() => setValue({ state: value.state }),
704-
value.state);
705-
return link;
706-
}
707-
} else {
708-
// Local state mount
709-
// eslint-disable-next-line react-hooks/rules-of-hooks
710-
const [value, setValue] = React.useState(() => ({ state: createStore(source) }));
711-
const link = useSubscribedStateMethods<S>(
712-
value.state,
713-
RootPath,
714-
() => setValue({ state: value.state }),
715-
value.state);
716-
React.useEffect(() => () => value.state.destroy(), []);
717-
return link;
718-
}
719-
}
720-
721697
const EmptyDevToolsExtensions: DevToolsExtensions = {
722698
label() { /* */ },
723699
log() { /* */ }

0 commit comments

Comments
 (0)