@@ -26,6 +26,8 @@ import com.lambda.threading.runConcurrent
2626import com.lambda.threading.runSafe
2727import com.lambda.util.Pointer
2828import com.lambda.util.selfReference
29+ import kotlinx.coroutines.CoroutineDispatcher
30+ import kotlinx.coroutines.Dispatchers
2931import kotlin.properties.ReadOnlyProperty
3032import kotlin.properties.ReadWriteProperty
3133import kotlin.reflect.KProperty
@@ -99,11 +101,11 @@ class SafeListener<T : Event>(
99101 *
100102 * Usage:
101103 * ```kotlin
102- * listener <MyEvent> { event ->
104+ * listen <MyEvent> { event ->
103105 * player.sendMessage("Event received: $event")
104106 * }
105107 *
106- * listener <MyEvent>(priority = 1) { event ->
108+ * listen <MyEvent>(priority = 1) { event ->
107109 * player.sendMessage("Event received before the previous listener: $event")
108110 * }
109111 * ```
@@ -114,18 +116,65 @@ class SafeListener<T : Event>(
114116 * @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
115117 * @return The newly created and registered [SafeListener].
116118 */
117- inline fun <reified T : Event > Any.listener (
119+ inline fun <reified T : Event > Any.listen (
118120 priority : Int = 0,
119121 alwaysListen : Boolean = false,
120122 noinline function : SafeContext .(T ) -> Unit = {},
121123 ): SafeListener <T > {
122- val listener = SafeListener <T >(priority, this , alwaysListen) { event -> function(event) }
124+ val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
125+ function(event)
126+ }
123127
124128 EventFlow .syncListeners.subscribe(listener)
125129
126130 return listener
127131 }
128132
133+ /* *
134+ * Registers a new [SafeListener] for a generic [Event] type [T] within the context of a [Task].
135+ * The [function] is executed on the same thread where the [Event] was dispatched.
136+ * The [function] will only be executed when the context satisfies certain safety conditions.
137+ * These conditions are met when none of the following [SafeContext] properties are null:
138+ * - [SafeContext.world]
139+ * - [SafeContext.player]
140+ * - [SafeContext.interaction]
141+ * - [SafeContext.connection]
142+ *
143+ * Usage:
144+ * ```kotlin
145+ * myTask.listen<MyEvent> { event ->
146+ * player.sendMessage("Event received: $event")
147+ * }
148+ *
149+ * myTask.listen<MyEvent>(priority = 1) { event ->
150+ * player.sendMessage("Event received before the previous listener: $event")
151+ * }
152+ * ```
153+ *
154+ * @param T The type of the event to listen for.
155+ * This should be a subclass of Event.
156+ * @param priority The priority of the listener.
157+ * Listeners with higher priority will be executed first.
158+ * The Default value is 0.
159+ * @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
160+ * @param function The function to be executed when the event is posted.
161+ * This function should take a SafeContext and an event of type T as parameters.
162+ * @return The newly created and registered [SafeListener].
163+ */
164+ inline fun <reified T : Event > Task <* >.listen (
165+ priority : Int = 0,
166+ alwaysListen : Boolean = false,
167+ noinline function : SafeContext .(T ) -> Unit = {},
168+ ): SafeListener <T > {
169+ val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
170+ function(event) // ToDo: run function always on game thread
171+ }
172+
173+ syncListeners.subscribe<T >(listener)
174+
175+ return listener
176+ }
177+
129178 /* *
130179 * This function registers a new [SafeListener] for a generic [Event] type [T].
131180 * The [transform] is executed on the same thread where the [Event] was dispatched.
@@ -142,7 +191,7 @@ class SafeListener<T : Event>(
142191 *
143192 * Usage:
144193 * ```kotlin
145- * private val event by listenNext <MyEvent> { event ->
194+ * private val event by listenOnce <MyEvent> { event ->
146195 * player.sendMessage("Event received only once: $event")
147196 * // event is stored in the value
148197 * // event is unsubscribed after execution
@@ -181,51 +230,6 @@ class SafeListener<T : Event>(
181230 return pointer
182231 }
183232
184- /* *
185- * Registers a new [SafeListener] for a generic [Event] type [T] within the context of a [Task].
186- * The [function] is executed on the same thread where the [Event] was dispatched.
187- * The [function] will only be executed when the context satisfies certain safety conditions.
188- * These conditions are met when none of the following [SafeContext] properties are null:
189- * - [SafeContext.world]
190- * - [SafeContext.player]
191- * - [SafeContext.interaction]
192- * - [SafeContext.connection]
193- *
194- * Usage:
195- * ```kotlin
196- * myTask.listener<MyEvent> { event ->
197- * player.sendMessage("Event received: $event")
198- * }
199- *
200- * myTask.listener<MyEvent>(priority = 1) { event ->
201- * player.sendMessage("Event received before the previous listener: $event")
202- * }
203- * ```
204- *
205- * @param T The type of the event to listen for.
206- * This should be a subclass of Event.
207- * @param priority The priority of the listener.
208- * Listeners with higher priority will be executed first.
209- * The Default value is 0.
210- * @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
211- * @param function The function to be executed when the event is posted.
212- * This function should take a SafeContext and an event of type T as parameters.
213- * @return The newly created and registered [SafeListener].
214- */
215- inline fun <reified T : Event > Task <* >.listener (
216- priority : Int = 0,
217- alwaysListen : Boolean = false,
218- noinline function : SafeContext .(T ) -> Unit = {},
219- ): SafeListener <T > {
220- val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
221- function(event) // ToDo: run function always on game thread
222- }
223-
224- syncListeners.subscribe<T >(listener)
225-
226- return listener
227- }
228-
229233 /* *
230234 * Registers a new [SafeListener] for a generic [Event] type [T].
231235 * The [function] is executed on a new thread running asynchronously to the game thread.
@@ -236,12 +240,12 @@ class SafeListener<T : Event>(
236240 *
237241 * Usage:
238242 * ```kotlin
239- * concurrentListener <MyEvent> { event ->
243+ * listenConcurrently <MyEvent> { event ->
240244 * println("Concurrent event received: $event")
241245 * // no safe access to player or world
242246 * }
243247 *
244- * concurrentListener <MyEvent>(priority = 1) { event ->
248+ * listenConcurrently <MyEvent>(priority = 1) { event ->
245249 * println("Concurrent event received before the previous listener: $event")
246250 * }
247251 * ```
@@ -251,13 +255,14 @@ class SafeListener<T : Event>(
251255 * @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
252256 * @return The newly created and registered [SafeListener].
253257 */
254- inline fun <reified T : Event > Any.concurrentListener (
258+ inline fun <reified T : Event > Any.listenConcurrently (
255259 priority : Int = 0,
256260 alwaysListen : Boolean = false,
261+ scheduler : CoroutineDispatcher = Dispatchers .Default ,
257262 noinline function : suspend SafeContext .(T ) -> Unit = {},
258263 ): SafeListener <T > {
259264 val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
260- runConcurrent {
265+ runConcurrent(scheduler) {
261266 function(event)
262267 }
263268 }
0 commit comments