@react-navigation/core 6.2.1 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/useChildListeners.js +6 -3
- package/lib/commonjs/useChildListeners.js.map +1 -1
- package/lib/commonjs/useEventEmitter.js +12 -2
- package/lib/commonjs/useEventEmitter.js.map +1 -1
- package/lib/commonjs/useKeyedChildListeners.js +2 -0
- package/lib/commonjs/useKeyedChildListeners.js.map +1 -1
- package/lib/module/useChildListeners.js +6 -3
- package/lib/module/useChildListeners.js.map +1 -1
- package/lib/module/useEventEmitter.js +12 -2
- package/lib/module/useEventEmitter.js.map +1 -1
- package/lib/module/useKeyedChildListeners.js +2 -0
- package/lib/module/useKeyedChildListeners.js.map +1 -1
- package/lib/typescript/src/NavigationBuilderContext.d.ts +1 -1
- package/package.json +7 -8
- package/src/useChildListeners.tsx +5 -3
- package/src/useEventEmitter.tsx +11 -2
- package/src/useKeyedChildListeners.tsx +2 -0
|
@@ -22,12 +22,15 @@ function useChildListeners() {
|
|
|
22
22
|
focus: []
|
|
23
23
|
});
|
|
24
24
|
const addListener = React.useCallback((type, listener) => {
|
|
25
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
26
25
|
listeners[type].push(listener);
|
|
26
|
+
let removed = false;
|
|
27
27
|
return () => {
|
|
28
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
29
28
|
const index = listeners[type].indexOf(listener);
|
|
30
|
-
|
|
29
|
+
|
|
30
|
+
if (!removed && index > -1) {
|
|
31
|
+
removed = true;
|
|
32
|
+
listeners[type].splice(index, 1);
|
|
33
|
+
}
|
|
31
34
|
};
|
|
32
35
|
}, [listeners]);
|
|
33
36
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useChildListeners.tsx"],"names":["useChildListeners","current","listeners","React","useRef","action","focus","addListener","useCallback","type","listener","push","index","indexOf","splice"],"mappings":";;;;;;;AAAA;;;;;;AAIA;AACA;AACA;AACe,SAASA,iBAAT,GAA6B;AAC1C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAAyBC,KAAK,CAACC,MAAN,CAE5B;AACDC,IAAAA,MAAM,EAAE,EADP;AAEDC,IAAAA,KAAK,EAAE;AAFN,GAF4B,CAA/B;AAOA,QAAMC,WAAW,GAAGJ,KAAK,CAACK,WAAN,CAClB,CAA8BC,IAA9B,EAAuCC,QAAvC,KAAoE;
|
|
1
|
+
{"version":3,"sources":["useChildListeners.tsx"],"names":["useChildListeners","current","listeners","React","useRef","action","focus","addListener","useCallback","type","listener","push","removed","index","indexOf","splice"],"mappings":";;;;;;;AAAA;;;;;;AAIA;AACA;AACA;AACe,SAASA,iBAAT,GAA6B;AAC1C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAAyBC,KAAK,CAACC,MAAN,CAE5B;AACDC,IAAAA,MAAM,EAAE,EADP;AAEDC,IAAAA,KAAK,EAAE;AAFN,GAF4B,CAA/B;AAOA,QAAMC,WAAW,GAAGJ,KAAK,CAACK,WAAN,CAClB,CAA8BC,IAA9B,EAAuCC,QAAvC,KAAoE;AAClER,IAAAA,SAAS,CAACO,IAAD,CAAT,CAAgBE,IAAhB,CAAqBD,QAArB;AAEA,QAAIE,OAAO,GAAG,KAAd;AACA,WAAO,MAAM;AACX,YAAMC,KAAK,GAAGX,SAAS,CAACO,IAAD,CAAT,CAAgBK,OAAhB,CAAwBJ,QAAxB,CAAd;;AAEA,UAAI,CAACE,OAAD,IAAYC,KAAK,GAAG,CAAC,CAAzB,EAA4B;AAC1BD,QAAAA,OAAO,GAAG,IAAV;AACAV,QAAAA,SAAS,CAACO,IAAD,CAAT,CAAgBM,MAAhB,CAAuBF,KAAvB,EAA8B,CAA9B;AACD;AACF,KAPD;AAQD,GAbiB,EAclB,CAACX,SAAD,CAdkB,CAApB;AAiBA,SAAO;AACLA,IAAAA,SADK;AAELK,IAAAA;AAFK,GAAP;AAID","sourcesContent":["import * as React from 'react';\n\nimport type { ListenerMap } from './NavigationBuilderContext';\n\n/**\n * Hook which lets child navigators add action listeners.\n */\nexport default function useChildListeners() {\n const { current: listeners } = React.useRef<{\n [K in keyof ListenerMap]: ListenerMap[K][];\n }>({\n action: [],\n focus: [],\n });\n\n const addListener = React.useCallback(\n <T extends keyof ListenerMap>(type: T, listener: ListenerMap[T]) => {\n listeners[type].push(listener);\n\n let removed = false;\n return () => {\n const index = listeners[type].indexOf(listener);\n\n if (!removed && index > -1) {\n removed = true;\n listeners[type].splice(index, 1);\n }\n };\n },\n [listeners]\n );\n\n return {\n listeners,\n addListener,\n };\n}\n"]}
|
|
@@ -29,14 +29,24 @@ function useEventEmitter(listen) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
const index = callbacks.indexOf(callback);
|
|
32
|
-
|
|
32
|
+
|
|
33
|
+
if (index > -1) {
|
|
34
|
+
callbacks.splice(index, 1);
|
|
35
|
+
}
|
|
33
36
|
};
|
|
34
37
|
|
|
35
38
|
const addListener = (type, callback) => {
|
|
36
39
|
listeners.current[type] = listeners.current[type] || {};
|
|
37
40
|
listeners.current[type][target] = listeners.current[type][target] || [];
|
|
38
41
|
listeners.current[type][target].push(callback);
|
|
39
|
-
|
|
42
|
+
let removed = false;
|
|
43
|
+
return () => {
|
|
44
|
+
// Prevent removing other listeners when unsubscribing same listener multiple times
|
|
45
|
+
if (!removed) {
|
|
46
|
+
removed = true;
|
|
47
|
+
removeListener(type, callback);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
40
50
|
};
|
|
41
51
|
|
|
42
52
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useEventEmitter.tsx"],"names":["useEventEmitter","listen","listenRef","React","useRef","useEffect","current","listeners","create","useCallback","target","removeListener","type","callback","callbacks","undefined","index","indexOf","splice","addListener","push","emit","data","canPreventDefault","items","slice","concat","Object","keys","map","t","filter","cb","i","self","lastIndexOf","event","defineProperty","enumerable","get","defaultPrevented","defineProperties","preventDefault","value","forEach","useMemo"],"mappings":";;;;;;;AAAA;;;;;;AAWA;AACA;AACA;AACe,SAASA,eAAT,CACbC,MADa,EAEc;AAC3B,QAAMC,SAAS,GAAGC,KAAK,CAACC,MAAN,CAAaH,MAAb,CAAlB;AAEAE,EAAAA,KAAK,CAACE,SAAN,CAAgB,MAAM;AACpBH,IAAAA,SAAS,CAACI,OAAV,GAAoBL,MAApB;AACD,GAFD;AAIA,QAAMM,SAAS,GAAGJ,KAAK,CAACC,MAAN,CAAwD,EAAxD,CAAlB;AAEA,QAAMI,MAAM,GAAGL,KAAK,CAACM,WAAN,CAAmBC,MAAD,IAAoB;AACnD,UAAMC,cAAc,GAAG,CAACC,IAAD,EAAeC,QAAf,KAAiD;AACtE,YAAMC,SAAS,GAAGP,SAAS,CAACD,OAAV,CAAkBM,IAAlB,IACdL,SAAS,CAACD,OAAV,CAAkBM,IAAlB,EAAwBF,MAAxB,CADc,GAEdK,SAFJ;;AAIA,UAAI,CAACD,SAAL,EAAgB;AACd;AACD;;AAED,YAAME,KAAK,GAAGF,SAAS,CAACG,OAAV,CAAkBJ,QAAlB,CAAd;
|
|
1
|
+
{"version":3,"sources":["useEventEmitter.tsx"],"names":["useEventEmitter","listen","listenRef","React","useRef","useEffect","current","listeners","create","useCallback","target","removeListener","type","callback","callbacks","undefined","index","indexOf","splice","addListener","push","removed","emit","data","canPreventDefault","items","slice","concat","Object","keys","map","t","filter","cb","i","self","lastIndexOf","event","defineProperty","enumerable","get","defaultPrevented","defineProperties","preventDefault","value","forEach","useMemo"],"mappings":";;;;;;;AAAA;;;;;;AAWA;AACA;AACA;AACe,SAASA,eAAT,CACbC,MADa,EAEc;AAC3B,QAAMC,SAAS,GAAGC,KAAK,CAACC,MAAN,CAAaH,MAAb,CAAlB;AAEAE,EAAAA,KAAK,CAACE,SAAN,CAAgB,MAAM;AACpBH,IAAAA,SAAS,CAACI,OAAV,GAAoBL,MAApB;AACD,GAFD;AAIA,QAAMM,SAAS,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C7BI,QAAAA,gBAAgB,EAAE;AAChBF,UAAAA,UAAU,EAAE,IADI;;AAEhBC,UAAAA,GAAG,GAAG;AACJ,mBAAOC,gBAAP;AACD;;AAJe,SADW;AAO7BE,QAAAA,cAAc,EAAE;AACdJ,UAAAA,UAAU,EAAE,IADE;;AAEdK,UAAAA,KAAK,GAAG;AACNH,YAAAA,gBAAgB,GAAG,IAAnB;AACD;;AAJa;AAPa,OAA/B;AAcD;;AAED,0BAAAvC,SAAS,CAACI,OAAV,+EAAAJ,SAAS,EAAWmC,KAAX,CAAT;AAEAvB,IAAAA,SAAS,SAAT,IAAAA,SAAS,WAAT,YAAAA,SAAS,CAAE+B,OAAX,CAAoBZ,EAAD,IAAQA,EAAE,CAACI,KAAD,CAA7B;AAEA,WAAOA,KAAP;AACD,GAtEU,EAuEX,EAvEW,CAAb;AA0EA,SAAOlC,KAAK,CAAC2C,OAAN,CAAc,OAAO;AAAEtC,IAAAA,MAAF;AAAUc,IAAAA;AAAV,GAAP,CAAd,EAAwC,CAACd,MAAD,EAASc,IAAT,CAAxC,CAAP;AACD","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\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"]}
|
|
@@ -22,8 +22,10 @@ function useKeyedChildListeners() {
|
|
|
22
22
|
beforeRemove: {}
|
|
23
23
|
});
|
|
24
24
|
const addKeyedListener = React.useCallback((type, key, listener) => {
|
|
25
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
25
26
|
keyedListeners[type][key] = listener;
|
|
26
27
|
return () => {
|
|
28
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
27
29
|
keyedListeners[type][key] = undefined;
|
|
28
30
|
};
|
|
29
31
|
}, [keyedListeners]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useKeyedChildListeners.tsx"],"names":["useKeyedChildListeners","current","keyedListeners","React","useRef","getState","beforeRemove","addKeyedListener","useCallback","type","key","listener","undefined"],"mappings":";;;;;;;AAAA;;;;;;AAIA;AACA;AACA;AACe,SAASA,sBAAT,GAAkC;AAC/C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAA8BC,KAAK,CAACC,MAAN,CAKjC;AACDC,IAAAA,QAAQ,EAAE,EADT;AAEDC,IAAAA,YAAY,EAAE;AAFb,GALiC,CAApC;AAUA,QAAMC,gBAAgB,GAAGJ,KAAK,CAACK,WAAN,CACvB,CACEC,IADF,EAEEC,GAFF,EAGEC,QAHF,KAIK;
|
|
1
|
+
{"version":3,"sources":["useKeyedChildListeners.tsx"],"names":["useKeyedChildListeners","current","keyedListeners","React","useRef","getState","beforeRemove","addKeyedListener","useCallback","type","key","listener","undefined"],"mappings":";;;;;;;AAAA;;;;;;AAIA;AACA;AACA;AACe,SAASA,sBAAT,GAAkC;AAC/C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAA8BC,KAAK,CAACC,MAAN,CAKjC;AACDC,IAAAA,QAAQ,EAAE,EADT;AAEDC,IAAAA,YAAY,EAAE;AAFb,GALiC,CAApC;AAUA,QAAMC,gBAAgB,GAAGJ,KAAK,CAACK,WAAN,CACvB,CACEC,IADF,EAEEC,GAFF,EAGEC,QAHF,KAIK;AACH;AACAT,IAAAA,cAAc,CAACO,IAAD,CAAd,CAAqBC,GAArB,IAA4BC,QAA5B;AAEA,WAAO,MAAM;AACX;AACAT,MAAAA,cAAc,CAACO,IAAD,CAAd,CAAqBC,GAArB,IAA4BE,SAA5B;AACD,KAHD;AAID,GAbsB,EAcvB,CAACV,cAAD,CAduB,CAAzB;AAiBA,SAAO;AACLA,IAAAA,cADK;AAELK,IAAAA;AAFK,GAAP;AAID","sourcesContent":["import * as React from 'react';\n\nimport type { KeyedListenerMap } from './NavigationBuilderContext';\n\n/**\n * Hook which lets child navigators add getters to be called for obtaining rehydrated state.\n */\nexport default function useKeyedChildListeners() {\n const { current: keyedListeners } = React.useRef<{\n [K in keyof KeyedListenerMap]: Record<\n string,\n KeyedListenerMap[K] | undefined\n >;\n }>({\n getState: {},\n beforeRemove: {},\n });\n\n const addKeyedListener = React.useCallback(\n <T extends keyof KeyedListenerMap>(\n type: T,\n key: string,\n listener: KeyedListenerMap[T]\n ) => {\n // @ts-expect-error: according to ref stated above you can use `key` to index type\n keyedListeners[type][key] = listener;\n\n return () => {\n // @ts-expect-error: according to ref stated above you can use `key` to index type\n keyedListeners[type][key] = undefined;\n };\n },\n [keyedListeners]\n );\n\n return {\n keyedListeners,\n addKeyedListener,\n };\n}\n"]}
|
|
@@ -11,12 +11,15 @@ export default function useChildListeners() {
|
|
|
11
11
|
focus: []
|
|
12
12
|
});
|
|
13
13
|
const addListener = React.useCallback((type, listener) => {
|
|
14
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
15
14
|
listeners[type].push(listener);
|
|
15
|
+
let removed = false;
|
|
16
16
|
return () => {
|
|
17
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
18
17
|
const index = listeners[type].indexOf(listener);
|
|
19
|
-
|
|
18
|
+
|
|
19
|
+
if (!removed && index > -1) {
|
|
20
|
+
removed = true;
|
|
21
|
+
listeners[type].splice(index, 1);
|
|
22
|
+
}
|
|
20
23
|
};
|
|
21
24
|
}, [listeners]);
|
|
22
25
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useChildListeners.tsx"],"names":["React","useChildListeners","current","listeners","useRef","action","focus","addListener","useCallback","type","listener","push","index","indexOf","splice"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAIA;AACA;AACA;AACA,eAAe,SAASC,iBAAT,GAA6B;AAC1C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAAyBH,KAAK,CAACI,MAAN,CAE5B;AACDC,IAAAA,MAAM,EAAE,EADP;AAEDC,IAAAA,KAAK,EAAE;AAFN,GAF4B,CAA/B;AAOA,QAAMC,WAAW,GAAGP,KAAK,CAACQ,WAAN,CAClB,CAA8BC,IAA9B,EAAuCC,QAAvC,KAAoE;
|
|
1
|
+
{"version":3,"sources":["useChildListeners.tsx"],"names":["React","useChildListeners","current","listeners","useRef","action","focus","addListener","useCallback","type","listener","push","removed","index","indexOf","splice"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAIA;AACA;AACA;AACA,eAAe,SAASC,iBAAT,GAA6B;AAC1C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAAyBH,KAAK,CAACI,MAAN,CAE5B;AACDC,IAAAA,MAAM,EAAE,EADP;AAEDC,IAAAA,KAAK,EAAE;AAFN,GAF4B,CAA/B;AAOA,QAAMC,WAAW,GAAGP,KAAK,CAACQ,WAAN,CAClB,CAA8BC,IAA9B,EAAuCC,QAAvC,KAAoE;AAClEP,IAAAA,SAAS,CAACM,IAAD,CAAT,CAAgBE,IAAhB,CAAqBD,QAArB;AAEA,QAAIE,OAAO,GAAG,KAAd;AACA,WAAO,MAAM;AACX,YAAMC,KAAK,GAAGV,SAAS,CAACM,IAAD,CAAT,CAAgBK,OAAhB,CAAwBJ,QAAxB,CAAd;;AAEA,UAAI,CAACE,OAAD,IAAYC,KAAK,GAAG,CAAC,CAAzB,EAA4B;AAC1BD,QAAAA,OAAO,GAAG,IAAV;AACAT,QAAAA,SAAS,CAACM,IAAD,CAAT,CAAgBM,MAAhB,CAAuBF,KAAvB,EAA8B,CAA9B;AACD;AACF,KAPD;AAQD,GAbiB,EAclB,CAACV,SAAD,CAdkB,CAApB;AAiBA,SAAO;AACLA,IAAAA,SADK;AAELI,IAAAA;AAFK,GAAP;AAID","sourcesContent":["import * as React from 'react';\n\nimport type { ListenerMap } from './NavigationBuilderContext';\n\n/**\n * Hook which lets child navigators add action listeners.\n */\nexport default function useChildListeners() {\n const { current: listeners } = React.useRef<{\n [K in keyof ListenerMap]: ListenerMap[K][];\n }>({\n action: [],\n focus: [],\n });\n\n const addListener = React.useCallback(\n <T extends keyof ListenerMap>(type: T, listener: ListenerMap[T]) => {\n listeners[type].push(listener);\n\n let removed = false;\n return () => {\n const index = listeners[type].indexOf(listener);\n\n if (!removed && index > -1) {\n removed = true;\n listeners[type].splice(index, 1);\n }\n };\n },\n [listeners]\n );\n\n return {\n listeners,\n addListener,\n };\n}\n"]}
|
|
@@ -18,14 +18,24 @@ export default function useEventEmitter(listen) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
const index = callbacks.indexOf(callback);
|
|
21
|
-
|
|
21
|
+
|
|
22
|
+
if (index > -1) {
|
|
23
|
+
callbacks.splice(index, 1);
|
|
24
|
+
}
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
const addListener = (type, callback) => {
|
|
25
28
|
listeners.current[type] = listeners.current[type] || {};
|
|
26
29
|
listeners.current[type][target] = listeners.current[type][target] || [];
|
|
27
30
|
listeners.current[type][target].push(callback);
|
|
28
|
-
|
|
31
|
+
let removed = false;
|
|
32
|
+
return () => {
|
|
33
|
+
// Prevent removing other listeners when unsubscribing same listener multiple times
|
|
34
|
+
if (!removed) {
|
|
35
|
+
removed = true;
|
|
36
|
+
removeListener(type, callback);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
29
39
|
};
|
|
30
40
|
|
|
31
41
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useEventEmitter.tsx"],"names":["React","useEventEmitter","listen","listenRef","useRef","useEffect","current","listeners","create","useCallback","target","removeListener","type","callback","callbacks","undefined","index","indexOf","splice","addListener","push","emit","data","canPreventDefault","items","slice","concat","Object","keys","map","t","filter","cb","i","self","lastIndexOf","event","defineProperty","enumerable","get","defaultPrevented","defineProperties","preventDefault","value","forEach","useMemo"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAWA;AACA;AACA;AACA,eAAe,SAASC,eAAT,CACbC,MADa,EAEc;AAC3B,QAAMC,SAAS,GAAGH,KAAK,CAACI,MAAN,CAAaF,MAAb,CAAlB;AAEAF,EAAAA,KAAK,CAACK,SAAN,CAAgB,MAAM;AACpBF,IAAAA,SAAS,CAACG,OAAV,GAAoBJ,MAApB;AACD,GAFD;AAIA,QAAMK,SAAS,GAAGP,KAAK,CAACI,MAAN,CAAwD,EAAxD,CAAlB;AAEA,QAAMI,MAAM,GAAGR,KAAK,CAACS,WAAN,CAAmBC,MAAD,IAAoB;AACnD,UAAMC,cAAc,GAAG,CAACC,IAAD,EAAeC,QAAf,KAAiD;AACtE,YAAMC,SAAS,GAAGP,SAAS,CAACD,OAAV,CAAkBM,IAAlB,IACdL,SAAS,CAACD,OAAV,CAAkBM,IAAlB,EAAwBF,MAAxB,CADc,GAEdK,SAFJ;;AAIA,UAAI,CAACD,SAAL,EAAgB;AACd;AACD;;AAED,YAAME,KAAK,GAAGF,SAAS,CAACG,OAAV,CAAkBJ,QAAlB,CAAd;
|
|
1
|
+
{"version":3,"sources":["useEventEmitter.tsx"],"names":["React","useEventEmitter","listen","listenRef","useRef","useEffect","current","listeners","create","useCallback","target","removeListener","type","callback","callbacks","undefined","index","indexOf","splice","addListener","push","removed","emit","data","canPreventDefault","items","slice","concat","Object","keys","map","t","filter","cb","i","self","lastIndexOf","event","defineProperty","enumerable","get","defaultPrevented","defineProperties","preventDefault","value","forEach","useMemo"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAWA;AACA;AACA;AACA,eAAe,SAASC,eAAT,CACbC,MADa,EAEc;AAC3B,QAAMC,SAAS,GAAGH,KAAK,CAACI,MAAN,CAAaF,MAAb,CAAlB;AAEAF,EAAAA,KAAK,CAACK,SAAN,CAAgB,MAAM;AACpBF,IAAAA,SAAS,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EAb,MAAAA,MAAM,CAACc,gBAAP,CAAwBL,KAAxB,EAA+B;AAC7BI,QAAAA,gBAAgB,EAAE;AAChBF,UAAAA,UAAU,EAAE,IADI;;AAEhBC,UAAAA,GAAG,GAAG;AACJ,mBAAOC,gBAAP;AACD;;AAJe,SADW;AAO7BE,QAAAA,cAAc,EAAE;AACdJ,UAAAA,UAAU,EAAE,IADE;;AAEdK,UAAAA,KAAK,GAAG;AACNH,YAAAA,gBAAgB,GAAG,IAAnB;AACD;;AAJa;AAPa,OAA/B;AAcD;;AAED,0BAAAtC,SAAS,CAACG,OAAV,+EAAAH,SAAS,EAAWkC,KAAX,CAAT;AAEAvB,IAAAA,SAAS,SAAT,IAAAA,SAAS,WAAT,YAAAA,SAAS,CAAE+B,OAAX,CAAoBZ,EAAD,IAAQA,EAAE,CAACI,KAAD,CAA7B;AAEA,WAAOA,KAAP;AACD,GAtEU,EAuEX,EAvEW,CAAb;AA0EA,SAAOrC,KAAK,CAAC8C,OAAN,CAAc,OAAO;AAAEtC,IAAAA,MAAF;AAAUc,IAAAA;AAAV,GAAP,CAAd,EAAwC,CAACd,MAAD,EAASc,IAAT,CAAxC,CAAP;AACD","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\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"]}
|
|
@@ -11,8 +11,10 @@ export default function useKeyedChildListeners() {
|
|
|
11
11
|
beforeRemove: {}
|
|
12
12
|
});
|
|
13
13
|
const addKeyedListener = React.useCallback((type, key, listener) => {
|
|
14
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
14
15
|
keyedListeners[type][key] = listener;
|
|
15
16
|
return () => {
|
|
17
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
16
18
|
keyedListeners[type][key] = undefined;
|
|
17
19
|
};
|
|
18
20
|
}, [keyedListeners]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useKeyedChildListeners.tsx"],"names":["React","useKeyedChildListeners","current","keyedListeners","useRef","getState","beforeRemove","addKeyedListener","useCallback","type","key","listener","undefined"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAIA;AACA;AACA;AACA,eAAe,SAASC,sBAAT,GAAkC;AAC/C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAA8BH,KAAK,CAACI,MAAN,CAKjC;AACDC,IAAAA,QAAQ,EAAE,EADT;AAEDC,IAAAA,YAAY,EAAE;AAFb,GALiC,CAApC;AAUA,QAAMC,gBAAgB,GAAGP,KAAK,CAACQ,WAAN,CACvB,CACEC,IADF,EAEEC,GAFF,EAGEC,QAHF,KAIK;
|
|
1
|
+
{"version":3,"sources":["useKeyedChildListeners.tsx"],"names":["React","useKeyedChildListeners","current","keyedListeners","useRef","getState","beforeRemove","addKeyedListener","useCallback","type","key","listener","undefined"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAIA;AACA;AACA;AACA,eAAe,SAASC,sBAAT,GAAkC;AAC/C,QAAM;AAAEC,IAAAA,OAAO,EAAEC;AAAX,MAA8BH,KAAK,CAACI,MAAN,CAKjC;AACDC,IAAAA,QAAQ,EAAE,EADT;AAEDC,IAAAA,YAAY,EAAE;AAFb,GALiC,CAApC;AAUA,QAAMC,gBAAgB,GAAGP,KAAK,CAACQ,WAAN,CACvB,CACEC,IADF,EAEEC,GAFF,EAGEC,QAHF,KAIK;AACH;AACAR,IAAAA,cAAc,CAACM,IAAD,CAAd,CAAqBC,GAArB,IAA4BC,QAA5B;AAEA,WAAO,MAAM;AACX;AACAR,MAAAA,cAAc,CAACM,IAAD,CAAd,CAAqBC,GAArB,IAA4BE,SAA5B;AACD,KAHD;AAID,GAbsB,EAcvB,CAACT,cAAD,CAduB,CAAzB;AAiBA,SAAO;AACLA,IAAAA,cADK;AAELI,IAAAA;AAFK,GAAP;AAID","sourcesContent":["import * as React from 'react';\n\nimport type { KeyedListenerMap } from './NavigationBuilderContext';\n\n/**\n * Hook which lets child navigators add getters to be called for obtaining rehydrated state.\n */\nexport default function useKeyedChildListeners() {\n const { current: keyedListeners } = React.useRef<{\n [K in keyof KeyedListenerMap]: Record<\n string,\n KeyedListenerMap[K] | undefined\n >;\n }>({\n getState: {},\n beforeRemove: {},\n });\n\n const addKeyedListener = React.useCallback(\n <T extends keyof KeyedListenerMap>(\n type: T,\n key: string,\n listener: KeyedListenerMap[T]\n ) => {\n // @ts-expect-error: according to ref stated above you can use `key` to index type\n keyedListeners[type][key] = listener;\n\n return () => {\n // @ts-expect-error: according to ref stated above you can use `key` to index type\n keyedListeners[type][key] = undefined;\n };\n },\n [keyedListeners]\n );\n\n return {\n keyedListeners,\n addKeyedListener,\n };\n}\n"]}
|
|
@@ -23,7 +23,7 @@ export declare type ChildBeforeRemoveListener = (action: NavigationAction) => bo
|
|
|
23
23
|
* Context which holds the required helpers needed to build nested navigators.
|
|
24
24
|
*/
|
|
25
25
|
declare const NavigationBuilderContext: React.Context<{
|
|
26
|
-
onAction?: ((action: NavigationAction, visitedNavigators?: Set<string>
|
|
26
|
+
onAction?: ((action: NavigationAction, visitedNavigators?: Set<string>) => boolean) | undefined;
|
|
27
27
|
addListener?: AddListener | undefined;
|
|
28
28
|
addKeyedListener?: AddKeyedListener | undefined;
|
|
29
29
|
onRouteFocus?: ((key: string) => void) | undefined;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-navigation/core",
|
|
3
3
|
"description": "Core utilities for building navigators",
|
|
4
|
-
"version": "6.2.
|
|
4
|
+
"version": "6.2.2",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
7
7
|
"react-native",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"clean": "del lib"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@react-navigation/routers": "^6.1.
|
|
38
|
+
"@react-navigation/routers": "^6.1.1",
|
|
39
39
|
"escape-string-regexp": "^4.0.0",
|
|
40
40
|
"nanoid": "^3.1.23",
|
|
41
41
|
"query-string": "^7.0.0",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@testing-library/react-native": "^7.2.0",
|
|
46
|
-
"@types/react": "^17.0.
|
|
46
|
+
"@types/react": "^17.0.47",
|
|
47
47
|
"@types/react-is": "^17.0.0",
|
|
48
48
|
"del-cli": "^3.0.1",
|
|
49
49
|
"immer": "^9.0.2",
|
|
50
|
-
"react": "17.0.
|
|
50
|
+
"react": "17.0.2",
|
|
51
51
|
"react-native-builder-bob": "^0.18.1",
|
|
52
|
-
"react-test-renderer": "17.0.
|
|
53
|
-
"typescript": "^4.
|
|
52
|
+
"react-test-renderer": "17.0.2",
|
|
53
|
+
"typescript": "^4.7.4"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"react": "*"
|
|
@@ -68,6 +68,5 @@
|
|
|
68
68
|
}
|
|
69
69
|
]
|
|
70
70
|
]
|
|
71
|
-
}
|
|
72
|
-
"gitHead": "b1c421445ecb75d514ce27791433b12f9a7e75b7"
|
|
71
|
+
}
|
|
73
72
|
}
|
|
@@ -15,14 +15,16 @@ export default function useChildListeners() {
|
|
|
15
15
|
|
|
16
16
|
const addListener = React.useCallback(
|
|
17
17
|
<T extends keyof ListenerMap>(type: T, listener: ListenerMap[T]) => {
|
|
18
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
19
18
|
listeners[type].push(listener);
|
|
20
19
|
|
|
20
|
+
let removed = false;
|
|
21
21
|
return () => {
|
|
22
|
-
// @ts-expect-error: listener should be correct type according to `type`
|
|
23
22
|
const index = listeners[type].indexOf(listener);
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
if (!removed && index > -1) {
|
|
25
|
+
removed = true;
|
|
26
|
+
listeners[type].splice(index, 1);
|
|
27
|
+
}
|
|
26
28
|
};
|
|
27
29
|
},
|
|
28
30
|
[listeners]
|
package/src/useEventEmitter.tsx
CHANGED
|
@@ -35,7 +35,9 @@ export default function useEventEmitter<T extends Record<string, any>>(
|
|
|
35
35
|
|
|
36
36
|
const index = callbacks.indexOf(callback);
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
if (index > -1) {
|
|
39
|
+
callbacks.splice(index, 1);
|
|
40
|
+
}
|
|
39
41
|
};
|
|
40
42
|
|
|
41
43
|
const addListener = (type: string, callback: (data: any) => void) => {
|
|
@@ -43,7 +45,14 @@ export default function useEventEmitter<T extends Record<string, any>>(
|
|
|
43
45
|
listeners.current[type][target] = listeners.current[type][target] || [];
|
|
44
46
|
listeners.current[type][target].push(callback);
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
let removed = false;
|
|
49
|
+
return () => {
|
|
50
|
+
// Prevent removing other listeners when unsubscribing same listener multiple times
|
|
51
|
+
if (!removed) {
|
|
52
|
+
removed = true;
|
|
53
|
+
removeListener(type, callback);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
47
56
|
};
|
|
48
57
|
|
|
49
58
|
return {
|
|
@@ -22,9 +22,11 @@ export default function useKeyedChildListeners() {
|
|
|
22
22
|
key: string,
|
|
23
23
|
listener: KeyedListenerMap[T]
|
|
24
24
|
) => {
|
|
25
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
25
26
|
keyedListeners[type][key] = listener;
|
|
26
27
|
|
|
27
28
|
return () => {
|
|
29
|
+
// @ts-expect-error: according to ref stated above you can use `key` to index type
|
|
28
30
|
keyedListeners[type][key] = undefined;
|
|
29
31
|
};
|
|
30
32
|
},
|