Skip to content

Commit 9b7786f

Browse files
committed
Cleanup
1 parent f7ea93b commit 9b7786f

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

common/src/main/kotlin/com/lambda/event/listener/SafeListener.kt

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,60 @@ class SafeListener<T : Event>(
121121
alwaysListen: Boolean = false,
122122
noinline function: SafeContext.(T) -> Unit = {},
123123
): SafeListener<T> {
124-
val listener = SafeListener<T>(priority, this, alwaysListen) { event -> function(event) }
124+
val listener = SafeListener<T>(priority, this, alwaysListen) { event ->
125+
function(event)
126+
}
125127

126128
EventFlow.syncListeners.subscribe(listener)
127129

128130
return listener
129131
}
130132

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+
131178
/**
132179
* This function registers a new [SafeListener] for a generic [Event] type [T].
133180
* The [transform] is executed on the same thread where the [Event] was dispatched.
@@ -183,51 +230,6 @@ class SafeListener<T : Event>(
183230
return pointer
184231
}
185232

186-
/**
187-
* Registers a new [SafeListener] for a generic [Event] type [T] within the context of a [Task].
188-
* The [function] is executed on the same thread where the [Event] was dispatched.
189-
* The [function] will only be executed when the context satisfies certain safety conditions.
190-
* These conditions are met when none of the following [SafeContext] properties are null:
191-
* - [SafeContext.world]
192-
* - [SafeContext.player]
193-
* - [SafeContext.interaction]
194-
* - [SafeContext.connection]
195-
*
196-
* Usage:
197-
* ```kotlin
198-
* myTask.listen<MyEvent> { event ->
199-
* player.sendMessage("Event received: $event")
200-
* }
201-
*
202-
* myTask.listen<MyEvent>(priority = 1) { event ->
203-
* player.sendMessage("Event received before the previous listener: $event")
204-
* }
205-
* ```
206-
*
207-
* @param T The type of the event to listen for.
208-
* This should be a subclass of Event.
209-
* @param priority The priority of the listener.
210-
* Listeners with higher priority will be executed first.
211-
* The Default value is 0.
212-
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
213-
* @param function The function to be executed when the event is posted.
214-
* This function should take a SafeContext and an event of type T as parameters.
215-
* @return The newly created and registered [SafeListener].
216-
*/
217-
inline fun <reified T : Event> Task<*>.listen(
218-
priority: Int = 0,
219-
alwaysListen: Boolean = false,
220-
noinline function: SafeContext.(T) -> Unit = {},
221-
): SafeListener<T> {
222-
val listener = SafeListener<T>(priority, this, alwaysListen) { event ->
223-
function(event) // ToDo: run function always on game thread
224-
}
225-
226-
syncListeners.subscribe<T>(listener)
227-
228-
return listener
229-
}
230-
231233
/**
232234
* Registers a new [SafeListener] for a generic [Event] type [T].
233235
* The [function] is executed on a new thread running asynchronously to the game thread.

common/src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class UnsafeListener<T : Event>(
123123
*
124124
* Usage:
125125
* ```kotlin
126-
* private val event by listenUnsafeOnce<MyEvent> { event ->
126+
* private val event by listenOnceUnsafe<MyEvent> { event ->
127127
* println("Unsafe event received only once: $event")
128128
* // no safe access to player or world
129129
* // event is stored in the value
@@ -139,7 +139,7 @@ class UnsafeListener<T : Event>(
139139
* @param transform The function used to transform the event into a value.
140140
* @return The newly created and registered [UnsafeListener].
141141
*/
142-
inline fun <reified T : Event, reified E> Any.listenUnsafeOnce(
142+
inline fun <reified T : Event, reified E> Any.listenOnceUnsafe(
143143
priority: Int = 0,
144144
alwaysListen: Boolean = false,
145145
noinline transform: (T) -> E? = { null },
@@ -148,7 +148,7 @@ class UnsafeListener<T : Event>(
148148
val pointer = Pointer<E>()
149149

150150
val destroyable by selfReference<UnsafeListener<T>> {
151-
UnsafeListener(priority, this@listenUnsafeOnce, alwaysListen) { event ->
151+
UnsafeListener(priority, this@listenOnceUnsafe, alwaysListen) { event ->
152152
pointer.value = transform(event)
153153

154154
if (predicate(event) &&
@@ -196,7 +196,9 @@ class UnsafeListener<T : Event>(
196196
alwaysListen: Boolean = false,
197197
noinline function: (T) -> Unit = {},
198198
): UnsafeListener<T> {
199-
val listener = UnsafeListener<T>(priority, this, alwaysListen) { event -> function(event) }
199+
val listener = UnsafeListener<T>(priority, this, alwaysListen) { event ->
200+
function(event)
201+
}
200202

201203
EventFlow.concurrentListeners.subscribe<T>(listener)
202204

0 commit comments

Comments
 (0)