@tanstack/react-router
Version:
Modern and scalable routing for React applications
1 lines • 3.3 kB
Source Map (JSON)
{"version":3,"file":"matchContext.cjs","names":["_isRouteDeparting","React"],"sources":["../../src/matchContext.tsx"],"sourcesContent":["'use client'\n\nimport * as React from 'react'\nimport { _isRouteDeparting } from '@tanstack/router-core'\nimport type { AnyRoute, AnyRouter, ParsedLocation } from '@tanstack/router-core'\n\ndeclare module '@tanstack/router-core' {\n // Declaration merging requires the original type parameters.\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface RouterStores<in out TRouteTree extends AnyRoute> {\n /**\n * The link scope of each route id that rendered links, keyed `undefined`\n * for links outside every match (route ids start with `/` or are the\n * root id). A scope without links holds no location.\n */\n _linkScopes?: Record<string, LinkScope>\n }\n}\n\n/**\n * The location source shared by the links of one route. It holds one location\n * subscription for all of them while any is subscribed, and skips a\n * publication whose navigation removes the route: those links keep the\n * location their route still presents until it unmounts or a newer\n * publication keeps it. Without subscribers it holds nothing and reads the\n * live location.\n */\nexport type LinkScope = [\n getSource: () => ParsedLocation,\n subscribe: (listener: () => void) => () => void,\n]\n\nexport function createLinkScope(\n router: AnyRouter,\n routeId?: string,\n): LinkScope {\n // Stores are created once per router.\n const store = router.stores.location\n const listeners = new Set<() => void>()\n let source: ParsedLocation | undefined\n let unsubscribe: () => void\n // Defined outside `subscribe` so the store's subscription does not share a\n // closure with, and retain, the first listener after it unsubscribes.\n const update = (location: ParsedLocation) => {\n if (!_isRouteDeparting(router, routeId, location)) {\n source = location\n listeners.forEach((listener) => listener())\n }\n }\n return [\n () => source ?? store.get(),\n (listener) => {\n if (!listeners.size) {\n source = store.get()\n unsubscribe = store.subscribe(update).unsubscribe\n }\n listeners.add(listener)\n return () => {\n listeners.delete(listener)\n if (!listeners.size) {\n unsubscribe()\n source = undefined\n }\n }\n },\n ]\n}\n\nexport const matchContext = React.createContext<string | undefined>(undefined)\n\n// N.B. this only exists so we can conditionally call useContext on it when we are not interested in the nearest match\nexport const dummyMatchContext = React.createContext<string | undefined>(\n undefined,\n)\n"],"mappings":";;;;;;AAgCA,SAAgB,gBACd,QACA,SACW;CAEX,MAAM,QAAQ,OAAO,OAAO;CAC5B,MAAM,4BAAY,IAAI,IAAgB;CACtC,IAAI;CACJ,IAAI;CAGJ,MAAM,UAAU,aAA6B;EAC3C,IAAI,EAAA,GAACA,sBAAAA,kBAAAA,CAAkB,QAAQ,SAAS,QAAQ,GAAG;GACjD,SAAS;GACT,UAAU,SAAS,aAAa,SAAS,CAAC;EAC5C;CACF;CACA,OAAO,OACC,UAAU,MAAM,IAAI,IACzB,aAAa;EACZ,IAAI,CAAC,UAAU,MAAM;GACnB,SAAS,MAAM,IAAI;GACnB,cAAc,MAAM,UAAU,MAAM,CAAC,CAAC;EACxC;EACA,UAAU,IAAI,QAAQ;EACtB,aAAa;GACX,UAAU,OAAO,QAAQ;GACzB,IAAI,CAAC,UAAU,MAAM;IACnB,YAAY;IACZ,SAAS,KAAA;GACX;EACF;CACF,CACF;AACF;AAEA,IAAa,eAAeC,MAAM,cAAkC,KAAA,CAAS;AAG7E,IAAa,oBAAoBA,MAAM,cACrC,KAAA,CACF"}