Skip to content

Commit 270230c

Browse files
committed
rename
1 parent bd0e73e commit 270230c

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

channel-event-bus/src/commonMain/kotlin/com/hoc081098/channeleventbus/ChannelEvent.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ public interface ChannelEvent<T : ChannelEvent<T>> {
1515
*/
1616
public val key: Key<T>
1717

18+
/**
19+
* The [ChannelEvent.Key] to identify a bus for this type of events.
20+
*
21+
* @param T the type of events.
22+
* @param eventClass the [KClass] of events.
23+
* @param capacity the [ChannelEventBusCapacity] of the [Channel] associated with this key.
24+
* Default is [ChannelEventBusCapacity.UNLIMITED].
25+
*
26+
* @see [ChannelEventBusCapacity]
27+
*/
1828
public open class Key<T : ChannelEvent<T>>(
1929
@JvmField
2030
internal val eventClass: KClass<T>,
@@ -24,12 +34,12 @@ public interface ChannelEvent<T : ChannelEvent<T>> {
2434
final override fun equals(other: Any?): Boolean {
2535
if (this === other) return true
2636
if (other !is Key<*>) return false
27-
return eventClass == other.eventClass
37+
return eventClass == other.eventClass && capacity == other.capacity
2838
}
2939

30-
final override fun hashCode(): Int = eventClass.hashCode()
40+
final override fun hashCode(): Int = 31 * eventClass.hashCode() + capacity.hashCode()
3141

32-
final override fun toString(): String = "ChannelEvent.Key(${eventClass.simpleName})"
42+
final override fun toString(): String = "ChannelEvent.Key(${eventClass.simpleName}, $capacity)"
3343

3444
@JvmSynthetic
3545
internal inline fun cast(it: Any): T = eventClass.cast(it)
@@ -56,5 +66,6 @@ public typealias ChannelEventKey<T> = ChannelEvent.Key<T>
5666
* bus.receiveAsFlow(awesomeEventKey).collect { e: AwesomeEvent -> println(e) }
5767
* ```
5868
*/
59-
public inline fun <reified T : ChannelEvent<T>> channelEventKeyOf(): ChannelEventKey<T> =
60-
ChannelEventKey(T::class)
69+
public inline fun <reified T : ChannelEvent<T>> channelEventKeyOf(
70+
capacity: ChannelEventBusCapacity = ChannelEventBusCapacity.UNLIMITED,
71+
): ChannelEventKey<T> = ChannelEventKey(T::class, capacity)

channel-event-bus/src/commonMain/kotlin/com/hoc081098/channeleventbus/ChannelEventBus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import kotlinx.coroutines.internal.synchronized as coroutinesSynchronized
3030
* - [ChannelEvent.Key] will be used to identify a bus for a specific type of events.
3131
* Each bus has a [Channel] to send events to and a [Flow] to receive events from.
3232
*
33-
* - The [Channel] is unbounded [Channel.UNLIMITED].
33+
* - The [Channel] is unbounded [Channel.UNLIMITED] (default) or conflated [Channel.CONFLATED].
3434
* The [Flow] is cold and only one collector is allowed at a time.
3535
* This make sure all events are consumed.
3636
*

channel-event-bus/src/commonMain/kotlin/com/hoc081098/channeleventbus/ChannelEventBusCapacity.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hoc081098.channeleventbus
22

33
import kotlin.jvm.JvmSynthetic
4+
import kotlinx.coroutines.channels.BufferOverflow
45
import kotlinx.coroutines.channels.Channel
56

67
@JvmSynthetic
@@ -11,14 +12,31 @@ internal inline fun ChannelEventBusCapacity.asInt(): Int {
1112
}
1213
}
1314

15+
/**
16+
* The capacity of the [Channel] associated with a [ChannelEvent.Key].
17+
*
18+
* @see [ChannelEvent.Key.capacity]
19+
*/
1420
public enum class ChannelEventBusCapacity {
1521
/**
1622
* The [Channel] is unbounded [Channel.UNLIMITED].
23+
*
24+
* The [Channel] associated with a [ChannelEvent.Key] will has an unlimited capacity buffer.
25+
* This means that the [Channel] will never suspend the sender and will never drop elements.
26+
*
27+
* @see [Channel.UNLIMITED]
1728
*/
1829
UNLIMITED,
1930

2031
/**
2132
* The [Channel] is bounded [Channel.CONFLATED].
33+
*
34+
* The [Channel] associated with a [ChannelEvent.Key] will has a conflated capacity buffer.
35+
* The size of buffer is 1 and will keep only the most recently sent element (drop the oldest element).
36+
* This means that the [Channel] will never suspend the sender, but will drop the oldest element when buffer is full.
37+
*
38+
* @see [Channel.CONFLATED]
39+
* @see [BufferOverflow.DROP_OLDEST]
2240
*/
2341
CONFLATED,
2442
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package com.hoc081098.channeleventbus.sample.android.ui.register
22

33
import com.hoc081098.channeleventbus.ChannelEvent
4+
import com.hoc081098.channeleventbus.ChannelEventBusCapacity.CONFLATED
45
import com.hoc081098.channeleventbus.ChannelEventKey
56

67
internal data class SubmitFirstNameEvent(val value: String?) : ChannelEvent<SubmitFirstNameEvent> {
78
override val key get() = Key
89

9-
companion object Key : ChannelEventKey<SubmitFirstNameEvent>(SubmitFirstNameEvent::class)
10+
companion object Key : ChannelEventKey<SubmitFirstNameEvent>(SubmitFirstNameEvent::class, CONFLATED)
1011
}
1112

1213
internal data class SubmitLastNameEvent(val value: String?) : ChannelEvent<SubmitLastNameEvent> {
1314
override val key get() = Key
1415

15-
companion object Key : ChannelEventKey<SubmitLastNameEvent>(SubmitLastNameEvent::class)
16+
companion object Key : ChannelEventKey<SubmitLastNameEvent>(SubmitLastNameEvent::class, CONFLATED)
1617
}
1718

1819
internal data class SubmitGenderEvent(val value: Gender?) : ChannelEvent<SubmitGenderEvent> {
1920
override val key get() = Key
2021

21-
companion object Key : ChannelEventKey<SubmitGenderEvent>(SubmitGenderEvent::class)
22+
companion object Key : ChannelEventKey<SubmitGenderEvent>(SubmitGenderEvent::class, CONFLATED)
2223
}

0 commit comments

Comments
 (0)