@amplitude/session-replay-react-native 0.1.0-beta.1 → 0.1.0-beta.2

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 (67) hide show
  1. package/AmplitudeSessionReplayReactNative.podspec +43 -2
  2. package/README.md +112 -0
  3. package/android/build.gradle +127 -9
  4. package/android/src/androidTest/java/com/amplitude/sessionreplayreactnative/SRMaskViewTest.kt +459 -0
  5. package/android/src/main/java/com/amplitude/sessionreplayreactnative/SRDefaultMaskingPrimitive.kt +36 -0
  6. package/android/src/main/java/com/amplitude/sessionreplayreactnative/SRMaskingPrimitive.kt +84 -0
  7. package/android/src/main/java/com/amplitude/sessionreplayreactnative/SessionReplayReactNativeModule.kt +35 -5
  8. package/android/src/newarch/java/com/amplitude/sessionreplayreactnative/SessionReplayReactNativePackage.kt +2 -1
  9. package/android/src/newarch/java/com/amplitude/sessionreplayreactnative/fabric/SRMaskView.kt +174 -0
  10. package/android/src/newarch/java/com/amplitude/sessionreplayreactnative/fabric/SRMaskViewManager.kt +44 -0
  11. package/android/src/newarch/java/com/amplitude/sessionreplayreactnative/fabric/SRMaskViewPointerEvents.java +36 -0
  12. package/cpp/SRMaskViewComponentDescriptor.h +20 -0
  13. package/cpp/SRMaskViewShadowNode.cpp +54 -0
  14. package/cpp/SRMaskViewShadowNode.h +56 -0
  15. package/ios/NativeSessionReplay.swift +29 -8
  16. package/ios/SRDefaultMaskingPrimitive.swift +34 -0
  17. package/ios/SRMaskingPrimitive.h +26 -0
  18. package/ios/SRMaskingRegistry.m +105 -0
  19. package/ios/fabric/SRMaskView.h +10 -0
  20. package/ios/fabric/SRMaskView.mm +178 -0
  21. package/lib/commonjs/Mask.types.js +29 -0
  22. package/lib/commonjs/Mask.types.js.map +1 -0
  23. package/lib/commonjs/fabric/MaskFabric.js +92 -0
  24. package/lib/commonjs/fabric/MaskFabric.js.map +1 -0
  25. package/lib/commonjs/index.js +19 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/mask-fallback.js +53 -0
  28. package/lib/commonjs/mask-fallback.js.map +1 -0
  29. package/lib/commonjs/paper/MaskPaper.js +27 -0
  30. package/lib/commonjs/paper/MaskPaper.js.map +1 -0
  31. package/lib/commonjs/specs/SRMaskViewNativeComponent.js +11 -0
  32. package/lib/commonjs/specs/SRMaskViewNativeComponent.js.map +1 -0
  33. package/lib/commonjs/version.js +1 -1
  34. package/lib/module/Mask.types.js +23 -0
  35. package/lib/module/Mask.types.js.map +1 -0
  36. package/lib/module/fabric/MaskFabric.js +85 -0
  37. package/lib/module/fabric/MaskFabric.js.map +1 -0
  38. package/lib/module/index.js +15 -0
  39. package/lib/module/index.js.map +1 -1
  40. package/lib/module/mask-fallback.js +45 -0
  41. package/lib/module/mask-fallback.js.map +1 -0
  42. package/lib/module/paper/MaskPaper.js +21 -0
  43. package/lib/module/paper/MaskPaper.js.map +1 -0
  44. package/lib/module/specs/SRMaskViewNativeComponent.js +5 -0
  45. package/lib/module/specs/SRMaskViewNativeComponent.js.map +1 -0
  46. package/lib/module/version.js +1 -1
  47. package/lib/typescript/Mask.types.d.ts +38 -0
  48. package/lib/typescript/Mask.types.d.ts.map +1 -0
  49. package/lib/typescript/fabric/MaskFabric.d.ts +8 -0
  50. package/lib/typescript/fabric/MaskFabric.d.ts.map +1 -0
  51. package/lib/typescript/index.d.ts +11 -0
  52. package/lib/typescript/index.d.ts.map +1 -1
  53. package/lib/typescript/mask-fallback.d.ts +27 -0
  54. package/lib/typescript/mask-fallback.d.ts.map +1 -0
  55. package/lib/typescript/paper/MaskPaper.d.ts +10 -0
  56. package/lib/typescript/paper/MaskPaper.d.ts.map +1 -0
  57. package/lib/typescript/specs/SRMaskViewNativeComponent.d.ts +11 -0
  58. package/lib/typescript/specs/SRMaskViewNativeComponent.d.ts.map +1 -0
  59. package/lib/typescript/version.d.ts +1 -1
  60. package/package.json +11 -6
  61. package/src/Mask.types.ts +45 -0
  62. package/src/fabric/MaskFabric.tsx +73 -0
  63. package/src/index.tsx +18 -0
  64. package/src/mask-fallback.tsx +60 -0
  65. package/src/paper/MaskPaper.tsx +24 -0
  66. package/src/specs/SRMaskViewNativeComponent.ts +12 -0
  67. package/src/version.ts +1 -1
@@ -0,0 +1,174 @@
1
+ package com.amplitude.sessionreplayreactnative.fabric
2
+
3
+ import android.content.Context
4
+ import android.view.View
5
+ import com.amplitude.sessionreplayreactnative.SRMaskingRegistry
6
+ import com.facebook.react.views.view.ReactViewGroup
7
+
8
+ /**
9
+ * Fabric host view for the layout-transparent mask component. Its Yoga node is
10
+ * `display:contents`, so Fabric assigns it a 0x0 native frame; the view
11
+ * compensates in two ways that must not affect the Yoga layout of its children:
12
+ * disabled child clipping (a 0x0 host would otherwise clip children invisible)
13
+ * and [expandBoundsToChildrenUnion], which widens only this host's own frame so
14
+ * the session-replay capture gate (width>0 && height>0) doesn't drop the
15
+ * (mask-tagged) subtree.
16
+ */
17
+ class SRMaskView(context: Context) : ReactViewGroup(context) {
18
+ var enabled: Boolean = true
19
+ private set
20
+ var unmask: Boolean = false
21
+ private set
22
+ var maskLevel: String = "mask"
23
+ private set
24
+
25
+ // Guards recursion: setLeftTopRightBottom() can trigger another onLayout.
26
+ private var expanding = false
27
+
28
+ init {
29
+ clipChildren = false
30
+ clipToPadding = false
31
+ // The widened frame (see expandBoundsToChildrenUnion) necessarily spans
32
+ // from the host's Fabric-assigned origin to the children's far corner, so
33
+ // it overlaps sibling views that have nothing to do with this mask. The
34
+ // host must therefore never participate in touch targeting itself:
35
+ // BOX_NONE makes RN's TouchTargetHelper skip the host (only its children
36
+ // can be targets) and lets misses fall through to views underneath.
37
+ // Children are unaffected. This view is never created from a JS
38
+ // pointerEvents prop, so nothing else writes this field.
39
+ //
40
+ // CROSS-VERSION CONSTRAINT (do not "simplify" to a Kotlin call/assignment
41
+ // or an accessor override): pointerEvents has three incompatible
42
+ // source-level shapes across RN versions and no single Kotlin syntax
43
+ // compiles against all of them — see SRMaskViewPointerEvents.java, which
44
+ // resolves the ever-present bytecode setter instead. Re-asserted in
45
+ // onLayout below so a recycling reset can never be observed by capture
46
+ // or touch.
47
+ SRMaskViewPointerEvents.forceBoxNone(this)
48
+ }
49
+
50
+ // Children can be laid out after the host's own layout pass; re-widen then.
51
+ private val childLayoutChangeListener =
52
+ OnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
53
+ if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
54
+ expandBoundsToChildrenUnion()
55
+ }
56
+ }
57
+
58
+ fun setMaskingProps(enabled: Boolean, unmask: Boolean, maskLevel: String) {
59
+ this.enabled = enabled
60
+ this.unmask = unmask
61
+ this.maskLevel = maskLevel
62
+ reapplyMaskingToAllChildren()
63
+ }
64
+
65
+ override fun addView(child: View, index: Int) {
66
+ super.addView(child, index)
67
+ applyMaskingToChild(child)
68
+ child.addOnLayoutChangeListener(childLayoutChangeListener)
69
+ expandBoundsToChildrenUnion()
70
+ }
71
+
72
+ override fun removeView(view: View) {
73
+ view.removeOnLayoutChangeListener(childLayoutChangeListener)
74
+ SRMaskingRegistry.reset(view)
75
+ super.removeView(view)
76
+ expandBoundsToChildrenUnion()
77
+ }
78
+
79
+ override fun removeViewAt(index: Int) {
80
+ val child = getChildAt(index)
81
+ child?.removeOnLayoutChangeListener(childLayoutChangeListener)
82
+ child?.let { SRMaskingRegistry.reset(it) }
83
+ super.removeViewAt(index)
84
+ expandBoundsToChildrenUnion()
85
+ }
86
+
87
+ // Fabric applies the degenerate 0x0 frame via the final View.layout();
88
+ // onLayout is the hook that runs right after, where we can re-widen it.
89
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
90
+ super.onLayout(changed, left, top, right, bottom)
91
+ // Re-assert touch transparency in case a recycling path reset the field
92
+ // (version-agnostic replacement for an accessor override; see init).
93
+ SRMaskViewPointerEvents.forceBoxNone(this)
94
+ expandBoundsToChildrenUnion()
95
+ }
96
+
97
+ // Widen this host's native frame to enclose its children WITHOUT moving it.
98
+ //
99
+ // Measured coordinate model (SDKRN-33, RN 0.77.2 Fabric, on-device):
100
+ // children are ALWAYS host-relative — a child renders at
101
+ // (host.left + child.left, host.top + child.top), standard Android. The
102
+ // host's Fabric-assigned degenerate frame is (X,Y,X,Y) where (X,Y) is the
103
+ // accumulated origin of any flattened views between the host and its
104
+ // mounted parent: (0,0) for top-level masks (where host-relative and
105
+ // parent-space coincide numerically), non-zero for e.g. an AmpUnmask nested
106
+ // inside an AmpMask through a flattened <View>, a mask inside a list row,
107
+ // or a mask whose child uses negative offsets. Therefore the enclosing
108
+ // extent in parent space is origin + max child extent. The origin must
109
+ // never move (children would shift on screen), so children at negative
110
+ // host-relative coordinates cannot be enclosed; the extents are clamped so
111
+ // the frame is never degenerate or inverted while children exist — the
112
+ // session-replay capture gate (width>0 && height>0) is the whole point of
113
+ // the widening. The widened frame can overlap unrelated siblings, which is
114
+ // why the host is pointer-events BOX_NONE (see init) and must never be a
115
+ // touch target itself.
116
+ private fun expandBoundsToChildrenUnion() {
117
+ if (expanding) return
118
+ if (childCount == 0) return
119
+
120
+ var maxChildRight = 0
121
+ var maxChildBottom = 0
122
+ for (i in 0 until childCount) {
123
+ val c = getChildAt(i) ?: continue
124
+ if (c.right > maxChildRight) maxChildRight = c.right
125
+ if (c.bottom > maxChildBottom) maxChildBottom = c.bottom
126
+ }
127
+
128
+ // Children are host-relative, so parent-space extent = origin + extent.
129
+ // Clamp to a 1px minimum so a child at fully negative coordinates can
130
+ // never produce a zero/inverted frame that the capture gate would drop.
131
+ val newRight = left + maxOf(maxChildRight, 1)
132
+ val newBottom = top + maxOf(maxChildBottom, 1)
133
+
134
+ if (newRight != right || newBottom != bottom) {
135
+ expanding = true
136
+ try {
137
+ setLeftTopRightBottom(left, top, newRight, newBottom)
138
+ } finally {
139
+ expanding = false
140
+ }
141
+ }
142
+ }
143
+
144
+ // Called from SRMaskViewManager.onDropViewInstance. Fabric can drop the host
145
+ // without routing children through removeView*; detach their listeners (which
146
+ // close over — and would keep alive — this host) and reset their masking.
147
+ fun onHostDropped() {
148
+ for (i in 0 until childCount) {
149
+ val child = getChildAt(i) ?: continue
150
+ child.removeOnLayoutChangeListener(childLayoutChangeListener)
151
+ SRMaskingRegistry.reset(child)
152
+ }
153
+ }
154
+
155
+ private fun reapplyMaskingToAllChildren() {
156
+ for (i in 0 until childCount) {
157
+ applyMaskingToChild(getChildAt(i))
158
+ }
159
+ }
160
+
161
+ private fun applyMaskingToChild(child: View) {
162
+ if (!enabled) {
163
+ SRMaskingRegistry.reset(child)
164
+ return
165
+ }
166
+
167
+ if (unmask) {
168
+ SRMaskingRegistry.unmask(child)
169
+ return
170
+ }
171
+
172
+ SRMaskingRegistry.mask(child, maskLevel)
173
+ }
174
+ }
@@ -0,0 +1,44 @@
1
+ package com.amplitude.sessionreplayreactnative.fabric
2
+
3
+ import com.facebook.react.module.annotations.ReactModule
4
+ import com.facebook.react.uimanager.ThemedReactContext
5
+ import com.facebook.react.uimanager.ViewGroupManager
6
+ import com.facebook.react.uimanager.ViewManagerDelegate
7
+ import com.facebook.react.viewmanagers.SRMaskViewManagerDelegate
8
+ import com.facebook.react.viewmanagers.SRMaskViewManagerInterface
9
+
10
+ @ReactModule(name = SRMaskViewManager.NAME)
11
+ class SRMaskViewManager :
12
+ ViewGroupManager<SRMaskView>(),
13
+ SRMaskViewManagerInterface<SRMaskView> {
14
+
15
+ private val delegate = SRMaskViewManagerDelegate(this)
16
+
17
+ override fun getDelegate(): ViewManagerDelegate<SRMaskView> = delegate
18
+
19
+ override fun getName(): String = NAME
20
+
21
+ override fun createViewInstance(reactContext: ThemedReactContext): SRMaskView =
22
+ SRMaskView(reactContext)
23
+
24
+ override fun setEnabled(view: SRMaskView, value: Boolean) {
25
+ view.setMaskingProps(value, view.unmask, view.maskLevel)
26
+ }
27
+
28
+ override fun setUnmask(view: SRMaskView, value: Boolean) {
29
+ view.setMaskingProps(view.enabled, value, view.maskLevel)
30
+ }
31
+
32
+ override fun setMaskLevel(view: SRMaskView, value: String?) {
33
+ view.setMaskingProps(view.enabled, view.unmask, value ?: "mask")
34
+ }
35
+
36
+ override fun onDropViewInstance(view: SRMaskView) {
37
+ super.onDropViewInstance(view)
38
+ view.onHostDropped()
39
+ }
40
+
41
+ companion object {
42
+ const val NAME = "SRMaskView"
43
+ }
44
+ }
@@ -0,0 +1,36 @@
1
+ package com.amplitude.sessionreplayreactnative.fabric;
2
+
3
+ import com.facebook.react.uimanager.PointerEvents;
4
+ import com.facebook.react.views.view.ReactViewGroup;
5
+
6
+ /**
7
+ * Java on purpose. {@code pointerEvents} has THREE incompatible source-level
8
+ * shapes across React Native versions, so no single Kotlin syntax compiles
9
+ * against all of them:
10
+ *
11
+ * <ul>
12
+ * <li>&le; ~0.77: Java interface + Java {@code ReactViewGroup} get/set pair
13
+ * (Kotlin property syntax OK, setter call OK)</li>
14
+ * <li>~0.78&ndash;0.80: Kotlin {@code ReactPointerEventsView} declares
15
+ * {@code val pointerEvents} while {@code ReactViewGroup} is still Java —
16
+ * Kotlin resolves assignment against the interface's {@code val} and
17
+ * rejects it ({@code 'val' cannot be reassigned}); only an explicit
18
+ * {@code setPointerEvents(...)} call compiles</li>
19
+ * <li>0.81+: {@code ReactViewGroup} is Kotlin with {@code var pointerEvents} —
20
+ * property syntax OK, but {@code setPointerEvents(...)} is an unresolved
21
+ * reference from Kotlin source</li>
22
+ * </ul>
23
+ *
24
+ * The BYTECODE, however, has a {@code setPointerEvents(PointerEvents)} method
25
+ * in every era (the Java method through 0.80; the Kotlin-var-generated setter
26
+ * from 0.81, verified via javap of react-android AARs). Java source resolves
27
+ * against bytecode, so this shim compiles and works on every supported RN
28
+ * version.
29
+ */
30
+ final class SRMaskViewPointerEvents {
31
+ private SRMaskViewPointerEvents() {}
32
+
33
+ static void forceBoxNone(ReactViewGroup view) {
34
+ view.setPointerEvents(PointerEvents.BOX_NONE);
35
+ }
36
+ }
@@ -0,0 +1,20 @@
1
+ #pragma once
2
+
3
+ #include "SRMaskViewShadowNode.h"
4
+
5
+ #include <react/renderer/core/ConcreteComponentDescriptor.h>
6
+
7
+ namespace facebook::react {
8
+
9
+ // Replaces the codegen typedef of the same name (patched on Android; iOS binds
10
+ // via +componentDescriptorProvider). Exists only to bind the custom
11
+ // SRMaskViewContentsShadowNode class — no adopt() override; the ShadowNode's
12
+ // own constructors handle the ForceFlattenView unset (see
13
+ // SRMaskViewShadowNode.h).
14
+ class SRMaskViewComponentDescriptor final
15
+ : public ConcreteComponentDescriptor<SRMaskViewContentsShadowNode> {
16
+ public:
17
+ using ConcreteComponentDescriptor::ConcreteComponentDescriptor;
18
+ };
19
+
20
+ } // namespace facebook::react
@@ -0,0 +1,54 @@
1
+ #include "SRMaskViewShadowNode.h"
2
+
3
+ #include <react/renderer/core/ShadowNodeFragment.h>
4
+ #include <react/renderer/core/ShadowNodeTraits.h>
5
+ #include <yoga/Yoga.h>
6
+
7
+ namespace facebook::react {
8
+
9
+ void SRMaskViewContentsShadowNode::initialize() {
10
+ // display:contents comes from the JS style prop and is already in
11
+ // yogaNode_'s style (parsed by RN's own updateYogaProps(), which also set
12
+ // ForceFlattenView). We never write the Yoga style in C++; the only trait
13
+ // customization is keeping the host view mounted (the session-replay SDK
14
+ // needs a real view to tag for masking) while Yoga still lays the children
15
+ // out as if the host didn't exist.
16
+ if (YGNodeStyleGetDisplay(&yogaNode_) != YGDisplayContents) {
17
+ return;
18
+ }
19
+
20
+ traits_.unset(ShadowNodeTraits::Trait::ForceFlattenView);
21
+
22
+ // Workaround for an upstream Yoga display:contents bug present in
23
+ // RN 0.77-0.82 (fixed by facebook/react-native#56422, shipped in 0.86 and
24
+ // backports): because contents nodes are skipped by Yoga's layout
25
+ // traversal, their children are cloned through side channels
26
+ // (cleanupContentsNodesRecursively / layoutAbsoluteDescendants) that call
27
+ // cloneChildrenIfNeeded() on a contents node even when it is clean and
28
+ // belongs to an already-committed (sealed) tree. If this node was cloned
29
+ // without re-adopting its children, that fires the ShadowNode clone
30
+ // callback on the sealed node and SIGABRTs debug builds
31
+ // (Sealable::ensureUnsealed). Defuse the precondition: eagerly take
32
+ // ownership of every Yoga child at construction time (we are unsealed
33
+ // here), so cloneChildrenIfNeeded() on this node is always a no-op later.
34
+ // This mirrors what YogaLayoutableShadowNode::cloneChildInPlace() does
35
+ // lazily during layout, just moved to a legal (unsealed) point in time.
36
+ auto yogaChildren = yogaNode_.getChildren(); // copy: replaceChild mutates
37
+ for (yoga::Node* childYogaNode : yogaChildren) {
38
+ if (childYogaNode->getOwner() == &yogaNode_) {
39
+ continue;
40
+ }
41
+ auto& childShadowNode = *static_cast<YogaLayoutableShadowNode*>(
42
+ childYogaNode->getContext());
43
+ auto clonedChildShadowNode = childShadowNode.clone(
44
+ {ShadowNodeFragment::propsPlaceholder(),
45
+ ShadowNodeFragment::childrenPlaceholder(),
46
+ childShadowNode.getState()});
47
+ // Public replaceChild() performs the same Yoga bookkeeping as RN's
48
+ // private cloneChildInPlace(): swaps the ShadowNode child and re-owns
49
+ // the fresh clone's Yoga node under yogaNode_.
50
+ replaceChild(childShadowNode, clonedChildShadowNode);
51
+ }
52
+ }
53
+
54
+ } // namespace facebook::react
@@ -0,0 +1,56 @@
1
+ #pragma once
2
+
3
+ #include <react/renderer/components/AmpSessionReplaySpec/EventEmitters.h>
4
+ #include <react/renderer/components/AmpSessionReplaySpec/Props.h>
5
+ #include <react/renderer/components/view/ConcreteViewShadowNode.h>
6
+
7
+ namespace facebook::react {
8
+
9
+ // Named distinctly from codegen's SRMaskViewShadowNode typealias to avoid ODR
10
+ // clashes with generated ShadowNodes.h while reusing the same component name.
11
+ extern const char SRMaskViewComponentName[];
12
+
13
+ // The JS component passes style={display:'contents'}; RN's own
14
+ // updateYogaProps() (run inside the base constructors) parses it into the
15
+ // Yoga style and sets ShadowNodeTraits::ForceFlattenView. We never mutate the
16
+ // Yoga style in C++ — the only customization is unsetting ForceFlattenView
17
+ // after each construction so the Android/iOS host view stays mounted (the
18
+ // session-replay SDK needs a real view to tag for masking) while Yoga still
19
+ // lays the children out as if the host didn't exist.
20
+ //
21
+ // This mirrors the pattern shipped by Expensify react-native-live-markdown
22
+ // (MarkdownTextInputDecoratorShadowNode) and Expo's expo-modules-core
23
+ // (ExpoViewShadowNode's disableForceFlatten): a post-constructor hook invoked
24
+ // from BOTH constructors (create + clone), no ComponentDescriptor::adopt()
25
+ // override, no Yoga style writes.
26
+ class SRMaskViewContentsShadowNode final
27
+ : public ConcreteViewShadowNode<
28
+ SRMaskViewComponentName,
29
+ SRMaskViewProps,
30
+ SRMaskViewEventEmitter> {
31
+ public:
32
+ SRMaskViewContentsShadowNode(
33
+ const ShadowNodeFragment& fragment,
34
+ const ShadowNodeFamily::Shared& family,
35
+ ShadowNodeTraits traits)
36
+ : ConcreteViewShadowNode(fragment, family, traits) {
37
+ initialize();
38
+ }
39
+
40
+ SRMaskViewContentsShadowNode(
41
+ const ShadowNode& sourceShadowNode,
42
+ const ShadowNodeFragment& fragment)
43
+ : ConcreteViewShadowNode(sourceShadowNode, fragment) {
44
+ initialize();
45
+ }
46
+
47
+ private:
48
+ // Runs after the base constructors (and therefore after updateYogaProps(),
49
+ // which re-sets ForceFlattenView on every clone that carries new props).
50
+ // Must be called from every constructor. Besides the trait unset it also
51
+ // eagerly re-owns this node's Yoga children — a workaround for an upstream
52
+ // RN 0.77-0.82 display:contents crash; see the .cpp for details.
53
+ void initialize();
54
+ };
55
+
56
+ } // namespace facebook::react
@@ -16,7 +16,7 @@ class NativeSessionReplay: NSObject, RCTBridgeModule {
16
16
  }
