Skip to content

Commit

Permalink
fix: fix useSubscribe init bug
Browse files Browse the repository at this point in the history
  • Loading branch information
97vack committed Jul 9, 2024
1 parent 8616687 commit 83a20dd
Showing 1 changed file with 40 additions and 74 deletions.
114 changes: 40 additions & 74 deletions src/use/useSubscribe.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,45 @@
import { cloneDeep } from 'lodash';
import { useEffect, useRef, useState } from 'react';
import { UseSubscribeNamespace } from 'react-form-simple/types/use';
import { useControllerRef } from 'react-form-simple/use/useControllerRef';
import { isEqual } from 'react-form-simple/utils/util';

const subscribeObject = () => {
const object = {
datas: [] as Array<{ key: symbol; cb: () => void }>,

getInstanceIndex(key: symbol) {
return this.get().findIndex((v) => v.key === key);
},
get() {
return this.datas;
},
set(cb: () => void, key: symbol) {
const index = this.getInstanceIndex(key);
if (index >= 0) {
this.datas[index].cb = cb;
} else {
this.datas.push({ key, cb });
}
import type { UseSubscribeNamespace } from 'react-form-simple';
import { RequiredContextType } from 'react-form-simple/driver/ObserverDriver/type';

export const useSubscribe = <T>(
contextProps: RequiredContextType<T>,
subscribeFun: UseSubscribeNamespace.SubscribeFunType<T, any>,
) => {
const symbolKey = useRef(Symbol('symbol')).current;

const [state, setState] = useState<any>(
subscribeFun({ model: cloneDeep(contextProps.model) }),
);

const { observerFactory } = contextProps;

observerFactory.subscribeManager.register(
symbolKey,
contextProps,
subscribeFun,
(value: any) => {
setState(value);
},
emit() {
setTimeout(() => {
const subscribeCbs = this.get();
subscribeCbs.forEach((subs) => {
subs.cb?.();
});
});
},
delete(key: symbol) {
const index = this.getInstanceIndex(key);
if (index >= 0) {
this.datas.splice(index, 1);
}
},
};
return object;
};

export const usePrivateSubscribe = <T extends Record<string, any>>(options: {
model: T;
}): UseSubscribeNamespace.UseSubscribeReturnType<
T,
ReturnType<typeof subscribeObject>
> => {
const subscribes = useControllerRef(subscribeObject());

const useSubscribe: UseSubscribeNamespace.UseSubscribe<T> = (cb) => {
const [state, setState] = useState<any>();
const preValueRef = useRef(null) as any;
const symbolKey = useRef(Symbol('symbol')).current;

useEffect(() => {
subscribes.set(() => {
const { model } = options;
const value = cb({ model: cloneDeep(model) });
if (isEqual(value, preValueRef.current)) return;
preValueRef.current = value;
setState(value);
}, symbolKey);
}, [cb]);

useEffect(() => {
subscribes.emit();
return () => {
subscribes.delete(symbolKey);
};
}, []);

return state;
};

return { useSubscribe, subscribes };
);

useEffect(() => {
return () => {
observerFactory.subscribeManager.destroy(symbolKey);
};
}, []);
return state;
};

export default usePrivateSubscribe;
// export const subscribe = <T>(
// contextProps: RequiredContextType<T>,
// sub: UseSubscribeNamespace.SubscribeFunType<T, any>,
// key: string,
// ) => {
// const { observerFactory } = contextProps;
// const _sub = (...args: any) => sub(...args);
// observerFactory.subscribeManager.register(key, contextProps, sub, _sub);

// return _sub()
// };

0 comments on commit 83a20dd

Please sign in to comment.