-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventScope.kt
50 lines (45 loc) · 1.15 KB
/
EventScope.kt
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
package br.com.devsrsouza.eventkt
import br.com.devsrsouza.eventkt.scopes.CombinedEventScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlin.reflect.KClass
interface EventScope : CoroutineScope {
/**
* Publish a new event [any].
*/
fun publish(any: Any)
/**
* Receives events as Flow from the given [type].
*/
fun <T : Any> listen(
type: KClass<T>
): Flow<T>
}
/**
* Receives events as Flow from the given [type].
*/
inline fun <reified T : Any> EventScope.listen(): Flow<T> = listen(T::class)
/**
* Combining EventScope on one, flowing the plus order:
* `first + second`
*
* Will publish as:
* ```
* first.publish()
* second.publish()
* ```
*/
operator fun EventScope.plus(eventScope: EventScope): EventScope {
return when {
this is CombinedEventScope -> clone().apply {
addScope(eventScope)
}
eventScope is CombinedEventScope -> eventScope.clone().apply {
addScopeToTop(this@plus)
}
else -> CombinedEventScope().apply {
addScope(this)
addScope(eventScope)
}
}
}