{"version":3,"sources":["../src/core/useable.ts","../src/core/context.ts"],"names":["Useable","isUseable","value","contextMap","resetContext","Context","defaultValue","props","_params","_callback","getContextValue","ctx","pushContext","id","prev","popContext","prevValue"],"mappings":"AAuBO,IAAeA,CAAAA,CAAf,KAAoE,CAApE,WAAA,EAAA,CAKL,IAAA,CAAS,WAAA,CAAc,KAAA,CAoCvB,UAAA,EAAkC,CAElC,CACF,EAKO,SAASC,EAAUC,CAAAA,CAAoD,CAC5E,OACEA,CAAAA,GAAU,IAAA,EACV,OAAOA,CAAAA,EAAU,QAAA,EACjB,gBAAiBA,CAAAA,EAChBA,CAAAA,CAAkC,WAAA,GAAgB,IAEvD,CC7EA,IAAMC,CAAAA,CAAa,IAAI,IAGhB,SAASC,CAAAA,EAAqB,CACnCD,CAAAA,CAAW,KAAA,GACb,CAuBO,IAAME,CAAAA,CAAN,cAAyBL,CAAW,CAKzC,WAAA,CAAYM,CAAAA,CAAiB,CAC3B,KAAA,GACA,IAAA,CAAK,EAAA,CAAK,MAAA,CAAO,SAAS,CAAA,CAC1B,IAAA,CAAK,YAAA,CAAeA,CAAAA,CACpB,KAAK,QAAA,CAAYC,CAAAA,EAA2CA,CAAAA,CAAM,QAAA,CAEhE,IAAA,CAAK,QAAA,CAAiB,UAAA,CAAa,IAAA,CAAK,GAC5C,CAKA,UAAA,EAAgB,CACd,OAAOJ,CAAAA,CAAW,GAAA,CAAI,IAAA,CAAK,EAAE,CAAA,CAAIA,CAAAA,CAAW,GAAA,CAAI,IAAA,CAAK,EAAE,CAAA,CAAS,IAAA,CAAK,YACvE,CAMA,SAAA,CAAUK,CAAAA,CAAoBC,CAAAA,CAA2C,CAGvE,OAAO,IAAM,CAAE,CACjB,CACF,EAGO,SAASC,CAAAA,CAAmBC,CAAAA,CAAoB,CACrD,OAAOA,CAAAA,CAAI,UAAA,EACb,CAGO,SAASC,CAAAA,CAAYC,CAAAA,CAAYX,CAAAA,CAAyB,CAC/D,IAAMY,CAAAA,CAAOX,CAAAA,CAAW,GAAA,CAAIU,CAAE,CAAA,CAC9B,OAAAV,CAAAA,CAAW,GAAA,CAAIU,CAAAA,CAAIX,CAAK,CAAA,CACjBY,CACT,CAGO,SAASC,CAAAA,CAAWF,CAAAA,CAAYG,CAAAA,CAA0B,CAC3DA,IAAc,MAAA,CAChBb,CAAAA,CAAW,MAAA,CAAOU,CAAE,CAAA,CAEpBV,CAAAA,CAAW,GAAA,CAAIU,CAAAA,CAAIG,CAAS,EAEhC","file":"chunk-WVSKSHKK.mjs","sourcesContent":["/**\n * Useable - Base class for reactive data sources that work with use()\n *\n * Extend this class to create custom data sources (Context, Stream, Shared, etc.)\n *\n * @example\n * ```tsx\n * class MySource<T> extends Useable<T> {\n *   getInitial() { return this.initialValue }\n *   subscribe(params, callback) {\n *     // setup subscription\n *     return () => { // cleanup }\n *   }\n * }\n *\n * const source = new MySource(...)\n * const [value] = use(source)\n * ```\n *\n * @typeParam T - The value type\n * @typeParam P - The params type for subscribe/send\n * @typeParam Actions - Tuple of additional actions returned by use() after the value\n */\nexport abstract class Useable<T, P = void, Actions extends unknown[] = []> {\n  /**\n   * Unique identifier for this Useable type\n   * Used internally by use() for type checking\n   */\n  readonly _useableTag = true as const\n\n  /**\n   * Get the initial/current value synchronously\n   * Called when use() first accesses this source\n   */\n  abstract getInitial(params?: P): T\n\n  /**\n   * Subscribe to value changes\n   * Called by use() to receive updates\n   *\n   * @param params - Optional parameters for the subscription\n   * @param callback - Function called when value changes\n   * @returns Cleanup function to unsubscribe\n   */\n  abstract subscribe(\n    params: P | undefined,\n    callback: (value: T) => void\n  ): () => void\n\n  /**\n   * Get additional actions to include in the use() tuple\n   * Override this to return [action1, action2, ...] that will be appended to [value, ...]\n   *\n   * @example\n   * ```tsx\n   * class SendableStream extends Useable<T, P> {\n   *   getActions() {\n   *     return [this.send.bind(this)]\n   *   }\n   * }\n   *\n   * const [value, send] = use(stream)\n   * ```\n   */\n  getActions(): Actions | undefined {\n    return undefined\n  }\n}\n\n/**\n * Type guard to check if a value is a Useable instance\n */\nexport function isUseable(value: unknown): value is Useable<unknown, unknown> {\n  return (\n    value !== null &&\n    typeof value === 'object' &&\n    '_useableTag' in value &&\n    (value as Record<string, unknown>)._useableTag === true\n  )\n}\n","import { Useable } from './useable'\n\nconst contextMap = new Map<symbol, unknown>()\n\n// Reset context state - used when app is re-rendered from root\nexport function resetContext(): void {\n  contextMap.clear()\n}\n\n/**\n * Context for passing data through the component tree\n *\n * @example\n * ```tsx\n * const ThemeContext = new Context('light')\n *\n * function App() {\n *   return (\n *     <ThemeContext.Provider value=\"dark\">\n *       <Child />\n *     </ThemeContext.Provider>\n *   )\n * }\n *\n * function Child() {\n *   const [theme] = use(ThemeContext)\n *   return <div>{theme}</div>\n * }\n * ```\n */\nexport class Context<T> extends Useable<T> {\n  readonly id: symbol\n  readonly defaultValue: T\n  readonly Provider: (props: { value: T; children: unknown }) => unknown\n\n  constructor(defaultValue: T) {\n    super()\n    this.id = Symbol('context')\n    this.defaultValue = defaultValue\n    this.Provider = (props: { value: T; children: unknown }) => props.children\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    ;(this.Provider as any)._contextId = this.id\n  }\n\n  /**\n   * Get current context value or default\n   */\n  getInitial(): T {\n    return contextMap.has(this.id) ? contextMap.get(this.id) as T : this.defaultValue\n  }\n\n  /**\n   * Context doesn't have traditional subscriptions\n   * Reactivity is handled by component re-rendering\n   */\n  subscribe(_params: undefined, _callback: (value: T) => void): () => void {\n    // Context changes trigger re-render through Provider mechanism\n    // No additional subscription needed\n    return () => { }\n  }\n}\n\n// Internal: get context value (used by use.ts)\nexport function getContextValue<T>(ctx: Context<T>): T {\n  return ctx.getInitial()\n}\n\n// Internal helpers for renderer\nexport function pushContext(id: symbol, value: unknown): unknown {\n  const prev = contextMap.get(id)\n  contextMap.set(id, value)\n  return prev\n}\n\n\nexport function popContext(id: symbol, prevValue: unknown): void {\n  if (prevValue === undefined) {\n    contextMap.delete(id)\n  } else {\n    contextMap.set(id, prevValue)\n  }\n}\n\nexport function snapshotContext(): Map<symbol, unknown> {\n  return new Map(contextMap)\n}\n\nexport function runWithContext<R>(snapshot: Map<symbol, unknown>, fn: () => R): R {\n  // 1. Save current context\n  const prevContext = new Map(contextMap)\n\n  // 2. Apply snapshot\n  contextMap.clear()\n  snapshot.forEach((value, key) => contextMap.set(key, value))\n\n  try {\n    return fn()\n  } finally {\n    // 3. Restore previous context\n    contextMap.clear()\n    prevContext.forEach((value, key) => contextMap.set(key, value))\n  }\n}\n"]}