-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathPersistable.kt
More file actions
59 lines (54 loc) · 1.95 KB
/
Persistable.kt
File metadata and controls
59 lines (54 loc) · 1.95 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
package io.sentry.android.replay.util
import android.annotation.SuppressLint
import android.annotation.TargetApi
import io.sentry.ReplayRecording
import io.sentry.SentryOptions
import io.sentry.android.replay.ReplayCache
import io.sentry.rrweb.RRWebEvent
import java.io.BufferedWriter
import java.io.StringWriter
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.ScheduledExecutorService
// TODO: enable this back after we are able to serialize individual touches to disk to not overload
// cpu
@SuppressLint("UseRequiresApi")
@Suppress("unused")
@TargetApi(26)
internal class PersistableLinkedList(
private val propertyName: String,
private val options: SentryOptions,
private val persistingExecutor: ScheduledExecutorService,
private val cacheProvider: () -> ReplayCache?,
) : ConcurrentLinkedDeque<RRWebEvent>() {
// only overriding methods that we use, to observe the collection
override fun addAll(elements: Collection<RRWebEvent>): Boolean {
val result = super.addAll(elements)
persistRecording()
return result
}
override fun add(element: RRWebEvent): Boolean {
val result = super.add(element)
persistRecording()
return result
}
override fun remove(): RRWebEvent {
val result = super.remove()
persistRecording()
return result
}
private fun persistRecording() {
val cache = cacheProvider() ?: return
val recording = ReplayRecording().apply { payload = ArrayList(this@PersistableLinkedList) }
if (options.threadChecker.isMainThread) {
persistingExecutor.submit {
val stringWriter = StringWriter()
options.serializer.serialize(recording, BufferedWriter(stringWriter))
cache.persistSegmentValues(propertyName, stringWriter.toString())
}
} else {
val stringWriter = StringWriter()
options.serializer.serialize(recording, BufferedWriter(stringWriter))
cache.persistSegmentValues(propertyName, stringWriter.toString())
}
}
}