-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (124 loc) · 2.81 KB
/
index.js
File metadata and controls
142 lines (124 loc) · 2.81 KB
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
136
137
138
139
140
141
142
'use strict';
import ReactNative from 'react-native';
const { DataCortex } = ReactNative.NativeModules;
export default {
init,
addUserTag,
event,
economyEvent,
log,
logEvent,
getDeviceTag,
};
let is_initialized = false;
let user_tag = false;
const event_list = [];
const economy_list = [];
const log_list = [];
export function init(api_key, org, done) {
if (!done) {
done = function () {};
}
DataCortex.sharedInstance(api_key, org, (err) => {
is_initialized = true;
if (user_tag !== false) {
addUserTag(user_tag);
}
event_list.forEach(event);
economy_list.forEach(economyEvent);
log_list.forEach(logEvent);
event_list.splice();
economy_list.splice();
log_list.splice();
done(err);
});
}
export function addUserTag(userTag) {
if (userTag && typeof userTag != 'string') {
userTag = userTag.toString();
}
DataCortex.addUserTag(userTag);
}
export function event(props) {
if (!props || typeof props !== 'object') {
throw new Error('props must be an object');
}
if (is_initialized) {
DataCortex.eventWithProperties(props);
} else {
event_list.push(props);
}
}
export function economyEvent(props) {
if (!props || typeof props != 'object') {
throw new Error('props must be an object');
}
if (!props.spendCurrency) {
throw new Error('spendCurrency is required');
}
if (typeof props.spendAmount != 'number') {
throw new Error('spendAmount is required');
}
if (is_initialized) {
if (props.spendType && !props.spend_type) {
props.spend_type = props.spendType;
}
DataCortex.economyWithProperties(
props,
props.spendCurrency,
props.spendAmount
);
} else {
economy_list.push(props);
}
}
export function log() {
if (!arguments || arguments.length == 0) {
throw new Error('log must have arguments');
}
let log_line = '';
for (let i = 0; i < arguments.length; i++) {
const arg = arguments[i];
if (i > 0) {
log_line += ' ';
}
if (_isError(arg)) {
log_line += String(arg) + ' ' + arg.stack;
} else if (typeof arg == 'object') {
try {
log_line += JSON.stringify(arg);
} catch (e) {
log_line += String(arg);
}
} else {
log_line += String(arg);
}
}
logEvent({ log_line });
}
export function logEvent(props) {
if (!props || typeof props != 'object') {
throw new Error('props must be an object.');
}
if (is_initialized) {
DataCortex.appLogWithProperties(props);
} else {
log_list.push(props);
}
}
function _isError(e) {
return (
e &&
e.stack &&
e.message &&
typeof e.stack === 'string' &&
typeof e.message === 'string'
);
}
export function getDeviceTag(done) {
if (is_initialized) {
DataCortex.getDeviceTag(done);
} else {
done('not_initialized');
}
}