Skip to content

Commit 15bea57

Browse files
committed
Rename helper function
Motivation: this function checks whether the `RNOneSignal` object is non null OR undefined (`!=`) so we should clarify this doesn't check for OneSignal initialization. The new function name should be more clear. Note: this check exists since PR OneSignal#485 which provided Expo support (not really support but rather avoid crashing the app in Expo environments)
1 parent 4c8028c commit 15bea57

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

src/helpers.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ export function isValidCallback(handler) {
1313
);
1414
}
1515

16-
export function checkIfInitialized(object) {
17-
const initialized = object != null;
18-
return initialized;
16+
export function isObjectNonNull(object) {
17+
return object != null;
1918
}
2019

2120
/**

src/index.js

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
EMAIL_SUBSCRIPTION_CHANGED,
1212
SMS_SUBSCRIPTION_CHANGED
1313
} from './events';
14-
import { isValidCallback, checkIfInitialized } from './helpers';
14+
import { isValidCallback, isObjectNonNull } from './helpers';
1515

1616
const RNOneSignal = NativeModules.OneSignal;
1717
const eventManager = new EventManager(RNOneSignal);
@@ -30,28 +30,28 @@ export default class OneSignal {
3030
/* O B S E R V E R S */
3131

3232
static addPermissionObserver(observer) {
33-
if (!checkIfInitialized(RNOneSignal)) return;
33+
if (!isObjectNonNull(RNOneSignal)) return;
3434
isValidCallback(observer);
3535
RNOneSignal.addPermissionObserver();
3636
eventManager.addEventHandler(PERMISSION_CHANGED, observer);
3737
}
3838

3939
static addSubscriptionObserver(observer) {
40-
if (!checkIfInitialized(RNOneSignal)) return;
40+
if (!isObjectNonNull(RNOneSignal)) return;
4141
isValidCallback(observer);
4242
RNOneSignal.addSubscriptionObserver();
4343
eventManager.addEventHandler(SUBSCRIPTION_CHANGED, observer);
4444
}
4545

4646
static addEmailSubscriptionObserver(observer) {
47-
if (!checkIfInitialized(RNOneSignal)) return;
47+
if (!isObjectNonNull(RNOneSignal)) return;
4848
isValidCallback(observer);
4949
RNOneSignal.addEmailSubscriptionObserver();
5050
eventManager.addEventHandler(EMAIL_SUBSCRIPTION_CHANGED, observer);
5151
}
5252

5353
static addSMSSubscriptionObserver(observer) {
54-
if (!checkIfInitialized(RNOneSignal)) return;
54+
if (!isObjectNonNull(RNOneSignal)) return;
5555
isValidCallback(observer);
5656
RNOneSignal.addSMSSubscriptionObserver();
5757
eventManager.addEventHandler(SMS_SUBSCRIPTION_CHANGED, observer);
@@ -67,14 +67,14 @@ export default class OneSignal {
6767
* @param {function} handler
6868
*/
6969
static setNotificationWillShowInForegroundHandler(handler){
70-
if (!checkIfInitialized(RNOneSignal)) return;
70+
if (!isObjectNonNull(RNOneSignal)) return;
7171
isValidCallback(handler);
7272
RNOneSignal.setNotificationWillShowInForegroundHandler();
7373
eventManager.setEventHandler(NOTIFICATION_WILL_SHOW, handler);
7474
}
7575

7676
static setNotificationOpenedHandler(handler){
77-
if (!checkIfInitialized(RNOneSignal)) return;
77+
if (!isObjectNonNull(RNOneSignal)) return;
7878
isValidCallback(handler);
7979

8080
RNOneSignal.setNotificationOpenedHandler();
@@ -84,18 +84,18 @@ export default class OneSignal {
8484
/* R E G I S T R A T I O N E T C */
8585

8686
static promptForPushNotificationsWithUserResponse(handler) {
87-
if (!checkIfInitialized(RNOneSignal)) return;
87+
if (!isObjectNonNull(RNOneSignal)) return;
8888

8989
if (Platform.OS === 'ios') {
9090
isValidCallback(handler);
9191
RNOneSignal.promptForPushNotificationsWithUserResponse(handler);
9292
} else {
9393
console.log("promptForPushNotificationsWithUserResponse: this function is not supported on Android");
9494
}
95-
}
96-
95+
}
96+
9797
static registerForProvisionalAuthorization(handler) {
98-
if (!checkIfInitialized(RNOneSignal)) return;
98+
if (!isObjectNonNull(RNOneSignal)) return;
9999

100100
if (Platform.OS === 'ios') {
101101
isValidCallback(handler);
@@ -106,13 +106,13 @@ export default class OneSignal {
106106
}
107107

108108
static disablePush(disable) {
109-
if (!checkIfInitialized(RNOneSignal)) return;
109+
if (!isObjectNonNull(RNOneSignal)) return;
110110

111111
RNOneSignal.disablePush(disable);
112112
}
113113

114114
static unsubscribeWhenNotificationsAreDisabled(unsubscribe) {
115-
if (!checkIfInitialized(RNOneSignal)) return;
115+
if (!isObjectNonNull(RNOneSignal)) return;
116116

117117
if (Platform.OS === 'android') {
118118
RNOneSignal.unsubscribeWhenNotificationsAreDisabled(unsubscribe);
@@ -125,18 +125,18 @@ export default class OneSignal {
125125

126126
static isLocationShared() {
127127
// must return a promise
128-
if (!checkIfInitialized(RNOneSignal)) return Promise.resolve();
128+
if (!isObjectNonNull(RNOneSignal)) return Promise.resolve();
129129
return RNOneSignal.isLocationShared();
130130
}
131131

132132
static setLocationShared(shared) {
133-
if (!checkIfInitialized(RNOneSignal)) return;
133+
if (!isObjectNonNull(RNOneSignal)) return;
134134

135135
RNOneSignal.setLocationShared(shared);
136136
}
137137

138138
static promptLocation() {
139-
if (!checkIfInitialized(RNOneSignal)) return;
139+
if (!isObjectNonNull(RNOneSignal)) return;
140140

141141
//Supported in both iOS & Android
142142
RNOneSignal.promptLocation();
@@ -148,7 +148,7 @@ export default class OneSignal {
148148
* Gets the device state.
149149
*/
150150
static async getDeviceState() {
151-
if (!checkIfInitialized(RNOneSignal)) return Promise.resolve();
151+
if (!isObjectNonNull(RNOneSignal)) return Promise.resolve();
152152
const deviceState = await RNOneSignal.getDeviceState();
153153

154154
if (Platform.OS === 'android') {
@@ -162,7 +162,7 @@ export default class OneSignal {
162162
/* T A G S */
163163

164164
static sendTag(key, value) {
165-
if (!checkIfInitialized(RNOneSignal)) return;
165+
if (!isObjectNonNull(RNOneSignal)) return;
166166

167167
if (!key || (!value && value !== "")) {
168168
console.error("OneSignal: sendTag: must include a key and a value");
@@ -176,7 +176,7 @@ export default class OneSignal {
176176
}
177177

178178
static sendTags(tags) {
179-
if (!checkIfInitialized(RNOneSignal)) return;
179+
if (!isObjectNonNull(RNOneSignal)) return;
180180
let keys = Object.keys(tags);
181181

182182
if (keys.length === 0) {
@@ -193,20 +193,20 @@ export default class OneSignal {
193193
}
194194

195195
static getTags(callback) {
196-
if (!checkIfInitialized(RNOneSignal)) return;
196+
if (!isObjectNonNull(RNOneSignal)) return;
197197
RNOneSignal.getTags(callback);
198198
}
199199

200200
static deleteTag(key) {
201-
if (!checkIfInitialized(RNOneSignal)) return;
201+
if (!isObjectNonNull(RNOneSignal)) return;
202202
if (typeof key !== "string") {
203203
console.error("OneSignal: deleteTag: key argument must be of type string");
204204
}
205205
RNOneSignal.deleteTags([key]);
206206
}
207207

208208
static deleteTags(tagKeys) {
209-
if (!checkIfInitialized(RNOneSignal)) return;
209+
if (!isObjectNonNull(RNOneSignal)) return;
210210

211211
if (!Array.isArray(tagKeys)) {
212212
console.error("OneSignal: deleteTags: argument must be of array type");
@@ -218,7 +218,7 @@ export default class OneSignal {
218218
/* E M A I L */
219219

220220
static setEmail(email, emailAuthCode, handler) {
221-
if (!checkIfInitialized(RNOneSignal)) return;
221+
if (!isObjectNonNull(RNOneSignal)) return;
222222

223223
if (emailAuthCode === undefined)
224224
emailAuthCode = null;
@@ -230,7 +230,7 @@ export default class OneSignal {
230230
}
231231

232232
static logoutEmail(handler) {
233-
if (!checkIfInitialized(RNOneSignal)) return;
233+
if (!isObjectNonNull(RNOneSignal)) return;
234234

235235
if (!handler)
236236
handler = function(){};
@@ -241,7 +241,7 @@ export default class OneSignal {
241241
/* S M S */
242242

243243
static setSMSNumber(smsNumber, smsAuthCode, handler) {
244-
if (!checkIfInitialized(RNOneSignal)) return;
244+
if (!isObjectNonNull(RNOneSignal)) return;
245245

246246
if (smsAuthCode === undefined)
247247
smsAuthCode = null;
@@ -253,7 +253,7 @@ export default class OneSignal {
253253
}
254254

255255
static logoutSMSNumber(handler) {
256-
if (!checkIfInitialized(RNOneSignal)) return;
256+
if (!isObjectNonNull(RNOneSignal)) return;
257257

258258
if (!handler)
259259
handler = function(){};
@@ -264,7 +264,7 @@ export default class OneSignal {
264264
/* N O T I F I C A T I O N S */
265265

266266
static postNotification(notificationObjectString, onSuccess, onFailure) {
267-
if (!checkIfInitialized(RNOneSignal)) return;
267+
if (!isObjectNonNull(RNOneSignal)) return;
268268

269269
if (!onSuccess)
270270
onSuccess = function(){};
@@ -276,7 +276,7 @@ export default class OneSignal {
276276
}
277277

278278
static clearOneSignalNotifications() {
279-
if (!checkIfInitialized(RNOneSignal)) return;
279+
if (!isObjectNonNull(RNOneSignal)) return;
280280

281281
if (Platform.OS === 'android') {
282282
RNOneSignal.clearOneSignalNotifications();
@@ -286,7 +286,7 @@ export default class OneSignal {
286286
}
287287

288288
static removeNotification(id) {
289-
if (!checkIfInitialized(RNOneSignal)) return;
289+
if (!isObjectNonNull(RNOneSignal)) return;
290290

291291
if (Platform.OS === 'android') {
292292
RNOneSignal.removeNotification(id);
@@ -296,7 +296,7 @@ export default class OneSignal {
296296
}
297297

298298
static removeGroupedNotifications(id) {
299-
if (!checkIfInitialized(RNOneSignal)) return;
299+
if (!isObjectNonNull(RNOneSignal)) return;
300300

301301
if (Platform.OS === 'android') {
302302
RNOneSignal.removeGroupedNotifications(id);
@@ -308,7 +308,7 @@ export default class OneSignal {
308308
/* E X T E R N A L U S E R I D */
309309

310310
static setExternalUserId(externalId, varArg1, varArg2) {
311-
if (!checkIfInitialized(RNOneSignal)) return;
311+
if (!isObjectNonNull(RNOneSignal)) return;
312312

313313
if (typeof varArg1 === "function") {
314314
RNOneSignal.setExternalUserId(externalId, null, varArg1);
@@ -323,7 +323,7 @@ export default class OneSignal {
323323
}
324324

325325
static removeExternalUserId(handler) {
326-
if (!checkIfInitialized(RNOneSignal)) return;
326+
if (!isObjectNonNull(RNOneSignal)) return;
327327

328328
if (handler === undefined)
329329
handler = function(){};
@@ -334,7 +334,7 @@ export default class OneSignal {
334334
/* I N A P P M E S S A G I N G */
335335

336336
static setInAppMessageClickHandler(handler) {
337-
if (!checkIfInitialized(RNOneSignal)) return;
337+
if (!isObjectNonNull(RNOneSignal)) return;
338338
isValidCallback(handler);
339339
RNOneSignal.initInAppMessageClickHandlerParams();
340340
RNOneSignal.setInAppMessageClickHandler();
@@ -343,7 +343,7 @@ export default class OneSignal {
343343

344344
// Pass a String key and any value and creates a trigger map to pass to addTriggers()
345345
static addTrigger(key, value) {
346-
if (!checkIfInitialized(RNOneSignal)) return;
346+
if (!isObjectNonNull(RNOneSignal)) return;
347347

348348
if (!key || !value) {
349349
console.error("OneSignal: addTrigger: must include a key and a value");
@@ -357,7 +357,7 @@ export default class OneSignal {
357357

358358
// Expected format is Map<String, Object>, make sure all values are Objects and keys are Strings
359359
static addTriggers(triggers) {
360-
if (!checkIfInitialized(RNOneSignal)) return;
360+
if (!isObjectNonNull(RNOneSignal)) return;
361361

362362
let keys = Object.keys(triggers);
363363

@@ -369,76 +369,76 @@ export default class OneSignal {
369369
}
370370

371371
static removeTriggersForKeys(keys) {
372-
if (!checkIfInitialized(RNOneSignal)) return;
372+
if (!isObjectNonNull(RNOneSignal)) return;
373373

374374
RNOneSignal.removeTriggersForKeys(keys);
375375
}
376376

377377
static removeTriggerForKey(key) {
378-
if (!checkIfInitialized(RNOneSignal)) return;
378+
if (!isObjectNonNull(RNOneSignal)) return;
379379
RNOneSignal.removeTriggerForKey(key);
380380
}
381381

382382
static getTriggerValueForKey(key) {
383383
// must return a promise
384-
if (!checkIfInitialized(RNOneSignal)) return Promise.resolve();
384+
if (!isObjectNonNull(RNOneSignal)) return Promise.resolve();
385385
return RNOneSignal.getTriggerValueForKey(key);
386386
}
387387

388388
static pauseInAppMessages(pause) {
389-
if (!checkIfInitialized(RNOneSignal)) return;
389+
if (!isObjectNonNull(RNOneSignal)) return;
390390
RNOneSignal.pauseInAppMessages(pause);
391391
}
392392

393393
/* O U T C O M E S */
394394

395395
static sendOutcome(name, handler=function(){}) {
396-
if (!checkIfInitialized(RNOneSignal)) return;
396+
if (!isObjectNonNull(RNOneSignal)) return;
397397
RNOneSignal.sendOutcome(name, handler);
398398
}
399399

400400
static sendUniqueOutcome(name, handler=function(){}) {
401-
if (!checkIfInitialized(RNOneSignal)) return;
401+
if (!isObjectNonNull(RNOneSignal)) return;
402402
RNOneSignal.sendUniqueOutcome(name, handler);
403403
}
404404

405405
static sendOutcomeWithValue(name, value, handler=function(){}) {
406-
if (!checkIfInitialized(RNOneSignal)) return;
406+
if (!isObjectNonNull(RNOneSignal)) return;
407407
RNOneSignal.sendOutcomeWithValue(name, Number(value), handler);
408408
}
409409

410410
/* P R I V A C Y C O N S E N T */
411411

412412
static userProvidedPrivacyConsent() {
413-
if (!checkIfInitialized(RNOneSignal)) return Promise.resolve();
413+
if (!isObjectNonNull(RNOneSignal)) return Promise.resolve();
414414

415415
//returns a promise
416416
return RNOneSignal.userProvidedPrivacyConsent();
417417
}
418418

419419
static requiresUserPrivacyConsent() {
420-
if (!checkIfInitialized(RNOneSignal)) return Promise.resolve();
420+
if (!isObjectNonNull(RNOneSignal)) return Promise.resolve();
421421

422422
//returns a promise
423423
return RNOneSignal.requiresUserPrivacyConsent();
424424
}
425425

426426
static setRequiresUserPrivacyConsent(required) {
427-
if (!checkIfInitialized(RNOneSignal)) return;
427+
if (!isObjectNonNull(RNOneSignal)) return;
428428

429429
RNOneSignal.setRequiresUserPrivacyConsent(required);
430430
}
431431

432432
static provideUserConsent(granted) {
433-
if (!checkIfInitialized(RNOneSignal)) return;
433+
if (!isObjectNonNull(RNOneSignal)) return;
434434

435435
RNOneSignal.provideUserConsent(granted);
436436
}
437437

438438
/* O T H E R F U N C T I O N S */
439439

440440
static setLogLevel(nsLogLevel, visualLogLevel) {
441-
if (!checkIfInitialized(RNOneSignal)) return;
441+
if (!isObjectNonNull(RNOneSignal)) return;
442442

443443
RNOneSignal.setLogLevel(nsLogLevel, visualLogLevel);
444444
}

0 commit comments

Comments
 (0)