Last active
October 31, 2024 02:03
-
-
Save rmarscher/2676b2b661c7119e7dd2a9449c179718 to your computer and use it in GitHub Desktop.
atomWithActorLogic.ts - derived atom example to integrate jotai and xstate
This file contains hidden or 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
import { type Getter, atom, useAtom } from "jotai"; | |
import { useCallback } from "react"; | |
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector"; | |
import type { | |
Actor, | |
ActorOptions, | |
ActorRefFromLogic, | |
AnyActorLogic, | |
AnyActorRef, | |
} from "xstate"; | |
import { createActor } from "xstate"; | |
// This import was inlined at the bottom of this file to work around dependency issues | |
// import { useSelector as useSelectorOriginal } from "@xstate/react"; | |
export function atomWithActorLogic<TLogic extends AnyActorLogic>( | |
logic: TLogic | ((get: Getter) => TLogic), | |
options?: ActorOptions<TLogic>, | |
) { | |
let actorRef: Actor<TLogic> | undefined = undefined; | |
const actorAtom = atom((get) => { | |
if (!actorRef) { | |
actorRef = createActor( | |
isGetter(logic) ? logic(get) : logic, | |
isGetter(options) ? options(get) : options, | |
); | |
actorRef.start(); | |
} | |
return actorRef; | |
}); | |
// TODO use actorAtom.onMount() to offer something similar to useSelector? | |
return actorAtom; | |
} | |
export function useSnapshot<TLogic extends AnyActorLogic>( | |
actorAtom: ReturnType<typeof atomWithActorLogic<TLogic>>, | |
) { | |
return useSelector(actorAtom, (x) => x); | |
} | |
type SyncExternalStoreSubscribe = Parameters< | |
typeof useSyncExternalStoreWithSelector | |
>[0]; | |
function defaultCompare<T>(a: T, b: T) { | |
return a === b; | |
} | |
export function useSelector<TLogic extends AnyActorLogic, T>( | |
actorAtom: ReturnType<typeof atomWithActorLogic<TLogic>>, | |
selector: Parameters< | |
typeof useSelectorOriginal<ActorRefFromLogic<TLogic>, T> | |
>[1], | |
compare?: Parameters< | |
typeof useSelectorOriginal<ActorRefFromLogic<TLogic>, T> | |
>[2], | |
) { | |
const [actor] = useAtom(actorAtom); | |
return useSelectorOriginal(actor, selector, compare); | |
} | |
const isGetter = <T>(v: T | ((get: Getter) => T)): v is (get: Getter) => T => | |
typeof v === "function"; | |
// This was copied from @xstate/react. Adding here | |
// to use React 19 compatible dependencies. | |
function useSelectorOriginal< | |
TActor extends Pick<AnyActorRef, "subscribe" | "getSnapshot"> | undefined, | |
T, | |
>( | |
actor: TActor, | |
selector: ( | |
snapshot: TActor extends { getSnapshot(): infer TSnapshot } | |
? TSnapshot | |
: undefined, | |
) => T, | |
compare: (a: T, b: T) => boolean = defaultCompare, | |
): T { | |
const subscribe: SyncExternalStoreSubscribe = useCallback( | |
(handleStoreChange: () => void) => { | |
if (!actor) { | |
return () => {}; | |
} | |
const { unsubscribe } = actor.subscribe(handleStoreChange); | |
return unsubscribe; | |
}, | |
[actor], | |
); | |
const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]); | |
const selectedSnapshot = useSyncExternalStoreWithSelector( | |
subscribe, | |
boundGetSnapshot, | |
boundGetSnapshot, | |
selector, | |
compare, | |
); | |
return selectedSnapshot; | |
} |
This file contains hidden or 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
MIT License | |
xstate: Copyright (c) 2015 David Khourshid | |
https://github.com/statelyai/xstate/blob/main/LICENSE | |
jotai-xstate: Copyright (c) 2020-2022 Poimandres | |
https://github.com/jotaijs/jotai-xstate/blob/main/LICENSE | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment