{"version":3,"file":"createRouter.mjs","names":[],"sources":["../../../src/domain/reactRouter/createRouter.ts"],"sourcesContent":["import { startReactRouterView } from './startReactRouterView'\nimport type { AnyCreateRouter, AnyRouterSubscriberOpts } from './types'\n\nexport function wrapCreateRouter<CreateRouter extends AnyCreateRouter<any>>(\n  originalCreateRouter: CreateRouter\n): CreateRouter {\n  return ((routes, options) => {\n    const router = originalCreateRouter(routes, options)\n    let location = router.state.location.pathname\n\n    // We subscribe before RouterProvider, so initial loader-error notifications\n    // reach us instead of it — either via react-router ≥7.15.1's one-shot\n    // buffer replay (sync) or as a regular broadcast before RouterProvider's\n    // deferred useLayoutEffect (async). Capture any such error and replay it\n    // to the next subscriber so RouterProvider's onError still fires.\n    // The next-subscriber patch is installed eagerly so a single flag\n    // (hasNextSubscriberAttached) reliably tells us whether an incoming error\n    // is pre- or post-RouterProvider — only pre-RouterProvider errors are\n    // captured, avoiding duplicate onError on later resubscribes.\n    let pendingInitialError: { state: typeof router.state; opts: AnyRouterSubscriberOpts } | null = null\n    let hasNextSubscriberAttached = false\n\n    router.subscribe((routerState, opts) => {\n      if (opts?.newErrors && !hasNextSubscriberAttached) {\n        pendingInitialError = { state: routerState, opts }\n      }\n      const newPathname = routerState.location.pathname\n      if (location !== newPathname) {\n        startReactRouterView(routerState.matches)\n        location = newPathname\n      }\n    })\n\n    const originalSubscribe = router.subscribe.bind(router)\n    router.subscribe = (fn) => {\n      hasNextSubscriberAttached = true\n      router.subscribe = originalSubscribe\n      const unsubscribe = originalSubscribe(fn)\n      if (pendingInitialError) {\n        fn(pendingInitialError.state, pendingInitialError.opts)\n        pendingInitialError = null\n      }\n      return unsubscribe\n    }\n\n    startReactRouterView(router.state.matches)\n    return router\n  }) as CreateRouter\n}\n"],"mappings":";;AAGA,SAAgB,iBACd,sBACc;CACd,SAAS,QAAQ,YAAY;EAC3B,MAAM,SAAS,qBAAqB,QAAQ,OAAO;EACnD,IAAI,WAAW,OAAO,MAAM,SAAS;EAWrC,IAAI,sBAA4F;EAChG,IAAI,4BAA4B;EAEhC,OAAO,WAAW,aAAa,SAAS;GACtC,IAAI,MAAM,aAAa,CAAC,2BACtB,sBAAsB;IAAE,OAAO;IAAa;GAAK;GAEnD,MAAM,cAAc,YAAY,SAAS;GACzC,IAAI,aAAa,aAAa;IAC5B,qBAAqB,YAAY,OAAO;IACxC,WAAW;GACb;EACF,CAAC;EAED,MAAM,oBAAoB,OAAO,UAAU,KAAK,MAAM;EACtD,OAAO,aAAa,OAAO;GACzB,4BAA4B;GAC5B,OAAO,YAAY;GACnB,MAAM,cAAc,kBAAkB,EAAE;GACxC,IAAI,qBAAqB;IACvB,GAAG,oBAAoB,OAAO,oBAAoB,IAAI;IACtD,sBAAsB;GACxB;GACA,OAAO;EACT;EAEA,qBAAqB,OAAO,MAAM,OAAO;EACzC,OAAO;CACT;AACF"}