Skip to content

Commit 60bc4ea

Browse files
committed
implement waiting
1 parent d53893a commit 60bc4ea

File tree

1 file changed

+60
-0
lines changed
  • core/src/main/java/com/segment/analytics/kotlin/core

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.segment.analytics.kotlin.core
2+
3+
import com.segment.analytics.kotlin.core.platform.Plugin
4+
import kotlinx.coroutines.delay
5+
import kotlinx.coroutines.launch
6+
7+
/**
8+
* An interface that provides functionality of pausing and resuming event processing on Analytics.
9+
*
10+
* By default plugins that implement this interface pauses processing when it is added to
11+
* analytics (via `setup()`) and resumes after 30s.
12+
*
13+
* To customize pausing and resuming, override `setup()` and call `pause()/resumes()` as needed
14+
*/
15+
interface WaitingPlugin: Plugin {
16+
override fun setup(analytics: Analytics) {
17+
super.setup(analytics)
18+
pause()
19+
}
20+
21+
fun pause() {
22+
analytics.pauseEventProcessing(this)
23+
}
24+
25+
fun resume() {
26+
analytics.resumeEventProcessing(this)
27+
}
28+
}
29+
30+
fun Analytics.pauseEventProcessing(plugin: WaitingPlugin) = analyticsScope.launch {
31+
store.dispatch(System.AddWaitingPlugin(plugin.hashCode()), System::class)
32+
}
33+
34+
35+
fun Analytics.resumeEventProcessing(plugin: WaitingPlugin) = analyticsScope.launch {
36+
store.dispatch(System.RemoveWaitingPlugin(plugin.hashCode()), System::class)
37+
}
38+
39+
internal suspend fun Analytics.running(): Boolean {
40+
val system = store.currentState(System::class)
41+
return system?.running ?: false
42+
}
43+
44+
internal suspend fun Analytics.pauseEventProcessing() {
45+
if (!running()) return
46+
47+
store.dispatch(System.ToggleRunningAction(false), System::class)
48+
startProcessingAfterTimeout()
49+
}
50+
51+
internal suspend fun Analytics.resumeEventProcessing() {
52+
if (running()) return
53+
store.dispatch(System.ToggleRunningAction(true), System::class)
54+
}
55+
56+
internal fun Analytics.startProcessingAfterTimeout() = analyticsScope.launch {
57+
delay(30_000)
58+
store.dispatch(System.ForceRunningAction(), System::class)
59+
}
60+

0 commit comments

Comments
 (0)