import { useRef , useEffect , MutableRefObject , Dispatch , SetStateAction , useState , } from "react" ; import { nanoid } from "nanoid" ; const store = new Map < string , any > ( ) ; class Atom < T > { key = nanoid ( ) ; subscribers = new Map < MutableRefObject < boolean > , Dispatch < SetStateAction < T > > > ( ) ; private _current : T ; constructor ( initialState ) { store . set ( this . key , initialState ) ; this . _current = initialState ; } subscribe ( ref , action ) { this . subscribers . set ( ref , action ) ; } unsubscribe ( ref ) { this . subscribers . delete ( ref ) ; } setState ( nextState ) { this . _current = nextState ; store . set ( this . key , nextState ) ; this . subscribers . forEach ( ( action ) => action . call ( null , nextState ) ) ; } get current ( ) { return this . _current ; } } export const atom = < T > ( initialState ) => new Atom ( initialState ) ; export const useAtomValue = < T > ( atom ) => { const ref = useRef ( false ) ; const [ state , setState ] = useState ( atom . current ) ; if ( ref . current === false ) { ref . current = true ; atom . subscribe ( ref , setState ) ; } useEffect ( ( ) => ( ) => atom . unsubscribe ( ref ) , [ ] ) ; return state ; } ; export const setAtomValue = < T > ( atom ) => ( nextState ) => atom . setState ( nextState ) ;