import type { ReadableFamilyToken, ReadableToken, ViewOf } from "atom.io" import type { Canonical } from "atom.io/foundations/canonical" import { arbitrary, getFromStore, subscribeToState } from "atom.io/internal" import { useContext } from "solid-js" import { parseStateOverloads } from "./parse-state-overloads.ts" import { StoreContext } from "./store-context.ts" import { useSyncExternalStore } from "./use-sync-external-store-solid.ts" export function useO( token: ReadableToken, ): () => ViewOf export function useO( token: ReadableFamilyToken, key: NoInfer, ): () => ViewOf export function useO( ...params: | [ReadableFamilyToken, NoInfer] | [ReadableToken] ): () => ViewOf { const store = useContext(StoreContext) const token = parseStateOverloads(store, ...params) const id = arbitrary() const sub = (dispatch: () => void) => subscribeToState(store, token, `use-o:${id}`, dispatch) const get = () => getFromStore(store, token) return useSyncExternalStore>(sub, get) }