{"version":3,"file":"routerStores.cjs","names":[],"sources":["../../src/routerStores.ts"],"sourcesContent":["import * as Solid from 'solid-js'\nimport {\n  createNonReactiveMutableStore,\n  createNonReactiveReadonlyStore,\n} from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport type {\n  AnyRoute,\n  GetStoreConfig,\n  RouterReadableStore,\n  RouterStores,\n  RouterWritableStore,\n} from '@tanstack/router-core'\n\ndeclare module '@tanstack/router-core' {\n  export interface RouterStores<in out TRouteTree extends AnyRoute> {\n    /** Maps each active routeId to the matchId of its child in the match tree. */\n    childMatchIdByRouteId: RouterReadableStore<Record<string, string>>\n    /** Maps each pending routeId to true for quick lookup. */\n    pendingRouteIds: RouterReadableStore<Record<string, boolean>>\n  }\n}\n\nfunction initRouterStores(\n  stores: RouterStores<AnyRoute>,\n  createReadonlyStore: <TValue>(\n    read: () => TValue,\n  ) => RouterReadableStore<TValue>,\n) {\n  stores.childMatchIdByRouteId = createReadonlyStore(() => {\n    const ids = stores.matchesId.state\n    const obj: Record<string, string> = {}\n    for (let i = 0; i < ids.length - 1; i++) {\n      const parentStore = stores.activeMatchStoresById.get(ids[i]!)\n      if (parentStore?.routeId) {\n        obj[parentStore.routeId] = ids[i + 1]!\n      }\n    }\n    return obj\n  })\n\n  stores.pendingRouteIds = createReadonlyStore(() => {\n    const ids = stores.pendingMatchesId.state\n    const obj: Record<string, boolean> = {}\n    for (const id of ids) {\n      const store = stores.pendingMatchStoresById.get(id)\n      if (store?.routeId) {\n        obj[store.routeId] = true\n      }\n    }\n    return obj\n  })\n}\n\nfunction createSolidMutableStore<TValue>(\n  initialValue: TValue,\n): RouterWritableStore<TValue> {\n  const [signal, setSignal] = Solid.createSignal(initialValue as any)\n\n  return {\n    get state() {\n      return signal()\n    },\n    setState: setSignal,\n  }\n}\n\nlet finalizationRegistry: FinalizationRegistry<() => void> | null = null\nif (typeof globalThis !== 'undefined' && 'FinalizationRegistry' in globalThis) {\n  finalizationRegistry = new FinalizationRegistry((cb) => cb())\n}\n\nfunction createSolidReadonlyStore<TValue>(\n  read: () => TValue,\n): RouterReadableStore<TValue> {\n  let dispose!: () => void\n  const memo = Solid.createRoot((d) => {\n    dispose = d\n    return Solid.createMemo(read)\n  })\n  const store = {\n    get state() {\n      return memo()\n    },\n  }\n  finalizationRegistry?.register(store, dispose)\n  return store\n}\n\nexport const getStoreFactory: GetStoreConfig = (opts) => {\n  if (isServer ?? opts.isServer) {\n    return {\n      createMutableStore: createNonReactiveMutableStore,\n      createReadonlyStore: createNonReactiveReadonlyStore,\n      batch: (fn) => fn(),\n      init: (stores) =>\n        initRouterStores(stores, createNonReactiveReadonlyStore),\n    }\n  }\n\n  let depth = 0\n\n  return {\n    createMutableStore: createSolidMutableStore,\n    createReadonlyStore: createSolidReadonlyStore,\n\n    batch: (fn) => {\n      depth++\n      fn()\n      depth--\n      if (depth === 0) {\n        try {\n          Solid.flush()\n        } catch {}\n      }\n    },\n    init: (stores) => initRouterStores(stores, createSolidReadonlyStore),\n  }\n}\n"],"mappings":";;;;;;AAuBA,SAAS,iBACP,QACA,qBAGA;AACA,QAAO,wBAAwB,0BAA0B;EACvD,MAAM,MAAM,OAAO,UAAU;EAC7B,MAAM,MAA8B,EAAE;AACtC,OAAK,IAAI,IAAI,GAAG,IAAI,IAAI,SAAS,GAAG,KAAK;GACvC,MAAM,cAAc,OAAO,sBAAsB,IAAI,IAAI,GAAI;AAC7D,OAAI,aAAa,QACf,KAAI,YAAY,WAAW,IAAI,IAAI;;AAGvC,SAAO;GACP;AAEF,QAAO,kBAAkB,0BAA0B;EACjD,MAAM,MAAM,OAAO,iBAAiB;EACpC,MAAM,MAA+B,EAAE;AACvC,OAAK,MAAM,MAAM,KAAK;GACpB,MAAM,QAAQ,OAAO,uBAAuB,IAAI,GAAG;AACnD,OAAI,OAAO,QACT,KAAI,MAAM,WAAW;;AAGzB,SAAO;GACP;;AAGJ,SAAS,wBACP,cAC6B;CAC7B,MAAM,CAAC,QAAQ,aAAa,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