{"version":3,"sources":["../../../../src/context/react/utils/useRuntimeState.ts"],"sourcesContent":["import { useDebugValue, useSyncExternalStore } from \"react\";\nimport { Unsubscribe } from \"../../../types\";\nimport { ensureBinding } from \"./ensureBinding\";\n\nexport type SubscribableRuntime<TState> = {\n  getState: () => TState;\n  subscribe: (callback: () => void) => Unsubscribe;\n};\n\nexport function useRuntimeStateInternal<TState, TSelected>(\n  runtime: SubscribableRuntime<TState>,\n  selector: ((state: TState) => TSelected | TState) | undefined = identity,\n): TSelected | TState {\n  // TODO move to useRuntimeState\n  ensureBinding(runtime);\n\n  const slice = useSyncExternalStore(\n    runtime.subscribe,\n    () => selector(runtime.getState()),\n    () => selector(runtime.getState()),\n  );\n  useDebugValue(slice);\n  return slice;\n}\n\nconst identity = <T>(arg: T): T => arg;\nexport function useRuntimeState<TState>(\n  runtime: SubscribableRuntime<TState>,\n): TState;\nexport function useRuntimeState<TState, TSelected>(\n  runtime: SubscribableRuntime<TState>,\n  selector: (state: TState) => TSelected,\n): TSelected;\nexport function useRuntimeState<TState, TSelected>(\n  runtime: SubscribableRuntime<TState>,\n  selector: ((state: TState) => TSelected) | undefined,\n): TSelected | TState;\nexport function useRuntimeState<TState, TSelected>(\n  runtime: SubscribableRuntime<TState>,\n  selector?: ((state: TState) => TSelected) | undefined,\n): TSelected | TState {\n  // ensure that the runtime is bound\n  // ensureBinding(runtime);\n\n  return useRuntimeStateInternal(runtime, selector);\n}\n"],"mappings":";AAAA,SAAS,eAAe,4BAA4B;AAEpD,SAAS,qBAAqB;AAOvB,SAAS,wBACd,SACA,WAAgE,UAC5C;AAEpB,gBAAc,OAAO;AAErB,QAAM,QAAQ;AAAA,IACZ,QAAQ;AAAA,IACR,MAAM,SAAS,QAAQ,SAAS,CAAC;AAAA,IACjC,MAAM,SAAS,QAAQ,SAAS,CAAC;AAAA,EACnC;AACA,gBAAc,KAAK;AACnB,SAAO;AACT;AAEA,IAAM,WAAW,CAAI,QAAc;AAY5B,SAAS,gBACd,SACA,UACoB;AAIpB,SAAO,wBAAwB,SAAS,QAAQ;AAClD;","names":[]}