Skip to content

Commit d773ed2

Browse files
authored
Merge pull request #29 from SourcePointUSA/DIA-5988_fix_events_with_payload
avoid `EventEmitter` with complex data types
2 parents 2d11946 + 50881b5 commit d773ed2

File tree

7 files changed

+63
-26
lines changed

7 files changed

+63
-26
lines changed

android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sourcepoint.reactnativecmp
22

33
import android.view.View
4-
import com.facebook.react.bridge.Arguments.createMap
54
import com.facebook.react.bridge.Callback
65
import com.facebook.react.bridge.Promise
76
import com.facebook.react.bridge.ReactApplicationContext
@@ -21,6 +20,8 @@ import com.sourcepoint.reactnativecmp.arguments.BuildOptions
2120
import com.sourcepoint.reactnativecmp.arguments.toList
2221
import com.sourcepoint.reactnativecmp.consents.RNSPGDPRConsent
2322
import com.sourcepoint.reactnativecmp.consents.RNSPUserData
23+
import kotlinx.serialization.encodeToString
24+
import kotlinx.serialization.json.Json
2425

2526
data class SPLoadMessageParams(val authId: String?) {
2627
constructor(fromReadableMap: ReadableMap?) : this(authId = fromReadableMap?.getString("authId"))
@@ -139,8 +140,8 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN
139140
vendors: ReadableArray,
140141
categories: ReadableArray,
141142
legIntCategories: ReadableArray,
142-
callback: Callback)
143-
{
143+
callback: Callback
144+
) {
144145
runOnMainThread {
145146
spConsentLib?.deleteCustomConsentTo(
146147
vendors.toList(),
@@ -162,17 +163,17 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN
162163
}
163164

164165
override fun onAction(view: View, consentAction: ConsentAction): ConsentAction {
165-
emitOnAction(createMap().apply {
166-
putString("actionType", RNSourcepointActionType.from(consentAction.actionType).name)
167-
putString("customActionId", consentAction.customActionId)
168-
})
166+
emitInternalOnAction(Json.encodeToString(mapOf(
167+
"actionType" to RNSourcepointActionType.from(consentAction.actionType).name,
168+
"customActionId" to consentAction.customActionId
169+
)))
169170
return consentAction
170171
}
171172

172173
override fun onConsentReady(consent: SPConsents) {}
173174

174175
override fun onError(error: Throwable) {
175-
emitOnError(createMap().apply { putString("description", error.message) })
176+
emitInternalOnError(Json.encodeToString(mapOf("description" to error.message)))
176177
}
177178

178179
override fun onNoIntentActivitiesFound(url: String) {}

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ PODS:
16591659
- React-logger (= 0.79.2)
16601660
- React-perflogger (= 0.79.2)
16611661
- React-utils (= 0.79.2)
1662-
- ReactNativeCmp (1.0.8):
1662+
- ReactNativeCmp (1.0.9-beta.2):
16631663
- ConsentViewController (= 7.12.1)
16641664
- DoubleConversion
16651665
- glog
@@ -1989,11 +1989,11 @@ SPEC CHECKSUMS:
19891989
ReactAppDependencyProvider: 04d5eb15eb46be6720e17a4a7fa92940a776e584
19901990
ReactCodegen: c63eda03ba1d94353fb97b031fc84f75a0d125ba
19911991
ReactCommon: 76d2dc87136d0a667678668b86f0fca0c16fdeb0
1992-
ReactNativeCmp: 332f0bc3333fc115006179d18a0dc7809d8e1c5f
1992+
ReactNativeCmp: d79d97ff49c3e5ca3aa5f8a6608dbe06a30e7c7a
19931993
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
19941994
SPMobileCore: 220d109e404cb17169f7e26f2a34c6022b80079a
19951995
Yoga: c758bfb934100bb4bf9cbaccb52557cee35e8bdf
19961996

19971997
PODFILE CHECKSUM: decdc7519d77aa5eae65b167fa59bcfce25e15d2
19981998

1999-
COCOAPODS: 1.15.2
1999+
COCOAPODS: 1.16.2

ios/RNSourcepointCmp.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ import React
2828
self.customActionId = customActionId
2929
}
3030

31-
@objc public func toDictionary() -> [String: Any] {
31+
func toDictionary() -> [String: Any] {
3232
["actionType": type.description, "customActionId": customActionId]
3333
}
34+
35+
@objc public func stringifiedJson() -> String {
36+
if let jsonData = try? JSONSerialization.data(withJSONObject: toDictionary()),
37+
let jsonString = String(data: jsonData, encoding: .utf8) {
38+
return jsonString
39+
} else {
40+
return "{\"actionType\":\"unknown\"}"
41+
}
42+
}
3443
}
3544

3645
@objc public protocol ReactNativeCmpImplDelegate {

ios/ReactNativeCmp.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ - (void)postDeleteCustomConsentGDPR:(NSArray *)vendors
130130

131131
// MARK: SPDelegate
132132
- (void)onAction:(RNAction*)action {
133-
[self emitOnAction: [action toDictionary]];
133+
[self emitInternalOnAction: [action stringifiedJson]];
134134
}
135135

136136
- (void)onErrorWithDescription:(NSString * _Nonnull)description {
137-
[self emitOnError: @{ @"description": description }];
137+
NSDictionary *dict = @{@"description": description};
138+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
139+
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
140+
[self emitInternalOnError: json];
138141
}
139142

140143
- (void)onFinished {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sourcepoint/react-native-cmp",
3-
"version": "1.0.8",
3+
"version": "1.0.9-beta.2",
44
"description": "The official react native bridge to the native Sourcepoint SDKs",
55
"main": "./lib/module/index.js",
66
"types": "./lib/typescript/src/index.d.ts",

src/NativeReactNativeCmp.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export type SPBuildOptions = {
201201
androidDismissMessageOnBackPress?: boolean;
202202
}
203203

204+
export type SPError = {
205+
description: string;
206+
};
207+
204208
export interface Spec extends TurboModule {
205209
build(
206210
accountId: number,
@@ -230,12 +234,12 @@ export interface Spec extends TurboModule {
230234
callback: (consent: GDPRConsent) => void
231235
): void;
232236

233-
readonly onAction: EventEmitter<SPAction>;
237+
readonly internalOnAction: EventEmitter<string>;
234238
readonly onSPUIReady: EventEmitter<void>;
235239
readonly onSPUIFinished: EventEmitter<void>;
236240
readonly onFinished: EventEmitter<void>;
237241
readonly onMessageInactivityTimeout: EventEmitter<void>;
238-
readonly onError: EventEmitter<{ description: string }>;
242+
readonly internalOnError: EventEmitter<string>;
239243
}
240244

241245
export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeCmp');

src/index.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
SPAction,
77
SPBuildOptions,
88
GDPRConsent,
9+
SPError
910
} from './NativeReactNativeCmp';
1011
import ReactNativeCmp, { SPMessageLanguage } from './NativeReactNativeCmp';
1112
import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes';
@@ -18,6 +19,32 @@ const defaultBuildOptions: SPBuildOptions = {
1819
}
1920

2021
export default class SPConsentManager implements Spec {
22+
/** intended to be used by the SDK only */
23+
internalOnAction: EventEmitter<string> = ReactNativeCmp.internalOnAction;
24+
/** intended to be used by the SDK only */
25+
internalOnError: EventEmitter<string> = ReactNativeCmp.internalOnError;
26+
27+
onSPUIReady: EventEmitter<void> = ReactNativeCmp.onSPUIReady;
28+
onSPUIFinished: EventEmitter<void> = ReactNativeCmp.onSPUIFinished;
29+
onFinished: EventEmitter<void> = ReactNativeCmp.onFinished;
30+
onMessageInactivityTimeout: EventEmitter<void> = ReactNativeCmp.onMessageInactivityTimeout;
31+
32+
onAction(handler: (action: SPAction) => void) {
33+
ReactNativeCmp.internalOnAction((stringifiedAction) => {
34+
handler(JSON.parse(stringifiedAction) as SPAction);
35+
});
36+
}
37+
38+
onError(handler: (error: SPError) => void) {
39+
ReactNativeCmp.internalOnError((stringifiedError) => {
40+
handler(JSON.parse(stringifiedError) as SPError);
41+
});
42+
}
43+
44+
getConstants?(): {} {
45+
throw new Error('Method not implemented.');
46+
}
47+
2148
build(
2249
accountId: number,
2350
propertyId: number,
@@ -74,14 +101,7 @@ export default class SPConsentManager implements Spec {
74101
categories: string[],
75102
legIntCategories: string[],
76103
callback: (consent: GDPRConsent) => void
77-
) {
78-
ReactNativeCmp.postDeleteCustomConsentGDPR(vendors, categories, legIntCategories, callback);
104+
) {
105+
ReactNativeCmp.postDeleteCustomConsentGDPR(vendors, categories, legIntCategories, callback);
79106
}
80-
81-
onAction: EventEmitter<SPAction> = ReactNativeCmp.onAction;
82-
onSPUIReady: EventEmitter<void> = ReactNativeCmp.onSPUIReady;
83-
onSPUIFinished: EventEmitter<void> = ReactNativeCmp.onSPUIFinished;
84-
onFinished: EventEmitter<void> = ReactNativeCmp.onFinished;
85-
onMessageInactivityTimeout: EventEmitter<void> = ReactNativeCmp.onMessageInactivityTimeout;
86-
onError: EventEmitter<{ description: string }> = ReactNativeCmp.onError;
87107
}

0 commit comments

Comments
 (0)