@datarangers/sdk-javascript 0.0.6 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/es/index.min.js +1 -0
- package/core/lib/index.min.js +1 -0
- package/es/index-base.min.js +6430 -1
- package/es/index.min.js +9936 -1
- package/es/plugin/ab.js +928 -1
- package/es/plugin/autotrack.js +1897 -1
- package/es/plugin/duration.js +117 -1
- package/es/plugin/route.js +363 -1
- package/es/plugin/stay.js +547 -1
- package/es/plugin/store.js +151 -1
- package/lib/index-base.min.js +6435 -1
- package/lib/index.min.js +9941 -1
- package/lib/plugin/ab.js +930 -1
- package/lib/plugin/autotrack.js +1899 -1
- package/lib/plugin/duration.js +119 -1
- package/lib/plugin/route.js +365 -1
- package/lib/plugin/stay.js +549 -1
- package/lib/plugin/store.js +153 -1
- package/package.json +2 -2
package/lib/plugin/store.js
CHANGED
|
@@ -1 +1,153 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Copyright 2022 Beijing Volcanoengine Technology Ltd. All Rights Reserved.
|
|
4
|
+
var Store =
|
|
5
|
+
/** @class */
|
|
6
|
+
function () {
|
|
7
|
+
function Store() {
|
|
8
|
+
this.retryWaitTime = 3000; // 3秒后重试
|
|
9
|
+
|
|
10
|
+
this.retryStatus = false;
|
|
11
|
+
this.retryCacheStatus = false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Store.prototype.apply = function (collect, config) {
|
|
15
|
+
var _this = this;
|
|
16
|
+
|
|
17
|
+
if (!config.enable_storage || config.disable_storage) return;
|
|
18
|
+
this.collect = collect;
|
|
19
|
+
this.config = config;
|
|
20
|
+
if (this.collect.destroyInstance) return;
|
|
21
|
+
var Types = collect.Types;
|
|
22
|
+
var _a = collect.adapters,
|
|
23
|
+
storage = _a.storage,
|
|
24
|
+
fetch = _a.fetch;
|
|
25
|
+
this.storage = new storage(false);
|
|
26
|
+
this.fetch = fetch;
|
|
27
|
+
this.eventUrl = this.collect.configManager.getUrl('event');
|
|
28
|
+
this.eventKey = "__tea_cache_events_" + config.app_id;
|
|
29
|
+
this.storageNum = config.storage_num || 50; // 默认最大存储50条数据
|
|
30
|
+
|
|
31
|
+
this.retryNum = config.retry_num || 3; // 默认最多重试3次
|
|
32
|
+
|
|
33
|
+
this.retryInterval = 1000; // 默认每隔1000ms重试一次
|
|
34
|
+
|
|
35
|
+
collect.on(Types.SubmitError, function (errorInfo) {
|
|
36
|
+
if (errorInfo.type !== 'f_data') return; // this.retryRightNow(errorInfo);
|
|
37
|
+
|
|
38
|
+
_this.storeData(errorInfo);
|
|
39
|
+
});
|
|
40
|
+
collect.on(Types.Ready, function () {
|
|
41
|
+
_this.checkStorage();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
Store.prototype.retryRightNow = function (errorInfo) {
|
|
46
|
+
var _this = this;
|
|
47
|
+
|
|
48
|
+
if (this.retryStatus) {
|
|
49
|
+
// 再重试过程中又有数据异常,则先暂存
|
|
50
|
+
this.errorCache.push(errorInfo);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var currentNum = 0;
|
|
55
|
+
this.retryStatus = true;
|
|
56
|
+
var currentInterval = setInterval(function () {
|
|
57
|
+
if (currentNum === 3) {
|
|
58
|
+
// 达到重试次数后不再重试,存储起来
|
|
59
|
+
_this.storeData(_this.errorCache);
|
|
60
|
+
|
|
61
|
+
_this.retryStatus = false;
|
|
62
|
+
clearInterval(currentInterval);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var eventData = errorInfo.eventData;
|
|
67
|
+
|
|
68
|
+
_this.fetchData(eventData, function () {
|
|
69
|
+
_this.retryStatus = false;
|
|
70
|
+
clearInterval(currentInterval);
|
|
71
|
+
|
|
72
|
+
if (_this.retryCacheStatus) {
|
|
73
|
+
_this.errorCache.splice(0, 1);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (_this.errorCache.length) {
|
|
77
|
+
_this.retryCacheStatus = true;
|
|
78
|
+
|
|
79
|
+
_this.retryRightNow(_this.errorCache[0]);
|
|
80
|
+
}
|
|
81
|
+
}, function () {
|
|
82
|
+
currentNum++;
|
|
83
|
+
});
|
|
84
|
+
}, this.retryInterval);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
Store.prototype.storeData = function (errorInfo) {
|
|
88
|
+
var data = this.storage.getItem(this.eventKey);
|
|
89
|
+
var eventData = errorInfo.eventData; // 数据错误不进行存储
|
|
90
|
+
|
|
91
|
+
if (Object.keys(data).length === this.storageNum) return; // 数据满了,就不再添加数据
|
|
92
|
+
|
|
93
|
+
data[Date.now()] = eventData;
|
|
94
|
+
this.storage.setItem(this.eventKey, data);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
Store.prototype.checkStorage = function () {
|
|
98
|
+
var _this = this;
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
if (!window.navigator.onLine) return; // 设备未联网
|
|
102
|
+
|
|
103
|
+
var data_1 = this.storage.getItem(this.eventKey);
|
|
104
|
+
if (!data_1 || !Object.keys(data_1).length) return;
|
|
105
|
+
var loadData = {
|
|
106
|
+
events: [{
|
|
107
|
+
event: 'ontest',
|
|
108
|
+
params: {
|
|
109
|
+
app_id: this.config.app_id
|
|
110
|
+
},
|
|
111
|
+
local_time_ms: Date.now()
|
|
112
|
+
}],
|
|
113
|
+
user: {
|
|
114
|
+
user_unique_id: this.collect.configManager.get('web_id')
|
|
115
|
+
},
|
|
116
|
+
header: {}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
var success = function success() {
|
|
120
|
+
var copyData = JSON.parse(JSON.stringify(data_1));
|
|
121
|
+
|
|
122
|
+
var _loop_1 = function _loop_1(key) {
|
|
123
|
+
_this.fetchData(data_1[key], function () {
|
|
124
|
+
delete copyData[key];
|
|
125
|
+
|
|
126
|
+
_this.storage.setItem(_this.eventKey, copyData);
|
|
127
|
+
}, function () {}, false);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
for (var key in data_1) {
|
|
131
|
+
_loop_1(key);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
this.fetchData([loadData], success, function () {}, true);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
console.warn('error check storage');
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
Store.prototype.fetchData = function (data, success, fail, test) {
|
|
142
|
+
this.fetch(this.eventUrl, data, 30000, false, function () {
|
|
143
|
+
success && success();
|
|
144
|
+
}, function () {
|
|
145
|
+
fail && fail();
|
|
146
|
+
console.log('network error,compensate report failk');
|
|
147
|
+
}, test && !this.config.channel_domain ? '566f58151b0ed37e' : '');
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
return Store;
|
|
151
|
+
}();
|
|
152
|
+
|
|
153
|
+
module.exports = Store;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datarangers/sdk-javascript",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"nclean": "rimraf es lib core",
|
|
6
6
|
"build": "export NODE_ENV=production && npm run nclean && npx rollup -c rollup.config.js",
|
|
@@ -69,4 +69,4 @@
|
|
|
69
69
|
"typescript": "^3.2.2",
|
|
70
70
|
"typescript-eslint-parser": "^22.0.0"
|
|
71
71
|
}
|
|
72
|
-
}
|
|
72
|
+
}
|