@logbrew/react-native 0.1.39 → 0.1.40

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.
Files changed (96) hide show
  1. package/android-native-diagnostics.d.ts +15 -5
  2. package/android-native-diagnostics.js +170 -94
  3. package/apollo.cjs +435 -226
  4. package/apollo.d.cts +2 -70
  5. package/apollo.d.ts +24 -12
  6. package/apollo.js +438 -221
  7. package/apple-native-diagnostics-config.js +176 -0
  8. package/apple-native-diagnostics-context.js +232 -0
  9. package/apple-native-diagnostics-module.js +229 -0
  10. package/apple-native-diagnostics.d.ts +42 -11
  11. package/apple-native-diagnostics.js +60 -389
  12. package/expo.cjs +29 -16
  13. package/expo.d.ts +10 -9
  14. package/expo.js +4 -3
  15. package/fatal-admission.cjs +113 -0
  16. package/fatal-record.cjs +317 -0
  17. package/fatal-replay.cjs +297 -508
  18. package/global-errors-capture.cjs +369 -0
  19. package/global-errors-event.cjs +174 -0
  20. package/global-errors-support.cjs +128 -0
  21. package/global-errors.cjs +75 -351
  22. package/global-errors.d.cts +29 -278
  23. package/global-errors.d.ts +57 -27
  24. package/global-errors.js +7 -2
  25. package/global-errors.native.js +83 -48
  26. package/index-breadcrumbs.cjs +139 -0
  27. package/index-errors.cjs +167 -0
  28. package/index-events.cjs +365 -0
  29. package/index-navigation.cjs +327 -0
  30. package/index-random.cjs +120 -0
  31. package/index-react.cjs +189 -0
  32. package/index-spans.cjs +190 -0
  33. package/index-trace-fetch.cjs +322 -0
  34. package/index-trace.cjs +335 -0
  35. package/index-transport.cjs +201 -0
  36. package/index-values.cjs +232 -0
  37. package/index.cjs +95 -1243
  38. package/index.d.cts +2 -505
  39. package/index.d.ts +130 -469
  40. package/index.native.d.ts +146 -22
  41. package/index.native.js +184 -159
  42. package/instrumentation-fetch.cjs +113 -0
  43. package/instrumentation-install.cjs +241 -0
  44. package/instrumentation-runtime.cjs +65 -0
  45. package/instrumentation-teardown.cjs +93 -0
  46. package/instrumentation-values.cjs +282 -0
  47. package/instrumentation-xhr-request.cjs +298 -0
  48. package/instrumentation-xhr.cjs +233 -0
  49. package/instrumentation.cjs +110 -599
  50. package/instrumentation.d.cts +2 -84
  51. package/instrumentation.d.ts +22 -8
  52. package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
  53. package/lifecycle.cjs +181 -78
  54. package/lifecycle.d.cts +1 -50
  55. package/lifecycle.d.ts +17 -6
  56. package/metadata.cjs +279 -114
  57. package/metro-debug-id.cjs +296 -0
  58. package/metro-serializer.cjs +219 -0
  59. package/metro.cjs +153 -297
  60. package/metro.d.cts +2 -57
  61. package/metro.d.ts +27 -13
  62. package/metro.js +2 -3
  63. package/native-bridge-async.cjs +165 -0
  64. package/native-bridge-scope.cjs +148 -0
  65. package/native-bridge.cjs +83 -186
  66. package/native-bridge.d.cts +2 -94
  67. package/native-bridge.d.ts +28 -15
  68. package/native-client-bindings.js +230 -0
  69. package/native-diagnostics-values.js +120 -0
  70. package/native-module-lookup.js +31 -0
  71. package/native-null.js +3 -0
  72. package/null-value.cjs +4 -0
  73. package/package.json +43 -1
  74. package/persistent-delivery-store.js +247 -0
  75. package/persistent-delivery.native.js +183 -261
  76. package/promise-rejection-handlers.cjs +368 -0
  77. package/promise-rejections.cjs +169 -361
  78. package/react-native.config.js +3 -1
  79. package/release-artifacts-files.cjs +94 -0
  80. package/release-artifacts-options.cjs +182 -0
  81. package/release-artifacts.cjs +278 -207
  82. package/release-artifacts.d.cts +2 -55
  83. package/release-artifacts.d.ts +18 -7
  84. package/release-artifacts.js +2 -4
  85. package/resource-fetch-graphql.cjs +265 -0
  86. package/resource-fetch-size.cjs +156 -0
  87. package/resource-fetch.cjs +227 -428
  88. package/resource-fetch.d.cts +1 -60
  89. package/resource-fetch.d.ts +17 -6
  90. package/scripts/sync-apple-native-sources.mjs +101 -53
  91. package/types/capture.d.ts +95 -0
  92. package/types/config.d.ts +85 -0
  93. package/types/defaults.d.ts +55 -0
  94. package/types/events.d.ts +203 -0
  95. package/types/provider.d.ts +81 -0
  96. package/types/trace.d.ts +104 -0
@@ -1,84 +1,2 @@
1
- import type { LogBrewClient, Metadata } from "@logbrew/sdk";
2
- import type {
3
- ReactNavigationContainerLike,
4
- ReactNativeAppStateLike,
5
- ReactNativePlatformLike,
6
- ReactNativeTraceContext,
7
- ReactNativeTraceInput,
8
- TracePropagationTarget
9
- } from "./index.cjs";
10
- import type { LogBrewNativeBridgeLike, LogBrewNativeBridgeScope, LogBrewNativeBridgeScopeInput } from "./native-bridge.cjs";
11
- import type { ReactNativeResourceFetchOptions } from "./resource-fetch.cjs";
12
-
13
- export type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown, TResponse = unknown> = {
14
- appState?: ReactNativeAppStateLike;
15
- captureInitialLifecycleState?: boolean;
16
- captureInitialNavigationRoute?: boolean;
17
- fetchImpl?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["fetchImpl"];
18
- globalObject?: ReactNativeGlobalObjectLike;
19
- includeRouteKey?: boolean;
20
- instrumentGlobalFetch?: boolean;
21
- instrumentGlobalXMLHttpRequest?: boolean;
22
- logger?: string;
23
- measureFetchResponseBodySize?: boolean;
24
- measureXhrResponseBodySize?: boolean;
25
- metadata?: Metadata;
26
- metadataFactory?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["metadataFactory"];
27
- nativeBridge?: LogBrewNativeBridgeLike;
28
- navigation?: ReactNavigationContainerLike;
29
- navigationContainer?: ReactNavigationContainerLike;
30
- now?: () => string;
31
- nowMs?: () => number;
32
- onError?: (error: unknown) => void;
33
- platform?: ReactNativePlatformLike;
34
- randomValues?: (length: number) => ArrayLike<number>;
35
- routeTemplate?: string;
36
- routeTemplateFactory?: ReactNativeResourceFetchOptions<TInput, TInit, TResponse>["routeTemplateFactory"];
37
- screen?: string;
38
- sessionId?: string;
39
- trace?: ReactNativeTraceInput;
40
- traceFlags?: string;
41
- tracePropagationTargets?: TracePropagationTarget[];
42
- };
43
-
44
- export type ReactNativeGlobalObjectLike = {
45
- fetch?: unknown;
46
- XMLHttpRequest?: unknown;
47
- };
48
-
49
- export type ReactNativeGlobalFetchInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
50
- readonly fetch: (input: TInput, init?: TInit) => Promise<TResponse>;
51
- remove(): void;
52
- stop(): void;
53
- };
54
-
55
- export type ReactNativeGlobalXMLHttpRequestInstrumentation = {
56
- remove(): void;
57
- stop(): void;
58
- };
59
-
60
- export type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
61
- readonly trace: ReactNativeTraceContext;
62
- readonly globalFetch?: ReactNativeGlobalFetchInstrumentation<TInput, TInit, TResponse>;
63
- readonly globalXMLHttpRequest?: ReactNativeGlobalXMLHttpRequestInstrumentation;
64
- readonly resourceFetch: (input: TInput, init?: TInit) => Promise<TResponse>;
65
- remove(): void;
66
- stop(): void;
67
- syncNativeBridgeScope(input?: LogBrewNativeBridgeScopeInput): LogBrewNativeBridgeScope | undefined;
68
- withNativeBridgeScope<TResult>(callback: (scope: LogBrewNativeBridgeScope) => TResult): TResult;
69
- withNativeBridgeScope<TResult>(
70
- input: LogBrewNativeBridgeScopeInput,
71
- callback: (scope: LogBrewNativeBridgeScope) => TResult
72
- ): TResult;
73
- };
74
-
75
- export declare function createLogBrewReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown>(
76
- client: LogBrewClient,
77
- options?: ReactNativeInstrumentationOptions<TInput, TInit, TResponse>
78
- ): ReactNativeInstrumentation<TInput, TInit, TResponse>;
79
-
80
- declare const defaultExport: {
81
- createLogBrewReactNativeInstrumentation: typeof createLogBrewReactNativeInstrumentation;
82
- };
83
-
84
- export default defaultExport;
1
+ export * from "./instrumentation.js";
2
+ export { default } from "./instrumentation.js";
@@ -1,16 +1,20 @@
1
1
  import type { LogBrewClient, Metadata } from "@logbrew/sdk";
2
2
  import type {
3
- ReactNavigationContainerLike,
3
+ LogBrewNativeBridgeLike,
4
+ LogBrewNativeBridgeScope,
5
+ LogBrewNativeBridgeScopeInput
6
+ } from "./native-bridge.js";
7
+ import type {
4
8
  ReactNativeAppStateLike,
5
9
  ReactNativePlatformLike,
6
10
  ReactNativeTraceContext,
7
11
  ReactNativeTraceInput,
12
+ ReactNavigationContainerLike,
8
13
  TracePropagationTarget
9
14
  } from "./index.js";
10
- import type { LogBrewNativeBridgeLike, LogBrewNativeBridgeScope, LogBrewNativeBridgeScopeInput } from "./native-bridge.js";
11
15
  import type { ReactNativeResourceFetchOptions } from "./resource-fetch.js";
12
16
 
13
- export type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown, TResponse = unknown> = {
17
+ type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown, TResponse = unknown> = {
14
18
  appState?: ReactNativeAppStateLike;
15
19
  captureInitialLifecycleState?: boolean;
16
20
  captureInitialNavigationRoute?: boolean;
@@ -41,23 +45,23 @@ export type ReactNativeInstrumentationOptions<TInput = unknown, TInit = unknown,
41
45
  tracePropagationTargets?: TracePropagationTarget[];
42
46
  };
43
47
 
44
- export type ReactNativeGlobalObjectLike = {
48
+ type ReactNativeGlobalObjectLike = {
45
49
  fetch?: unknown;
46
50
  XMLHttpRequest?: unknown;
47
51
  };
48
52
 
49
- export type ReactNativeGlobalFetchInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
53
+ type ReactNativeGlobalFetchInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
50
54
  readonly fetch: (input: TInput, init?: TInit) => Promise<TResponse>;
51
55
  remove(): void;
52
56
  stop(): void;
53
57
  };
54
58
 
55
- export type ReactNativeGlobalXMLHttpRequestInstrumentation = {
59
+ type ReactNativeGlobalXMLHttpRequestInstrumentation = {
56
60
  remove(): void;
57
61
  stop(): void;
58
62
  };
59
63
 
60
- export type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
64
+ type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown> = {
61
65
  readonly trace: ReactNativeTraceContext;
62
66
  readonly globalFetch?: ReactNativeGlobalFetchInstrumentation<TInput, TInit, TResponse>;
63
67
  readonly globalXMLHttpRequest?: ReactNativeGlobalXMLHttpRequestInstrumentation;
@@ -72,7 +76,7 @@ export type ReactNativeInstrumentation<TInput = unknown, TInit = unknown, TRespo
72
76
  ): TResult;
73
77
  };
74
78
 
75
- export declare function createLogBrewReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown>(
79
+ declare function createLogBrewReactNativeInstrumentation<TInput = unknown, TInit = unknown, TResponse = unknown>(
76
80
  client: LogBrewClient,
77
81
  options?: ReactNativeInstrumentationOptions<TInput, TInit, TResponse>
78
82
  ): ReactNativeInstrumentation<TInput, TInit, TResponse>;
@@ -81,4 +85,14 @@ declare const defaultExport: {
81
85
  createLogBrewReactNativeInstrumentation: typeof createLogBrewReactNativeInstrumentation;
82
86
  };
83
87
 
88
+ export type {
89
+ ReactNativeGlobalFetchInstrumentation,
90
+ ReactNativeGlobalObjectLike,
91
+ ReactNativeGlobalXMLHttpRequestInstrumentation,
92
+ ReactNativeInstrumentation,
93
+ ReactNativeInstrumentationOptions
94
+ };
95
+ export {
96
+ createLogBrewReactNativeInstrumentation
97
+ };
84
98
  export default defaultExport;
@@ -3,7 +3,7 @@ import Foundation
3
3
  @objc(LBRNAppleNativeDiagnostics)
4
4
  public final class LBRNAppleNativeDiagnostics: NSObject, @unchecked Sendable {
5
5
  private static let shared = LBRNAppleNativeDiagnostics()
6
- private static let sdkVersion = "0.1.39"
6
+ private static let sdkVersion = "0.1.40"
7
7
 
8
8
  private let lock = NSLock()
9
9
  private let replayQueue = DispatchQueue(label: "co.logbrew.react-native.apple-diagnostics-replay")
package/lifecycle.cjs CHANGED
@@ -1,13 +1,61 @@
1
1
  "use strict";
2
2
 
3
3
  const { SdkError } = require("@logbrew/sdk");
4
- const {
5
- createReactNativeSpanAttributes,
6
- createReactNativeTraceContext,
7
- getActiveLogBrewTrace,
8
- getReactNativeContext
9
- } = require("./index.cjs");
4
+ const { indexRuntime, isCallable, readField } = require("./instrumentation-values.cjs");
10
5
 
6
+ /**
7
+ * @import { LogBrewClient, Metadata, SpanAttributes } from "@logbrew/sdk"
8
+ * @import { ReactNativeSpanEvent } from "./index.cjs"
9
+ * @import { AppStateInput, PlatformInput, TraceInput } from "./instrumentation-values.cjs"
10
+ * @import { LifecycleListenerOptions } from "./instrumentation-runtime.cjs"
11
+ */
12
+
13
+ /**
14
+ * @typedef {{ readonly fromState: string | undefined, readonly screen: string | undefined, readonly toState: string | undefined }} LifecycleIdContext
15
+ * @typedef {{
16
+ * readonly appState?: AppStateInput | undefined,
17
+ * readonly durationMs?: number | undefined,
18
+ * readonly fromState?: unknown,
19
+ * readonly id?: string | undefined,
20
+ * readonly idFactory?: ((context: LifecycleIdContext) => string) | undefined,
21
+ * readonly metadata?: Readonly<Metadata> | undefined,
22
+ * readonly name?: string | undefined,
23
+ * readonly now?: (() => string) | undefined,
24
+ * readonly platform?: PlatformInput | undefined,
25
+ * readonly screen?: string | undefined,
26
+ * readonly sessionId?: string | undefined,
27
+ * readonly state?: unknown,
28
+ * readonly status?: SpanAttributes["status"] | undefined,
29
+ * readonly timestamp?: string | undefined,
30
+ * readonly toState?: unknown,
31
+ * readonly trace?: TraceInput | undefined
32
+ * }} LifecycleSpanInput
33
+ * @typedef {{
34
+ * readonly appState: AppStateInput,
35
+ * readonly client: LogBrewClient,
36
+ * readonly options: Readonly<{ metadata: Readonly<Metadata>, now: () => string, nowMs: () => number }> & LifecycleListenerOptions
37
+ * }} LifecycleContext
38
+ * @typedef {{ readonly changedAtMs: number, readonly state: string | undefined }} LifecycleMoment
39
+ */
40
+
41
+ const MIN_DURATION_MS = 0;
42
+
43
+ function defaultLifecycleSpanEventId() {
44
+ return `evt_native_lifecycle_${indexRuntime.createReactNativeTraceContext().traceId}`;
45
+ }
46
+
47
+ /**
48
+ * @param {unknown} state
49
+ * @returns {string | undefined}
50
+ */
51
+ function normalizeLifecycleState(state) {
52
+ return typeof state === "string" && state.trim() !== "" ? state.trim() : undefined;
53
+ }
54
+
55
+ /**
56
+ * @param {LifecycleSpanInput} [input]
57
+ * @returns {ReactNativeSpanEvent}
58
+ */
11
59
  function createReactNativeLifecycleSpanEvent({
12
60
  durationMs, fromState, id, idFactory = defaultLifecycleSpanEventId, metadata = {}, name,
13
61
  now = () => new Date().toISOString(), platform, appState, screen, sessionId, state, status = "ok",
@@ -15,32 +63,52 @@ function createReactNativeLifecycleSpanEvent({
15
63
  } = {}) {
16
64
  const safeFromState = normalizeLifecycleState(fromState);
17
65
  const safeToState = normalizeLifecycleState(toState ?? state);
18
- const transition = [safeFromState, safeToState].filter(Boolean).join("->");
19
- const spanName = name ?? `app_state:${transition || safeToState || safeFromState || "change"}`;
20
- const activeTrace = trace ?? getActiveLogBrewTrace() ?? createReactNativeTraceContext();
21
- return {
22
- id: id ?? idFactory({ fromState: safeFromState, screen, toState: safeToState }),
23
- timestamp: timestamp ?? now(),
24
- attributes: createReactNativeSpanAttributes({
25
- name: spanName,
26
- status,
66
+ const transition = [safeFromState, safeToState].filter(knownState => knownState !== undefined).join("->");
67
+ const spanName = name ?? `app_state:${transition === "" ? "change" : transition}`;
68
+ const activeTrace = trace ?? indexRuntime.getActiveLogBrewTrace() ?? indexRuntime.createReactNativeTraceContext();
69
+ const eventId = id ?? idFactory({ fromState: safeFromState, screen, toState: safeToState });
70
+ const eventTimestamp = timestamp ?? now();
71
+ const attributes = indexRuntime.createReactNativeSpanAttributes({
72
+ durationMs,
73
+ metadata: {
74
+ ...indexRuntime.getReactNativeContext({ appState, platform }),
75
+ source: "react-native.lifecycle",
76
+
77
+ appState: safeToState,
27
78
  durationMs,
28
- trace: activeTrace,
29
- metadata: {
30
- ...getReactNativeContext({ platform, appState }),
31
- source: "react-native.lifecycle",
32
- appState: safeToState,
33
- durationMs,
34
- fromAppState: safeFromState,
35
- screen,
36
- sessionId,
37
- toAppState: safeToState,
38
- ...metadata
39
- }
40
- })
79
+ fromAppState: safeFromState,
80
+ screen,
81
+ sessionId,
82
+ toAppState: safeToState,
83
+ ...metadata
84
+ },
85
+ name: spanName,
86
+ status,
87
+ trace: activeTrace
88
+ });
89
+ return {
90
+ id: eventId,
91
+ timestamp: eventTimestamp,
92
+
93
+ attributes
41
94
  };
42
95
  }
43
96
 
97
+ /**
98
+ * @param {LogBrewClient | null | undefined} client
99
+ * @returns {asserts client is LogBrewClient}
100
+ */
101
+ function requireClient(client) {
102
+ if (!client) {
103
+ throw new SdkError("configuration_error", "LogBrew React Native lifecycle helpers require a client");
104
+ }
105
+ }
106
+
107
+ /**
108
+ * @param {LogBrewClient | null | undefined} client
109
+ * @param {LifecycleSpanInput} [input]
110
+ * @returns {ReactNativeSpanEvent}
111
+ */
44
112
  function captureReactNativeLifecycleSpan(client, input = {}) {
45
113
  requireClient(client);
46
114
  const event = createReactNativeLifecycleSpanEvent(input);
@@ -48,71 +116,106 @@ function captureReactNativeLifecycleSpan(client, input = {}) {
48
116
  return event;
49
117
  }
50
118
 
51
- function createAppStateLifecycleSpanListener(client, appState, {
52
- captureInitialState = false, metadata = {}, now = () => new Date().toISOString(), nowMs = () => Date.now(),
53
- onError, platform, screen, sessionId, trace
54
- } = {}) {
55
- requireClient(client);
56
- if (!appState || typeof appState.addEventListener !== "function") {
57
- throw new SdkError("configuration_error", "createAppStateLifecycleSpanListener requires AppState.addEventListener");
119
+ /**
120
+ * @param {unknown} subscription
121
+ * @returns {() => unknown}
122
+ */
123
+ function subscriptionRemover(subscription) {
124
+ if (isCallable(subscription)) {
125
+ return subscription;
58
126
  }
127
+ if (isCallable(readField(subscription, "remove"))) {
128
+ return () => {
129
+ const remove = readField(subscription, "remove");
130
+ if (!isCallable(remove)) {
131
+ throw new TypeError("subscription.remove is not a function");
132
+ }
133
+ return Reflect.apply(remove, subscription, []);
134
+ };
135
+ }
136
+ return () => undefined;
137
+ }
59
138
 
60
- let previousState = normalizeLifecycleState(appState.currentState);
61
- let previousChangedAtMs = nowMs();
62
- if (captureInitialState && previousState !== undefined) {
63
- captureReactNativeLifecycleSpan(client, {
64
- appState, metadata, now, platform, screen, sessionId, toState: previousState, trace
65
- });
139
+ /**
140
+ * @param {LifecycleContext} context
141
+ * @param {LifecycleMoment} previous
142
+ * @param {LifecycleMoment} next
143
+ * @returns {LifecycleSpanInput}
144
+ */
145
+ function lifecycleChangeInput({ appState, options }, previous, next) {
146
+ return {
147
+ appState,
148
+ durationMs: previous.state === undefined ? undefined : Math.max(MIN_DURATION_MS, next.changedAtMs - previous.changedAtMs),
149
+ fromState: previous.state,
150
+ metadata: options.metadata,
151
+ now: options.now,
152
+ platform: options.platform,
153
+ screen: options.screen,
154
+ sessionId: options.sessionId,
155
+ timestamp: options.now(),
156
+ toState: next.state,
157
+ trace: options.trace
158
+ };
159
+ }
160
+
161
+ /**
162
+ * @param {((error: unknown) => void) | undefined} onError
163
+ * @param {unknown} error
164
+ * @returns {void}
165
+ */
166
+ function reportLifecycleError(onError, error) {
167
+ if (typeof onError === "function") {
168
+ onError(error);
169
+ } else {
170
+ throw error;
66
171
  }
172
+ }
67
173
 
68
- const subscription = appState.addEventListener("change", (nextState) => {
174
+ /**
175
+ * @param {LifecycleContext} context
176
+ * @param {LifecycleMoment} initial
177
+ * @returns {(nextState: string) => void}
178
+ */
179
+ function lifecycleChangeListener(context, initial) {
180
+ let previous = initial;
181
+ return (nextState) => {
69
182
  try {
70
183
  const safeNextState = normalizeLifecycleState(nextState);
71
184
  if (safeNextState === undefined) {
72
185
  return;
73
186
  }
74
- const changedAtMs = nowMs();
75
- const durationMs = previousState === undefined ? undefined : Math.max(0, changedAtMs - previousChangedAtMs);
76
- captureReactNativeLifecycleSpan(client, {
77
- appState, durationMs, fromState: previousState, metadata, now, platform, screen, sessionId,
78
- timestamp: now(), toState: safeNextState, trace
79
- });
80
- previousState = safeNextState;
81
- previousChangedAtMs = changedAtMs;
187
+ const next = { changedAtMs: context.options.nowMs(), state: safeNextState };
188
+ captureReactNativeLifecycleSpan(context.client, lifecycleChangeInput(context, previous, next));
189
+ previous = next;
82
190
  } catch (error) {
83
- if (typeof onError === "function") {
84
- onError(error);
85
- } else {
86
- throw error;
87
- }
191
+ reportLifecycleError(context.options.onError, error);
88
192
  }
89
- });
90
-
91
- return subscriptionRemover(subscription);
92
- }
93
-
94
- function requireClient(client) {
95
- if (!client) {
96
- throw new SdkError("configuration_error", "LogBrew React Native lifecycle helpers require a client");
97
- }
98
- }
99
-
100
- function defaultLifecycleSpanEventId() {
101
- return `evt_native_lifecycle_${createReactNativeTraceContext().traceId}`;
102
- }
103
-
104
- function normalizeLifecycleState(state) {
105
- return typeof state === "string" && state.trim() !== "" ? state.trim() : undefined;
193
+ };
106
194
  }
107
195
 
108
- function subscriptionRemover(subscription) {
109
- if (typeof subscription === "function") {
110
- return subscription;
196
+ /**
197
+ * @param {LogBrewClient | null | undefined} client
198
+ * @param {AppStateInput | null | undefined} appState
199
+ * @param {LifecycleListenerOptions} [options]
200
+ * @returns {() => unknown}
201
+ */
202
+ function createAppStateLifecycleSpanListener(client, appState, {
203
+ captureInitialState = false, metadata = {}, now = () => new Date().toISOString(), nowMs = () => Date.now(),
204
+ onError, platform, screen, sessionId, trace
205
+ } = {}) {
206
+ requireClient(client);
207
+ if (!appState || typeof appState.addEventListener !== "function") {
208
+ throw new SdkError("configuration_error", "createAppStateLifecycleSpanListener requires AppState.addEventListener");
111
209
  }
112
- if (subscription && typeof subscription.remove === "function") {
113
- return () => subscription.remove();
210
+ const options = { metadata, now, nowMs, onError, platform, screen, sessionId, trace };
211
+ const state = normalizeLifecycleState(appState.currentState);
212
+ const initial = { changedAtMs: nowMs(), state };
213
+ if (captureInitialState && state !== undefined) {
214
+ captureReactNativeLifecycleSpan(client, {
215
+ appState, metadata, now, platform, screen, sessionId, toState: state, trace
216
+ });
114
217
  }
115
- return () => {};
218
+ return subscriptionRemover(appState.addEventListener("change", lifecycleChangeListener({ appState, client, options }, initial)));
116
219
  }
117
220
 
118
221
  module.exports = {
package/lifecycle.d.cts CHANGED
@@ -1,50 +1 @@
1
- import type { LogBrewClient, Metadata, SpanAttributes } from "@logbrew/sdk";
2
- import type {
3
- ReactNativeAppStateLike,
4
- ReactNativeContextOptions,
5
- ReactNativeSpanEvent
6
- } from "./index.cjs";
7
-
8
- export type ReactNativeLifecycleSpanInput = ReactNativeContextOptions & {
9
- durationMs?: number;
10
- fromState?: string;
11
- id?: string;
12
- idFactory?: (context: ReactNativeLifecycleSpanIdFactoryContext) => string;
13
- name?: string;
14
- now?: () => string;
15
- screen?: string;
16
- sessionId?: string;
17
- state?: string;
18
- status?: SpanAttributes["status"];
19
- timestamp?: string;
20
- toState?: string;
21
- };
22
-
23
- export type ReactNativeLifecycleSpanIdFactoryContext = {
24
- fromState?: string;
25
- screen?: string;
26
- toState?: string;
27
- };
28
-
29
- export type AppStateLifecycleSpanListenerOptions = ReactNativeContextOptions & {
30
- captureInitialState?: boolean;
31
- metadata?: Metadata;
32
- now?: () => string;
33
- nowMs?: () => number;
34
- onError?: (error: unknown) => void;
35
- screen?: string;
36
- sessionId?: string;
37
- };
38
-
39
- export declare function createReactNativeLifecycleSpanEvent(
40
- input?: ReactNativeLifecycleSpanInput
41
- ): ReactNativeSpanEvent;
42
- export declare function captureReactNativeLifecycleSpan(
43
- client: LogBrewClient,
44
- input?: ReactNativeLifecycleSpanInput
45
- ): ReactNativeSpanEvent;
46
- export declare function createAppStateLifecycleSpanListener(
47
- client: LogBrewClient,
48
- appState: ReactNativeAppStateLike,
49
- options?: AppStateLifecycleSpanListenerOptions
50
- ): () => void;
1
+ export * from "./lifecycle.js";
package/lifecycle.d.ts CHANGED
@@ -5,7 +5,7 @@ import type {
5
5
  ReactNativeSpanEvent
6
6
  } from "./index.js";
7
7
 
8
- export type ReactNativeLifecycleSpanInput = ReactNativeContextOptions & {
8
+ type ReactNativeLifecycleSpanInput = ReactNativeContextOptions & {
9
9
  durationMs?: number;
10
10
  fromState?: string;
11
11
  id?: string;
@@ -20,13 +20,13 @@ export type ReactNativeLifecycleSpanInput = ReactNativeContextOptions & {
20
20
  toState?: string;
21
21
  };
22
22
 
23
- export type ReactNativeLifecycleSpanIdFactoryContext = {
23
+ type ReactNativeLifecycleSpanIdFactoryContext = {
24
24
  fromState?: string;
25
25
  screen?: string;
26
26
  toState?: string;
27
27
  };
28
28
 
29
- export type AppStateLifecycleSpanListenerOptions = ReactNativeContextOptions & {
29
+ type AppStateLifecycleSpanListenerOptions = ReactNativeContextOptions & {
30
30
  captureInitialState?: boolean;
31
31
  metadata?: Metadata;
32
32
  now?: () => string;
@@ -36,15 +36,26 @@ export type AppStateLifecycleSpanListenerOptions = ReactNativeContextOptions & {
36
36
  sessionId?: string;
37
37
  };
38
38
 
39
- export declare function createReactNativeLifecycleSpanEvent(
39
+ declare function createReactNativeLifecycleSpanEvent(
40
40
  input?: ReactNativeLifecycleSpanInput
41
41
  ): ReactNativeSpanEvent;
42
- export declare function captureReactNativeLifecycleSpan(
42
+ declare function captureReactNativeLifecycleSpan(
43
43
  client: LogBrewClient,
44
44
  input?: ReactNativeLifecycleSpanInput
45
45
  ): ReactNativeSpanEvent;
46
- export declare function createAppStateLifecycleSpanListener(
46
+ declare function createAppStateLifecycleSpanListener(
47
47
  client: LogBrewClient,
48
48
  appState: ReactNativeAppStateLike,
49
49
  options?: AppStateLifecycleSpanListenerOptions
50
50
  ): () => void;
51
+
52
+ export type {
53
+ AppStateLifecycleSpanListenerOptions,
54
+ ReactNativeLifecycleSpanIdFactoryContext,
55
+ ReactNativeLifecycleSpanInput
56
+ };
57
+ export {
58
+ createReactNativeLifecycleSpanEvent,
59
+ captureReactNativeLifecycleSpan,
60
+ createAppStateLifecycleSpanListener
61
+ };