11package com.lambda.event
22
33import com.lambda.event.callback.ICancellable
4- import com.lambda.event.callback.Returnable
54import com.lambda.event.listener.Listener
65import com.lambda.threading.runConcurrent
76import kotlinx.coroutines.CoroutineScope
@@ -13,6 +12,13 @@ import kotlinx.coroutines.flow.MutableSharedFlow
1312import kotlinx.coroutines.flow.filter
1413import kotlinx.coroutines.flow.filterNot
1514
15+
16+ /* *
17+ * [EventFlow] is an object that manages the flow ([MutableSharedFlow]) of [Event]s in the application.
18+ * It provides methods to post [Event]s to both synchronous and asynchronous [Listener]s.
19+ *
20+ * The [EventFlow] also provides methods to unsubscribe from event flows for a specific event type.
21+ */
1622object EventFlow {
1723 /* *
1824 * [lambdaScope] is a [CoroutineScope] which is used to launch coroutines.
@@ -56,52 +62,46 @@ object EventFlow {
5662 * An instant callback ([CallbackEvent]) can only be achieved by synchronous listening objects
5763 * as the concurrent listener will be executed "later".
5864 *
59- * @param event The [Event] to be posted to the event flow.
65+ * CAUTION: The returned [Event] may have not yet been processed by concurrent listeners.
66+ *
67+ * @param E The type of the event to be posted. This should be a subclass of Event.
68+ * @receiver The [Event] to be posted to the event flow.
6069 */
61- @JvmStatic fun post (event : Event ) {
62- concurrentFlow.tryEmit(event)
63- event.executeListenerSynchronous()
70+ @JvmStatic fun <E : Event > E.post (): E {
71+ concurrentFlow.tryEmit(this )
72+ executeListenerSynchronous()
73+ return this @post
6474 }
6575
6676 /* *
67- * Posts a [cancellable] [ Event] to the event flow and returns the [Event] .
77+ * Posts an [ Event] to the event flow and then applies the given [process] function to the event .
6878 *
69- * This function is a variant of the [post] function specifically for [ICancellable] [Event]s.
70- * It posts the [Event] to the event flow,
71- * runs it through the synchronous set of listeners ([syncListeners]), and then returns the [Event].
72- * This is useful as [ICancellable] [Event]s are often checked after being processed by the [Listener]s.
79+ * This function first posts the event to the event flow by calling the [post] function.
80+ * After the event has been posted, it applies the [process] function to the event.
7381 *
74- * The returned event is guaranteed to be of the same type as the input,
75- * thanks to the type parameter [E] which is a subtype of both [Event] and [ICancellable].
82+ * CAUTION: The processed [Event] may have not yet been processed by concurrent listeners.
7683 *
77- * @param cancellable The cancellable [Event] to be posted to the event flow .
78- * @return The same [ICancellable] [Event] after being processed by the [Listener]s .
84+ * @param E The type of the event to be posted. This should be a subclass of Event .
85+ * @param process A function to be applied to the event after it has been posted .
7986 */
80- @JvmStatic fun <E > post (cancellable : E ) : E where E : Event , E : ICancellable {
81- post(cancellable as Event )
82- return cancellable
83- }
84-
85- @JvmStatic fun <E > post (cancellable : E , process : E .() -> Unit ) where E : Event, E : ICancellable {
86- post(cancellable)
87- cancellable.process()
88- }
89-
90- @JvmStatic fun <E > postChecked (cancellable : E , process : E .() -> Unit ) where E : Event, E : ICancellable {
91- post(cancellable)
92- if (! cancellable.isCanceled()) cancellable.process()
87+ @JvmStatic fun <E : Event > E.post (process : E .() -> Unit ) {
88+ post()
89+ process()
9390 }
9491
95- @JvmStatic fun <E , R > postR (returnable : E ): E where E : Returnable <R >, E : Event {
96- post(returnable as Event )
97- return returnable
98- }
99-
100- @JvmStatic fun <R , E > post (
101- returnable : E , process : E .() -> Unit
102- ) where E : Returnable<R>, E : Event {
103- post(returnable as Event )
104- returnable.process()
92+ /* *
93+ * Posts an [Event] to the event flow and then applies the given [process] function to the event
94+ * if it is not canceled.
95+ * If not it applies the [process] function to the event.
96+ *
97+ * CAUTION: The processed [Event] may have not yet been processed by concurrent listeners.
98+ *
99+ * @param E The type of the event to be posted. This should be a subclass of [Event] and implement [ICancellable].
100+ * @param process A function to be applied to the event after it has been posted if the [Event] is not canceled.
101+ */
102+ @JvmStatic fun <E > E.postChecked (process : E .() -> Unit ) where E : Event, E : ICancellable {
103+ post()
104+ if (! isCanceled()) process()
105105 }
106106
107107 /* *
0 commit comments