UNPKG

7 kBSource Map (JSON)View Raw
1{"version":3,"names":["React","useEventEmitter","listen","listenRef","useRef","useEffect","current","listeners","Object","create","useCallback","target","removeListener","type","callback","callbacks","undefined","index","indexOf","splice","addListener","push","removed","emit","data","canPreventDefault","items","slice","concat","keys","map","t","filter","cb","i","self","lastIndexOf","event","defineProperty","enumerable","get","defaultPrevented","defineProperties","preventDefault","value","forEach","useMemo"],"sources":["useEventEmitter.tsx"],"sourcesContent":["import * as React from 'react';\n\nimport type { EventArg, EventConsumer, EventEmitter } from './types';\n\nexport type NavigationEventEmitter<T extends Record<string, any>> =\n EventEmitter<T> & {\n create: (target: string) => EventConsumer<T>;\n };\n\ntype Listeners = ((e: any) => void)[];\n\n/**\n * Hook to manage the event system used by the navigator to notify screens of various events.\n */\nexport default function useEventEmitter<T extends Record<string, any>>(\n listen?: (e: any) => void\n): NavigationEventEmitter<T> {\n const listenRef = React.useRef(listen);\n\n React.useEffect(() => {\n listenRef.current = listen;\n });\n\n const listeners = React.useRef<Record<string, Record<string, Listeners>>>(\n Object.create(null)\n );\n\n const create = React.useCallback((target: string) => {\n const removeListener = (type: string, callback: (data: any) => void) => {\n const callbacks = listeners.current[type]\n ? listeners.current[type][target]\n : undefined;\n\n if (!callbacks) {\n return;\n }\n\n const index = callbacks.indexOf(callback);\n\n if (index > -1) {\n callbacks.splice(index, 1);\n }\n };\n\n const addListener = (type: string, callback: (data: any) => void) => {\n listeners.current[type] = listeners.current[type] || {};\n listeners.current[type][target] = listeners.current[type][target] || [];\n listeners.current[type][target].push(callback);\n\n let removed = false;\n return () => {\n // Prevent removing other listeners when unsubscribing same listener multiple times\n if (!removed) {\n removed = true;\n removeListener(type, callback);\n }\n };\n };\n\n return {\n addListener,\n removeListener,\n };\n }, []);\n\n const emit = React.useCallback(\n ({\n type,\n data,\n target,\n canPreventDefault,\n }: {\n type: string;\n data?: any;\n target?: string;\n canPreventDefault?: boolean;\n }) => {\n const items = listeners.current[type] || {};\n\n // Copy the current list of callbacks in case they are mutated during execution\n const callbacks =\n target !== undefined\n ? items[target]?.slice()\n : ([] as Listeners)\n .concat(...Object.keys(items).map((t) => items[t]))\n .filter((cb, i, self) => self.lastIndexOf(cb) === i);\n\n const event: EventArg<any, any, any> = {\n get type() {\n return type;\n },\n };\n\n if (target !== undefined) {\n Object.defineProperty(event, 'target', {\n enumerable: true,\n get() {\n return target;\n },\n });\n }\n\n if (data !== undefined) {\n Object.defineProperty(event, 'data', {\n enumerable: true,\n get() {\n return data;\n },\n });\n }\n\n if (canPreventDefault) {\n let defaultPrevented = false;\n\n Object.defineProperties(event, {\n defaultPrevented: {\n enumerable: true,\n get() {\n return defaultPrevented;\n },\n },\n preventDefault: {\n enumerable: true,\n value() {\n defaultPrevented = true;\n },\n },\n });\n }\n\n listenRef.current?.(event);\n\n callbacks?.forEach((cb) => cb(event));\n\n return event as any;\n },\n []\n );\n\n return React.useMemo(() => ({ create, emit }), [create, emit]);\n}\n"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAWA;AACA;AACA;AACA,eAAe,SAASC,eAAT,CACbC,MADa,EAEc;EAC3B,MAAMC,SAAS,GAAGH,KAAK,CAACI,MAAN,CAAaF,MAAb,CAAlB;EAEAF,KAAK,CAACK,SAAN,CAAgB,MAAM;IACpBF,SAAS,CAACG,OAAV,GAAoBJ,MAApB;EACD,CAFD;EAIA,MAAMK,SAAS,GAAGP,KAAK,CAACI,MAAN,CAChBI,MAAM,CAACC,MAAP,CAAc,IAAd,CADgB,CAAlB;EAIA,MAAMA,MAAM,GAAGT,KAAK,CAACU,WAAN,CAAmBC,MAAD,IAAoB;IACnD,MAAMC,cAAc,GAAG,CAACC,IAAD,EAAeC,QAAf,KAAiD;MACtE,MAAMC,SAAS,GAAGR,SAAS,CAACD,OAAV,CAAkBO,IAAlB,IACdN,SAAS,CAACD,OAAV,CAAkBO,IAAlB,EAAwBF,MAAxB,CADc,GAEdK,SAFJ;;MAIA,IAAI,CAACD,SAAL,EAA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