@libo11y/rum-react-native 0.0.0-bootstrap.0 → 0.6.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/README.md +287 -2
- package/dist/app-start.d.ts +36 -0
- package/dist/app-start.js +81 -0
- package/dist/binding.d.ts +27 -0
- package/dist/binding.js +31 -0
- package/dist/config-cache.d.ts +19 -0
- package/dist/config-cache.js +38 -0
- package/dist/contracts/env.d.ts +62 -0
- package/dist/contracts/env.js +5 -0
- package/dist/env.d.ts +12 -0
- package/dist/env.js +54 -0
- package/dist/errors.d.ts +50 -0
- package/dist/errors.js +213 -0
- package/dist/expo.d.ts +7 -0
- package/dist/expo.js +16 -0
- package/dist/hermes-stack.d.ts +13 -0
- package/dist/hermes-stack.js +84 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +65 -0
- package/dist/internal.d.ts +19 -0
- package/dist/internal.js +17 -0
- package/dist/options.d.ts +27 -0
- package/dist/options.js +1 -0
- package/dist/screens.d.ts +12 -0
- package/dist/screens.js +32 -0
- package/dist/session-store.d.ts +20 -0
- package/dist/session-store.js +28 -0
- package/dist/setup.d.ts +38 -0
- package/dist/setup.js +81 -0
- package/dist/tap-target.d.ts +74 -0
- package/dist/tap-target.js +147 -0
- package/dist/taps.d.ts +80 -0
- package/dist/taps.js +206 -0
- package/dist/touch-boundary.d.ts +22 -0
- package/dist/touch-boundary.js +28 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +50 -4
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { parseSessionState, SESSION_STORAGE_KEY } from "@libo11y/rum/core";
|
|
2
|
+
import { diag } from "@opentelemetry/api";
|
|
3
|
+
/** A SessionStore over storage. */
|
|
4
|
+
export function storageSessionStore(storage) {
|
|
5
|
+
/** The last write, settled: every write and load runs after it. */
|
|
6
|
+
let last = Promise.resolve();
|
|
7
|
+
return {
|
|
8
|
+
async load() {
|
|
9
|
+
await last;
|
|
10
|
+
try {
|
|
11
|
+
const raw = await storage.getItem(SESSION_STORAGE_KEY);
|
|
12
|
+
return raw === null ? undefined : parseSessionState(JSON.parse(raw));
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
save(state) {
|
|
19
|
+
const write = () => state === undefined
|
|
20
|
+
? storage.removeItem(SESSION_STORAGE_KEY)
|
|
21
|
+
: storage.setItem(SESSION_STORAGE_KEY, JSON.stringify({ v: 1, ...state }));
|
|
22
|
+
last = last.then(write).catch((err) => diag.warn("o11y: rum: saving the session failed", err));
|
|
23
|
+
},
|
|
24
|
+
flush: () => last,
|
|
25
|
+
onChange: () => () => { },
|
|
26
|
+
close: () => { },
|
|
27
|
+
};
|
|
28
|
+
}
|
package/dist/setup.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* setupRumReactNativeWith: the RUM core (`@libo11y/rum/core`) bound to React Native. The platform is
|
|
3
|
+
* `@libo11y/react-native`'s setup through reactNativeBinding, the lifecycle its AppState, and every
|
|
4
|
+
* view a screen; a new screen is activity. Device storage (options.storage, default memoryStorage()) holds the retry queue,
|
|
5
|
+
* the retention buffer of sessions not yet kept, the session, so a relaunch within the idle window
|
|
6
|
+
* continues it, and the remote configuration cache. RUM writes none of it before consent is
|
|
7
|
+
* granted. RUM's core removes it: a start with consent denied removes what an earlier launch left
|
|
8
|
+
* before setup resolves, and a revoke removes it all, also what an earlier launch left, before the
|
|
9
|
+
* revoke resolves. A pending start leaves it untouched. The app_load span is recorded once per process (app-start.ts). setupRumReactNative,
|
|
10
|
+
* in index.ts, passes the real dependencies from env.ts.
|
|
11
|
+
*/
|
|
12
|
+
import { type ReactNativeDeps } from "@libo11y/react-native/internal";
|
|
13
|
+
import type { RumReactNativeEnv } from "./contracts/env.ts";
|
|
14
|
+
import type { ReactNativeRumTelemetry, RumReactNativeSetupOptions } from "./options.ts";
|
|
15
|
+
/** The fixed URL RUM's core reads where a page has its location: an app has none. */
|
|
16
|
+
export declare const APP_LOCATION = "app:///";
|
|
17
|
+
/** What setupRumReactNativeWith builds on. Internal: tests replace parts of it. */
|
|
18
|
+
export interface RumReactNativeDeps {
|
|
19
|
+
/** `@libo11y/react-native`'s dependencies, which the binding wraps. */
|
|
20
|
+
readonly platform: ReactNativeDeps;
|
|
21
|
+
readonly env: RumReactNativeEnv;
|
|
22
|
+
/** Epoch milliseconds. */
|
|
23
|
+
readonly now: () => number;
|
|
24
|
+
/** performance.now(). */
|
|
25
|
+
readonly perfNow: () => number;
|
|
26
|
+
/** The epoch milliseconds perfNow counts from. */
|
|
27
|
+
readonly timeOrigin: number;
|
|
28
|
+
/**
|
|
29
|
+
* The fetch the OTLP transport and remote configuration use. Default the global fetch captured
|
|
30
|
+
* before the instrumentation patched it.
|
|
31
|
+
*/
|
|
32
|
+
readonly fetch?: typeof fetch;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* React Native telemetry with RUM over deps, installing it globally when install is true. Resolves
|
|
36
|
+
* with the platform's telemetry, with rum added.
|
|
37
|
+
*/
|
|
38
|
+
export declare function setupRumReactNativeWith(options: RumReactNativeSetupOptions, deps: RumReactNativeDeps, install: boolean): Promise<ReactNativeRumTelemetry>;
|
package/dist/setup.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* setupRumReactNativeWith: the RUM core (`@libo11y/rum/core`) bound to React Native. The platform is
|
|
3
|
+
* `@libo11y/react-native`'s setup through reactNativeBinding, the lifecycle its AppState, and every
|
|
4
|
+
* view a screen; a new screen is activity. Device storage (options.storage, default memoryStorage()) holds the retry queue,
|
|
5
|
+
* the retention buffer of sessions not yet kept, the session, so a relaunch within the idle window
|
|
6
|
+
* continues it, and the remote configuration cache. RUM writes none of it before consent is
|
|
7
|
+
* granted. RUM's core removes it: a start with consent denied removes what an earlier launch left
|
|
8
|
+
* before setup resolves, and a revoke removes it all, also what an earlier launch left, before the
|
|
9
|
+
* revoke resolves. A pending start leaves it untouched. The app_load span is recorded once per process (app-start.ts). setupRumReactNative,
|
|
10
|
+
* in index.ts, passes the real dependencies from env.ts.
|
|
11
|
+
*/
|
|
12
|
+
import { memoryStorage, openStorageQueue } from "@libo11y/react-native/internal";
|
|
13
|
+
import { CONFIG_PINNED_STORAGE_KEY, CONFIG_STORAGE_KEY, setupRumCore } from "@libo11y/rum/core";
|
|
14
|
+
import { RUM_PLATFORM_VALUE_REACT_NATIVE } from "@libo11y/spec";
|
|
15
|
+
import { createAppStart } from "./app-start.js";
|
|
16
|
+
import { reactNativeBinding, rumLifecycle } from "./binding.js";
|
|
17
|
+
import { preloadedStorage } from "./config-cache.js";
|
|
18
|
+
import { storageSessionStore } from "./session-store.js";
|
|
19
|
+
/** The fixed URL RUM's core reads where a page has its location: an app has none. */
|
|
20
|
+
export const APP_LOCATION = "app:///";
|
|
21
|
+
/**
|
|
22
|
+
* React Native telemetry with RUM over deps, installing it globally when install is true. Resolves
|
|
23
|
+
* with the platform's telemetry, with rum added.
|
|
24
|
+
*/
|
|
25
|
+
export async function setupRumReactNativeWith(options, deps, install) {
|
|
26
|
+
const { rum: rumOptions = {}, ...platformOptions } = options;
|
|
27
|
+
const { initialViewName = "app", ...core } = rumOptions;
|
|
28
|
+
const storage = platformOptions.storage ?? memoryStorage();
|
|
29
|
+
const appStart = createAppStart({ env: deps.env, now: deps.perfNow });
|
|
30
|
+
/** Activity listeners RUM's core registered: a new screen is the user moving through the app. */
|
|
31
|
+
const activity = new Set();
|
|
32
|
+
const t = await setupRumCore({ ...platformOptions, storage }, { ...core, viewName: () => initialViewName }, {
|
|
33
|
+
platform: RUM_PLATFORM_VALUE_REACT_NATIVE,
|
|
34
|
+
bind: reactNativeBinding(deps.platform, {
|
|
35
|
+
install,
|
|
36
|
+
...(deps.fetch === undefined ? {} : { fetch: deps.fetch }),
|
|
37
|
+
}),
|
|
38
|
+
lifecycle: {
|
|
39
|
+
...rumLifecycle(deps.platform.lifecycle),
|
|
40
|
+
activity(listener) {
|
|
41
|
+
activity.add(listener);
|
|
42
|
+
return () => activity.delete(listener);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
// The retention buffer claims on deps.now, so its entries are stamped on that clock too.
|
|
46
|
+
openRetentionQueue: (o) => openStorageQueue({ now: deps.now, ...o, storage }),
|
|
47
|
+
sessionStore: () => storageSessionStore(storage),
|
|
48
|
+
now: deps.now,
|
|
49
|
+
perfNow: deps.perfNow,
|
|
50
|
+
timeOrigin: deps.timeOrigin,
|
|
51
|
+
location: () => APP_LOCATION,
|
|
52
|
+
prerendered: () => false,
|
|
53
|
+
connection: () => undefined,
|
|
54
|
+
...(deps.fetch === undefined ? {} : { fetch: deps.fetch }),
|
|
55
|
+
storage: () => preloadedStorage(storage, [CONFIG_STORAGE_KEY, CONFIG_PINNED_STORAGE_KEY]),
|
|
56
|
+
viewKind: "screen",
|
|
57
|
+
// performance.now() counts from the device's boot on React Native, not from the app's start.
|
|
58
|
+
initialViewStart: () => appStart.start,
|
|
59
|
+
onRuntime: (rt) => appStart.attach(rt),
|
|
60
|
+
});
|
|
61
|
+
// The app start hears of every screen and render, consent or not, and a new screen is activity.
|
|
62
|
+
const rum = t.rum;
|
|
63
|
+
const startView = rum.startView.bind(rum);
|
|
64
|
+
const viewRendered = rum.viewRendered.bind(rum);
|
|
65
|
+
rum.startView = (o) => {
|
|
66
|
+
for (const l of [...activity])
|
|
67
|
+
l();
|
|
68
|
+
appStart.screenStarted();
|
|
69
|
+
startView(o);
|
|
70
|
+
};
|
|
71
|
+
rum.viewRendered = () => {
|
|
72
|
+
appStart.rendered(deps.perfNow());
|
|
73
|
+
viewRendered();
|
|
74
|
+
};
|
|
75
|
+
const shutdown = t.shutdown.bind(t);
|
|
76
|
+
t.shutdown = (opts) => {
|
|
77
|
+
appStart.close();
|
|
78
|
+
return shutdown(opts);
|
|
79
|
+
};
|
|
80
|
+
return t;
|
|
81
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a tap acts on, read from React's fiber tree. React Native hands a touch handler the touched
|
|
3
|
+
* host element; its props are reachable only through React's internal instance handle: the
|
|
4
|
+
* `__internalInstanceHandle` of the event target on Fabric's EventTarget dispatch (the default in
|
|
5
|
+
* React Native 0.88), or `_targetInst` on the legacy plugin dispatch. The walk is read-only and
|
|
6
|
+
* bounded, and reads only the props named here, never children or text. Without a handle a tap has
|
|
7
|
+
* no name and a generic selector.
|
|
8
|
+
*
|
|
9
|
+
* The target is the closest element, from the touched one up, that is interactive: it has an
|
|
10
|
+
* onPress, onLongPress or onClick handler, a button-like role, or is accessible with a responder
|
|
11
|
+
* release handler (what Pressable, the Touchables and pressable Text render). Without one, the
|
|
12
|
+
* touched element is the target.
|
|
13
|
+
*
|
|
14
|
+
* The name is the closest explicit name, from the touched element up: an o11yName prop on any
|
|
15
|
+
* component, or a RumName around the element. Else, unless accessibilityNames is false, the
|
|
16
|
+
* target's aria-label or accessibilityLabel, read from the target and the components that rendered
|
|
17
|
+
* it, and only when the target is interactive. It is at most 64 characters, white space collapsed.
|
|
18
|
+
*
|
|
19
|
+
* The selector is the target's type after up to four names of the components above it, outermost
|
|
20
|
+
* first, such as "ProjectRow > Pressable > RCTView", or "unknown". One-letter names, as a minifier
|
|
21
|
+
* leaves them, are skipped. With testIds, a testID or nativeID on the target or the components
|
|
22
|
+
* that rendered it is the selector "#<id>" when it looks like a static identifier: lowercase
|
|
23
|
+
* letters, "_" and "-" only, starting with a letter, at most 64 characters. An ID with a digit, a
|
|
24
|
+
* capital, an at sign or a space may carry a record's ID or a person's data, so it never is.
|
|
25
|
+
*/
|
|
26
|
+
import type { ReactNode } from "react";
|
|
27
|
+
/** The fields of a React fiber the walk reads. */
|
|
28
|
+
export interface FiberLike {
|
|
29
|
+
/** A host component's name, such as RCTView, or the component itself. */
|
|
30
|
+
readonly type?: unknown;
|
|
31
|
+
/** The props the element rendered with. */
|
|
32
|
+
readonly memoizedProps?: unknown;
|
|
33
|
+
/** The parent fiber. */
|
|
34
|
+
readonly return?: FiberLike | null;
|
|
35
|
+
/** A host component's instance, stable while it is mounted. */
|
|
36
|
+
readonly stateNode?: unknown;
|
|
37
|
+
}
|
|
38
|
+
/** What a tap acts on. */
|
|
39
|
+
export interface TapTarget {
|
|
40
|
+
/** The action's name, or undefined when there is no privacy-safe one. */
|
|
41
|
+
readonly name: string | undefined;
|
|
42
|
+
/** o11y.rum.element.selector. */
|
|
43
|
+
readonly selector: string;
|
|
44
|
+
/** The breadcrumb label: the name, else the target's type. */
|
|
45
|
+
readonly label: string;
|
|
46
|
+
/** Identifies the target element itself, for rage detection: the selector and an element number. */
|
|
47
|
+
readonly key: string;
|
|
48
|
+
}
|
|
49
|
+
/** What tapTarget reads besides the fiber tree. */
|
|
50
|
+
export interface TapTargetOptions {
|
|
51
|
+
/** Names from aria-label and accessibilityLabel. Default true. */
|
|
52
|
+
readonly accessibilityNames?: boolean;
|
|
53
|
+
/** Selectors from static-looking testID and nativeID values. Default false. */
|
|
54
|
+
readonly testIds?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/** Props of RumName. */
|
|
57
|
+
export interface RumNameProps {
|
|
58
|
+
/** The action name of a tap on anything inside. A label, never user data. */
|
|
59
|
+
readonly name: string;
|
|
60
|
+
/** The elements whose taps it names. */
|
|
61
|
+
readonly children?: ReactNode;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Names the taps on its children, as an o11yName prop does, for an element whose own props cannot
|
|
65
|
+
* carry o11yName. It renders its children as they are.
|
|
66
|
+
*/
|
|
67
|
+
export declare function RumName(props: RumNameProps): ReactNode;
|
|
68
|
+
/** The fiber a touch event's target belongs to, or undefined without an internal instance handle. */
|
|
69
|
+
export declare function fiberOfEvent(event: {
|
|
70
|
+
readonly target?: unknown;
|
|
71
|
+
readonly _targetInst?: unknown;
|
|
72
|
+
}): FiberLike | undefined;
|
|
73
|
+
/** What a touch on the host element of fiber acts on. */
|
|
74
|
+
export declare function tapTarget(touched: FiberLike | undefined, options?: TapTargetOptions): TapTarget;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names the taps on its children, as an o11yName prop does, for an element whose own props cannot
|
|
3
|
+
* carry o11yName. It renders its children as they are.
|
|
4
|
+
*/
|
|
5
|
+
export function RumName(props) {
|
|
6
|
+
return props.children ?? null;
|
|
7
|
+
}
|
|
8
|
+
const MAX_DEPTH = 500;
|
|
9
|
+
const MAX_NAME = 64;
|
|
10
|
+
const MAX_SELECTOR = 200;
|
|
11
|
+
const MAX_COMPONENTS = 4;
|
|
12
|
+
/** A testID that reads as a fixed name in code, never as data: no digits, no capitals, no spaces. */
|
|
13
|
+
const STATIC_ID = /^[a-z][a-z_-]{0,63}$/;
|
|
14
|
+
const COMPONENT_NAME = /^[A-Za-z_$][\w$.]+$/;
|
|
15
|
+
const HANDLERS = ["onPress", "onLongPress", "onClick"];
|
|
16
|
+
const ROLES = new Set([
|
|
17
|
+
"button",
|
|
18
|
+
"link",
|
|
19
|
+
"menuitem",
|
|
20
|
+
"tab",
|
|
21
|
+
"checkbox",
|
|
22
|
+
"switch",
|
|
23
|
+
"radio",
|
|
24
|
+
"togglebutton",
|
|
25
|
+
"imagebutton",
|
|
26
|
+
]);
|
|
27
|
+
/** The fiber a touch event's target belongs to, or undefined without an internal instance handle. */
|
|
28
|
+
export function fiberOfEvent(event) {
|
|
29
|
+
const legacy = event._targetInst;
|
|
30
|
+
if (isFiber(legacy))
|
|
31
|
+
return legacy;
|
|
32
|
+
const t = event.target;
|
|
33
|
+
if (typeof t !== "object" || t === null)
|
|
34
|
+
return undefined;
|
|
35
|
+
const handle = t.__internalInstanceHandle;
|
|
36
|
+
return isFiber(handle) ? handle : undefined;
|
|
37
|
+
}
|
|
38
|
+
function isFiber(v) {
|
|
39
|
+
return typeof v === "object" && v !== null && "memoizedProps" in v && "return" in v;
|
|
40
|
+
}
|
|
41
|
+
function propsOf(f) {
|
|
42
|
+
const p = f.memoizedProps;
|
|
43
|
+
return typeof p === "object" && p !== null ? p : {};
|
|
44
|
+
}
|
|
45
|
+
const isHost = (f) => typeof f.type === "string";
|
|
46
|
+
function interactive(f) {
|
|
47
|
+
const p = propsOf(f);
|
|
48
|
+
if (HANDLERS.some((h) => typeof p[h] === "function"))
|
|
49
|
+
return true;
|
|
50
|
+
const role = p.role ?? p.accessibilityRole;
|
|
51
|
+
if (typeof role === "string" && ROLES.has(role))
|
|
52
|
+
return true;
|
|
53
|
+
return p.accessible === true && typeof p.onResponderRelease === "function";
|
|
54
|
+
}
|
|
55
|
+
function text(v, max) {
|
|
56
|
+
if (typeof v !== "string")
|
|
57
|
+
return undefined;
|
|
58
|
+
const t = v.replace(/\s+/g, " ").trim();
|
|
59
|
+
return t === "" ? undefined : t.slice(0, max);
|
|
60
|
+
}
|
|
61
|
+
/** A component's name: a host's type, a function's or class's displayName or name, through memo and forwardRef. */
|
|
62
|
+
function componentName(type, depth = 0) {
|
|
63
|
+
if (typeof type === "string")
|
|
64
|
+
return type;
|
|
65
|
+
if (depth > 3 || (typeof type !== "function" && (typeof type !== "object" || type === null)))
|
|
66
|
+
return undefined;
|
|
67
|
+
const t = type;
|
|
68
|
+
if (typeof t.displayName === "string" && t.displayName !== "")
|
|
69
|
+
return t.displayName;
|
|
70
|
+
if (typeof type === "function")
|
|
71
|
+
return typeof t.name === "string" && t.name !== "" ? t.name : undefined;
|
|
72
|
+
return componentName(t.render, depth + 1) ?? componentName(t.type, depth + 1);
|
|
73
|
+
}
|
|
74
|
+
function usable(name) {
|
|
75
|
+
return name !== undefined && name.length > 1 && name.length <= 60 && COMPONENT_NAME.test(name);
|
|
76
|
+
}
|
|
77
|
+
/** The target and the components that rendered it, up to (not including) the next host above. */
|
|
78
|
+
function owners(target) {
|
|
79
|
+
const out = [target];
|
|
80
|
+
for (let f = target.return ?? null, n = 0; f !== null && n < MAX_DEPTH && !isHost(f); f = f.return ?? null, n++) {
|
|
81
|
+
out.push(f);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
const elementNumbers = new WeakMap();
|
|
86
|
+
let nextElement = 0;
|
|
87
|
+
function elementNumber(f) {
|
|
88
|
+
const node = typeof f.stateNode === "object" && f.stateNode !== null ? f.stateNode : f;
|
|
89
|
+
let n = elementNumbers.get(node);
|
|
90
|
+
if (n === undefined) {
|
|
91
|
+
n = ++nextElement;
|
|
92
|
+
elementNumbers.set(node, n);
|
|
93
|
+
}
|
|
94
|
+
return n;
|
|
95
|
+
}
|
|
96
|
+
/** What a touch on the host element of fiber acts on. */
|
|
97
|
+
export function tapTarget(touched, options = {}) {
|
|
98
|
+
if (touched === undefined)
|
|
99
|
+
return { name: undefined, selector: "unknown", label: "unknown", key: "unknown" };
|
|
100
|
+
let target;
|
|
101
|
+
let name;
|
|
102
|
+
let n = 0;
|
|
103
|
+
for (let f = touched; f !== null && n < MAX_DEPTH; f = f.return ?? null, n++) {
|
|
104
|
+
const p = propsOf(f);
|
|
105
|
+
name ??= text(f.type === RumName ? p.name : p.o11yName, MAX_NAME);
|
|
106
|
+
if (target === undefined && interactive(f))
|
|
107
|
+
target = f;
|
|
108
|
+
if (name !== undefined && target !== undefined)
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
const isTarget = target !== undefined;
|
|
112
|
+
target ??= touched;
|
|
113
|
+
// A host target's own element, for the selector's last step and the key.
|
|
114
|
+
const own = isHost(target) ? target : touched;
|
|
115
|
+
const rendered = owners(target);
|
|
116
|
+
if (name === undefined && isTarget && options.accessibilityNames !== false) {
|
|
117
|
+
for (const f of rendered) {
|
|
118
|
+
const p = propsOf(f);
|
|
119
|
+
name = text(p["aria-label"], MAX_NAME) ?? text(p.accessibilityLabel, MAX_NAME);
|
|
120
|
+
if (name !== undefined)
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
let selector;
|
|
125
|
+
for (const f of options.testIds === true ? rendered : []) {
|
|
126
|
+
const p = propsOf(f);
|
|
127
|
+
const id = [p.testID, p.nativeID].find((v) => typeof v === "string" && STATIC_ID.test(v));
|
|
128
|
+
if (id !== undefined) {
|
|
129
|
+
selector = `#${id}`;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const last = componentName(own.type) ?? "unknown";
|
|
134
|
+
if (selector === undefined) {
|
|
135
|
+
const steps = [last];
|
|
136
|
+
n = 0;
|
|
137
|
+
for (let f = own.return ?? null; f !== null && n < MAX_DEPTH && steps.length <= MAX_COMPONENTS; f = f.return ?? null, n++) {
|
|
138
|
+
if (isHost(f))
|
|
139
|
+
continue;
|
|
140
|
+
const c = componentName(f.type);
|
|
141
|
+
if (usable(c) && steps[0] !== c)
|
|
142
|
+
steps.unshift(c);
|
|
143
|
+
}
|
|
144
|
+
selector = steps.join(" > ").slice(0, MAX_SELECTOR);
|
|
145
|
+
}
|
|
146
|
+
return { name, selector, label: name ?? last, key: `${selector}\u0000${elementNumber(own)}` };
|
|
147
|
+
}
|
package/dist/taps.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* taps(): taps and long presses as actions, and frustration signals, from the touches
|
|
3
|
+
* RumTouchBoundary (touch-boundary.ts) passes to dispatchTouch.
|
|
4
|
+
*
|
|
5
|
+
* A gesture is one finger down and up. It is a tap when the finger moved at most TAP_SLOP points
|
|
6
|
+
* and was not cancelled (a scroll view taking the touch cancels it), and a long press when it was
|
|
7
|
+
* down LONG_PRESS_MILLIS or more. A gesture that ever had a second finger is neither. A touch
|
|
8
|
+
* start with one finger on the screen always begins a new gesture.
|
|
9
|
+
*
|
|
10
|
+
* React Native runs the responder system, and so onPress and onLongPress, before the touch events
|
|
11
|
+
* bubble to the boundary, so a tap is known only after its handler ran. The action span "action
|
|
12
|
+
* tap" (or "action long_press") starts then, at the touch's end (a long press at the moment React
|
|
13
|
+
* Native's Pressability fires onLongPress, LONG_PRESS_MILLIS after the touch began), as a root
|
|
14
|
+
* with o11y.rum.span.shape action, the type, the name, o11y.rum.element.selector and the view ID,
|
|
15
|
+
* linked to the view span (tap-target.ts names the target). It is the open action until it
|
|
16
|
+
* settles, by the browser's rule: the RUM context manager makes it the parent of requests started
|
|
17
|
+
* meanwhile, and those requests, the requests started since the touch began (the ones its handler
|
|
18
|
+
* started) and view starts are its activity. A view the handler navigated to starts after the
|
|
19
|
+
* tap opened (router adapters start it when the new navigation state is committed), so the tap is
|
|
20
|
+
* its cause. Each tap touches the session, and its breadcrumb (type and name, else the target's
|
|
21
|
+
* type) is published when it starts.
|
|
22
|
+
*
|
|
23
|
+
* Frustration (when frustration is not false): rageTaps taps on one element within a second are a
|
|
24
|
+
* rage signal, and an error within a second of a tap, or while its action is open, an error
|
|
25
|
+
* signal, as the browser's actions() detects them for clicks. Dead taps are not detected: React
|
|
26
|
+
* Native has no mutation signal to tell that a tap changed nothing. Each signal counts in the
|
|
27
|
+
* summary, is published, retains the session and is emitted as o11y.rum.frustration in the open
|
|
28
|
+
* action's context. Stop removes the collector from the boundary, ends the open action and
|
|
29
|
+
* cancels every timer.
|
|
30
|
+
*/
|
|
31
|
+
import { type RumCollector, type SettleTimers } from "@libo11y/rum/core";
|
|
32
|
+
/** How far, in points, a finger may move and still tap. */
|
|
33
|
+
export declare const TAP_SLOP = 10;
|
|
34
|
+
/** A touch held this long is a long press, as React Native's Pressability defaults its delayLongPress. */
|
|
35
|
+
export declare const LONG_PRESS_MILLIS = 500;
|
|
36
|
+
/** Options of taps(). */
|
|
37
|
+
export interface TapsOptions {
|
|
38
|
+
/** How long the app must be idle before a tap's action settles. Default 100. */
|
|
39
|
+
readonly idleMillis?: number;
|
|
40
|
+
/** A tap's action ends this long after it started at the latest. Default 10000. */
|
|
41
|
+
readonly capMillis?: number;
|
|
42
|
+
/** Detect rage and error taps. Default true. */
|
|
43
|
+
readonly frustration?: boolean;
|
|
44
|
+
/** Taps on one element within a second that make a rage signal. Default 3. */
|
|
45
|
+
readonly rageTaps?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Names taps from the tapped element's aria-label or accessibilityLabel when no o11yName or
|
|
48
|
+
* RumName names them. Default true. Set false when labels may carry a person's data.
|
|
49
|
+
*/
|
|
50
|
+
readonly accessibilityNames?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Uses a testID or nativeID as the selector "#<id>" when it looks like a static identifier
|
|
53
|
+
* (lowercase letters, "_" and "-", at most 64 characters). Default false: IDs built from data,
|
|
54
|
+
* such as a person's email or a record's ID, would leave the phone in the selector.
|
|
55
|
+
*/
|
|
56
|
+
readonly testIds?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/** The parts of a React Native touch event the collector reads. */
|
|
59
|
+
export interface TouchEventLike {
|
|
60
|
+
/** The touch as the platform reported it, in points from the screen's top left. */
|
|
61
|
+
readonly nativeEvent?: {
|
|
62
|
+
readonly pageX?: unknown;
|
|
63
|
+
readonly pageY?: unknown;
|
|
64
|
+
/** The touches still on the screen, the one that started included and the one that ended not. */
|
|
65
|
+
readonly touches?: unknown;
|
|
66
|
+
} | null;
|
|
67
|
+
/** The touched element: a node with Fabric's internal instance handle, or a view tag. */
|
|
68
|
+
readonly target?: unknown;
|
|
69
|
+
/** The touched element's fiber on React Native's legacy event dispatch. */
|
|
70
|
+
readonly _targetInst?: unknown;
|
|
71
|
+
}
|
|
72
|
+
/** Where a phase of a touch goes. */
|
|
73
|
+
export type TouchPhase = "start" | "end" | "cancel";
|
|
74
|
+
/**
|
|
75
|
+
* Passes one phase of a touch to every running taps() collector. RumTouchBoundary calls it from
|
|
76
|
+
* its touch handlers; it never throws.
|
|
77
|
+
*/
|
|
78
|
+
export declare function dispatchTouch(phase: TouchPhase, event: TouchEventLike): void;
|
|
79
|
+
/** The taps collector over timers. */
|
|
80
|
+
export declare function tapsWith(timers: SettleTimers, opts?: TapsOptions): RumCollector;
|
package/dist/taps.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* taps(): taps and long presses as actions, and frustration signals, from the touches
|
|
3
|
+
* RumTouchBoundary (touch-boundary.ts) passes to dispatchTouch.
|
|
4
|
+
*
|
|
5
|
+
* A gesture is one finger down and up. It is a tap when the finger moved at most TAP_SLOP points
|
|
6
|
+
* and was not cancelled (a scroll view taking the touch cancels it), and a long press when it was
|
|
7
|
+
* down LONG_PRESS_MILLIS or more. A gesture that ever had a second finger is neither. A touch
|
|
8
|
+
* start with one finger on the screen always begins a new gesture.
|
|
9
|
+
*
|
|
10
|
+
* React Native runs the responder system, and so onPress and onLongPress, before the touch events
|
|
11
|
+
* bubble to the boundary, so a tap is known only after its handler ran. The action span "action
|
|
12
|
+
* tap" (or "action long_press") starts then, at the touch's end (a long press at the moment React
|
|
13
|
+
* Native's Pressability fires onLongPress, LONG_PRESS_MILLIS after the touch began), as a root
|
|
14
|
+
* with o11y.rum.span.shape action, the type, the name, o11y.rum.element.selector and the view ID,
|
|
15
|
+
* linked to the view span (tap-target.ts names the target). It is the open action until it
|
|
16
|
+
* settles, by the browser's rule: the RUM context manager makes it the parent of requests started
|
|
17
|
+
* meanwhile, and those requests, the requests started since the touch began (the ones its handler
|
|
18
|
+
* started) and view starts are its activity. A view the handler navigated to starts after the
|
|
19
|
+
* tap opened (router adapters start it when the new navigation state is committed), so the tap is
|
|
20
|
+
* its cause. Each tap touches the session, and its breadcrumb (type and name, else the target's
|
|
21
|
+
* type) is published when it starts.
|
|
22
|
+
*
|
|
23
|
+
* Frustration (when frustration is not false): rageTaps taps on one element within a second are a
|
|
24
|
+
* rage signal, and an error within a second of a tap, or while its action is open, an error
|
|
25
|
+
* signal, as the browser's actions() detects them for clicks. Dead taps are not detected: React
|
|
26
|
+
* Native has no mutation signal to tell that a tap changed nothing. Each signal counts in the
|
|
27
|
+
* summary, is published, retains the session and is emitted as o11y.rum.frustration in the open
|
|
28
|
+
* action's context. Stop removes the collector from the boundary, ends the open action and
|
|
29
|
+
* cancels every timer.
|
|
30
|
+
*/
|
|
31
|
+
import { actionSpanName, actionSpanStart, createFrustrationDetector, createSettleTracker, } from "@libo11y/rum/core";
|
|
32
|
+
import { ATTR_RUM_ELEMENT_SELECTOR, ATTR_RUM_FRUSTRATION_TYPE, EVENT_RUM_FRUSTRATION, } from "@libo11y/spec";
|
|
33
|
+
import { context, diag, ROOT_CONTEXT, trace } from "@opentelemetry/api";
|
|
34
|
+
import { fiberOfEvent, tapTarget } from "./tap-target.js";
|
|
35
|
+
/** How far, in points, a finger may move and still tap. */
|
|
36
|
+
export const TAP_SLOP = 10;
|
|
37
|
+
/** A touch held this long is a long press, as React Native's Pressability defaults its delayLongPress. */
|
|
38
|
+
export const LONG_PRESS_MILLIS = 500;
|
|
39
|
+
/** Requests remembered while in flight, so a tap can adopt the ones its handler started. */
|
|
40
|
+
const MAX_IN_FLIGHT = 256;
|
|
41
|
+
/** The running collectors' sinks. RumTouchBoundary dispatches every touch to them. */
|
|
42
|
+
const sinks = new Set();
|
|
43
|
+
/**
|
|
44
|
+
* Passes one phase of a touch to every running taps() collector. RumTouchBoundary calls it from
|
|
45
|
+
* its touch handlers; it never throws.
|
|
46
|
+
*/
|
|
47
|
+
export function dispatchTouch(phase, event) {
|
|
48
|
+
for (const sink of [...sinks]) {
|
|
49
|
+
try {
|
|
50
|
+
sink(phase, event);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
diag.warn("o11y: rum: a touch could not be recorded", err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function num(v) {
|
|
58
|
+
return typeof v === "number" && Number.isFinite(v) ? v : undefined;
|
|
59
|
+
}
|
|
60
|
+
/** How many fingers the event says are on the screen. */
|
|
61
|
+
function fingers(e) {
|
|
62
|
+
const t = e.nativeEvent?.touches;
|
|
63
|
+
return Array.isArray(t) ? t.length : 0;
|
|
64
|
+
}
|
|
65
|
+
/** The taps collector over timers. */
|
|
66
|
+
export function tapsWith(timers, opts = {}) {
|
|
67
|
+
const idleMillis = opts.idleMillis ?? 100;
|
|
68
|
+
const capMillis = opts.capMillis ?? 10_000;
|
|
69
|
+
const targetOptions = {
|
|
70
|
+
...(opts.accessibilityNames === undefined ? {} : { accessibilityNames: opts.accessibilityNames }),
|
|
71
|
+
...(opts.testIds === undefined ? {} : { testIds: opts.testIds }),
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
name: "actions",
|
|
75
|
+
start(rt) {
|
|
76
|
+
let current;
|
|
77
|
+
/** The span of the last tap, so an error counts against it only while its own action is open. */
|
|
78
|
+
let lastTapSpan;
|
|
79
|
+
let gesture;
|
|
80
|
+
/** Requests in flight: span ID to start time. */
|
|
81
|
+
const inFlight = new Map();
|
|
82
|
+
const signal = (type, key, at) => {
|
|
83
|
+
const selector = key.split("\u0000")[0] ?? key;
|
|
84
|
+
rt.summary.count(`frustrations.${type}`);
|
|
85
|
+
rt.bus.publish({ type: "frustration", frustration: type, selector, at });
|
|
86
|
+
rt.session.retain("frustration");
|
|
87
|
+
const action = current?.span;
|
|
88
|
+
rt.emit(EVENT_RUM_FRUSTRATION, { [ATTR_RUM_FRUSTRATION_TYPE]: type, [ATTR_RUM_ELEMENT_SELECTOR]: selector }, action === undefined ? undefined : trace.setSpan(context.active(), action));
|
|
89
|
+
};
|
|
90
|
+
const detector = opts.frustration === false
|
|
91
|
+
? undefined
|
|
92
|
+
: createFrustrationDetector({
|
|
93
|
+
timers,
|
|
94
|
+
onSignal: signal,
|
|
95
|
+
...(opts.rageTaps === undefined ? {} : { rageClicks: opts.rageTaps }),
|
|
96
|
+
});
|
|
97
|
+
const begin = (type, g) => {
|
|
98
|
+
current?.settle.end();
|
|
99
|
+
const now = rt.now();
|
|
100
|
+
const start = type === "long_press" ? Math.min(now, g.at + LONG_PRESS_MILLIS) : now;
|
|
101
|
+
const target = tapTarget(g.fiber, targetOptions);
|
|
102
|
+
const span = rt.tracer.startSpan(actionSpanName(type), {
|
|
103
|
+
startTime: rt.timeOrigin + start,
|
|
104
|
+
...actionSpanStart(rt, type, target.name, { [ATTR_RUM_ELEMENT_SELECTOR]: target.selector }),
|
|
105
|
+
}, ROOT_CONTEXT);
|
|
106
|
+
const settle = createSettleTracker({
|
|
107
|
+
start,
|
|
108
|
+
idleMillis,
|
|
109
|
+
capMillis,
|
|
110
|
+
now: rt.now,
|
|
111
|
+
timers,
|
|
112
|
+
onSettle: (end) => {
|
|
113
|
+
span.end(rt.timeOrigin + end);
|
|
114
|
+
if (current?.span === span)
|
|
115
|
+
current = undefined;
|
|
116
|
+
if (rt.activeAction() === span)
|
|
117
|
+
rt.setActiveAction(undefined);
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
// The requests the tap's handler started ran before the tap was known.
|
|
121
|
+
for (const [id, at] of inFlight)
|
|
122
|
+
if (at >= g.at)
|
|
123
|
+
settle.requestStarted(id, at);
|
|
124
|
+
current = { span, settle };
|
|
125
|
+
rt.setActiveAction(span);
|
|
126
|
+
rt.session.touch();
|
|
127
|
+
rt.bus.publish({ type: "breadcrumb", kind: type, label: target.label, at: start });
|
|
128
|
+
lastTapSpan = span;
|
|
129
|
+
detector?.click(target.key, start, false);
|
|
130
|
+
};
|
|
131
|
+
const onTouch = (phase, e) => {
|
|
132
|
+
if (phase === "cancel") {
|
|
133
|
+
gesture = undefined;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const x = num(e.nativeEvent?.pageX);
|
|
137
|
+
const y = num(e.nativeEvent?.pageY);
|
|
138
|
+
if (phase === "start") {
|
|
139
|
+
const n = fingers(e);
|
|
140
|
+
// One finger on the screen starts a gesture, whatever an earlier one left (an end that
|
|
141
|
+
// never bubbled to the boundary). A second finger, or a start whose count is unknown
|
|
142
|
+
// while a gesture is open, makes it a multi-touch gesture.
|
|
143
|
+
if (n > 1 || (n === 0 && gesture !== undefined)) {
|
|
144
|
+
if (gesture !== undefined)
|
|
145
|
+
gesture.multi = true;
|
|
146
|
+
else
|
|
147
|
+
gesture = { at: rt.now(), x, y, fiber: undefined, multi: true };
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
gesture = { at: rt.now(), x, y, fiber: fiberOfEvent(e), multi: false };
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const g = gesture;
|
|
154
|
+
if (g === undefined)
|
|
155
|
+
return;
|
|
156
|
+
if (fingers(e) > 0) {
|
|
157
|
+
g.multi = true;
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
gesture = undefined;
|
|
161
|
+
if (g.multi)
|
|
162
|
+
return;
|
|
163
|
+
if (x !== undefined && y !== undefined && g.x !== undefined && g.y !== undefined) {
|
|
164
|
+
if (Math.hypot(x - g.x, y - g.y) > TAP_SLOP)
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
begin(rt.now() - g.at >= LONG_PRESS_MILLIS ? "long_press" : "tap", g);
|
|
168
|
+
};
|
|
169
|
+
sinks.add(onTouch);
|
|
170
|
+
const offBus = rt.bus.subscribe((s) => {
|
|
171
|
+
const a = current;
|
|
172
|
+
if (s.type === "request-start") {
|
|
173
|
+
inFlight.set(s.spanId, s.at);
|
|
174
|
+
if (inFlight.size > MAX_IN_FLIGHT) {
|
|
175
|
+
const oldest = inFlight.keys().next().value;
|
|
176
|
+
if (oldest !== undefined)
|
|
177
|
+
inFlight.delete(oldest);
|
|
178
|
+
}
|
|
179
|
+
if (a !== undefined && s.parentSpanId === a.span.spanContext().spanId) {
|
|
180
|
+
a.settle.requestStarted(s.spanId, s.at);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else if (s.type === "request-end") {
|
|
184
|
+
inFlight.delete(s.spanId);
|
|
185
|
+
a?.settle.requestEnded(s.spanId, s.at);
|
|
186
|
+
}
|
|
187
|
+
else if (s.type === "error")
|
|
188
|
+
detector?.error(s.at, a !== undefined && a.span === lastTapSpan);
|
|
189
|
+
});
|
|
190
|
+
const offViews = rt.views.subscribe((e) => {
|
|
191
|
+
if (e.type === "start")
|
|
192
|
+
current?.settle.activity(rt.now());
|
|
193
|
+
});
|
|
194
|
+
return () => {
|
|
195
|
+
sinks.delete(onTouch);
|
|
196
|
+
offBus();
|
|
197
|
+
offViews();
|
|
198
|
+
current?.settle.end();
|
|
199
|
+
current = undefined;
|
|
200
|
+
gesture = undefined;
|
|
201
|
+
inFlight.clear();
|
|
202
|
+
detector?.stop();
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|