Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit 47f50de

Browse files
committed
Expose promises to prevent possible race conditions
1 parent 74ac993 commit 47f50de

File tree

5 files changed

+57
-31
lines changed

5 files changed

+57
-31
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ When your app is ready to be interacted with (for example, after showing the spl
171171
```js
172172
import { BatchMessaging } from '@bam.tech/react-native-batch';
173173

174-
BatchMessaging.setNotDisturbed(false);
175-
BatchMessaging.showPendingMessage();
174+
await BatchMessaging.setNotDisturbed(false);
175+
await BatchMessaging.showPendingMessage();
176176
```
177177

178178
**NB:** Batch's Do Not Disturb mode is enabled by default in React Native because your JS app might not be loaded when the user interacts with it. This could lead to problems when a button should redirect inside your JS app. You can disable this behaviour (see below).
@@ -247,7 +247,7 @@ Add `BATCH_OPTED_OUT_BY_DEFAULT=true` in your `Info.plist` as described in the [
247247
```js
248248
import { Batch } from '@bam.tech/react-native-batch';
249249

250-
Batch.optIn();
250+
await Batch.optIn();
251251
```
252252

253253
#### 3. Delete the user's data when the user wants to delete its account
@@ -258,7 +258,7 @@ As per the [SDK documentation](https://doc.batch.com/android/advanced/opt-out#op
258258
```js
259259
import { Batch } from '@bam.tech/react-native-batch';
260260

261-
Batch.optOutAndWipeData();
261+
await Batch.optOutAndWipeData();
262262
```
263263

264264
### Handling push notification initial deeplink

android/src/main/java/tech/bam/RNBatchPush/RNBatchModule.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,22 @@ public void start() {
113113
// BASE MODULE
114114

115115
@ReactMethod
116-
public void optIn() {
116+
public void optIn(Promise promise) {
117117
Batch.optIn(reactContext);
118118
start();
119+
promise.resolve(null);
119120
}
120121

121122
@ReactMethod
122-
public void optOut() {
123+
public void optOut(Promise promise) {
123124
Batch.optOut(reactContext);
125+
promise.resolve(null);
124126
}
125127

126128
@ReactMethod
127-
public void optOutAndWipeData() {
129+
public void optOutAndWipeData(Promise promise) {
128130
Batch.optOutAndWipeData(reactContext);
131+
promise.resolve(null);
129132
}
130133

131134
// PUSH MODULE
@@ -154,27 +157,32 @@ public void push_getLastKnownPushToken(Promise promise) {
154157
// MESSAGING MODULE
155158

156159
@ReactMethod
157-
public void messaging_showPendingMessage() {
158-
Boolean test = Batch.Messaging.isDoNotDisturbEnabled();
160+
public void messaging_showPendingMessage(Promise promise) {
159161
BatchMessage msg = Batch.Messaging.popPendingMessage();
160162
if (msg != null) {
161163
Batch.Messaging.show(getCurrentActivity(), msg);
162164
}
165+
166+
promise.resolve(null);
163167
}
164168

165169
@ReactMethod
166-
public void messaging_setNotDisturbed(final boolean active) {
170+
public void messaging_setNotDisturbed(final boolean active, Promise promise) {
167171
Batch.Messaging.setDoNotDisturbEnabled(active);
172+
173+
promise.resolve(null);
168174
}
169175

170176
@ReactMethod
171-
public void messaging_setTypefaceOverride(@Nullable String normalTypefaceName, @Nullable String boldTypefaceName) {
177+
public void messaging_setTypefaceOverride(@Nullable String normalTypefaceName, @Nullable String boldTypefaceName, Promise promise) {
172178
AssetManager assetManager = this.reactContext.getAssets();
173179
@Nullable Typeface normalTypeface = normalTypefaceName != null ? createTypeface(normalTypefaceName, Typeface.NORMAL, assetManager) : null;
174180
@Nullable Typeface boldTypeface = boldTypefaceName != null ? createTypeface(boldTypefaceName, Typeface.BOLD, assetManager) : null;
175181
@Nullable Typeface boldTypefaceFallback = boldTypefaceName != null ? createTypeface(boldTypefaceName, Typeface.NORMAL, assetManager) : null;
176182

177183
Batch.Messaging.setTypefaceOverride(normalTypeface, boldTypeface != null ? boldTypeface : boldTypefaceFallback);
184+
185+
promise.resolve(null);
178186
}
179187

180188
// from https://github.com/facebook/react-native/blob/dc80b2dcb52fadec6a573a9dd1824393f8c29fdc/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java#L118

ios/RNBatch.m

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,26 @@ + (void)start
4646
[Batch startWithAPIKey:batchAPIKey];
4747
}
4848

49-
RCT_EXPORT_METHOD(optIn)
49+
RCT_EXPORT_METHOD(optIn:(RCTPromiseResolveBlock)resolve
50+
rejecter:(RCTPromiseRejectBlock)reject)
5051
{
5152
[Batch optIn];
5253
[RNBatch start];
54+
resolve([NSNull null]);
5355
}
5456

55-
RCT_EXPORT_METHOD(optOut)
57+
RCT_EXPORT_METHOD(optOut:(RCTPromiseResolveBlock)resolve
58+
rejecter:(RCTPromiseRejectBlock)reject)
5659
{
5760
[Batch optOut];
61+
resolve([NSNull null]);
5862
}
5963

60-
RCT_EXPORT_METHOD(optOutAndWipeData)
64+
RCT_EXPORT_METHOD(optOutAndWipeData:(RCTPromiseResolveBlock)resolve
65+
rejecter:(RCTPromiseRejectBlock)reject)
6166
{
6267
[Batch optOutAndWipeData];
68+
resolve([NSNull null]);
6369
}
6470

6571
RCT_EXPORT_METHOD(presentDebugViewController)
@@ -568,25 +574,34 @@ - (NSDictionary*) dictionaryWithNotification:(BatchInboxNotificationContent*)not
568574

569575
// Messaging module
570576

571-
RCT_EXPORT_METHOD(messaging_setNotDisturbed:(BOOL) active)
577+
RCT_EXPORT_METHOD(messaging_setNotDisturbed:(BOOL) active
578+
resolver: (RCTPromiseResolveBlock)resolve
579+
rejecter:(RCTPromiseRejectBlock)reject)
572580
{
573581
[BatchMessaging setDoNotDisturb:active];
582+
resolve([NSNull null]);
574583
}
575584

576-
RCT_EXPORT_METHOD(messaging_showPendingMessage) {
585+
RCT_EXPORT_METHOD(messaging_showPendingMessage:(RCTPromiseResolveBlock)resolve
586+
rejecter:(RCTPromiseRejectBlock)reject) {
577587
dispatch_async(dispatch_get_main_queue(), ^{
578588
[BatchMessaging showPendingMessage];
589+
resolve([NSNull null]);
579590
});
580591
}
581592

582-
RCT_EXPORT_METHOD(messaging_setFontOverride:(nullable NSString*) normalFontName boldFontName:(nullable NSString*) boldFontName italicFontName:(nullable NSString*) italicFontName italicBoldFontName:(nullable NSString*) italicBoldFontName)
593+
RCT_EXPORT_METHOD(messaging_setFontOverride:(nullable NSString*) normalFontName boldFontName:(nullable NSString*) boldFontName italicFontName:(nullable NSString*) italicFontName italicBoldFontName:(nullable NSString*) italicBoldFontName
594+
resolver: (RCTPromiseResolveBlock)resolve
595+
rejecter:(RCTPromiseRejectBlock)reject)
583596
{
584597
UIFont* normalFont = normalFontName != nil ? [UIFont fontWithName:normalFontName size: 14] : nil;
585598
UIFont* boldFont = boldFontName != nil ? [UIFont fontWithName:boldFontName size: 14] : nil;
586599
UIFont* italicFont = italicFontName != nil ? [UIFont fontWithName:italicFontName size: 14] : nil;
587600
UIFont* italicBoldFont = italicBoldFontName != nil ? [UIFont fontWithName:italicBoldFontName size: 14] : nil;
588601

589602
[BatchMessaging setFontOverride:normalFont boldFont:boldFont italicFont:italicFont boldItalicFont:italicBoldFont];
603+
604+
resolve([NSNull null]);
590605
}
591606

592607
@end

src/Batch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const Batch = {
2222
* Some features might not be disabled until the next app start if you call this late into the application's life. It is strongly
2323
* advised to restart the application (or at least the current activity) after opting in.
2424
*/
25-
optIn: (): void => RNBatch.optIn(),
25+
optIn: (): Promise<void> => RNBatch.optIn(),
2626

2727
/**
2828
* Opt Out from Batch SDK Usage
@@ -46,7 +46,7 @@ export const Batch = {
4646
* Your app should be prepared to handle these cases.
4747
* Some features might not be disabled until the next app start.
4848
*/
49-
optOut: (): void => RNBatch.optOut(),
49+
optOut: (): Promise<void> => RNBatch.optOut(),
5050

5151
/**
5252
* Opt Out from Batch SDK Usage
@@ -56,7 +56,7 @@ export const Batch = {
5656
* Note that calling this method will stop Batch.
5757
* Your app should be prepared to handle these cases.
5858
*/
59-
optOutAndWipeData: (): void => RNBatch.optOutAndWipeData(),
59+
optOutAndWipeData: (): Promise<void> => RNBatch.optOutAndWipeData(),
6060

6161
/**
6262
* Shows debug view

src/BatchMessaging.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ const RNBatch = NativeModules.RNBatch;
33

44
export const BatchMessaging = {
55
/**
6-
* - Shows the currently enqueued message, if any.
7-
* - Used in conjonction with `Batch.start(true)` to display pending messages
6+
* Shows the currently enqueued message, if any.
87
*/
9-
showPendingMessage: (): void => RNBatch.messaging_showPendingMessage(),
8+
showPendingMessage: (): Promise<void> =>
9+
RNBatch.messaging_showPendingMessage(),
1010

1111
/**
1212
* Define if incoming messages have to be enqueued or displayed directly
1313
*
1414
* @param active
1515
*/
16-
setNotDisturbed: (active: boolean): void =>
16+
setNotDisturbed: (active: boolean): Promise<void> =>
1717
RNBatch.messaging_setNotDisturbed(active),
1818

1919
/**
@@ -31,16 +31,19 @@ export const BatchMessaging = {
3131
boldFontName?: string | null,
3232
italicFontName?: string | null,
3333
italicBoldFontName?: string | null
34-
): void => {
34+
): Promise<void> => {
3535
if (Platform.OS === 'android') {
36-
RNBatch.messaging_setTypefaceOverride(normalFontName, boldFontName);
37-
} else {
38-
RNBatch.messaging_setFontOverride(
36+
return RNBatch.messaging_setTypefaceOverride(
3937
normalFontName,
40-
boldFontName,
41-
italicFontName,
42-
italicBoldFontName
38+
boldFontName
4339
);
4440
}
41+
42+
return RNBatch.messaging_setFontOverride(
43+
normalFontName,
44+
boldFontName,
45+
italicFontName,
46+
italicBoldFontName
47+
);
4548
},
4649
};

0 commit comments

Comments
 (0)