visang-tracker 0.9.9 → 0.9.10
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/dist/index.js +59 -22
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,12 +5,26 @@ const LOG_STORAGE_KEY = 'aidt_trace_log_queue_v1';
|
|
|
5
5
|
let logQueue = [];
|
|
6
6
|
let flushTimerId = null;
|
|
7
7
|
let isFlushing = false;
|
|
8
|
+
const hasWindow = typeof window !== 'undefined';
|
|
9
|
+
const hasDocument = typeof document !== 'undefined';
|
|
10
|
+
const canUseStorage = () => {
|
|
11
|
+
if (!hasWindow)
|
|
12
|
+
return false;
|
|
13
|
+
try {
|
|
14
|
+
return !!window.localStorage;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
8
20
|
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
if (canUseStorage()) {
|
|
22
|
+
const saved = window.localStorage.getItem(LOG_STORAGE_KEY);
|
|
23
|
+
if (saved) {
|
|
24
|
+
const parsed = JSON.parse(saved);
|
|
25
|
+
if (Array.isArray(parsed)) {
|
|
26
|
+
logQueue = parsed;
|
|
27
|
+
}
|
|
14
28
|
}
|
|
15
29
|
}
|
|
16
30
|
}
|
|
@@ -18,8 +32,10 @@ catch (e) {
|
|
|
18
32
|
console.log(e);
|
|
19
33
|
}
|
|
20
34
|
const persistQueue = () => {
|
|
35
|
+
if (!canUseStorage())
|
|
36
|
+
return;
|
|
21
37
|
try {
|
|
22
|
-
localStorage.setItem(LOG_STORAGE_KEY, JSON.stringify(logQueue));
|
|
38
|
+
window.localStorage.setItem(LOG_STORAGE_KEY, JSON.stringify(logQueue));
|
|
23
39
|
}
|
|
24
40
|
catch {
|
|
25
41
|
/* storage may be full or unavailable */
|
|
@@ -40,44 +56,65 @@ const flushLogQueue = async () => {
|
|
|
40
56
|
return;
|
|
41
57
|
isFlushing = true;
|
|
42
58
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
while (logQueue.length > 0) {
|
|
60
|
+
const batch = logQueue.slice(0, LOG_BATCH_MAX);
|
|
61
|
+
try {
|
|
62
|
+
await transportBatch(batch);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
console.error(err);
|
|
66
|
+
// Keep remaining queue for a future retry
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
logQueue.splice(0, batch.length);
|
|
70
|
+
persistQueue();
|
|
71
|
+
}
|
|
48
72
|
}
|
|
49
73
|
finally {
|
|
50
74
|
isFlushing = false;
|
|
51
75
|
}
|
|
52
76
|
};
|
|
53
77
|
const scheduleFlush = () => {
|
|
78
|
+
if (!hasWindow || !hasDocument)
|
|
79
|
+
return;
|
|
54
80
|
if (flushTimerId != null)
|
|
55
81
|
return;
|
|
56
82
|
flushTimerId = window.setInterval(() => {
|
|
57
83
|
if (document.visibilityState === 'visible') {
|
|
58
|
-
flushLogQueue()
|
|
84
|
+
flushLogQueue().catch(err => {
|
|
85
|
+
console.error(err);
|
|
86
|
+
});
|
|
59
87
|
}
|
|
60
88
|
}, LOG_FLUSH_INTERVAL_MS);
|
|
61
89
|
};
|
|
62
90
|
const enqueueTraceLog = (item) => {
|
|
63
91
|
logQueue.push(item);
|
|
64
92
|
persistQueue();
|
|
65
|
-
// if (logQueue.length >= LOG_BATCH_MAX) {
|
|
66
|
-
// flushLogQueue();
|
|
67
|
-
// } else {
|
|
68
93
|
scheduleFlush();
|
|
69
|
-
// }
|
|
70
94
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
95
|
+
if (hasWindow) {
|
|
96
|
+
window.addEventListener('visibilitychange', () => {
|
|
97
|
+
flushLogQueue().catch(err => {
|
|
98
|
+
console.error(err);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
74
102
|
const sendTraceLogDebug = (errorLog) => {
|
|
75
103
|
enqueueTraceLog(errorLog);
|
|
76
104
|
};
|
|
105
|
+
const resetLogQueue = () => {
|
|
106
|
+
logQueue = [];
|
|
107
|
+
if (!canUseStorage())
|
|
108
|
+
return;
|
|
109
|
+
try {
|
|
110
|
+
window.localStorage.removeItem(LOG_STORAGE_KEY);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
/* storage may be full or unavailable */
|
|
114
|
+
}
|
|
115
|
+
};
|
|
77
116
|
const initLog = (opts) => {
|
|
78
|
-
|
|
79
|
-
console.log('test', opts.batchMax);
|
|
80
|
-
console.log('test', opts.flushIntervalMs);
|
|
117
|
+
resetLogQueue();
|
|
81
118
|
setBaseUrl('log', opts.logBaseUrl);
|
|
82
119
|
if (typeof opts?.batchMax === 'number' && opts.batchMax > 0) {
|
|
83
120
|
LOG_BATCH_MAX = opts.batchMax;
|