-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchSubscriber.js
135 lines (117 loc) · 3.89 KB
/
BranchSubscriber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { NativeModules, NativeEventEmitter, DeviceEventEmitter, Platform } from 'react-native'
const { RNBranch, RNBranchEventEmitter } = NativeModules
/**
* Utility class to encapsulate the logic of subscription to native events.
*/
export default class BranchSubscriber {
/**
* Native event emitter (private)
*/
_nativeEventEmitter = Platform.select({
android: DeviceEventEmitter,
ios: new NativeEventEmitter(RNBranchEventEmitter),
})
/**
* Tracks whether cached events should be checked on the next
* call to subscribe(). Initialized by options.checkCachedEvents
* if present, true otherwise.
*/
_checkCachedEvents = true
/**
* Keep track of whether subscription is active.
*/
_subscribed = false
/**
* The options Object passed to the constructor
* @type {Object}
*/
options = {}
/**
* Creates a new BranchSubscriber object with options.
*
* @param {!Object} options an Object containing options
*/
constructor(options) {
this.options = options || {}
if ('checkCachedEvents' in this.options) {
this._checkCachedEvents = this.options.checkCachedEvents
}
this.initSessionStart = null
this.initSessionSuccess = null
this.initSessionError = null
}
subscribe() {
if (this._subscribed) return
this._subscribed = true
if (this._checkCachedEvents) {
/*
* Only check for events from the native layer on the first call to
* subscribe().
*/
this._checkCachedEvents = false
RNBranch.redeemInitSessionResult().then((result) => {
if (result) {
/*** Cached value is returned, so set it as cached. ***/
if('params' in result && !!result['params']) {
result['params']['+rn_cached_initial_event'] = true
}
/*
* TODO: There can easily be a race here on cold launch from a link.
* An INIT_SESSION_START event can be emitted by the native layer
* before the subscribe() method is called in JS. Then subscribe() is
* called before a cached response is available, so here we get
* result == null. Then the live INIT_SESSION_SUCCESS event is
* transmitted to JS, and onOpenComplete is called with a non-null
* URI, but onOpenStart was never called. This can be addressed
* by caching the pending URI in the native layers.
*/
if (this.options.onOpenStart && 'uri' in result) {
this.options.onOpenStart({uri: result.uri, cachedInitialEvent: true})
}
if (this.options.onOpenComplete) {
// result includes uri and +rn_cached_initial_event.
this.options.onOpenComplete(result)
}
}
this._subscribe()
})
}
else {
this._subscribe()
}
}
/**
* Activates subscription to native events. Private. Use
* subscribe() and set checkCachedEvents true or false in
* constructor.
*/
_subscribe() {
if (this.options.onOpenStart) {
this.initSessionStart = this._nativeEventEmitter.addListener(RNBranch.INIT_SESSION_START, this.options.onOpenStart)
}
if (this.options.onOpenComplete) {
this.initSessionSuccess = this._nativeEventEmitter.addListener(RNBranch.INIT_SESSION_SUCCESS, this.options.onOpenComplete)
this.initSessionError = this._nativeEventEmitter.addListener(RNBranch.INIT_SESSION_ERROR, this.options.onOpenComplete)
}
}
/**
* Deactivates subscription to native events
*/
unsubscribe() {
if (!this._subscribed) return
this._subscribed = false
if (this.options.onOpenStart) {
if (this.initSessionStart) {
this.initSessionStart.remove()
};
}
if (this.options.onOpenComplete) {
if (this.initSessionSuccess) {
this.initSessionSuccess.remove();
}
if (this.initSessionError) {
this.initSessionError.remove()
}
}
}
}