Skip to content

Commit 83a20dd

Browse files
committed
fix: fix useSubscribe init bug
1 parent 8616687 commit 83a20dd

File tree

1 file changed

+40
-74
lines changed

1 file changed

+40
-74
lines changed

src/use/useSubscribe.ts

+40-74
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,45 @@
11
import { cloneDeep } from 'lodash';
22
import { useEffect, useRef, useState } from 'react';
3-
import { UseSubscribeNamespace } from 'react-form-simple/types/use';
4-
import { useControllerRef } from 'react-form-simple/use/useControllerRef';
5-
import { isEqual } from 'react-form-simple/utils/util';
6-
7-
const subscribeObject = () => {
8-
const object = {
9-
datas: [] as Array<{ key: symbol; cb: () => void }>,
10-
11-
getInstanceIndex(key: symbol) {
12-
return this.get().findIndex((v) => v.key === key);
13-
},
14-
get() {
15-
return this.datas;
16-
},
17-
set(cb: () => void, key: symbol) {
18-
const index = this.getInstanceIndex(key);
19-
if (index >= 0) {
20-
this.datas[index].cb = cb;
21-
} else {
22-
this.datas.push({ key, cb });
23-
}
3+
import type { UseSubscribeNamespace } from 'react-form-simple';
4+
import { RequiredContextType } from 'react-form-simple/driver/ObserverDriver/type';
5+
6+
export const useSubscribe = <T>(
7+
contextProps: RequiredContextType<T>,
8+
subscribeFun: UseSubscribeNamespace.SubscribeFunType<T, any>,
9+
) => {
10+
const symbolKey = useRef(Symbol('symbol')).current;
11+
12+
const [state, setState] = useState<any>(
13+
subscribeFun({ model: cloneDeep(contextProps.model) }),
14+
);
15+
16+
const { observerFactory } = contextProps;
17+
18+
observerFactory.subscribeManager.register(
19+
symbolKey,
20+
contextProps,
21+
subscribeFun,
22+
(value: any) => {
23+
setState(value);
2424
},
25-
emit() {
26-
setTimeout(() => {
27-
const subscribeCbs = this.get();
28-
subscribeCbs.forEach((subs) => {
29-
subs.cb?.();
30-
});
31-
});
32-
},
33-
delete(key: symbol) {
34-
const index = this.getInstanceIndex(key);
35-
if (index >= 0) {
36-
this.datas.splice(index, 1);
37-
}
38-
},
39-
};
40-
return object;
41-
};
42-
43-
export const usePrivateSubscribe = <T extends Record<string, any>>(options: {
44-
model: T;
45-
}): UseSubscribeNamespace.UseSubscribeReturnType<
46-
T,
47-
ReturnType<typeof subscribeObject>
48-
> => {
49-
const subscribes = useControllerRef(subscribeObject());
50-
51-
const useSubscribe: UseSubscribeNamespace.UseSubscribe<T> = (cb) => {
52-
const [state, setState] = useState<any>();
53-
const preValueRef = useRef(null) as any;
54-
const symbolKey = useRef(Symbol('symbol')).current;
55-
56-
useEffect(() => {
57-
subscribes.set(() => {
58-
const { model } = options;
59-
const value = cb({ model: cloneDeep(model) });
60-
if (isEqual(value, preValueRef.current)) return;
61-
preValueRef.current = value;
62-
setState(value);
63-
}, symbolKey);
64-
}, [cb]);
65-
66-
useEffect(() => {
67-
subscribes.emit();
68-
return () => {
69-
subscribes.delete(symbolKey);
70-
};
71-
}, []);
72-
73-
return state;
74-
};
75-
76-
return { useSubscribe, subscribes };
25+
);
26+
27+
useEffect(() => {
28+
return () => {
29+
observerFactory.subscribeManager.destroy(symbolKey);
30+
};
31+
}, []);
32+
return state;
7733
};
7834

79-
export default usePrivateSubscribe;
35+
// export const subscribe = <T>(
36+
// contextProps: RequiredContextType<T>,
37+
// sub: UseSubscribeNamespace.SubscribeFunType<T, any>,
38+
// key: string,
39+
// ) => {
40+
// const { observerFactory } = contextProps;
41+
// const _sub = (...args: any) => sub(...args);
42+
// observerFactory.subscribeManager.register(key, contextProps, sub, _sub);
43+
44+
// return _sub()
45+
// };

0 commit comments

Comments
 (0)