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