@rn-perf-kit/navigation 0.0.0-stage → 0.1.0
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/package.json +21 -4
- package/src/index.ts +4 -0
- package/src/nav-state.ts +56 -0
- package/src/useNavigationPerf.ts +19 -0
- package/src/withNavigationPerf.ts +34 -0
- package/tsconfig.json +5 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-perf-kit/navigation",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React Navigation + Expo Router adapter for rn-perf-kit",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src/"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@react-navigation/core": ">=6.0.0",
|
|
13
|
+
"react": ">=18.0.0",
|
|
14
|
+
"react-native": ">=0.73.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"typescript": "^5.3.0",
|
|
18
|
+
"@types/react": "^18.0.0"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rn-perf-kit/core": "^0.1.0"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { withNavigationPerf, installNavigationListeners } from './withNavigationPerf';
|
|
2
|
+
export { useNavigationPerf } from './useNavigationPerf';
|
|
3
|
+
export { recordNavPhase, getActiveNavSession, startNavSession, endNavSession } from './nav-state';
|
|
4
|
+
export type { NavSession } from './nav-state';
|
package/src/nav-state.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Navigation session state manager.
|
|
3
|
+
* Tracks per-navigation timing data as it flows through phases.
|
|
4
|
+
*/
|
|
5
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
6
|
+
|
|
7
|
+
export interface NavSession {
|
|
8
|
+
navId: string;
|
|
9
|
+
fromRoute: string;
|
|
10
|
+
toRoute: string;
|
|
11
|
+
tapAt?: number;
|
|
12
|
+
dispatchAt?: number;
|
|
13
|
+
firstCommitAt?: number;
|
|
14
|
+
transitionStartAt?: number;
|
|
15
|
+
transitionEndAt?: number;
|
|
16
|
+
contentReadyAt?: number;
|
|
17
|
+
interactiveAt?: number;
|
|
18
|
+
droppedFrames: number;
|
|
19
|
+
totalFrames: number;
|
|
20
|
+
commitsDuringTransition: Array<{ t: number; dur: number; source: string }>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let sessionCounter = 0;
|
|
24
|
+
let activeSession: NavSession | null = null;
|
|
25
|
+
|
|
26
|
+
export function startNavSession(fromRoute: string, toRoute: string): NavSession {
|
|
27
|
+
const navId = String(++sessionCounter);
|
|
28
|
+
activeSession = { navId, fromRoute, toRoute, droppedFrames: 0, totalFrames: 0, commitsDuringTransition: [] };
|
|
29
|
+
timeline.add({ type: 'nav', t: timeline.now(), phase: 'dispatch', route: toRoute, fromRoute, navId });
|
|
30
|
+
return activeSession;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getActiveNavSession(): NavSession | null { return activeSession; }
|
|
34
|
+
export function endNavSession(): NavSession | null {
|
|
35
|
+
const s = activeSession;
|
|
36
|
+
activeSession = null;
|
|
37
|
+
return s;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function recordNavPhase(
|
|
41
|
+
phase: 'tap' | 'dispatch' | 'first-commit' | 'transition-start' | 'transition-end' | 'content-ready' | 'interactive',
|
|
42
|
+
t?: number
|
|
43
|
+
): void {
|
|
44
|
+
if (!activeSession) return;
|
|
45
|
+
const now = t ?? timeline.now();
|
|
46
|
+
switch (phase) {
|
|
47
|
+
case 'tap': activeSession.tapAt = now; break;
|
|
48
|
+
case 'dispatch': activeSession.dispatchAt = now; break;
|
|
49
|
+
case 'first-commit': activeSession.firstCommitAt = now; break;
|
|
50
|
+
case 'transition-start': activeSession.transitionStartAt = now; break;
|
|
51
|
+
case 'transition-end': activeSession.transitionEndAt = now; break;
|
|
52
|
+
case 'content-ready': activeSession.contentReadyAt = now; break;
|
|
53
|
+
case 'interactive': activeSession.interactiveAt = now; break;
|
|
54
|
+
}
|
|
55
|
+
timeline.add({ type: 'nav', t: now, phase, route: activeSession.toRoute, fromRoute: activeSession.fromRoute, navId: activeSession.navId });
|
|
56
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook that connects navigation screen lifecycle events to the timeline.
|
|
3
|
+
*/
|
|
4
|
+
import { useFocusEffect, useRoute } from '@react-navigation/core';
|
|
5
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
6
|
+
import { recordNavPhase, getActiveNavSession } from './nav-state';
|
|
7
|
+
|
|
8
|
+
export function useNavigationPerf(screenName?: string): void {
|
|
9
|
+
const route = useRoute();
|
|
10
|
+
const name = screenName ?? route.name;
|
|
11
|
+
|
|
12
|
+
useFocusEffect(() => {
|
|
13
|
+
const session = getActiveNavSession();
|
|
14
|
+
if (session && session.toRoute === name) {
|
|
15
|
+
recordNavPhase('transition-end');
|
|
16
|
+
}
|
|
17
|
+
return () => { timeline.mark(`screen:blur:${name}`); };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* withNavigationPerf(navigationRef)
|
|
3
|
+
* Wraps a React Navigation ref to intercept navigation events.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* <NavigationContainer ref={withNavigationPerf(navigationRef)}>
|
|
7
|
+
*/
|
|
8
|
+
import type { NavigationContainerRef, ParamListBase } from '@react-navigation/core';
|
|
9
|
+
import { startNavSession, recordNavPhase, getActiveNavSession } from './nav-state';
|
|
10
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
11
|
+
|
|
12
|
+
type NavRef = NavigationContainerRef<ParamListBase>;
|
|
13
|
+
|
|
14
|
+
export function withNavigationPerf(ref: React.RefObject<NavRef>): React.RefObject<NavRef> {
|
|
15
|
+
return ref;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function installNavigationListeners(nav: NavRef): () => void {
|
|
19
|
+
let currentRoute = '';
|
|
20
|
+
|
|
21
|
+
const unsubscribeState = nav.addListener('state', () => {
|
|
22
|
+
const route = nav.getCurrentRoute()?.name ?? '';
|
|
23
|
+
if (route && route !== currentRoute) {
|
|
24
|
+
startNavSession(currentRoute, route);
|
|
25
|
+
currentRoute = route;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const unsubscribeAction = nav.addListener('__unsafe_action__' as any, () => {
|
|
30
|
+
recordNavPhase('tap');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return () => { unsubscribeState(); unsubscribeAction(); };
|
|
34
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED