MCPcopy Create free account
hub / github.com/Effect-TS/effect / build

Function build

packages/effect/src/ui/Router.ts:2002–2847  ·  view source on GitHub ↗
(
  options: Options
)

Source from the content-addressed store, hash-verified

2000 Layer.effectContext(Effect.suspend(() => build(options)))
2001
2002const build = (
2003 options: Options
2004): Effect.Effect<Context.Context<Services>, never, Scope.Scope> =>
2005 Effect.gen(function*() {
2006 const { history, routes } = options
2007 const restore = options.restore !== false
2008 // With the `#` base the URL is the fragment, so there's no in-page hash.
2009 const split = (href: string): readonly [string, string] => routes.base() === "#" ? [href, ""] : splitHash(href)
2010 const start = history.current()
2011 const [startUrl, startHash] = split(start.url)
2012 const resolved = routes.resolve(startUrl)
2013 if (Option.isNone(resolved)) {
2014 return yield* Effect.die(
2015 new Error(
2016 `The Router's routes don't parse the initial URL "${start.url}"`
2017 )
2018 )
2019 }
2020 // The problems sink of the first mounted root, or the one where the Router is built.
2021 const fallbackReport = yield* queryCore.Problems
2022 let index = readState(start.state)?.index ?? 0
2023 const initialUrl = canonical(routes, resolved.value, startUrl)
2024 if (
2025 resolved.value.redirected || readState(start.state) === undefined ||
2026 initialUrl !== startUrl
2027 ) {
2028 history.replace(initialUrl + startHash, writeState({ index }))
2029 }
2030 let current: Location = {
2031 route: resolved.value.route,
2032 url: initialUrl,
2033 hash: startHash,
2034 action: "initial",
2035 seq: 0,
2036 index
2037 }
2038 /** The held blocks, oldest first. */
2039 const holds: Array<{
2040 readonly key: string
2041 readonly onAttempt: ((attempt: Attempt) => void) | undefined
2042 }> = []
2043 /** Reports `attempt` to the newest block with `onAttempt`. */
2044 const stopped = (attempt: Attempt): Outcome => {
2045 for (let i = holds.length - 1; i >= 0; i--) {
2046 const onAttempt = holds[i]!.onAttempt
2047 if (onAttempt !== undefined) {
2048 onAttempt(attempt)
2049 break
2050 }
2051 }
2052 return "blocked"
2053 }
2054 const snapshots = new Map<number, Record<string, unknown>>()
2055 const roots = new Set<services.RootHandle>()
2056 let restoring: Record<string, unknown> | undefined = restore
2057 ? readState(start.state)?.snapshot
2058 : undefined
2059 restoring &&= { ...restoring }

Callers 1

makeFunction · 0.70

Calls 15

canonicalFunction · 0.90
readStateFunction · 0.85
writeStateFunction · 0.85
dropChecksFunction · 0.85
forgetRestoringFunction · 0.85
primaryFunction · 0.85
eachDeclarerFunction · 0.85
routerContextFunction · 0.85
currentMethod · 0.80
resolveMethod · 0.80
contextMethod · 0.80
addFinalizerMethod · 0.80

Tested by

no test coverage detected