-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathbranchUniversalObject.js
110 lines (93 loc) · 3.7 KB
/
branchUniversalObject.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
import { NativeModules, Platform } from 'react-native'
import BranchEvent from './BranchEvent'
const { RNBranch } = NativeModules
export default async function createBranchUniversalObject(identifier, options = {}) {
if (typeof identifier !== 'string') throw new Error('react-native-branch: identifier must be a string')
const contentMetadata = options.contentMetadata || {}
if (contentMetadata.customMetadata) {
for (const key in contentMetadata.customMetadata) {
const valueType = typeof contentMetadata.customMetadata[key]
if (valueType == 'string') continue
console.warn('[Branch] customMetadata values must be strings. Value for property ' + key + ' has type ' + valueType + '.')
// TODO: throw?
}
}
const branchUniversalObject = {
canonicalIdentifier: identifier,
contentMetadata: contentMetadata,
...options
}
// For the benefit of NSDecimalNumber on iOS.
const price = contentMetadata.price === undefined ? undefined : '' + contentMetadata.price
branchUniversalObject.contentMetadata.price = price
if (options.automaticallyListOnSpotlight !== undefined) {
console.info('[Branch] automaticallyListOnSpotlight is deprecated. Please use locallyIndex instead.')
}
if (options.price !== undefined) {
console.info('[Branch] price is deprecated. Please use contentMetadata.price instead.')
}
if (options.currency !== undefined) {
console.info('[Branch] currency is deprecated. Please use contentMetadata.price instead.')
}
if (options.metadata !== undefined) {
console.info('[Branch] metadata is deprecated. Please use contentMetadata.customMetadata instead.')
}
if (options.contentIndexingMode !== undefined) {
console.info('[Branch] contentIndexingMode is deprecated. Please use locallyIndex or publiclyIndex instead.')
}
const { ident } = await RNBranch.createUniversalObject(branchUniversalObject)
return {
ident: ident,
showShareSheet(shareOptions = {}, linkProperties = {}, controlParams = {}) {
shareOptions = {
title: options.title || '',
text: options.contentDescription || '',
...shareOptions,
}
linkProperties = {
feature: 'share',
channel: 'RNApp',
...linkProperties,
}
return this._tryFunction(RNBranch.showShareSheet, shareOptions, linkProperties, controlParams)
},
// deprecated in favor of BranchEvent
registerView() {
console.info('[Branch] registerView is deprecated. Please use logEvent(BranchEvent.ViewItem) instead.')
return this._tryFunction(RNBranch.registerView)
},
generateShortUrl(linkProperties = {}, controlParams = {}) {
return this._tryFunction(RNBranch.generateShortUrl, linkProperties, controlParams)
},
listOnSpotlight() {
console.info('[Branch] listOnSpotlight is deprecated. Please use locallyIndex instead.')
if (Platform.OS !== 'ios') return Promise.resolve()
return this._tryFunction(RNBranch.listOnSpotlight)
},
logEvent(eventName, params = {}) {
return new BranchEvent(eventName, this, params).logEvent()
},
release() {
return RNBranch.releaseUniversalObject(this.ident)
},
/**
* Used by exception handlers when RNBranch::Error::BUONotFound is caught.
*/
_newIdent() {
return RNBranch.createUniversalObject(branchUniversalObject).then(({ident}) => {
this.ident = ident
return ident
})
},
_tryFunction(func, ...args) {
return func(this.ident, ...args).catch((error) => {
if (error.code != 'RNBranch::Error::BUONotFound') {
throw error
}
return this._newIdent().then((ident) => {
return func(ident, ...args)
})
})
}
}
}