summaryrefslogtreecommitdiff
path: root/odcsim/odcsim-api/src/main/kotlin
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2020-03-18 00:50:05 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-03-25 10:51:05 +0100
commitbc64182612ad06f15bff5b48637ed7d241e293b2 (patch)
tree7b2a5fb78cc1c5f0f2a8a3f850a4652f0a433044 /odcsim/odcsim-api/src/main/kotlin
parentb1cf9b2bd9559328c3c9d26e73123e67d2bfea05 (diff)
[ci skip] refactor: Refactor monitors into EventFlow
Diffstat (limited to 'odcsim/odcsim-api/src/main/kotlin')
-rw-r--r--odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/EventFlow.kt99
-rw-r--r--odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/StateFlow.kt (renamed from odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt)18
2 files changed, 108 insertions, 9 deletions
diff --git a/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/EventFlow.kt b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/EventFlow.kt
new file mode 100644
index 00000000..5d9af9ec
--- /dev/null
+++ b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/EventFlow.kt
@@ -0,0 +1,99 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.atlarge.odcsim.flow
+
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.FlowPreview
+import kotlinx.coroutines.InternalCoroutinesApi
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.channels.SendChannel
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.FlowCollector
+import kotlinx.coroutines.flow.consumeAsFlow
+import java.util.WeakHashMap
+
+/**
+ * A [Flow] that can be used to emit events.
+ */
+public interface EventFlow<T> : Flow<T> {
+ /**
+ * Emit the specified [event].
+ */
+ public fun emit(event: T)
+
+ /**
+ * Close the flow.
+ */
+ public fun close()
+}
+
+/**
+ * Creates a new [EventFlow].
+ */
+@Suppress("FunctionName")
+public fun <T> EventFlow(): EventFlow<T> = EventFlowImpl()
+
+/**
+ * Internal implementation of the [EventFlow] class.
+ */
+@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
+private class EventFlowImpl<T> : EventFlow<T> {
+ private var closed: Boolean = false
+ private val subscribers = WeakHashMap<SendChannel<T>, Unit>()
+
+ override fun emit(event: T) {
+ synchronized(this) {
+ for ((chan, _) in subscribers) {
+ chan.offer(event)
+ }
+ }
+ }
+
+ override fun close() {
+ synchronized(this) {
+ closed = true
+
+ for ((chan, _) in subscribers) {
+ chan.close()
+ }
+ }
+ }
+
+ @InternalCoroutinesApi
+ override suspend fun collect(collector: FlowCollector<T>) {
+ val channel: Channel<T>
+ synchronized(this) {
+ if (closed) {
+ return
+ }
+
+ channel = Channel(Channel.UNLIMITED)
+ subscribers[channel] = Unit
+ }
+ channel.consumeAsFlow().collect(collector)
+ }
+
+ override fun toString(): String = "EventFlow"
+}
diff --git a/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/StateFlow.kt
index da6298a3..429d932b 100644
--- a/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/signal/Signal.kt
+++ b/odcsim/odcsim-api/src/main/kotlin/com/atlarge/odcsim/flow/StateFlow.kt
@@ -22,7 +22,7 @@
* SOFTWARE.
*/
-package com.atlarge.odcsim.signal
+package com.atlarge.odcsim.flow
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
@@ -40,9 +40,9 @@ import kotlinx.coroutines.flow.asFlow
* in the future, but is not available yet.
* See: https://github.com/Kotlin/kotlinx.coroutines/pull/1354
*/
-public interface Signal<T> : Flow<T> {
+public interface StateFlow<T> : Flow<T> {
/**
- * The current value of this signal.
+ * The current value of this flow.
*
* Setting a value that is [equal][Any.equals] to the previous one does nothing.
*/
@@ -50,23 +50,23 @@ public interface Signal<T> : Flow<T> {
}
/**
- * Creates a [Signal] with a given initial [value].
+ * Creates a [StateFlow] with a given initial [value].
*/
@Suppress("FunctionName")
-public fun <T> Signal(value: T): Signal<T> = SignalImpl(value)
+public fun <T> StateFlow(value: T): StateFlow<T> = StateFlowImpl(value)
/**
- * Internal implementation of the [Signal] interface.
+ * Internal implementation of the [StateFlow] interface.
*/
-private class SignalImpl<T>(initialValue: T) : Signal<T> {
+private class StateFlowImpl<T>(initialValue: T) : StateFlow<T> {
/**
- * The [BroadcastChannel] to back this signal.
+ * The [BroadcastChannel] to back this flow.
*/
@OptIn(ExperimentalCoroutinesApi::class)
private val chan = BroadcastChannel<T>(Channel.CONFLATED)
/**
- * The internal [Flow] backing this signal.
+ * The internal [Flow] backing this flow.
*/
@OptIn(FlowPreview::class)
private val flow = chan.asFlow()