-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from Parsely/coroutines-queue-manager
- Loading branch information
Showing
10 changed files
with
241 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
parsely/src/main/java/com/parsely/parselyandroid/InMemoryBuffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 | ||
|
||
internal class InMemoryBuffer( | ||
private val coroutineScope: CoroutineScope, | ||
private val localStorageRepository: LocalStorageRepository, | ||
private val onEventAddedListener: () -> Unit, | ||
) { | ||
|
||
private val mutex = Mutex() | ||
private val buffer = mutableListOf<Map<String, Any?>>() | ||
|
||
init { | ||
coroutineScope.launch { | ||
while (isActive) { | ||
mutex.withLock { | ||
if (buffer.isNotEmpty()) { | ||
ParselyTracker.PLog("Persisting ${buffer.size} events") | ||
localStorageRepository.insertEvents(buffer) | ||
buffer.clear() | ||
} | ||
} | ||
delay(1.seconds) | ||
} | ||
} | ||
} | ||
|
||
fun add(event: Map<String, Any>) { | ||
coroutineScope.launch { | ||
mutex.withLock { | ||
ParselyTracker.PLog("Event added to buffer") | ||
buffer.add(event) | ||
onEventAddedListener() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
parsely/src/main/java/com/parsely/parselyandroid/ParselyCoroutineScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
|
||
val sdkScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) | ||
internal val sdkScope = | ||
CoroutineScope(SupervisorJob() + Dispatchers.IO + CoroutineName("Parse.ly SDK Scope")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.