-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
// }; |