import { _cloneDeep , _isPlainObject , _mergeWith } from '@felte/core' ; import { createEffect , createSignal , createRoot , batch } from 'solid-js' ; import { createStore , reconcile } from 'solid-js/store' ; const isCallback = ( maybeFunction ) : maybeFunction is ( ... args : any [ ] ) => void => typeof maybeFunction === 'function' ; function createSubscriber < T > ( store ) { return function subscribe ( fn ) { const value = isCallback ( store ) ? store : ( ) => store ; fn ( value ( ) ) ; let disposer ; createRoot ( ( dispose ) => { disposer = dispose ; createEffect ( ( ) => fn ( value ( ) ) ) ; return dispose ; } ) ; return ( ) => disposer ( ) ; } ; } export const storeFactory = < Value > ( initialValue ) => { if ( ! _isPlainObject ( initialValue ) ) { const [ signal , setSignal ] = createSignal < Value > ( initialValue ) ; function signalSetter ( value ) { setSignal ( ( ) => value ) ; } function signalUpdater ( updater ) { signalSetter ( updater ( signal ( ) ) ) ; } return { subscribe : createSubscriber < Value > ( signal ) , update : signalUpdater , set : signalSetter , getSolidValue : ( ) => signal , } ; } const [ store , setStore ] = createStore < Value > ( initialValue ) ; function storeSetter ( value ) { batch ( ( ) => { setStore ( value ) ; setStore ( reconcile ( value ) ) ; } ) ; } function storeUpdater ( updater ) { const updatedStore = updater ( store ) as Value ; storeSetter ( updatedStore ) ; } return { subscribe : createSubscriber < Value > ( store ) , update : storeUpdater , set : storeSetter , getSolidValue : ( ) => store , } ; } ;