@oodle-ai/mobile-sdk-reactnative 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/LICENSE +201 -0
- package/OodleMobileSdkReactNative.podspec +24 -0
- package/README.md +191 -2
- package/android/build.gradle +55 -0
- package/android/proguard-rules.pro +5 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/ai/oodle/rum/Connectivity.kt +19 -0
- package/android/src/main/java/ai/oodle/rum/CrashReporter.kt +88 -0
- package/android/src/main/java/ai/oodle/rum/DeviceInfo.kt +50 -0
- package/android/src/main/java/ai/oodle/rum/ExitReasons.kt +104 -0
- package/android/src/main/java/ai/oodle/rum/NativeEvents.kt +24 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumModule.kt +96 -0
- package/android/src/main/java/ai/oodle/rum/OodleRumPackage.kt +26 -0
- package/android/src/main/java/ai/oodle/rum/Storage.kt +84 -0
- package/android/src/main/java/ai/oodle/rum/Vitals.kt +188 -0
- package/android/src/main/java/ai/oodle/rum/Watchdog.kt +81 -0
- package/android/src/main/java/ai/oodle/rum/WireframeCapture.kt +398 -0
- package/bin/oodle-rum-upload.mjs +104 -0
- package/ios/OodleCrashReporter.h +16 -0
- package/ios/OodleCrashReporter.m +202 -0
- package/ios/OodleRum.h +18 -0
- package/ios/OodleRum.mm +129 -0
- package/ios/OodleRumImpl.swift +450 -0
- package/ios/WireframeWalker.swift +279 -0
- package/lib/core/clock.d.ts +19 -0
- package/lib/core/clock.js +47 -0
- package/lib/core/clock.js.map +1 -0
- package/lib/core/config.d.ts +142 -0
- package/lib/core/config.js +178 -0
- package/lib/core/config.js.map +1 -0
- package/lib/core/consent.d.ts +5 -0
- package/lib/core/consent.js +33 -0
- package/lib/core/consent.js.map +1 -0
- package/lib/core/context.d.ts +36 -0
- package/lib/core/context.js +172 -0
- package/lib/core/context.js.map +1 -0
- package/lib/core/flags.d.ts +3 -0
- package/lib/core/flags.js +20 -0
- package/lib/core/flags.js.map +1 -0
- package/lib/core/ids.d.ts +7 -0
- package/lib/core/ids.js +40 -0
- package/lib/core/ids.js.map +1 -0
- package/lib/core/internal.d.ts +6 -0
- package/lib/core/internal.js +10 -0
- package/lib/core/internal.js.map +1 -0
- package/lib/core/logger.d.ts +8 -0
- package/lib/core/logger.js +38 -0
- package/lib/core/logger.js.map +1 -0
- package/lib/core/native.d.ts +71 -0
- package/lib/core/native.js +156 -0
- package/lib/core/native.js.map +1 -0
- package/lib/core/rate-limiter.d.ts +7 -0
- package/lib/core/rate-limiter.js +31 -0
- package/lib/core/rate-limiter.js.map +1 -0
- package/lib/core/session.d.ts +51 -0
- package/lib/core/session.js +196 -0
- package/lib/core/session.js.map +1 -0
- package/lib/core/size.d.ts +15 -0
- package/lib/core/size.js +68 -0
- package/lib/core/size.js.map +1 -0
- package/lib/core/tags.d.ts +10 -0
- package/lib/core/tags.js +33 -0
- package/lib/core/tags.js.map +1 -0
- package/lib/core/telemetry.d.ts +6 -0
- package/lib/core/telemetry.js +37 -0
- package/lib/core/telemetry.js.map +1 -0
- package/lib/core/tracing.d.ts +9 -0
- package/lib/core/tracing.js +57 -0
- package/lib/core/tracing.js.map +1 -0
- package/lib/core/transport.d.ts +76 -0
- package/lib/core/transport.js +560 -0
- package/lib/core/transport.js.map +1 -0
- package/lib/core/user.d.ts +12 -0
- package/lib/core/user.js +41 -0
- package/lib/core/user.js.map +1 -0
- package/lib/events/actions.d.ts +13 -0
- package/lib/events/actions.js +147 -0
- package/lib/events/actions.js.map +1 -0
- package/lib/events/emit.d.ts +21 -0
- package/lib/events/emit.js +144 -0
- package/lib/events/emit.js.map +1 -0
- package/lib/events/errors.d.ts +19 -0
- package/lib/events/errors.js +257 -0
- package/lib/events/errors.js.map +1 -0
- package/lib/events/lifecycle.d.ts +7 -0
- package/lib/events/lifecycle.js +101 -0
- package/lib/events/lifecycle.js.map +1 -0
- package/lib/events/longtasks.d.ts +2 -0
- package/lib/events/longtasks.js +42 -0
- package/lib/events/longtasks.js.map +1 -0
- package/lib/events/resources.d.ts +15 -0
- package/lib/events/resources.js +156 -0
- package/lib/events/resources.js.map +1 -0
- package/lib/events/tap-target.d.ts +42 -0
- package/lib/events/tap-target.js +191 -0
- package/lib/events/tap-target.js.map +1 -0
- package/lib/events/view-state.d.ts +13 -0
- package/lib/events/view-state.js +25 -0
- package/lib/events/view-state.js.map +1 -0
- package/lib/events/views.d.ts +21 -0
- package/lib/events/views.js +347 -0
- package/lib/events/views.js.map +1 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +24 -0
- package/lib/index.js.map +1 -0
- package/lib/navigation/react-navigation.d.ts +33 -0
- package/lib/navigation/react-navigation.js +61 -0
- package/lib/navigation/react-navigation.js.map +1 -0
- package/lib/react/OodlePrivacy.d.ts +15 -0
- package/lib/react/OodlePrivacy.js +13 -0
- package/lib/react/OodlePrivacy.js.map +1 -0
- package/lib/react/OodleProvider.d.ts +18 -0
- package/lib/react/OodleProvider.js +72 -0
- package/lib/react/OodleProvider.js.map +1 -0
- package/lib/replay/recorder.d.ts +22 -0
- package/lib/replay/recorder.js +317 -0
- package/lib/replay/recorder.js.map +1 -0
- package/lib/replay/rrweb.d.ts +105 -0
- package/lib/replay/rrweb.js +335 -0
- package/lib/replay/rrweb.js.map +1 -0
- package/lib/rum.d.ts +52 -0
- package/lib/rum.js +281 -0
- package/lib/rum.js.map +1 -0
- package/lib/specs/NativeOodleRum.d.ts +43 -0
- package/lib/specs/NativeOodleRum.js +5 -0
- package/lib/specs/NativeOodleRum.js.map +1 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +6 -0
- package/lib/version.js.map +1 -0
- package/package.json +91 -4
- package/react-native.config.js +10 -0
- package/src/core/clock.ts +43 -0
- package/src/core/config.ts +344 -0
- package/src/core/consent.ts +32 -0
- package/src/core/context.ts +189 -0
- package/src/core/flags.ts +15 -0
- package/src/core/ids.ts +35 -0
- package/src/core/internal.ts +6 -0
- package/src/core/logger.ts +37 -0
- package/src/core/native.ts +201 -0
- package/src/core/rate-limiter.ts +34 -0
- package/src/core/session.ts +209 -0
- package/src/core/size.ts +79 -0
- package/src/core/tags.ts +30 -0
- package/src/core/telemetry.ts +35 -0
- package/src/core/tracing.ts +60 -0
- package/src/core/transport.ts +590 -0
- package/src/core/user.ts +45 -0
- package/src/events/actions.ts +157 -0
- package/src/events/emit.ts +131 -0
- package/src/events/errors.ts +283 -0
- package/src/events/lifecycle.ts +102 -0
- package/src/events/longtasks.ts +40 -0
- package/src/events/resources.ts +167 -0
- package/src/events/tap-target.ts +210 -0
- package/src/events/view-state.ts +30 -0
- package/src/events/views.ts +356 -0
- package/src/index.ts +34 -0
- package/src/navigation/react-navigation.ts +72 -0
- package/src/react/OodlePrivacy.tsx +25 -0
- package/src/react/OodleProvider.tsx +88 -0
- package/src/replay/recorder.ts +310 -0
- package/src/replay/rrweb.ts +460 -0
- package/src/rum.ts +295 -0
- package/src/specs/NativeOodleRum.ts +51 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getConfig } from '../core/config';
|
|
2
|
+
import { emit } from './emit';
|
|
3
|
+
import { isoTime } from '../core/clock';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A timer that should fire every TICK_MS. When the JS thread is busy the
|
|
7
|
+
* timer fires late, and the delay is the time the thread was blocked.
|
|
8
|
+
* A timer is used rather than a frame callback so an idle app does not
|
|
9
|
+
* wake the JS thread on every frame.
|
|
10
|
+
*/
|
|
11
|
+
const TICK_MS = 50;
|
|
12
|
+
|
|
13
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
14
|
+
let expected = 0;
|
|
15
|
+
|
|
16
|
+
function tick() {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
const blocked = now - expected;
|
|
19
|
+
const threshold = getConfig()?.longTaskThresholdMs ?? 0;
|
|
20
|
+
if (threshold > 0 && blocked >= threshold) {
|
|
21
|
+
emit({
|
|
22
|
+
event_type: 'long_task',
|
|
23
|
+
timestamp: isoTime(now - blocked),
|
|
24
|
+
long_task_duration_ms: blocked,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
expected = now + TICK_MS;
|
|
28
|
+
timer = setTimeout(tick, TICK_MS);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function startLongTaskTracking() {
|
|
32
|
+
if (timer || !(getConfig()?.longTaskThresholdMs ?? 0)) return;
|
|
33
|
+
expected = Date.now() + TICK_MS;
|
|
34
|
+
timer = setTimeout(tick, TICK_MS);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function stopLongTaskTracking() {
|
|
38
|
+
if (timer) clearTimeout(timer);
|
|
39
|
+
timer = null;
|
|
40
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { getConfig } from '../core/config';
|
|
2
|
+
import { INTERNAL_REQUEST } from '../core/internal';
|
|
3
|
+
import { newTraceIds, tracingHeaders } from '../core/tracing';
|
|
4
|
+
import { emit } from './emit';
|
|
5
|
+
|
|
6
|
+
interface TrackedXHR {
|
|
7
|
+
[INTERNAL_REQUEST]?: boolean;
|
|
8
|
+
__oodle?: {
|
|
9
|
+
method: string;
|
|
10
|
+
url: string;
|
|
11
|
+
headers: Map<string, string>;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type XHRProto = {
|
|
16
|
+
open: (...args: unknown[]) => void;
|
|
17
|
+
send: (body?: unknown) => void;
|
|
18
|
+
setRequestHeader: (name: string, value: string) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
let originals: XHRProto | null = null;
|
|
22
|
+
|
|
23
|
+
function now(): number {
|
|
24
|
+
const p = (globalThis as unknown as { performance?: { now?: () => number } }).performance;
|
|
25
|
+
return typeof p?.now === 'function' ? p.now() : Date.now();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The URL without query string or fragment, which can hold tokens. */
|
|
29
|
+
/** The trace of a W3C `traceparent` header (version 00), or null. */
|
|
30
|
+
export function parseTraceparent(
|
|
31
|
+
value: string,
|
|
32
|
+
): { traceId: string; spanId: string; sampled: boolean } | null {
|
|
33
|
+
const m = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i.exec(value.trim());
|
|
34
|
+
if (!m || /^0+$/.test(m[1]) || /^0+$/.test(m[2])) return null;
|
|
35
|
+
return { traceId: m[1].toLowerCase(), spanId: m[2].toLowerCase(), sampled: (parseInt(m[3], 16) & 1) === 1 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function stripQuery(url: string): string {
|
|
39
|
+
const cut = url.search(/[?#]/);
|
|
40
|
+
return cut >= 0 ? url.slice(0, cut) : url;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isCollectorUrl(url: string): boolean {
|
|
44
|
+
const endpoint = getConfig()?.endpoint;
|
|
45
|
+
return Boolean(endpoint && url.startsWith(endpoint));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* React Native's fetch runs on XMLHttpRequest, so patching XHR records
|
|
50
|
+
* both. `responseType` tells them apart: the fetch polyfill asks for a
|
|
51
|
+
* blob, and app code that uses XHR directly rarely does.
|
|
52
|
+
*/
|
|
53
|
+
export function installResourceTracking() {
|
|
54
|
+
const XHR = (globalThis as unknown as { XMLHttpRequest?: { prototype: XHRProto } })
|
|
55
|
+
.XMLHttpRequest;
|
|
56
|
+
if (!XHR || originals) return;
|
|
57
|
+
const proto = XHR.prototype;
|
|
58
|
+
originals = { open: proto.open, send: proto.send, setRequestHeader: proto.setRequestHeader };
|
|
59
|
+
const orig = originals;
|
|
60
|
+
|
|
61
|
+
proto.open = function (this: TrackedXHR, ...args: unknown[]) {
|
|
62
|
+
const method = typeof args[0] === 'string' ? args[0].toUpperCase() : 'GET';
|
|
63
|
+
const url = typeof args[1] === 'string' ? args[1] : String(args[1] ?? '');
|
|
64
|
+
this.__oodle = { method, url, headers: new Map<string, string>() };
|
|
65
|
+
return orig.open.apply(this, args);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
proto.setRequestHeader = function (this: TrackedXHR, name: string, value: string) {
|
|
69
|
+
this.__oodle?.headers.set(String(name).toLowerCase(), String(value));
|
|
70
|
+
return orig.setRequestHeader.call(this, name, value);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
proto.send = function (this: TrackedXHR & XMLHttpRequest, body?: unknown) {
|
|
74
|
+
const info = this.__oodle;
|
|
75
|
+
const config = getConfig();
|
|
76
|
+
if (!info || this[INTERNAL_REQUEST] || !config || isCollectorUrl(info.url)) {
|
|
77
|
+
return orig.send.call(this, body);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let traceId = '';
|
|
81
|
+
let spanId = '';
|
|
82
|
+
const rule = config.tracingUrls.find((r) => r.test(info.url));
|
|
83
|
+
const existing = info.headers.get('traceparent');
|
|
84
|
+
if (existing !== undefined) {
|
|
85
|
+
// The app or another tracing SDK set the header. It is kept, and the
|
|
86
|
+
// resource gets its trace, so RUM links to the same backend trace.
|
|
87
|
+
const parent = parseTraceparent(existing);
|
|
88
|
+
if (parent?.sampled) {
|
|
89
|
+
traceId = parent.traceId;
|
|
90
|
+
spanId = parent.spanId;
|
|
91
|
+
}
|
|
92
|
+
} else if (rule) {
|
|
93
|
+
const ids = newTraceIds(config.traceSampleRate);
|
|
94
|
+
for (const [k, v] of Object.entries(tracingHeaders(ids, rule.propagators))) {
|
|
95
|
+
if (!info.headers.has(k)) orig.setRequestHeader.call(this, k, v);
|
|
96
|
+
}
|
|
97
|
+
if (ids.sampled) {
|
|
98
|
+
traceId = ids.traceId;
|
|
99
|
+
spanId = ids.spanId;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const start = now();
|
|
104
|
+
let reported = false;
|
|
105
|
+
const done = () => {
|
|
106
|
+
if (reported) return;
|
|
107
|
+
reported = true;
|
|
108
|
+
const status = this.status || 0;
|
|
109
|
+
const duration = Math.round(now() - start);
|
|
110
|
+
let size = 0;
|
|
111
|
+
try {
|
|
112
|
+
const len = this.getResponseHeader?.('content-length');
|
|
113
|
+
if (len) size = parseInt(len, 10) || 0;
|
|
114
|
+
} catch {
|
|
115
|
+
// header access can throw after an abort
|
|
116
|
+
}
|
|
117
|
+
const type = this.responseType === 'blob' ? 'fetch' : 'xhr';
|
|
118
|
+
const data: Record<string, unknown> = {
|
|
119
|
+
event_type: 'resource',
|
|
120
|
+
url_full: stripQuery(info.url),
|
|
121
|
+
http_request_method: info.method,
|
|
122
|
+
http_response_status_code: status,
|
|
123
|
+
resource_duration_ms: duration,
|
|
124
|
+
http_response_body_size: size,
|
|
125
|
+
resource_type: type,
|
|
126
|
+
};
|
|
127
|
+
if (traceId) {
|
|
128
|
+
data.trace_id = traceId;
|
|
129
|
+
data.span_id = spanId;
|
|
130
|
+
}
|
|
131
|
+
emit(data);
|
|
132
|
+
if (status === 0) {
|
|
133
|
+
emit({
|
|
134
|
+
event_type: 'error',
|
|
135
|
+
exception_message: `${info.method} ${stripQuery(info.url)} failed: network error`,
|
|
136
|
+
exception_type: 'NetworkError',
|
|
137
|
+
exception_stacktrace: '',
|
|
138
|
+
error_source: 'network',
|
|
139
|
+
error_is_crash: 0,
|
|
140
|
+
url_full: stripQuery(info.url),
|
|
141
|
+
http_request_method: info.method,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
try {
|
|
146
|
+
this.addEventListener('loadend', done);
|
|
147
|
+
} catch {
|
|
148
|
+
// older XHR without addEventListener: fall back to readyState
|
|
149
|
+
const prev = this.onreadystatechange;
|
|
150
|
+
this.onreadystatechange = function (this: XMLHttpRequest, ev: Event) {
|
|
151
|
+
if (this.readyState === 4) done();
|
|
152
|
+
if (prev) return (prev as (e: Event) => unknown).call(this, ev);
|
|
153
|
+
} as XMLHttpRequest['onreadystatechange'];
|
|
154
|
+
}
|
|
155
|
+
return orig.send.call(this, body);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function uninstallResourceTracking() {
|
|
160
|
+
const XHR = (globalThis as unknown as { XMLHttpRequest?: { prototype: XHRProto } })
|
|
161
|
+
.XMLHttpRequest;
|
|
162
|
+
if (!XHR || !originals) return;
|
|
163
|
+
XHR.prototype.open = originals.open;
|
|
164
|
+
XHR.prototype.send = originals.send;
|
|
165
|
+
XHR.prototype.setRequestHeader = originals.setRequestHeader;
|
|
166
|
+
originals = null;
|
|
167
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds what a tap hit by walking the React fiber tree from the touched
|
|
3
|
+
* host view. React does not expose fibers as public API, so every field
|
|
4
|
+
* read here is guarded, and a failure yields a generic name instead of an
|
|
5
|
+
* exception.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface FiberLike {
|
|
9
|
+
tag?: number;
|
|
10
|
+
type?: unknown;
|
|
11
|
+
elementType?: unknown;
|
|
12
|
+
memoizedProps?: unknown;
|
|
13
|
+
return?: FiberLike | null;
|
|
14
|
+
child?: FiberLike | null;
|
|
15
|
+
sibling?: FiberLike | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TapTarget {
|
|
19
|
+
/** `Pressable ("Notifications, 1 unread")` */
|
|
20
|
+
name: string;
|
|
21
|
+
/** `Pressable` */
|
|
22
|
+
component: string;
|
|
23
|
+
/** `Notifications, 1 unread`, or empty */
|
|
24
|
+
label: string;
|
|
25
|
+
/** Component path above the target, outermost first. */
|
|
26
|
+
path: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ResolveOptions {
|
|
30
|
+
actionNameAttribute: string;
|
|
31
|
+
useAccessibilityLabel: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const MAX_DEPTH = 40;
|
|
35
|
+
const MAX_TEXT_NODES = 40;
|
|
36
|
+
const MAX_LABEL_CHARS = 100;
|
|
37
|
+
const HANDLER_PROPS = ['onPress', 'onLongPress', 'onValueChange', 'onPressIn'];
|
|
38
|
+
const BUTTON_ROLES = new Set([
|
|
39
|
+
'button',
|
|
40
|
+
'link',
|
|
41
|
+
'switch',
|
|
42
|
+
'checkbox',
|
|
43
|
+
'radio',
|
|
44
|
+
'tab',
|
|
45
|
+
'menuitem',
|
|
46
|
+
'togglebutton',
|
|
47
|
+
'imagebutton',
|
|
48
|
+
]);
|
|
49
|
+
/** Host component names that say nothing about what was tapped. */
|
|
50
|
+
const GENERIC_HOSTS = new Set(['RCTView', 'View', 'RCTText', 'Text', 'RCTScrollView', 'AndroidHorizontalScrollView']);
|
|
51
|
+
|
|
52
|
+
function props(f: FiberLike): Record<string, unknown> {
|
|
53
|
+
const p = f.memoizedProps;
|
|
54
|
+
return p && typeof p === 'object' ? (p as Record<string, unknown>) : {};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function componentName(type: unknown): string {
|
|
58
|
+
if (typeof type === 'string') return type;
|
|
59
|
+
if (!type || (typeof type !== 'function' && typeof type !== 'object')) return '';
|
|
60
|
+
const t = type as {
|
|
61
|
+
displayName?: unknown;
|
|
62
|
+
name?: unknown;
|
|
63
|
+
render?: { displayName?: unknown; name?: unknown };
|
|
64
|
+
type?: unknown;
|
|
65
|
+
};
|
|
66
|
+
if (typeof t.displayName === 'string' && t.displayName) return t.displayName;
|
|
67
|
+
if (typeof t.name === 'string' && t.name) return t.name;
|
|
68
|
+
if (t.render) {
|
|
69
|
+
if (typeof t.render.displayName === 'string' && t.render.displayName) return t.render.displayName;
|
|
70
|
+
if (typeof t.render.name === 'string' && t.render.name) return t.render.name;
|
|
71
|
+
}
|
|
72
|
+
if (t.type && t.type !== type) return componentName(t.type);
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function displayNameOf(type: unknown): string {
|
|
77
|
+
if (!type || (typeof type !== 'function' && typeof type !== 'object')) return '';
|
|
78
|
+
const d = (type as { displayName?: unknown }).displayName;
|
|
79
|
+
return typeof d === 'string' ? d : '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Name of the component a fiber renders. `memo(forwardRef(Fn))` makes two
|
|
84
|
+
* fibers that share one props object, and only the outer one may carry a
|
|
85
|
+
* `displayName`. Release builds can rename functions, so a `displayName`
|
|
86
|
+
* on any of the wrappers wins over a function name.
|
|
87
|
+
*/
|
|
88
|
+
export function fiberName(f: FiberLike): string {
|
|
89
|
+
let fallback = '';
|
|
90
|
+
let cur: FiberLike | null | undefined = f;
|
|
91
|
+
for (let i = 0; cur && i < 4; i++) {
|
|
92
|
+
const d = displayNameOf(cur.type) || displayNameOf(cur.elementType);
|
|
93
|
+
if (d) return d;
|
|
94
|
+
if (!fallback) fallback = componentName(cur.type) || componentName(cur.elementType);
|
|
95
|
+
const parent: FiberLike | null | undefined = cur.return;
|
|
96
|
+
if (!parent || parent.memoizedProps !== cur.memoizedProps) break;
|
|
97
|
+
cur = parent;
|
|
98
|
+
}
|
|
99
|
+
return fallback;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isPressable(f: FiberLike): boolean {
|
|
103
|
+
const p = props(f);
|
|
104
|
+
for (const h of HANDLER_PROPS) if (typeof p[h] === 'function') return true;
|
|
105
|
+
const role = p.accessibilityRole ?? p.role;
|
|
106
|
+
return typeof role === 'string' && BUTTON_ROLES.has(role) && typeof f.type !== 'string';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function stringProp(p: Record<string, unknown>, key: string): string {
|
|
110
|
+
const v = p[key];
|
|
111
|
+
return typeof v === 'string' && v.trim() ? v.trim() : '';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function clipLabel(s: string): string {
|
|
115
|
+
const one = s.replace(/\s+/g, ' ').trim();
|
|
116
|
+
return one.length > MAX_LABEL_CHARS ? one.slice(0, MAX_LABEL_CHARS - 1) + '…' : one;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Text rendered under a fiber, in order, for a button without a label. */
|
|
120
|
+
export function collectText(root: FiberLike | null | undefined): string {
|
|
121
|
+
if (!root) return '';
|
|
122
|
+
const parts: string[] = [];
|
|
123
|
+
const stack: FiberLike[] = root.child ? [root.child] : [];
|
|
124
|
+
let visited = 0;
|
|
125
|
+
while (stack.length > 0 && parts.length < MAX_TEXT_NODES && visited < 500) {
|
|
126
|
+
const f = stack.pop()!;
|
|
127
|
+
visited++;
|
|
128
|
+
if (f.sibling) stack.push(f.sibling);
|
|
129
|
+
const p = f.memoizedProps;
|
|
130
|
+
// Host text fibers keep the string itself as their props.
|
|
131
|
+
if (typeof p === 'string' || typeof p === 'number') {
|
|
132
|
+
if (String(p).trim()) parts.push(String(p));
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (f.child) stack.push(f.child);
|
|
136
|
+
}
|
|
137
|
+
return clipLabel(parts.join(' '));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function resolveTapTarget(
|
|
141
|
+
start: FiberLike | null | undefined,
|
|
142
|
+
opts: ResolveOptions,
|
|
143
|
+
): TapTarget | null {
|
|
144
|
+
if (!start) return null;
|
|
145
|
+
let pressable: FiberLike | null = null;
|
|
146
|
+
let explicitName = '';
|
|
147
|
+
let a11yLabel = '';
|
|
148
|
+
const names: string[] = [];
|
|
149
|
+
// Levels walked above the button. The name attribute may sit on a
|
|
150
|
+
// wrapper a little higher up, so the walk goes on for a few levels.
|
|
151
|
+
let above = 0;
|
|
152
|
+
let f: FiberLike | null | undefined = start;
|
|
153
|
+
for (let depth = 0; f && depth < MAX_DEPTH; depth++, f = f.return) {
|
|
154
|
+
const p = props(f);
|
|
155
|
+
if (!explicitName) explicitName = stringProp(p, opts.actionNameAttribute);
|
|
156
|
+
if (!pressable && opts.useAccessibilityLabel && !a11yLabel) {
|
|
157
|
+
a11yLabel = stringProp(p, 'accessibilityLabel') || stringProp(p, 'aria-label');
|
|
158
|
+
}
|
|
159
|
+
if (!pressable && isPressable(f)) {
|
|
160
|
+
pressable = f;
|
|
161
|
+
// A button's own label beats one found on a child below it.
|
|
162
|
+
if (opts.useAccessibilityLabel) {
|
|
163
|
+
a11yLabel =
|
|
164
|
+
stringProp(p, 'accessibilityLabel') || stringProp(p, 'aria-label') || a11yLabel;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (typeof f.type !== 'string') {
|
|
168
|
+
const n = componentName(f.type);
|
|
169
|
+
if (n && names.length < 6 && names[names.length - 1] !== n) names.push(n);
|
|
170
|
+
}
|
|
171
|
+
if (pressable && (explicitName || ++above > 3)) break;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const target = pressable ?? start;
|
|
175
|
+
let component = fiberName(target);
|
|
176
|
+
if (!component || GENERIC_HOSTS.has(component)) {
|
|
177
|
+
component = pressable ? component || 'Touchable' : 'View';
|
|
178
|
+
}
|
|
179
|
+
const tp = props(target);
|
|
180
|
+
const label = clipLabel(
|
|
181
|
+
explicitName ||
|
|
182
|
+
a11yLabel ||
|
|
183
|
+
stringProp(tp, 'title') ||
|
|
184
|
+
stringProp(tp, 'label') ||
|
|
185
|
+
collectText(target),
|
|
186
|
+
);
|
|
187
|
+
// A tap on something that does not respond to taps is not an action.
|
|
188
|
+
if (!pressable && !explicitName) return null;
|
|
189
|
+
return {
|
|
190
|
+
name: explicitName ? explicitName : label ? `${component} ("${label}")` : component,
|
|
191
|
+
component,
|
|
192
|
+
label,
|
|
193
|
+
path: names.slice().reverse().join(' > '),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Gets the fiber of the touched view from a React Native touch event. */
|
|
198
|
+
export function fiberFromEvent(event: unknown): FiberLike | null {
|
|
199
|
+
if (!event || typeof event !== 'object') return null;
|
|
200
|
+
const e = event as {
|
|
201
|
+
_targetInst?: FiberLike;
|
|
202
|
+
target?: { __internalInstanceHandle?: FiberLike; _internalFiberInstanceHandleDEV?: FiberLike };
|
|
203
|
+
};
|
|
204
|
+
if (e._targetInst) return e._targetInst;
|
|
205
|
+
const t = e.target;
|
|
206
|
+
if (t && typeof t === 'object') {
|
|
207
|
+
return t.__internalInstanceHandle ?? t._internalFiberInstanceHandleDEV ?? null;
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The screen on display. Kept apart from views.ts so the event context can read it without an import cycle. */
|
|
2
|
+
export interface ViewInfo {
|
|
3
|
+
id: string;
|
|
4
|
+
key: string;
|
|
5
|
+
name: string;
|
|
6
|
+
url: string;
|
|
7
|
+
path: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let current: ViewInfo | null = null;
|
|
11
|
+
let previousName = '';
|
|
12
|
+
|
|
13
|
+
export function setCurrentViewInfo(next: ViewInfo | null) {
|
|
14
|
+
if (current && next && current.id !== next.id) previousName = current.name;
|
|
15
|
+
current = next;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function getCurrentViewInfo(): ViewInfo | null {
|
|
19
|
+
return current;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The screen shown before the current one (`app.screen.previous.name`). */
|
|
23
|
+
export function getPreviousScreenName(): string {
|
|
24
|
+
return previousName;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function resetViewInfo() {
|
|
28
|
+
current = null;
|
|
29
|
+
previousName = '';
|
|
30
|
+
}
|