Skip to content

Commit

Permalink
chore(release): v1.4.9
Browse files Browse the repository at this point in the history
chore(release): v1.4.9
  • Loading branch information
97vack authored May 6, 2024
2 parents f60ac50 + 45ffa31 commit 9040b68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-form-simple",
"version": "1.4.8",
"version": "1.4.9",
"description": "A form library for quickly controlling react form input",
"keywords": [
"react",
Expand Down
7 changes: 7 additions & 0 deletions src/use/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const useForm = <T extends Record<string, any>>(

const { contextProps, overlayApis, globalDatas } = useContextApi();

const globalMaps = useRef({
proxyMap: new WeakMap(),
rawMap: new WeakMap(),
});

const debounceFn = useRef({
watch: debounce(() => {
watchInstance.emit();
Expand All @@ -44,6 +49,8 @@ const useForm = <T extends Record<string, any>>(
{
path: [],
onChangeLength: debounceFn.onChangeLength,
rawMap: globalMaps.current.rawMap,
proxyMap: globalMaps.current.proxyMap,
},
);
}).current;
Expand Down
24 changes: 19 additions & 5 deletions src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const Proxys = window.Proxy || proxyPolyfill;

export type ObserverOptions = {
path?: string[];
proxyMap?: WeakMap<object, any>;
rawMap?: WeakMap<object, any>;
proxyMap: WeakMap<object, any>;
rawMap: WeakMap<object, any>;
onChangeLength?: () => void;
};

Expand Down Expand Up @@ -136,15 +136,29 @@ export const observer = <T extends object>(
cb?: (args: ObserverCb) => void,
options?: ObserverOptions,
): T => {
const { path = [], onChangeLength } = options || {};
const {
path = [],
onChangeLength,
proxyMap,
rawMap,
} = (options || {}) as ObserverOptions;

const existingProxy = proxyMap.get(initialVal);
if (existingProxy) {
return existingProxy;
}

if (rawMap.has(initialVal)) {
return initialVal;
}

const proxy = new Proxys(initialVal, {
get(target, key, receiver) {
const ret = Reflect.get(target, key, receiver);
if (React.isValidElement(ret)) return ret;
return isObjectOrArray(ret)
? observer(ret as T, cb, {
...options,
...(options as ObserverOptions),
path: [...path, key.toString()],
})
: ret;
Expand Down Expand Up @@ -192,7 +206,7 @@ export const createControllerObserver = <T extends object>(
if (React.isValidElement(ret)) return ret;
return isObjectOrArray(ret)
? createControllerObserver(ret as T, cb, {
...options,
...(options as ObserverOptions),
path: [...path, key.toString()],
})
: ret;
Expand Down

0 comments on commit 9040b68

Please sign in to comment.