UNPKG

4.84 kBJavaScriptView Raw
1import { NativeModules, NativeEventEmitter, DeviceEventEmitter, Platform } from 'react-native'
2
3const { RNBranch, RNBranchEventEmitter } = NativeModules
4
5import createBranchUniversalObject from './branchUniversalObject'
6import BranchEvent from './BranchEvent'
7
8const packageFile = require('./../package.json')
9export const VERSION = packageFile.version
10
11export const AddToCartEvent = RNBranch.ADD_TO_CART_EVENT
12export const AddToWishlistEvent = RNBranch.ADD_TO_WISHLIST_EVENT
13export const PurchasedEvent = RNBranch.PURCHASED_EVENT
14export const PurchaseInitiatedEvent = RNBranch.PURCHASE_INITIATED_EVENT
15export const RegisterViewEvent = RNBranch.REGISTER_VIEW_EVENT
16export const ShareCompletedEvent = RNBranch.SHARE_COMPLETED_EVENT
17export const ShareInitiatedEvent = RNBranch.SHARE_INITIATED_EVENT
18
19class Branch {
20 nativeEventEmitter = Platform.select({
21 android: DeviceEventEmitter,
22 ios: new NativeEventEmitter(RNBranchEventEmitter)
23 })
24
25 key = null;
26 _checkCachedEvents = true;
27 _debug = false;
28
29 constructor(options = {}) {
30 if (options.debug) this._debug = true
31
32 console.info('Initializing react-native-branch v. ' + VERSION)
33 }
34
35 subscribe(listener) {
36
37 /*
38 * If _checkCachedEvents flag is set, get the cached value from the native layer (asynchronously).
39 * If none, the listener is not called. If there is a cached value, it is passed to the listener.
40 */
41 if (this._checkCachedEvents) {
42 this._checkCachedEvents = false
43
44 RNBranch.redeemInitSessionResult().then((result) => {
45 if (result) {
46 /*** Cached value is returned, so set it as cached. ***/
47 if('params' in result && !!result['params']) {
48 result['params']['cached_initial_event'] = true
49 }
50
51 listener(result)
52 }
53
54 /*
55 * https://github.com/BranchMetrics/react-native-branch-deep-linking/issues/79
56 *
57 * By waiting until redeemInitSessionResult() returns, we roughly simulate a
58 * synchronous call to the native layer.
59 *
60 * Note that this is equivalent to
61 *
62 * let result = await RNBranch.redeemInitSessionResult()
63 * if (result) listener(result)
64 * this._addListener(listener)
65 *
66 * But by using then(), the subscribe method does not have to be async.
67 * This way, we don't add event listeners until the listener has received the
68 * initial cached value, which essentially eliminates all possibility of
69 * getting the same event twice.
70 */
71 this._addListener(listener)
72 })
73 }
74 else {
75 this._addListener(listener)
76 }
77
78 // Initialize the native Branch SDK from JS
79 // -- Unsupportable on Android for the time being.
80 // RNBranch.initializeBranch(this.key)
81
82 const unsubscribe = () => {
83 this._removeListener(listener)
84 }
85
86 return unsubscribe
87 }
88
89 skipCachedEvents() {
90 /*** Sets to ignore cached events. ***/
91 this._checkCachedEvents = false
92 }
93
94 _addListener(listener) {
95 this.nativeEventEmitter.addListener(RNBranch.INIT_SESSION_SUCCESS, listener)
96 this.nativeEventEmitter.addListener(RNBranch.INIT_SESSION_ERROR, listener)
97 }
98
99 _removeListener(listener) {
100 this.nativeEventEmitter.removeListener(RNBranch.INIT_SESSION_SUCCESS, listener)
101 this.nativeEventEmitter.removeListener(RNBranch.INIT_SESSION_ERROR, listener)
102 }
103
104 /*** Tracking related methods ***/
105 disableTracking = (disable) => RNBranch.disableTracking(disable)
106 isTrackingDisabled = RNBranch.isTrackingDisabled
107
108 /*** RNBranch singleton methods ***/
109 setDebug = () => { throw 'setDebug() is not supported in the RN SDK. For other solutions, please see https://rnbranch.app.link/setDebug' }
110 getLatestReferringParams = RNBranch.getLatestReferringParams
111 getFirstReferringParams = RNBranch.getFirstReferringParams
112 setIdentity = (identity) => RNBranch.setIdentity(identity)
113 logout = RNBranch.logout
114 userCompletedAction = (event, state = {}) => RNBranch.userCompletedAction(event, state)
115 getShortUrl = RNBranch.getShortUrl
116 sendCommerceEvent = (revenue, metadata) => {
117 console.info('[Branch] sendCommerceEvent is deprecated. Please use the BranchEvent class instead.')
118 return RNBranch.sendCommerceEvent('' + revenue, metadata)
119 }
120 openURL = (url, options = {}) => {
121 return Platform.select({
122 android: () => RNBranch.openURL(url, options),
123 ios: () => RNBranch.openURL(url)
124 })()
125 }
126
127 /*** Referral Methods ***/
128 redeemRewards = (amount, bucket) => RNBranch.redeemRewards(amount, bucket)
129 loadRewards = (bucket) => RNBranch.loadRewards(bucket)
130 getCreditHistory = RNBranch.getCreditHistory
131
132 /*** BranchUniversalObject ***/
133 createBranchUniversalObject = createBranchUniversalObject
134}
135
136export { Branch, BranchEvent }
137export default new Branch()