1 | import { NativeModules, NativeEventEmitter, DeviceEventEmitter, Platform } from 'react-native'
|
2 |
|
3 | const { RNBranch, RNBranchEventEmitter } = NativeModules
|
4 |
|
5 | import createBranchUniversalObject from './branchUniversalObject'
|
6 | import BranchEvent from './BranchEvent'
|
7 |
|
8 | const packageFile = require('./../package.json')
|
9 | export const VERSION = packageFile.version
|
10 |
|
11 | export const AddToCartEvent = RNBranch.ADD_TO_CART_EVENT
|
12 | export const AddToWishlistEvent = RNBranch.ADD_TO_WISHLIST_EVENT
|
13 | export const PurchasedEvent = RNBranch.PURCHASED_EVENT
|
14 | export const PurchaseInitiatedEvent = RNBranch.PURCHASE_INITIATED_EVENT
|
15 | export const RegisterViewEvent = RNBranch.REGISTER_VIEW_EVENT
|
16 | export const ShareCompletedEvent = RNBranch.SHARE_COMPLETED_EVENT
|
17 | export const ShareInitiatedEvent = RNBranch.SHARE_INITIATED_EVENT
|
18 |
|
19 | class 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 |
|
39 |
|
40 |
|
41 | if (this._checkCachedEvents) {
|
42 | this._checkCachedEvents = false
|
43 |
|
44 | RNBranch.redeemInitSessionResult().then((result) => {
|
45 | if (result) {
|
46 |
|
47 | if('params' in result && !!result['params']) {
|
48 | result['params']['cached_initial_event'] = true
|
49 | }
|
50 |
|
51 | listener(result)
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | this._addListener(listener)
|
72 | })
|
73 | }
|
74 | else {
|
75 | this._addListener(listener)
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | const unsubscribe = () => {
|
83 | this._removeListener(listener)
|
84 | }
|
85 |
|
86 | return unsubscribe
|
87 | }
|
88 |
|
89 | skipCachedEvents() {
|
90 |
|
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 |
|
105 | disableTracking = (disable) => RNBranch.disableTracking(disable)
|
106 | isTrackingDisabled = RNBranch.isTrackingDisabled
|
107 |
|
108 |
|
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 |
|
128 | redeemRewards = (amount, bucket) => RNBranch.redeemRewards(amount, bucket)
|
129 | loadRewards = (bucket) => RNBranch.loadRewards(bucket)
|
130 | getCreditHistory = RNBranch.getCreditHistory
|
131 |
|
132 |
|
133 | createBranchUniversalObject = createBranchUniversalObject
|
134 | }
|
135 |
|
136 | export { Branch, BranchEvent }
|
137 | export default new Branch()
|