@aochuang/client-sdk 1.0.400 → 1.0.402
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/components/wechat-chat-control/location/location.vue +11 -3
- package/package.json +1 -1
- package/services/client-chat/client-chat-wechat/client-chat-account.js +37 -7
- package/services/client-chat/client-chat-wechat/client-chat-message.js +175 -18
- package/services/client-chat/client-chat-wechat/client-chat-session.js +190 -12
- package/services/client-chat/client-chat-wechat/client-chat-socket.js +80 -22
- package/services/client-chat/client-chat-wechat/client-chat.js +87 -11
- package/services/socket/socket.js +56 -7
- package/utils/log/constants.js +13 -0
- package/utils/log/export.js +88 -0
- package/utils/log/findings.md +141 -0
- package/utils/log/index.js +331 -0
- package/utils/log/indexed-db.js +228 -0
- package/utils/log/progress.md +267 -0
- package/utils/log/serialize.js +146 -0
- package/utils/log/task_plan.md +149 -0
- package/utils/log//345/210/206/346/236/220/344/270/216/344/274/230/345/214/226/346/226/271/346/241/210.md +303 -0
- package/utils/log//346/212/200/346/234/257/346/226/271/346/241/210/346/226/207/346/241/243.md +201 -0
- package/utils/log//346/236/266/346/236/204/346/226/207/346/241/243.md +169 -0
- package/utils/log//351/234/200/346/261/202/346/226/207/346/241/243.md +124 -0
- package/utils/log.js +2 -626
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { formatDate } from '../date'
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_INDEXED_DB_MAX_SIZE,
|
|
4
|
+
DEFAULT_FLUSH_INTERVAL,
|
|
5
|
+
DEFAULT_FLUSH_THRESHOLD,
|
|
6
|
+
DEFAULT_FLUSH_BATCH_SIZE,
|
|
7
|
+
DEFAULT_MAX_PENDING_COUNT,
|
|
8
|
+
DEFAULT_MAX_PENDING_BYTES
|
|
9
|
+
} from './constants'
|
|
10
|
+
import { safeStringify, getByteSize } from './serialize'
|
|
11
|
+
import IndexedDbLogStorage from './indexed-db'
|
|
12
|
+
import { createExportFiles, recordPayload } from './export'
|
|
13
|
+
|
|
14
|
+
let instanceSequence = 0
|
|
15
|
+
|
|
16
|
+
function createPageInstanceId () {
|
|
17
|
+
instanceSequence++
|
|
18
|
+
return `${Date.now()}_${instanceSequence}_${Math.random().toString(36).slice(2, 10)}`
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getDay (date) {
|
|
22
|
+
return formatDate(date, 'YYYY-mm-dd') || 'unknown-date'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default class Log {
|
|
26
|
+
static checkAndSync (storageKeys) {
|
|
27
|
+
return Promise.resolve([].concat(storageKeys || []).filter(Boolean).map(storageKey => ({
|
|
28
|
+
storageKey,
|
|
29
|
+
synced: false,
|
|
30
|
+
migratedCount: 0,
|
|
31
|
+
duplicateCount: 0,
|
|
32
|
+
removedCount: 0
|
|
33
|
+
})))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
constructor (options) {
|
|
37
|
+
options = Object.assign({
|
|
38
|
+
name: null,
|
|
39
|
+
console: false,
|
|
40
|
+
indexedDbMaxSize: DEFAULT_INDEXED_DB_MAX_SIZE,
|
|
41
|
+
flushInterval: DEFAULT_FLUSH_INTERVAL,
|
|
42
|
+
flushThreshold: DEFAULT_FLUSH_THRESHOLD,
|
|
43
|
+
flushBatchSize: DEFAULT_FLUSH_BATCH_SIZE,
|
|
44
|
+
maxPendingCount: DEFAULT_MAX_PENDING_COUNT,
|
|
45
|
+
maxPendingBytes: DEFAULT_MAX_PENDING_BYTES
|
|
46
|
+
}, options)
|
|
47
|
+
if (!options.name) {
|
|
48
|
+
throw new Error('必须指定日志的name名称')
|
|
49
|
+
}
|
|
50
|
+
this.prefix = 'log'
|
|
51
|
+
this.name = options.name
|
|
52
|
+
this.options = options
|
|
53
|
+
this.maxSize = options.maxSize
|
|
54
|
+
this.validTime = options.validTime
|
|
55
|
+
this.beforeAddCallback = null
|
|
56
|
+
this.tempStore = []
|
|
57
|
+
this._pendingBytes = 0
|
|
58
|
+
this._droppedCount = 0
|
|
59
|
+
this._internalFailureCount = 0
|
|
60
|
+
this._sequence = 0
|
|
61
|
+
this._pageInstanceId = createPageInstanceId()
|
|
62
|
+
this._customStorageKey = null
|
|
63
|
+
this._publishTimer = null
|
|
64
|
+
this._retryCount = 0
|
|
65
|
+
this._writePromise = Promise.resolve()
|
|
66
|
+
this._storage = new IndexedDbLogStorage({
|
|
67
|
+
name: this.indexedDbName,
|
|
68
|
+
storageKey: this.storageKey,
|
|
69
|
+
maxBytes: options.indexedDbMaxSize
|
|
70
|
+
})
|
|
71
|
+
this._bindLifecycleFlush()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get storageKey () {
|
|
75
|
+
return this._customStorageKey || `${this.prefix}_${this.name}`
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get indexedDbName () {
|
|
79
|
+
return this.storageKey
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get useIndexedDb () {
|
|
83
|
+
return this._storage ? this._storage.supported : (
|
|
84
|
+
typeof window !== 'undefined' && !!window.indexedDB
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onBeforeAdd (callback) {
|
|
89
|
+
this.beforeAddCallback = callback
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_recordInternalFailure () {
|
|
93
|
+
this._internalFailureCount++
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_bindLifecycleFlush () {
|
|
97
|
+
if (typeof document !== 'undefined' && document.addEventListener) {
|
|
98
|
+
document.addEventListener('visibilitychange', () => {
|
|
99
|
+
if (document.visibilityState === 'hidden') {
|
|
100
|
+
this._backgroundFlush()
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
105
|
+
window.addEventListener('pagehide', () => this._backgroundFlush())
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
_enqueueWrite (callback) {
|
|
110
|
+
const task = this._writePromise.then(callback, callback)
|
|
111
|
+
this._writePromise = task.catch(() => {})
|
|
112
|
+
return task
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_clearPublishTimer () {
|
|
116
|
+
if (this._publishTimer) {
|
|
117
|
+
clearTimeout(this._publishTimer)
|
|
118
|
+
this._publishTimer = null
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_schedulePublish (delay = this.options.flushInterval) {
|
|
123
|
+
if (this._publishTimer) {
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
this._publishTimer = setTimeout(() => {
|
|
127
|
+
this._publishTimer = null
|
|
128
|
+
this._backgroundFlush()
|
|
129
|
+
}, delay)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
_backgroundFlush () {
|
|
133
|
+
this.publishLogData().catch(() => {})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_createRecord (data) {
|
|
137
|
+
let payload = safeStringify(data)
|
|
138
|
+
if (!payload) {
|
|
139
|
+
return null
|
|
140
|
+
}
|
|
141
|
+
data.size = getByteSize(payload)
|
|
142
|
+
payload = safeStringify(data)
|
|
143
|
+
if (!payload) {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
const byteSize = getByteSize(payload)
|
|
147
|
+
return {
|
|
148
|
+
recordId: `${this._pageInstanceId}:${this._sequence}`,
|
|
149
|
+
order: 0,
|
|
150
|
+
byteSize,
|
|
151
|
+
day: getDay(new Date(data.timestamp)),
|
|
152
|
+
group: data.group,
|
|
153
|
+
payload
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
_enqueueRecord (record) {
|
|
158
|
+
this.tempStore.push(record)
|
|
159
|
+
this._pendingBytes += record.byteSize
|
|
160
|
+
while (
|
|
161
|
+
this.tempStore.length > this.options.maxPendingCount ||
|
|
162
|
+
this._pendingBytes > this.options.maxPendingBytes
|
|
163
|
+
) {
|
|
164
|
+
const removed = this.tempStore.shift()
|
|
165
|
+
this._pendingBytes = Math.max(0, this._pendingBytes - (removed.byteSize || 0))
|
|
166
|
+
this._droppedCount++
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
addLog (input) {
|
|
171
|
+
try {
|
|
172
|
+
this._sequence++
|
|
173
|
+
const now = new Date()
|
|
174
|
+
const data = Object.assign({
|
|
175
|
+
group: this.defautGroup,
|
|
176
|
+
refererId: undefined,
|
|
177
|
+
content: null
|
|
178
|
+
}, input, {
|
|
179
|
+
id: `${this._pageInstanceId}:${this._sequence}`,
|
|
180
|
+
createTime: formatDate(now),
|
|
181
|
+
timestamp: now.getTime()
|
|
182
|
+
})
|
|
183
|
+
if (this.beforeAddCallback && this.beforeAddCallback({ data }) === false) {
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
const record = this._createRecord(data)
|
|
187
|
+
if (!record) {
|
|
188
|
+
this._recordInternalFailure()
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
if (this.useIndexedDb) {
|
|
192
|
+
this._enqueueRecord(record)
|
|
193
|
+
if (this.tempStore.length >= this.options.flushThreshold) {
|
|
194
|
+
this._clearPublishTimer()
|
|
195
|
+
this._backgroundFlush()
|
|
196
|
+
} else {
|
|
197
|
+
this._schedulePublish()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (this.options.console && typeof console !== 'undefined' && console.log) {
|
|
201
|
+
console.log(`【${this.name || 'operateLog'}】${record.payload}`)
|
|
202
|
+
}
|
|
203
|
+
return data
|
|
204
|
+
} catch (error) {
|
|
205
|
+
this._recordInternalFailure()
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
checkAndSync () {
|
|
211
|
+
return Promise.resolve({
|
|
212
|
+
synced: false,
|
|
213
|
+
migratedCount: 0,
|
|
214
|
+
duplicateCount: 0,
|
|
215
|
+
removedCount: 0
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
publishLogData () {
|
|
220
|
+
this._clearPublishTimer()
|
|
221
|
+
if (!this.useIndexedDb || !this.tempStore.length) {
|
|
222
|
+
if (!this.useIndexedDb) {
|
|
223
|
+
this.tempStore = []
|
|
224
|
+
this._pendingBytes = 0
|
|
225
|
+
}
|
|
226
|
+
return Promise.resolve({ writtenCount: 0, removedCount: 0 })
|
|
227
|
+
}
|
|
228
|
+
const records = this.tempStore.splice(0, this.tempStore.length)
|
|
229
|
+
const recordsBytes = records.reduce((sum, record) => sum + (record.byteSize || 0), 0)
|
|
230
|
+
this._pendingBytes = Math.max(0, this._pendingBytes - recordsBytes)
|
|
231
|
+
let committedCount = 0
|
|
232
|
+
return this._enqueueWrite(() => {
|
|
233
|
+
let offset = 0
|
|
234
|
+
let result = { writtenCount: 0, removedCount: 0 }
|
|
235
|
+
const writeNext = () => {
|
|
236
|
+
if (offset >= records.length) {
|
|
237
|
+
return result
|
|
238
|
+
}
|
|
239
|
+
const batch = records.slice(offset, offset + this.options.flushBatchSize)
|
|
240
|
+
offset += batch.length
|
|
241
|
+
return this._storage.write(batch).then(batchResult => {
|
|
242
|
+
committedCount += batch.length
|
|
243
|
+
result.writtenCount += batchResult.writtenCount
|
|
244
|
+
result.removedCount += batchResult.removedCount
|
|
245
|
+
return writeNext()
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
return writeNext()
|
|
249
|
+
}).then(result => {
|
|
250
|
+
this._retryCount = 0
|
|
251
|
+
if (this._droppedCount) {
|
|
252
|
+
const droppedCount = this._droppedCount
|
|
253
|
+
this._droppedCount = 0
|
|
254
|
+
this.addLog({
|
|
255
|
+
group: 'diagnostic:logger_dropped',
|
|
256
|
+
content: {
|
|
257
|
+
event: 'logger_dropped',
|
|
258
|
+
droppedCount
|
|
259
|
+
}
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
return result
|
|
263
|
+
}).catch(error => {
|
|
264
|
+
this._retryCount++
|
|
265
|
+
records.slice(committedCount).reverse().forEach(record => {
|
|
266
|
+
this.tempStore.unshift(record)
|
|
267
|
+
this._pendingBytes += record.byteSize || 0
|
|
268
|
+
})
|
|
269
|
+
while (
|
|
270
|
+
this.tempStore.length > this.options.maxPendingCount ||
|
|
271
|
+
this._pendingBytes > this.options.maxPendingBytes
|
|
272
|
+
) {
|
|
273
|
+
const removed = this.tempStore.shift()
|
|
274
|
+
this._pendingBytes = Math.max(0, this._pendingBytes - (removed.byteSize || 0))
|
|
275
|
+
this._droppedCount++
|
|
276
|
+
}
|
|
277
|
+
this._schedulePublish(Math.min(30000, 500 * Math.pow(2, Math.min(this._retryCount, 6))))
|
|
278
|
+
throw error
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
loadLogData () {
|
|
283
|
+
if (!this.useIndexedDb) {
|
|
284
|
+
return Promise.resolve({ size: 0, data: [] })
|
|
285
|
+
}
|
|
286
|
+
return this._writePromise.then(() => this._storage.readRecords('prev')).then(records => {
|
|
287
|
+
let size = 0
|
|
288
|
+
const data = records.map(record => {
|
|
289
|
+
size += record.byteSize || 0
|
|
290
|
+
if (record.log !== undefined) {
|
|
291
|
+
return record.log
|
|
292
|
+
}
|
|
293
|
+
try {
|
|
294
|
+
return JSON.parse(recordPayload(record))
|
|
295
|
+
} catch (error) {
|
|
296
|
+
return null
|
|
297
|
+
}
|
|
298
|
+
}).filter(item => item !== null)
|
|
299
|
+
return { size, data }
|
|
300
|
+
}).catch(() => ({ size: 0, data: [] }))
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
exportLogData () {
|
|
304
|
+
return this.publishLogData().then(() => this.loadLogData())
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
exportLogFiles (options) {
|
|
308
|
+
if (!this.useIndexedDb) {
|
|
309
|
+
return Promise.resolve([])
|
|
310
|
+
}
|
|
311
|
+
return this.publishLogData()
|
|
312
|
+
.then(() => this._storage.readRecords('next'))
|
|
313
|
+
.then(records => createExportFiles(records, options))
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
clearLogData () {
|
|
317
|
+
this._clearPublishTimer()
|
|
318
|
+
this.tempStore = []
|
|
319
|
+
this._pendingBytes = 0
|
|
320
|
+
if (!this.useIndexedDb) {
|
|
321
|
+
return Promise.resolve()
|
|
322
|
+
}
|
|
323
|
+
return this._enqueueWrite(() => this._storage.clear())
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
_closeIndexedDb () {
|
|
327
|
+
return this._storage.close()
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export { safeStringify, createExportFiles }
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {
|
|
2
|
+
INDEXED_DB_VERSION,
|
|
3
|
+
INDEXED_DB_STORE_NAME,
|
|
4
|
+
INDEXED_DB_META_STORE_NAME,
|
|
5
|
+
INDEXED_DB_ORDER_INDEX_NAME,
|
|
6
|
+
INDEXED_DB_STATS_KEY
|
|
7
|
+
} from './constants'
|
|
8
|
+
import { safeStringify, getByteSize } from './serialize'
|
|
9
|
+
|
|
10
|
+
function supported () {
|
|
11
|
+
return typeof window !== 'undefined' && !!window.indexedDB
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createStores (db) {
|
|
15
|
+
const store = db.createObjectStore(INDEXED_DB_STORE_NAME, { keyPath: 'recordId' })
|
|
16
|
+
store.createIndex(INDEXED_DB_ORDER_INDEX_NAME, 'order', { unique: false })
|
|
17
|
+
if (!db.objectStoreNames.contains(INDEXED_DB_META_STORE_NAME)) {
|
|
18
|
+
db.createObjectStore(INDEXED_DB_META_STORE_NAME, { keyPath: 'key' })
|
|
19
|
+
}
|
|
20
|
+
return store
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseLegacyStore (value) {
|
|
24
|
+
try {
|
|
25
|
+
if (value && value.value !== undefined) {
|
|
26
|
+
value = value.value
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === 'string') {
|
|
29
|
+
value = JSON.parse(value)
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
return value
|
|
33
|
+
}
|
|
34
|
+
return value && Array.isArray(value.data) ? value.data : []
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return []
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function legacyRecord (log, index) {
|
|
41
|
+
const payload = safeStringify(log) || '{}'
|
|
42
|
+
return {
|
|
43
|
+
recordId: `legacy:${index}:${Date.now()}`,
|
|
44
|
+
order: index + 1,
|
|
45
|
+
byteSize: getByteSize(payload),
|
|
46
|
+
day: log && typeof log.createTime === 'string' ? log.createTime.slice(0, 10) : 'unknown-date',
|
|
47
|
+
payload,
|
|
48
|
+
log
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default class IndexedDbLogStorage {
|
|
53
|
+
constructor (options) {
|
|
54
|
+
this.name = options.name
|
|
55
|
+
this.storageKey = options.storageKey
|
|
56
|
+
this.maxBytes = options.maxBytes
|
|
57
|
+
this._dbPromise = null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get supported () {
|
|
61
|
+
return supported()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
_upgrade (request) {
|
|
65
|
+
const db = request.result
|
|
66
|
+
const transaction = request.transaction
|
|
67
|
+
if (!db.objectStoreNames.contains(INDEXED_DB_STORE_NAME)) {
|
|
68
|
+
createStores(db)
|
|
69
|
+
transaction.objectStore(INDEXED_DB_META_STORE_NAME).put({
|
|
70
|
+
key: INDEXED_DB_STATS_KEY,
|
|
71
|
+
totalBytes: 0,
|
|
72
|
+
nextOrder: 0
|
|
73
|
+
})
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// v1 used one key/value entry. Convert it inside the v2 upgrade transaction.
|
|
78
|
+
if (!db.objectStoreNames.contains(INDEXED_DB_META_STORE_NAME)) {
|
|
79
|
+
const legacyStore = transaction.objectStore(INDEXED_DB_STORE_NAME)
|
|
80
|
+
const requestLegacy = legacyStore.get(this.storageKey)
|
|
81
|
+
requestLegacy.onsuccess = () => {
|
|
82
|
+
const logs = parseLegacyStore(requestLegacy.result)
|
|
83
|
+
db.deleteObjectStore(INDEXED_DB_STORE_NAME)
|
|
84
|
+
const store = createStores(db)
|
|
85
|
+
let totalBytes = 0
|
|
86
|
+
let retained = 0
|
|
87
|
+
logs.slice().reverse().some((log, index) => {
|
|
88
|
+
const record = legacyRecord(log, index)
|
|
89
|
+
if (totalBytes + record.byteSize > this.maxBytes) {
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
totalBytes += record.byteSize
|
|
93
|
+
retained++
|
|
94
|
+
store.put(record)
|
|
95
|
+
return false
|
|
96
|
+
})
|
|
97
|
+
transaction.objectStore(INDEXED_DB_META_STORE_NAME).put({
|
|
98
|
+
key: INDEXED_DB_STATS_KEY,
|
|
99
|
+
totalBytes,
|
|
100
|
+
nextOrder: retained
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
open () {
|
|
107
|
+
if (!this.supported) {
|
|
108
|
+
return Promise.reject(new Error('indexedDB不可用'))
|
|
109
|
+
}
|
|
110
|
+
if (this._dbPromise) {
|
|
111
|
+
return this._dbPromise
|
|
112
|
+
}
|
|
113
|
+
this._dbPromise = new Promise((resolve, reject) => {
|
|
114
|
+
const request = window.indexedDB.open(this.name, INDEXED_DB_VERSION)
|
|
115
|
+
request.onupgradeneeded = () => this._upgrade(request)
|
|
116
|
+
request.onerror = () => {
|
|
117
|
+
this._dbPromise = null
|
|
118
|
+
reject(request.error || new Error('打开日志数据库失败'))
|
|
119
|
+
}
|
|
120
|
+
request.onsuccess = () => {
|
|
121
|
+
const db = request.result
|
|
122
|
+
db.onversionchange = () => {
|
|
123
|
+
db.close()
|
|
124
|
+
this._dbPromise = null
|
|
125
|
+
}
|
|
126
|
+
resolve(db)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
return this._dbPromise
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
close () {
|
|
133
|
+
const promise = this._dbPromise
|
|
134
|
+
this._dbPromise = null
|
|
135
|
+
if (!promise) {
|
|
136
|
+
return Promise.resolve()
|
|
137
|
+
}
|
|
138
|
+
return promise.then(db => db.close(), () => {})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
write (records) {
|
|
142
|
+
if (!records || !records.length) {
|
|
143
|
+
return Promise.resolve({ writtenCount: 0, removedCount: 0 })
|
|
144
|
+
}
|
|
145
|
+
return this.open().then(db => new Promise((resolve, reject) => {
|
|
146
|
+
const transaction = db.transaction(
|
|
147
|
+
[INDEXED_DB_STORE_NAME, INDEXED_DB_META_STORE_NAME],
|
|
148
|
+
'readwrite'
|
|
149
|
+
)
|
|
150
|
+
const store = transaction.objectStore(INDEXED_DB_STORE_NAME)
|
|
151
|
+
const metaStore = transaction.objectStore(INDEXED_DB_META_STORE_NAME)
|
|
152
|
+
const metaRequest = metaStore.get(INDEXED_DB_STATS_KEY)
|
|
153
|
+
let removedCount = 0
|
|
154
|
+
transaction.oncomplete = () => resolve({ writtenCount: records.length, removedCount })
|
|
155
|
+
transaction.onerror = () => reject(transaction.error || new Error('写入日志失败'))
|
|
156
|
+
transaction.onabort = () => reject(transaction.error || new Error('写入日志失败'))
|
|
157
|
+
metaRequest.onerror = () => transaction.abort()
|
|
158
|
+
metaRequest.onsuccess = () => {
|
|
159
|
+
const stats = metaRequest.result || {
|
|
160
|
+
key: INDEXED_DB_STATS_KEY,
|
|
161
|
+
totalBytes: 0,
|
|
162
|
+
nextOrder: 0
|
|
163
|
+
}
|
|
164
|
+
records.forEach(record => {
|
|
165
|
+
stats.nextOrder++
|
|
166
|
+
record.order = stats.nextOrder
|
|
167
|
+
stats.totalBytes += record.byteSize || 0
|
|
168
|
+
store.put(record)
|
|
169
|
+
})
|
|
170
|
+
if (stats.totalBytes <= this.maxBytes) {
|
|
171
|
+
metaStore.put(stats)
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
const cursorRequest = store.index(INDEXED_DB_ORDER_INDEX_NAME).openCursor(null, 'next')
|
|
175
|
+
cursorRequest.onerror = () => transaction.abort()
|
|
176
|
+
cursorRequest.onsuccess = event => {
|
|
177
|
+
const cursor = event.target.result
|
|
178
|
+
if (stats.totalBytes <= this.maxBytes || !cursor) {
|
|
179
|
+
metaStore.put(stats)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
stats.totalBytes = Math.max(0, stats.totalBytes - (cursor.value.byteSize || 0))
|
|
183
|
+
cursor.delete()
|
|
184
|
+
removedCount++
|
|
185
|
+
cursor.continue()
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
readRecords (direction = 'prev') {
|
|
192
|
+
return this.open().then(db => new Promise((resolve, reject) => {
|
|
193
|
+
const transaction = db.transaction([INDEXED_DB_STORE_NAME], 'readonly')
|
|
194
|
+
const store = transaction.objectStore(INDEXED_DB_STORE_NAME)
|
|
195
|
+
const request = store.index(INDEXED_DB_ORDER_INDEX_NAME).openCursor(null, direction)
|
|
196
|
+
const records = []
|
|
197
|
+
request.onerror = () => reject(request.error || new Error('读取日志失败'))
|
|
198
|
+
request.onsuccess = event => {
|
|
199
|
+
const cursor = event.target.result
|
|
200
|
+
if (!cursor) {
|
|
201
|
+
resolve(records)
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
records.push(cursor.value)
|
|
205
|
+
cursor.continue()
|
|
206
|
+
}
|
|
207
|
+
}))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
clear () {
|
|
211
|
+
return this.open().then(db => new Promise((resolve, reject) => {
|
|
212
|
+
const transaction = db.transaction(
|
|
213
|
+
[INDEXED_DB_STORE_NAME, INDEXED_DB_META_STORE_NAME],
|
|
214
|
+
'readwrite'
|
|
215
|
+
)
|
|
216
|
+
transaction.objectStore(INDEXED_DB_STORE_NAME).clear()
|
|
217
|
+
transaction.objectStore(INDEXED_DB_META_STORE_NAME).put({
|
|
218
|
+
key: INDEXED_DB_STATS_KEY,
|
|
219
|
+
totalBytes: 0,
|
|
220
|
+
nextOrder: 0
|
|
221
|
+
})
|
|
222
|
+
transaction.oncomplete = resolve
|
|
223
|
+
transaction.onerror = () => reject(transaction.error || new Error('清理日志失败'))
|
|
224
|
+
transaction.onabort = () => reject(transaction.error || new Error('清理日志失败'))
|
|
225
|
+
}))
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|