Skip to content

Commit 79cb795

Browse files
committed
fix: update subscribe function
1 parent 10fe3de commit 79cb795

11 files changed

+88
-338
lines changed

src/v2/hooks/useContainer.ts

+5-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import {
2-
useCallback, useContext, useEffect, useRef,
3-
} from "react";
4-
import useSubscribe from "./useSubscribe";
5-
import useUpdate from "./useUpdate";
6-
import { IForm, IState, initializeState } from "../logic/createForm";
1+
import { useContext } from "react";
2+
import { IForm } from "../logic/createForm";
73
import { FormContext } from "../contexts/FormContext";
84
import { ISchema } from "../types";
5+
import useSubscribeAndCompare from "./useSubscribeAndCompare";
96
// import type { ISchema } from "../../types";
107

118
export const useContainer = (props: {
@@ -15,34 +12,15 @@ export const useContainer = (props: {
1512
}) => {
1613
const { form: formContext } = useContext(FormContext);
1714
const { form = formContext } = props as { form: IForm<ISchema> };
18-
const _state = useRef<IState["containerFormState"]>(structuredClone(initializeState.containerFormState));
19-
const update = useUpdate();
2015

21-
const latestState = useCallback(
22-
() => {
23-
const latestState = _state.current;
24-
const { containerFormState } = form.state;
25-
if (JSON.stringify(containerFormState) !== JSON.stringify(latestState)) {
26-
_state.current = containerFormState;
27-
update();
28-
}
29-
},
30-
[],
31-
);
32-
33-
useSubscribe({
16+
useSubscribeAndCompare({
3417
form,
35-
callback: latestState,
3618
subject: "containers",
3719
});
3820

39-
useEffect(() => {
40-
latestState();
41-
}, []);
42-
4321
return {
4422
form,
45-
state: _state.current,
23+
state: form.state.containerFormState,
4624
};
4725
};
4826

src/v2/hooks/useField.ts

+9-21
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import {
44
useEffect,
55
useRef,
66
} from "react";
7-
import useSubscribe from "./useSubscribe";
8-
import useUpdate from "./useUpdate";
97
import type { IForm } from "../logic/createForm";
108
import { IDefaultProp, ISchemaFieldCore } from "../types";
119
import { FormContext } from "../contexts/FormContext";
10+
import useSubscribeAndCompare from "./useSubscribeAndCompare";
1211

1312
// eslint-disable-next-line no-use-before-define
1413
export const useField = <TSchema extends ISchemaFieldCore>(props: {
@@ -20,30 +19,19 @@ export const useField = <TSchema extends ISchemaFieldCore>(props: {
2019
const { form: formContext } = useContext(FormContext);
2120
const { form = formContext, schema } = props as { form: IForm<TSchema>, schema: TSchema };
2221
const _ref = useRef<any>();
23-
const _state = useRef({});
24-
const update = useUpdate();
2522

26-
const latestState = useCallback(
27-
() => {
28-
const latestState = _state.current;
29-
const state = form.getSchemaFieldState(schema as any);
30-
if (JSON.stringify(state) !== JSON.stringify(latestState)) {
31-
_state.current = structuredClone(state);
32-
update();
33-
}
34-
},
35-
[schema],
36-
);
37-
38-
useSubscribe({
23+
useSubscribeAndCompare({
3924
form,
40-
callback: latestState,
25+
getState() {
26+
return form.getSchemaFieldState(schema as any);
27+
},
4128
subject: "fields",
4229
});
4330

44-
useEffect(() => {
45-
latestState();
46-
}, [schema]);
31+
useSubscribeAndCompare({
32+
form,
33+
subject: "containers",
34+
});
4735

4836
useEffect(() => {
4937
form.fieldRef[schema.config.name] = _ref;

src/v2/hooks/useFieldArray.ts

-150
This file was deleted.

src/v2/hooks/useFieldForm.ts

+5-22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import {
44
useEffect,
55
useRef,
66
} from "react";
7-
import useSubscribe from "./useSubscribe";
8-
import useUpdate from "./useUpdate";
97
import type { IEventCallback, IForm } from "../logic/createForm";
108
import { IDefaultProp, ISchemaFieldCore } from "../types";
119
import { FormContext } from "../contexts/FormContext";
10+
import useSubscribeAndCompare from "./useSubscribeAndCompare";
1211

1312
// eslint-disable-next-line no-use-before-define
1413
export const useFieldForm = <TSchema extends ISchemaFieldCore>(props: {
@@ -20,31 +19,15 @@ export const useFieldForm = <TSchema extends ISchemaFieldCore>(props: {
2019
const { form: formContext } = useContext(FormContext);
2120
const { form = formContext, schema } = props as { form: IForm<TSchema>, schema: TSchema };
2221
const _ref = useRef<any>();
23-
const _state = useRef({});
24-
const update = useUpdate();
2522

26-
const latestState = useCallback(
27-
() => {
28-
const latestState = _state.current;
29-
const state = form.getSchemaFieldState(schema as any);
30-
if (JSON.stringify(state) !== JSON.stringify(latestState)) {
31-
_state.current = state;
32-
update();
33-
}
34-
},
35-
[schema],
36-
);
37-
38-
useSubscribe({
23+
useSubscribeAndCompare({
3924
form,
40-
callback: latestState,
25+
getState() {
26+
return form.getSchemaFieldState(schema as any);
27+
},
4128
subject: "fields",
4229
});
4330

44-
useEffect(() => {
45-
latestState();
46-
}, [schema]);
47-
4831
useEffect(() => {
4932
form.fieldRef[schema.config.name] = _ref;
5033
return () => {

src/v2/hooks/useForm.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,25 @@
11
import {
2-
useCallback, useEffect, useRef,
2+
useEffect, useRef,
33
} from "react";
44
import createForm, {
5-
IForm, ICreateFormProps, IState, initializeState,
5+
IForm, ICreateFormProps,
66
} from "../logic/createForm";
77
import useUpdate from "./useUpdate";
8-
import useSubscribe from "./useSubscribe";
8+
import useSubscribeAndCompare from "./useSubscribeAndCompare";
99

1010
export const useForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
1111
const update = useUpdate();
1212
const _form = useRef<IForm<TSchema>>(null as any);
13-
const _formState = useRef<IState["containerFormState"]>(structuredClone(initializeState.containerFormState));
1413

1514
if (!_form.current) {
1615
_form.current = createForm<TSchema>(props);
1716
}
1817

19-
const latestState = useCallback(
20-
() => {
21-
update();
22-
},
23-
[],
24-
);
25-
26-
useSubscribe({
18+
useSubscribeAndCompare({
2719
form: _form.current,
28-
callback: latestState,
2920
subject: "containers",
3021
});
3122

32-
useEffect(() => {
33-
latestState();
34-
}, []);
35-
3623
useEffect(() => {
3724
_form.current.reset({
3825
schemas: props.schemas,
@@ -44,7 +31,7 @@ export const useForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
4431

4532
return {
4633
form: _form.current,
47-
state: _formState.current,
34+
state: _form.current.state.containerFormState,
4835
handleSubmit: _form.current.handleSubmit,
4936
};
5037
};
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useContext, useEffect } from "react";
2+
import type { ISubject } from "../logic/createForm";
3+
import { FormContext } from "../contexts/FormContext";
4+
import useUpdate from "./useUpdate";
5+
// import type { ISchema } from "../../types";
6+
7+
type IUseSubscribeAndCompareProps = {
8+
subject: keyof ISubject;
9+
// eslint-disable-next-line no-unused-vars
10+
form?: any;
11+
disabled?: boolean;
12+
getState?: () => any;
13+
}
14+
15+
const useSubscribeAndCompare = (props: IUseSubscribeAndCompareProps) => {
16+
const update = useUpdate();
17+
const { form: formContext } = useContext(FormContext);
18+
const {
19+
form = formContext,
20+
disabled,
21+
subject = "fields",
22+
getState,
23+
} = props;
24+
25+
useEffect(() => {
26+
if (disabled) return;
27+
28+
let prevState = structuredClone(getState?.());
29+
const unsubscribe = form.subscribe(subject, () => {
30+
if (!getState?.()) {
31+
update();
32+
} else if (JSON.stringify(prevState) !== JSON.stringify(getState())) {
33+
prevState = structuredClone(getState());
34+
update();
35+
}
36+
});
37+
// eslint-disable-next-line consistent-return
38+
return unsubscribe;
39+
}, [disabled, form]);
40+
};
41+
42+
export default useSubscribeAndCompare;

0 commit comments

Comments
 (0)