-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExecutionStateDispatcher.kt
More file actions
53 lines (43 loc) · 1.21 KB
/
ExecutionStateDispatcher.kt
File metadata and controls
53 lines (43 loc) · 1.21 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
package com.freeraspreactnative.dispatchers
import com.freeraspreactnative.events.RaspExecutionStateEvent
import com.freeraspreactnative.interfaces.PluginExecutionStateListener
internal class ExecutionStateDispatcher(private val listener: PluginExecutionStateListener) {
private val cache = mutableSetOf<RaspExecutionStateEvent>()
private var isAppInForeground = false
private var isListenerRegistered = false
fun registerListener() {
isListenerRegistered = true
isAppInForeground = true
flushCache()
}
fun unregisterListener() {
isListenerRegistered = false
isAppInForeground = false
}
fun onResume() {
isAppInForeground = true
if (isListenerRegistered) {
flushCache()
}
}
fun onPause() {
isAppInForeground = false
}
fun dispatch(event: RaspExecutionStateEvent) {
if (isAppInForeground && isListenerRegistered) {
listener.raspExecutionStateChanged(event)
} else {
synchronized(cache) {
cache.add(event)
}
}
}
private fun flushCache() {
val events = synchronized(cache) {
val snapshot = cache.toSet()
cache.clear()
snapshot
}
events.forEach { listener.raspExecutionStateChanged(it) }
}
}