1 | {"version":3,"names":["CommonActions","NOT_INITIALIZED_ERROR","createNavigationContainerRef","methods","Object","keys","listeners","removeListener","event","callback","filter","cb","current","ref","value","entries","forEach","callbacks","addListener","isReady","reduce","acc","name","args","push","console","error"],"sources":["createNavigationContainerRef.tsx"],"sourcesContent":["import { CommonActions } from '@react-navigation/routers';\n\nimport type {\n NavigationContainerEventMap,\n NavigationContainerRef,\n NavigationContainerRefWithCurrent,\n} from './types';\n\nexport const NOT_INITIALIZED_ERROR =\n \"The 'navigation' object hasn't been initialized yet. This might happen if you don't have a navigator mounted, or if the navigator hasn't finished mounting. See https://reactnavigation.org/docs/navigating-without-navigation-prop#handling-initialization for more details.\";\n\nexport default function createNavigationContainerRef<\n ParamList extends {} = ReactNavigation.RootParamList\n>(): NavigationContainerRefWithCurrent<ParamList> {\n const methods = [\n ...Object.keys(CommonActions),\n 'addListener',\n 'removeListener',\n 'resetRoot',\n 'dispatch',\n 'isFocused',\n 'canGoBack',\n 'getRootState',\n 'getState',\n 'getParent',\n 'getCurrentRoute',\n 'getCurrentOptions',\n ] as const;\n\n const listeners: Record<string, ((...args: any[]) => void)[]> = {};\n\n const removeListener = (\n event: string,\n callback: (...args: any[]) => void\n ) => {\n if (listeners[event]) {\n listeners[event] = listeners[event].filter((cb) => cb !== callback);\n }\n };\n\n let current: NavigationContainerRef<ParamList> | null = null;\n\n const ref: NavigationContainerRefWithCurrent<ParamList> = {\n get current() {\n return current;\n },\n set current(value: NavigationContainerRef<ParamList> | null) {\n current = value;\n\n if (value != null) {\n Object.entries(listeners).forEach(([event, callbacks]) => {\n callbacks.forEach((callback) => {\n value.addListener(\n event as keyof NavigationContainerEventMap,\n callback\n );\n });\n });\n }\n },\n isReady: () => {\n if (current == null) {\n return false;\n }\n\n return current.isReady();\n },\n ...methods.reduce<any>((acc, name) => {\n acc[name] = (...args: any[]) => {\n if (current == null) {\n switch (name) {\n case 'addListener': {\n const [event, callback] = args;\n\n listeners[event] = listeners[event] || [];\n listeners[event].push(callback);\n\n return () => removeListener(event, callback);\n }\n case 'removeListener': {\n const [event, callback] = args;\n\n removeListener(event, callback);\n break;\n }\n default:\n console.error(NOT_INITIALIZED_ERROR);\n }\n } else {\n // @ts-expect-error: this is ok\n return current[name](...args);\n }\n };\n return acc;\n }, {}),\n };\n\n return ref;\n}\n"],"mappings":"AAAA,SAASA,aAAT,QAA8B,2BAA9B;AAQA,OAAO,MAAMC,qBAAqB,GAChC,+QADK;AAGP,eAAe,SAASC,4BAAT,GAEmC;EAChD,MAAMC,OAAO,GAAG,CACd,GAAGC,MAAM,CAACC,IAAP,CAAYL,aAAZ,CADW,EAEd,aAFc,EAGd,gBAHc,EAId,WAJc,EAKd,UALc,EAMd,WANc,EAOd,WAPc,EAQd,cARc,EASd,UATc,EAUd,WAVc,EAWd,iBAXc,EAYd,mBAZc,CAAhB;EAeA,MAAMM,SAAuD,GAAG,EAAhE;;EAEA,MAAMC,cAAc,GAAG,CACrBC,KADqB,EAErBC,QAFqB,KAGlB;IACH,IAAIH,SAAS,CAACE,KAAD,CAAb,EAAsB;MACpBF,SAAS,CAACE,KAAD,CAAT,GAAmBF,SAAS,CAACE,KAAD,CAAT,CAAiBE,MAAjB,CAAyBC,EAAD,IAAQA,EAAE,KAAKF,QAAvC,CAAnB;IACD;EACF,CAPD;;EASA,IAAIG,OAAiD,GAAG,IAAxD;EAEA,MAAMC,GAAiD,GAAG;IACxD,IAAID,OAAJ,GAAc;MACZ,OAAOA,OAAP;IACD,CAHuD;;IAIxD,IAAIA,OAAJ,CAAYE,KAAZ,EAA6D;MAC3DF,OAAO,GAAGE,KAAV;;MAEA,IAAIA,KAAK,IAAI,IAAb,EAAmB;QACjBV,MAAM,CAACW,OAAP,CAAeT,SAAf,EAA0BU,OAA1B,CAAkC,QAAwB;UAAA,IAAvB,CAACR,KAAD,EAAQS,SAAR,CAAuB;UACxDA,SAAS,CAACD,OAAV,CAAmBP,QAAD,IAAc;YAC9BK,KAAK,CAACI,WAAN,CACEV,KADF,E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