@libo11y/rum-expo-router 0.0.0-stage → 0.7.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/LICENSE +202 -0
- package/README.md +114 -2
- package/dist/hook.d.ts +14 -0
- package/dist/hook.js +29 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/instrument.d.ts +94 -0
- package/dist/instrument.js +116 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,115 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @libo11y/rum-expo-router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Expo Router adapter for
|
|
4
|
+
[`@libo11y/rum-react-native`](../rum-react-native). It names each RUM
|
|
5
|
+
screen by its route template, such as `/projects/[id]/rooms`, never by a
|
|
6
|
+
path with ids in it. It starts a view when the app moves to a new screen,
|
|
7
|
+
and marks the screen rendered when its navigation state is committed, or
|
|
8
|
+
leaves that to the screen.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @libo11y/rum-react-native @libo11y/rum-expo-router
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It needs Expo Router 58 (Expo SDK 58) and React 19. Expo Router is an
|
|
15
|
+
optional peer dependency, because the package types only the parts of it
|
|
16
|
+
that it reads, but an app that imports the hook must have it installed.
|
|
17
|
+
Tested on Expo SDK 58 only.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Call `useExpoRouterRum` in the root layout with the RUM handle. It takes
|
|
22
|
+
`undefined` while telemetry is being set up. From its first render it
|
|
23
|
+
notes the route the app launched on, and once it gets the handle it names
|
|
24
|
+
the launch view after that screen, or, when the user already left it,
|
|
25
|
+
starts a view for the screen shown:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// app/_layout.tsx
|
|
29
|
+
import { useExpoRouterRum } from "@libo11y/rum-expo-router";
|
|
30
|
+
import { RumTouchBoundary } from "@libo11y/rum-react-native";
|
|
31
|
+
import { Stack } from "expo-router";
|
|
32
|
+
import { useEffect, useState } from "react";
|
|
33
|
+
import { telemetryReady } from "../telemetry";
|
|
34
|
+
|
|
35
|
+
export default function RootLayout() {
|
|
36
|
+
const [telemetry, setTelemetry] = useState<Awaited<typeof telemetryReady>>();
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
void telemetryReady.then(setTelemetry);
|
|
39
|
+
}, []);
|
|
40
|
+
useExpoRouterRum(telemetry?.rum);
|
|
41
|
+
return (
|
|
42
|
+
<RumTouchBoundary>
|
|
43
|
+
<Stack />
|
|
44
|
+
</RumTouchBoundary>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`instrumentExpoRouter(navigationRef, rum, options)` is the same without
|
|
50
|
+
the hook, for the ref `useNavigationContainerRef()` returns. It returns a
|
|
51
|
+
function that removes its listener, also one added before the container
|
|
52
|
+
mounted. To get the hook's handling of a late handle, call
|
|
53
|
+
`trackLaunch(navigationRef)` as the app launches and pass its `key()` as
|
|
54
|
+
`launchKey`; without it, the first state names the launch view.
|
|
55
|
+
|
|
56
|
+
## What it records
|
|
57
|
+
|
|
58
|
+
The adapter listens to the `state` event of Expo Router's navigation
|
|
59
|
+
container. The container emits it once each new navigation state is
|
|
60
|
+
committed, so the new screen has rendered.
|
|
61
|
+
|
|
62
|
+
- The first state names the screen RUM started at launch, so the first
|
|
63
|
+
screen's view span and `app_load` end at the first frame after it. When
|
|
64
|
+
the handle arrived after the user left the launch screen, the first
|
|
65
|
+
state starts a view for the screen shown instead, and the launch view
|
|
66
|
+
ends unnamed at its cap.
|
|
67
|
+
- A new focused route starts a view: a push, a pop, a replace, a tab
|
|
68
|
+
switch, and coming back to a tab or screen shown before. The focused
|
|
69
|
+
route is followed down through every nested navigator, and a route's
|
|
70
|
+
key tells one screen from another, so two projects opened one after the
|
|
71
|
+
other are two views of `/projects/[id]`.
|
|
72
|
+
- A state that keeps the focused route starts no view: new params, or
|
|
73
|
+
another navigator's change. When a nested navigator's state arrives
|
|
74
|
+
under the route that was focused, the view takes the deeper name.
|
|
75
|
+
- A tap that caused the navigation is the view's cause. React Native runs
|
|
76
|
+
`onPress` before the touch reaches `RumTouchBoundary`, but the
|
|
77
|
+
navigation state is committed after that, so the tap's action is open
|
|
78
|
+
when the view starts.
|
|
79
|
+
|
|
80
|
+
## Rendered
|
|
81
|
+
|
|
82
|
+
`viewRendered` decides when a screen's view span ends:
|
|
83
|
+
|
|
84
|
+
- `"navigation"` (default): the adapter calls `rum.viewRendered()` when
|
|
85
|
+
the screen's state is committed. The span ends at the next frame.
|
|
86
|
+
- `"screen"`: each screen calls `rum.viewRendered()` itself, for example
|
|
87
|
+
once its data loaded, through `useRum()` from `@libo11y/rum-react`. A
|
|
88
|
+
screen that never calls it ends at the 10 second cap.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
useExpoRouterRum(telemetry?.rum, { viewRendered: "screen" });
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The span covers the time from the committed navigation state to the
|
|
95
|
+
first frame after the render. React renders the new screen before the
|
|
96
|
+
state is committed, so that render is not part of the span.
|
|
97
|
+
|
|
98
|
+
## View names
|
|
99
|
+
|
|
100
|
+
`routeTemplate(state)` names a view after the focused route's file path
|
|
101
|
+
in the app directory: groups such as `(tabs)` are left out, a trailing
|
|
102
|
+
`index` is left out, and dynamic segments stay as written, such as
|
|
103
|
+
`[id]` and `[...path]`. The root is `/`. Expo Router's own screens are
|
|
104
|
+
`/+not-found` and `/_sitemap`.
|
|
105
|
+
|
|
106
|
+
## Status
|
|
107
|
+
|
|
108
|
+
A preview. The package first ships in `ts/v0.6.0`, with
|
|
109
|
+
`@libo11y/rum-react-native` at the same version. It is published on npm at
|
|
110
|
+
`0.7.0` (and attached as a tarball to the
|
|
111
|
+
[`ts/v0.7.0` GitHub Release](https://github.com/libo11y/libo11y/releases/tag/ts/v0.7.0)):
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
npm install @libo11y/rum-react-native @libo11y/rum-expo-router
|
|
115
|
+
```
|
package/dist/hook.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useExpoRouterRum: instrumentExpoRouter as a hook, over Expo Router's container ref. The only
|
|
3
|
+
* module that imports expo-router and React, so node tests never load them.
|
|
4
|
+
*/
|
|
5
|
+
import type { Rum } from "@libo11y/rum";
|
|
6
|
+
import { type ExpoRouterOptions } from "./instrument.ts";
|
|
7
|
+
/**
|
|
8
|
+
* Names RUM screens by route template and starts a view on each new screen, for as long as the
|
|
9
|
+
* component that calls it is mounted. Call it in the root layout (app/_layout.tsx). rum may be
|
|
10
|
+
* undefined while telemetry is being set up; the hook notes the launch route from its first
|
|
11
|
+
* render, and once it gets the handle it names the launch view, or, when the user already left
|
|
12
|
+
* the launch screen, starts a view for the screen shown.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useExpoRouterRum(rum: Rum | undefined, options?: Pick<ExpoRouterOptions, "viewRendered">): void;
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useNavigationContainerRef } from "expo-router";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { instrumentExpoRouter, trackLaunch, } from "./instrument.js";
|
|
4
|
+
/**
|
|
5
|
+
* Names RUM screens by route template and starts a view on each new screen, for as long as the
|
|
6
|
+
* component that calls it is mounted. Call it in the root layout (app/_layout.tsx). rum may be
|
|
7
|
+
* undefined while telemetry is being set up; the hook notes the launch route from its first
|
|
8
|
+
* render, and once it gets the handle it names the launch view, or, when the user already left
|
|
9
|
+
* the launch screen, starts a view for the screen shown.
|
|
10
|
+
*/
|
|
11
|
+
export function useExpoRouterRum(rum, options = {}) {
|
|
12
|
+
const ref = useNavigationContainerRef();
|
|
13
|
+
const launch = useRef(undefined);
|
|
14
|
+
const mode = options.viewRendered;
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const tracker = trackLaunch(ref);
|
|
17
|
+
launch.current = tracker;
|
|
18
|
+
return () => tracker.stop();
|
|
19
|
+
}, [ref]);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (rum === undefined)
|
|
22
|
+
return undefined;
|
|
23
|
+
const launchKey = launch.current?.key();
|
|
24
|
+
return instrumentExpoRouter(ref, rum, {
|
|
25
|
+
...(mode === undefined ? {} : { viewRendered: mode }),
|
|
26
|
+
...(launchKey === undefined ? {} : { launchKey }),
|
|
27
|
+
});
|
|
28
|
+
}, [ref, rum, mode]);
|
|
29
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expo Router adapter for `@libo11y/rum-react-native`: screens named by route template, such as
|
|
3
|
+
* /projects/[id]/rooms, a view started on each new screen, and the screen marked rendered when its
|
|
4
|
+
* navigation state is committed (or by the screen itself).
|
|
5
|
+
*/
|
|
6
|
+
export { useExpoRouterRum } from "./hook.ts";
|
|
7
|
+
export { type ExpoRouterOptions, instrumentExpoRouter, type LaunchTracker, type NavigationContainerRefLike, type NavigationStateLike, type RouteLike, routeTemplate, trackLaunch, } from "./instrument.ts";
|
|
8
|
+
export { VERSION } from "./version.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expo Router adapter for `@libo11y/rum-react-native`: screens named by route template, such as
|
|
3
|
+
* /projects/[id]/rooms, a view started on each new screen, and the screen marked rendered when its
|
|
4
|
+
* navigation state is committed (or by the screen itself).
|
|
5
|
+
*/
|
|
6
|
+
export { useExpoRouterRum } from "./hook.js";
|
|
7
|
+
export { instrumentExpoRouter, routeTemplate, trackLaunch, } from "./instrument.js";
|
|
8
|
+
export { VERSION } from "./version.js";
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* instrumentExpoRouter: RUM screens from Expo Router's navigation state.
|
|
3
|
+
*
|
|
4
|
+
* Expo Router keeps one navigation container, whose ref useNavigationContainerRef returns. The
|
|
5
|
+
* container emits state once each navigation state is committed, so the new screen has rendered,
|
|
6
|
+
* and after the touch that caused the navigation was dispatched, so the tap's action is open and
|
|
7
|
+
* becomes the view's cause. On each state the adapter reads the focused route, down through every
|
|
8
|
+
* nested navigator:
|
|
9
|
+
*
|
|
10
|
+
* - The first time, it names the screen RUM started at launch. Given the launch route's key
|
|
11
|
+
* (launchKey), a first state that has left that route starts a view instead.
|
|
12
|
+
* - A different focused route (by its key: a push, a pop, a tab switch, a replace) starts a view.
|
|
13
|
+
* - The same route, or a navigator's state arriving under the route that was focused, names the
|
|
14
|
+
* current view and starts none. A change of params alone starts no view.
|
|
15
|
+
*
|
|
16
|
+
* With viewRendered "navigation" (the default) the adapter marks each screen rendered when its
|
|
17
|
+
* state is committed, so its view span ends at the next frame. With "screen" the screen calls
|
|
18
|
+
* rum.viewRendered() itself, such as once its data loaded; a screen that never does ends at its
|
|
19
|
+
* 10 second cap.
|
|
20
|
+
*
|
|
21
|
+
* A view's name is the route template, the focused route's file path without groups and without a
|
|
22
|
+
* trailing index, such as /projects/[id]/rooms, never a path with ids in it. The adapter needs only
|
|
23
|
+
* the structural types below, which Expo Router 58's container ref satisfies.
|
|
24
|
+
*/
|
|
25
|
+
import type { Rum } from "@libo11y/rum";
|
|
26
|
+
/** The parts of a navigation route the adapter reads. */
|
|
27
|
+
export interface RouteLike {
|
|
28
|
+
/** The route's name: its file path relative to its navigator's layout, such as [id]/rooms. */
|
|
29
|
+
readonly name: string;
|
|
30
|
+
/** Unique per screen instance. */
|
|
31
|
+
readonly key?: string;
|
|
32
|
+
/** The state of the navigator this route renders, when it renders one. */
|
|
33
|
+
readonly state?: NavigationStateLike | undefined;
|
|
34
|
+
}
|
|
35
|
+
/** The parts of a navigation state the adapter reads. */
|
|
36
|
+
export interface NavigationStateLike {
|
|
37
|
+
/** The focused route's index. Default 0. */
|
|
38
|
+
readonly index?: number | undefined;
|
|
39
|
+
/** The navigator's routes, in order. */
|
|
40
|
+
readonly routes: readonly RouteLike[];
|
|
41
|
+
}
|
|
42
|
+
/** The parts of Expo Router's navigation container ref the adapter uses. */
|
|
43
|
+
export interface NavigationContainerRefLike {
|
|
44
|
+
/** Adds a listener for the committed state. Returns its removal. */
|
|
45
|
+
addListener(event: "state", callback: () => void): () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Removes a listener. Expo Router's ref queues a listener added before its container mounts and
|
|
48
|
+
* hands it over on mount, after which only this reaches it.
|
|
49
|
+
*/
|
|
50
|
+
removeListener?(event: "state", callback: () => void): void;
|
|
51
|
+
/** Whether the container is mounted with a navigator. */
|
|
52
|
+
isReady(): boolean;
|
|
53
|
+
/** The root state, with every nested navigator's. Only while ready. */
|
|
54
|
+
getRootState(): NavigationStateLike | undefined;
|
|
55
|
+
}
|
|
56
|
+
/** Options of instrumentExpoRouter. */
|
|
57
|
+
export interface ExpoRouterOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Who marks a screen rendered: "navigation" (default), the adapter, when the screen's navigation
|
|
60
|
+
* state is committed; or "screen", the screen itself through rum.viewRendered().
|
|
61
|
+
*/
|
|
62
|
+
readonly viewRendered?: "navigation" | "screen";
|
|
63
|
+
/**
|
|
64
|
+
* The key of the route focused when the app launched (trackLaunch). When the first state the
|
|
65
|
+
* adapter reads has left that route, as when the RUM handle arrived after the user moved on, the
|
|
66
|
+
* adapter starts a view for the screen shown instead of naming the launch view after it.
|
|
67
|
+
* useExpoRouterRum passes it.
|
|
68
|
+
*/
|
|
69
|
+
readonly launchKey?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The route template of the focused route: its file path from the app directory, without groups
|
|
73
|
+
* such as (tabs) and without a trailing index, such as /projects/[id]/rooms. Undefined for a state
|
|
74
|
+
* with no route.
|
|
75
|
+
*/
|
|
76
|
+
export declare function routeTemplate(state: NavigationStateLike | undefined): string | undefined;
|
|
77
|
+
/** The key of the route focused at launch, as trackLaunch keeps it. */
|
|
78
|
+
export interface LaunchTracker {
|
|
79
|
+
/** The launch route's key, once the container has a state. */
|
|
80
|
+
key(): string | undefined;
|
|
81
|
+
/** Stops listening. */
|
|
82
|
+
stop(): void;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Keeps the key of the first focused route: read at once when the container is ready, else from
|
|
86
|
+
* its first committed state. Call it as the app launches, before the RUM handle may exist.
|
|
87
|
+
*/
|
|
88
|
+
export declare function trackLaunch(ref: NavigationContainerRefLike): LaunchTracker;
|
|
89
|
+
/**
|
|
90
|
+
* Names RUM screens by route template and starts a view on each new focused route, from the
|
|
91
|
+
* container's committed states. Call it once, with the ref useNavigationContainerRef returns (or
|
|
92
|
+
* use useExpoRouterRum). Returns the uninstall.
|
|
93
|
+
*/
|
|
94
|
+
export declare function instrumentExpoRouter(ref: NavigationContainerRefLike, rum: Rum, options?: ExpoRouterOptions): () => void;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** Expo Router's root slot, which every app route sits under. */
|
|
2
|
+
const ROOT_SLOT = "__root";
|
|
3
|
+
const MAX_DEPTH = 32;
|
|
4
|
+
function focus(state) {
|
|
5
|
+
const segments = [];
|
|
6
|
+
const keys = [];
|
|
7
|
+
let found = false;
|
|
8
|
+
let s = state;
|
|
9
|
+
for (let depth = 0; s !== undefined && depth < MAX_DEPTH; depth++) {
|
|
10
|
+
const r = s.routes[s.index ?? 0] ?? s.routes[0];
|
|
11
|
+
if (r === undefined)
|
|
12
|
+
break;
|
|
13
|
+
found = true;
|
|
14
|
+
if (r.key !== undefined)
|
|
15
|
+
keys.push(r.key);
|
|
16
|
+
if (r.name !== ROOT_SLOT) {
|
|
17
|
+
for (const part of r.name.split("/"))
|
|
18
|
+
if (part !== "")
|
|
19
|
+
segments.push(part);
|
|
20
|
+
}
|
|
21
|
+
s = r.state;
|
|
22
|
+
}
|
|
23
|
+
if (!found)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (segments.at(-1) === "index")
|
|
26
|
+
segments.pop();
|
|
27
|
+
const path = segments.filter((p) => !(p.startsWith("(") && p.endsWith(")")));
|
|
28
|
+
return { name: `/${path.join("/")}`, keys };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The route template of the focused route: its file path from the app directory, without groups
|
|
32
|
+
* such as (tabs) and without a trailing index, such as /projects/[id]/rooms. Undefined for a state
|
|
33
|
+
* with no route.
|
|
34
|
+
*/
|
|
35
|
+
export function routeTemplate(state) {
|
|
36
|
+
return focus(state)?.name;
|
|
37
|
+
}
|
|
38
|
+
/** Listens for the committed state until the returned function runs, reliably. */
|
|
39
|
+
function listen(ref, callback) {
|
|
40
|
+
let removed = false;
|
|
41
|
+
const guarded = () => {
|
|
42
|
+
if (!removed)
|
|
43
|
+
callback();
|
|
44
|
+
};
|
|
45
|
+
const off = ref.addListener("state", guarded);
|
|
46
|
+
return () => {
|
|
47
|
+
if (removed)
|
|
48
|
+
return;
|
|
49
|
+
removed = true;
|
|
50
|
+
off();
|
|
51
|
+
ref.removeListener?.("state", guarded);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Keeps the key of the first focused route: read at once when the container is ready, else from
|
|
56
|
+
* its first committed state. Call it as the app launches, before the RUM handle may exist.
|
|
57
|
+
*/
|
|
58
|
+
export function trackLaunch(ref) {
|
|
59
|
+
let key;
|
|
60
|
+
let off;
|
|
61
|
+
const read = () => {
|
|
62
|
+
if (key !== undefined || !ref.isReady())
|
|
63
|
+
return;
|
|
64
|
+
key = focus(ref.getRootState())?.keys.at(-1);
|
|
65
|
+
if (key !== undefined)
|
|
66
|
+
off?.();
|
|
67
|
+
};
|
|
68
|
+
read();
|
|
69
|
+
if (key === undefined)
|
|
70
|
+
off = listen(ref, read);
|
|
71
|
+
return {
|
|
72
|
+
key: () => key,
|
|
73
|
+
stop: () => off?.(),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Names RUM screens by route template and starts a view on each new focused route, from the
|
|
78
|
+
* container's committed states. Call it once, with the ref useNavigationContainerRef returns (or
|
|
79
|
+
* use useExpoRouterRum). Returns the uninstall.
|
|
80
|
+
*/
|
|
81
|
+
export function instrumentExpoRouter(ref, rum, options = {}) {
|
|
82
|
+
const auto = options.viewRendered !== "screen";
|
|
83
|
+
let shown;
|
|
84
|
+
const update = () => {
|
|
85
|
+
if (!ref.isReady())
|
|
86
|
+
return;
|
|
87
|
+
const f = focus(ref.getRootState());
|
|
88
|
+
if (f === undefined)
|
|
89
|
+
return;
|
|
90
|
+
const prev = shown;
|
|
91
|
+
shown = f;
|
|
92
|
+
if (prev === undefined) {
|
|
93
|
+
const launch = options.launchKey;
|
|
94
|
+
if (launch !== undefined && !f.keys.includes(launch))
|
|
95
|
+
rum.startView({ name: f.name });
|
|
96
|
+
else
|
|
97
|
+
rum.setViewName(f.name);
|
|
98
|
+
if (auto)
|
|
99
|
+
rum.viewRendered();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const prevKey = prev.keys.at(-1);
|
|
103
|
+
// A route without a key (not one Expo Router makes) is the same screen when its name is.
|
|
104
|
+
if (prevKey === undefined ? f.name === prev.name : f.keys.includes(prevKey)) {
|
|
105
|
+
if (f.name !== prev.name)
|
|
106
|
+
rum.setViewName(f.name);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
rum.startView({ name: f.name });
|
|
110
|
+
if (auto)
|
|
111
|
+
rum.viewRendered();
|
|
112
|
+
};
|
|
113
|
+
const off = listen(ref, update);
|
|
114
|
+
update();
|
|
115
|
+
return off;
|
|
116
|
+
}
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libo11y/rum-expo-router",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Expo Router adapter for @libo11y/rum-react-native: names screens by route template, starts a view on each new screen and marks it rendered when its navigation state is committed.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/libo11y/libo11y.git",
|
|
17
|
+
"directory": "ts/packages/rum-expo-router"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@libo11y/rum": "0.7.0",
|
|
21
|
+
"expo-router": ">=58.0.0",
|
|
22
|
+
"react": "^19.0.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependenciesMeta": {
|
|
25
|
+
"expo-router": {
|
|
26
|
+
"optional": true
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@libo11y/rum": "0.7.0",
|
|
31
|
+
"@types/react": "19.3.0",
|
|
32
|
+
"react": "19.3.1"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
42
|
+
}
|
|
43
|
+
}
|