From 8a518cec533534a8bece1adaabc5eac3da941c87 Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Tue, 7 Nov 2023 19:22:44 +0100 Subject: [PATCH] feat: check the buffer every second To reduce overhead of constant loop, the `InMemoryBuffer` will check `buffer` list every second. --- .../main/java/com/parsely/parselyandroid/InMemoryBuffer.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/parsely/src/main/java/com/parsely/parselyandroid/InMemoryBuffer.kt b/parsely/src/main/java/com/parsely/parselyandroid/InMemoryBuffer.kt index 610586af..aabefd64 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/InMemoryBuffer.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/InMemoryBuffer.kt @@ -1,6 +1,9 @@ package com.parsely.parselyandroid +import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -15,13 +18,14 @@ internal class InMemoryBuffer( init { coroutineScope.launch { - while (true) { + while (isActive) { mutex.withLock { if (buffer.isNotEmpty()) { localStorageRepository.insertEvents(buffer) buffer.clear() } } + delay(1.seconds) } } }