import { memo , PropsWithChildren , useCallback as rCallback , useEffect as rEffect , useMemo as rMemo , useRef as rRef , useState as rState } from "react" ; import { createComputed as sComputed , createEffect as sEffect , createMemo as sMemo , createRoot , createSignal as sSignal , onCleanup as sCleanup , createReaction } from "solid-js" ; import { createMutable , createStore , Store } from "solid-js/store" ; export { batch , untrack } from "solid-js" ; export { reconcile , produce } from "solid-js/store" ; interface CreateComputed < T > { ( fn , value ) : void ; value ? : undefined ; options ? : { equals ? : false | ( ( prev : T , next : T ) => boolean ) ; } ; } interface CreateEffect < T > { ( fn , value ) : void ; value ? : undefined ; options ? : { equals ? : false | ( ( prev : T , next : T ) => boolean ) ; } ; } interface CreateMemo < T > { ( fn , value ? , options ? ) : ( ) => T ; } let inSolidEffect = false ; function trackNesting < T extends any [ ] > ( args ) { const fn = args [ 0 ] as ( ... args : any [ ] ) => void ; return [ function ( ... args ) { const outside = inSolidEffect ; inSolidEffect = true ; const ret = fn ( ... args ) ; inSolidEffect = outside ; return ret ; } , ... args . slice ( 1 ) ] as T ; } function useForceUpdate ( ) { const [ , setTick ] = rState ( 0 ) ; return rCallback ( ( ) => setTick ( t => t + 1 ) , [ ] ) ; } export function useObserver < T > ( fn ) { const forceUpdate = useForceUpdate ( ) , reaction = rRef < { dispose : ( ) => void ; track : ( fn : ( ) => void ) => void ; } > ( ) ; if ( ! reaction . current ) { reaction . current = createRoot ( dispose => ( { dispose , track : createReaction ( forceUpdate ) } ) ) ; } rEffect ( ( ) => reaction . current ! . dispose , [ ] ) ; let rendering ! ; reaction . current . track ( ( ) => ( rendering = fn ( ) ) ) ; return rendering ; } export function withSolid < P extends object > ( ComponentType ) { return memo < P > ( ( p , r ) => { const component = ComponentType ( p , r ) ; return ( component && useObserver ( component ) ) || null ; } ) ; } export function useStore < T > ( state , options ? ) { if ( inSolidEffect ) return createStore < T > ( state , options ) ; return rMemo ( ( ) => createStore < T > ( state , options ) , [ ] ) ; } export function useMutable < T > ( state , options ? ) { if ( inSolidEffect ) return createMutable < T > ( state , options ) ; return rMemo ( ( ) => createMutable < T > ( state , options ) , [ ] ) ; } export function useSignal < T > ( value , options ? ) { if ( inSolidEffect ) return sSignal < T > ( value , options ) ; return rMemo ( ( ) => sSignal < T > ( value , options ) , [ ] ) ; } export function useEffect < T > ( ... args ) { if ( inSolidEffect ) return sEffect < T > ( ... args ) ; const dispose = rRef < ( ) => void > ( ) ; rEffect ( ( ) => dispose . current , [ ] ) ; if ( ! dispose . current ) { createRoot ( disposer => { dispose . current = disposer ; sEffect < T > ( ... trackNesting ( args ) ) ; } ) ; } } export function useComputed < T > ( ... args ) { if ( inSolidEffect ) return sComputed < T > ( ... args ) ; const dispose = rRef < ( ) => void > ( ) ; rEffect ( ( ) => dispose . current , [ ] ) ; if ( ! dispose . current ) { createRoot ( disposer => { dispose . current = disposer ; sComputed ( ... trackNesting ( args ) ) ; } ) ; } } export function useMemo < T > ( ... args ) { if ( inSolidEffect ) return sMemo < T > ( ... args ) ; let dispose ; rEffect ( ( ) => dispose , [ ] ) ; return rMemo ( ( ) => createRoot ( disposer => { dispose = disposer ; return sMemo ( ... trackNesting ( args ) ) ; } ) , [ ] ) ; } export function useCleanup ( fn ) { inSolidEffect ? sCleanup ( fn ) : rEffect ( ( ) => fn , [ ] ) ; } export { useSignal as createSignal , useMemo as createMemo , useCleanup as onCleanup , useEffect as createEffect , useComputed as createComputed , useStore as createStore , useMutable as createMutable } ;