17
17
 
18
18
  @objc(setup:resolve:reject:)
19
- func setup(_ config: NSDictionary, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
19
+ func setup(_ config: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
20
20
  guard let apiKey = config["apiKey"] as? String,
21
21
  let sessionId = config["sessionId"] as? NSNumber,
22
22
  let serverZone = config["serverZone"] as? String,
@@ -62,10 +62,25 @@ class NativeSessionReplay: NSObject, RCTBridgeModule {
62
62
  enableRemoteConfig: enableRemoteConfig
63
63
  )
64
64
 
65
- if (autoStart) {
66
- sessionReplay.start()
65
+ // Register the primitive, start capture, and resolve in ONE main-thread
66
+ // block: the registry is main-thread-only (setup runs on the RN
67
+ // native-modules queue), and registration must precede start() on the
68
+ // capture (main) thread so an early capture frame can't snapshot
69
+ // SRMaskView children before their masking intents apply.
70
+ // Re-registering on repeated setup() calls is harmless.
71
+ DispatchQueue.main.async {
72
+ // Invalidated (e.g. RN reload) before deferred setup ran — settle
73
+ // the promise so JS init() never hangs.
74
+ guard let sessionReplay = self.sessionReplay else {
75
+ reject("SETUP_ERROR", "Session Replay module was invalidated before setup completed", nil)
76
+ return
77
+ }
78
+ SRMaskingRegistry.primitive = SRDefaultMaskingPrimitive()
79
+ if (autoStart) {
80
+ sessionReplay.start()
81
+ }
82
+ resolve(nil)
67
83
  }
68
- resolve(nil)
69
84
  }
70
85
 
71
86
  @objc(setSessionId:resolve:reject:)
@@ -127,10 +142,16 @@ class NativeSessionReplay: NSObject, RCTBridgeModule {
127
142
 
128
143
  @objc(invalidate)
129
144
  func invalidate() {
130
- print("invalidate")
131
- // could be nil here
132
- sessionReplay?.stop()
133
- sessionReplay = nil
145
+ // Serialize teardown with the deferred setup() block on the main queue —
146
+ // a mid-setup invalidate can no longer interleave; stop always runs either
147
+ // before the block (guard rejects) or after start() (normal teardown).
148
+ // Strong self capture is intentional: keep the module alive until teardown runs.
149
+ DispatchQueue.main.async {
150
+ print("invalidate")
151
+ // could be nil here
152
+ self.sessionReplay?.stop()
153
+ self.sessionReplay = nil
154
+ }
134
155
  }
135
156
  }
136
157
 
@@ -0,0 +1,34 @@
1
+ import UIKit
2
+ import AmplitudeSessionReplay
3
+
4
+ /// Default `SRMaskingPrimitive` bridging the masking seam to the Amplitude
5
+ /// Session Replay iOS SDK's existing hook (`UIView.amp_isBlocked`):
6
+ ///
7
+ /// - `maskView(_:level:)` — both `"mask"` and `"block"` map to
8
+ /// `amp_isBlocked = true` (the iOS SDK exposes a single blocking hook).
9
+ /// - `unmaskView(_:)` — `amp_isBlocked = false`.
10
+ /// - `resetView(_:)` — `amp_isBlocked = false`. Approximation: the SDK has no
11
+ /// per-view "inherit" reset today; a precise `.none` reset is a documented
12
+ /// fast-follow.
13
+ ///
14
+ /// Registered on the main thread at SDK init (`NativeSessionReplay.setup`);
15
+ /// registration replays intents recorded before init, so mount-before-init
16
+ /// masking still applies.
17
+ ///
18
+ /// The explicit ObjC name lets the native canaries reach this class via
19
+ /// `NSClassFromString` without new public headers.
20
+ @objc(SRDefaultMaskingPrimitive)
21
+ class SRDefaultMaskingPrimitive: NSObject, SRMaskingPrimitive {
22
+ func maskView(_ view: UIView, level: String) {
23
+ // "mask", "block", and unknown levels all fail safe to blocking.
24
+ view.amp_isBlocked = true
25
+ }
26
+
27
+ func unmaskView(_ view: UIView) {
28
+ view.amp_isBlocked = false
29
+ }
30
+
31
+ func resetView(_ view: UIView) {
32
+ view.amp_isBlocked = false
33
+ }
34
+ }
@@ -0,0 +1,26 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <UIKit/UIKit.h>
3
+
4
+ NS_ASSUME_NONNULL_BEGIN
5
+
6
+ /// Recorder-agnostic masking primitive. Until a concrete implementation is
7
+ /// registered at SDK init, the registry is inert.
8
+ @protocol SRMaskingPrimitive <NSObject>
9
+ - (void)maskView:(UIView *)view level:(NSString *)level;
10
+ - (void)unmaskView:(UIView *)view;
11
+ /// Return the view to "inherit" — no longer explicitly masked or unmasked.
12
+ - (void)resetView:(UIView *)view;
13
+ @end
14
+
15
+ /// Indirection between the Fabric `SRMaskView` and the concrete masking
16
+ /// primitive. The registry records each live view's masking intent and replays
17
+ /// it when a primitive registers, so views masked before registration are still
18
+ /// applied (without a JS re-render).
19
+ @interface SRMaskingRegistry : NSObject
20
+ @property (class, nonatomic, nullable, strong) id<SRMaskingPrimitive> primitive;
21
+ + (void)maskView:(UIView *)view level:(NSString *)level;
22
+ + (void)unmaskView:(UIView *)view;
23
+ + (void)resetView:(UIView *)view;
24
+ @end
25
+
26
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,105 @@
1
+ #import "SRMaskingPrimitive.h"
2
+
3
+ NS_ASSUME_NONNULL_BEGIN
4
+
5
+ /// A view's recorded masking intent; `level` is only meaningful when `unmask == NO`.
6
+ @interface SRMaskingIntent : NSObject
7
+ @property (nonatomic) BOOL unmask;
8
+ @property (nonatomic, copy, nullable) NSString *level;
9
+ @end
10
+
11
+ @implementation SRMaskingIntent
12
+ @end
13
+
14
+ @implementation SRMaskingRegistry
15
+
16
+ static id<SRMaskingPrimitive> _primitive = nil;
17
+ static BOOL _warnedUnregistered = NO;
18
+
19
+ /// Weak keys let views dealloc naturally. Main-thread-only, so no locking.
20
+ static NSMapTable<UIView *, SRMaskingIntent *> *_intents = nil;
21
+
22
+ + (NSMapTable<UIView *, SRMaskingIntent *> *)intents
23
+ {
24
+ if (_intents == nil) {
25
+ _intents = [NSMapTable weakToStrongObjectsMapTable];
26
+ }
27
+ return _intents;
28
+ }
29
+
30
+ + (nullable id<SRMaskingPrimitive>)primitive
31
+ {
32
+ return _primitive;
33
+ }
34
+
35
+ + (void)setPrimitive:(nullable id<SRMaskingPrimitive>)primitive
36
+ {
37
+ _primitive = primitive;
38
+ if (primitive == nil) {
39
+ return;
40
+ }
41
+ for (UIView *view in [[self intents] keyEnumerator]) {
42
+ SRMaskingIntent *intent = [[self intents] objectForKey:view];
43
+ if (intent == nil) {
44
+ continue;
45
+ }
46
+ if (intent.unmask) {
47
+ [primitive unmaskView:view];
48
+ } else {
49
+ [primitive maskView:view level:(intent.level ?: @"")];
50
+ }
51
+ }
52
+ }
53
+
54
+ + (void)maskView:(UIView *)view level:(NSString *)level
55
+ {
56
+ SRMaskingIntent *intent = [[SRMaskingIntent alloc] init];
57
+ intent.unmask = NO;
58
+ intent.level = level;
59
+ [[self intents] setObject:intent forKey:view];
60
+
61
+ if (_primitive != nil) {
62
+ [_primitive maskView:view level:level];
63
+ } else {
64
+ [self warnUnregisteredOnce];
65
+ }
66
+ }
67
+
68
+ + (void)unmaskView:(UIView *)view
69
+ {
70
+ SRMaskingIntent *intent = [[SRMaskingIntent alloc] init];
71
+ intent.unmask = YES;
72
+ [[self intents] setObject:intent forKey:view];
73
+
74
+ if (_primitive != nil) {
75
+ [_primitive unmaskView:view];
76
+ } else {
77
+ [self warnUnregisteredOnce];
78
+ }
79
+ }
80
+
81
+ + (void)resetView:(UIView *)view
82
+ {
83
+ [[self intents] removeObjectForKey:view];
84
+
85
+ if (_primitive != nil) {
86
+ [_primitive resetView:view];
87
+ } else {
88
+ [self warnUnregisteredOnce];
89
+ }
90
+ }
91
+
92
+ + (void)warnUnregisteredOnce
93
+ {
94
+ #if DEBUG
95
+ if (!_warnedUnregistered) {
96
+ _warnedUnregistered = YES;
97
+ NSLog(@"[SRMaskingRegistry] No masking primitive registered; masking calls "
98
+ @"are recorded and will replay once a primitive registers.");
99
+ }
100
+ #endif
101
+ }
102
+
103
+ @end
104
+
105
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,10 @@
1
+ #import <React/RCTViewComponentView.h>
2
+ #import <UIKit/UIKit.h>
3
+
4
+ NS_ASSUME_NONNULL_BEGIN
5
+
6
+ @interface SRMaskView : RCTViewComponentView
7
+
8
+ @end
9
+
10
+ NS_ASSUME_NONNULL_END