Skip to content

Commit f7ea93b

Browse files
committed
Refactor listener registrations listener -> listen, new scheduling
1 parent 95beed1 commit f7ea93b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+365
-363
lines changed

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import com.lambda.Lambda.LOG
2424
import com.lambda.Lambda.gson
2525
import com.lambda.config.configurations.ModuleConfig
2626
import com.lambda.event.events.ClientEvent
27-
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
27+
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2828
import com.lambda.threading.runIO
2929
import com.lambda.util.Communication.info
3030
import com.lambda.util.Communication.logError
@@ -57,9 +57,9 @@ abstract class Configuration : Jsonable {
5757
get() = File("${primary.parent}/${primary.nameWithoutExtension}-backup.${primary.extension}")
5858

5959
init {
60-
unsafeListener<ClientEvent.Startup> { tryLoad() }
60+
listenUnsafe<ClientEvent.Startup> { tryLoad() }
6161

62-
unsafeListener<ClientEvent.Shutdown>(Int.MIN_VALUE) { trySave() }
62+
listenUnsafe<ClientEvent.Shutdown>(Int.MIN_VALUE) { trySave() }
6363

6464
register()
6565
}

common/src/main/kotlin/com/lambda/core/PingManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.lambda.core
1919

2020
import com.lambda.event.events.PacketEvent
2121
import com.lambda.event.events.TickEvent
22-
import com.lambda.event.listener.SafeListener.Companion.listener
22+
import com.lambda.event.listener.SafeListener.Companion.listen
2323
import com.lambda.util.collections.LimitedOrderedSet
2424
import net.minecraft.network.packet.c2s.query.QueryPingC2SPacket
2525
import net.minecraft.network.packet.s2c.query.PingResultS2CPacket
@@ -33,12 +33,12 @@ object PingManager : Loadable {
3333
get() = pings.lastOrNull() ?: 0
3434

3535
init {
36-
listener<TickEvent.Pre> {
36+
listen<TickEvent.Pre> {
3737
connection.sendPacket(QueryPingC2SPacket(Util.getMeasuringTimeMs()))
3838
}
3939

40-
listener<PacketEvent.Receive.Pre> { event ->
41-
if (event.packet !is PingResultS2CPacket) return@listener
40+
listen<PacketEvent.Receive.Pre> { event ->
41+
if (event.packet !is PingResultS2CPacket) return@listen
4242

4343
pings.add(Util.getMeasuringTimeMs() - event.packet.startTime)
4444
}

common/src/main/kotlin/com/lambda/event/EventFlow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ object EventFlow {
6666
* the oldest event will be dropped to accommodate a new event.
6767
*/
6868
val concurrentFlow = MutableSharedFlow<Event>(
69-
extraBufferCapacity = 1000,
69+
extraBufferCapacity = 10000,
7070
onBufferOverflow = BufferOverflow.DROP_OLDEST
7171
)
7272

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.lambda.threading.runConcurrent
2626
import com.lambda.threading.runSafe
2727
import com.lambda.util.Pointer
2828
import com.lambda.util.selfReference
29+
import kotlinx.coroutines.CoroutineDispatcher
30+
import kotlinx.coroutines.Dispatchers
2931
import kotlin.properties.ReadOnlyProperty
3032
import kotlin.properties.ReadWriteProperty
3133
import 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,7 +116,7 @@ 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 = {},
@@ -142,7 +144,7 @@ class SafeListener<T : Event>(
142144
*
143145
* Usage:
144146
* ```kotlin
145-
* private val event by listenNext<MyEvent> { event ->
147+
* private val event by listenOnce<MyEvent> { event ->
146148
* player.sendMessage("Event received only once: $event")
147149
* // event is stored in the value
148150
* // event is unsubscribed after execution
@@ -193,11 +195,11 @@ class SafeListener<T : Event>(
193195
*
194196
* Usage:
195197
* ```kotlin
196-
* myTask.listener<MyEvent> { event ->
198+
* myTask.listen<MyEvent> { event ->
197199
* player.sendMessage("Event received: $event")
198200
* }
199201
*
200-
* myTask.listener<MyEvent>(priority = 1) { event ->
202+
* myTask.listen<MyEvent>(priority = 1) { event ->
201203
* player.sendMessage("Event received before the previous listener: $event")
202204
* }
203205
* ```
@@ -212,7 +214,7 @@ class SafeListener<T : Event>(
212214
* This function should take a SafeContext and an event of type T as parameters.
213215
* @return The newly created and registered [SafeListener].
214216
*/
215-
inline fun <reified T : Event> Task<*>.listener(
217+
inline fun <reified T : Event> Task<*>.listen(
216218
priority: Int = 0,
217219
alwaysListen: Boolean = false,
218220
noinline function: SafeContext.(T) -> Unit = {},
@@ -236,12 +238,12 @@ class SafeListener<T : Event>(
236238
*
237239
* Usage:
238240
* ```kotlin
239-
* concurrentListener<MyEvent> { event ->
241+
* listenConcurrently<MyEvent> { event ->
240242
* println("Concurrent event received: $event")
241243
* // no safe access to player or world
242244
* }
243245
*
244-
* concurrentListener<MyEvent>(priority = 1) { event ->
246+
* listenConcurrently<MyEvent>(priority = 1) { event ->
245247
* println("Concurrent event received before the previous listener: $event")
246248
* }
247249
* ```
@@ -251,13 +253,14 @@ class SafeListener<T : Event>(
251253
* @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.
252254
* @return The newly created and registered [SafeListener].
253255
*/
254-
inline fun <reified T : Event> Any.concurrentListener(
256+
inline fun <reified T : Event> Any.listenConcurrently(
255257
priority: Int = 0,
256258
alwaysListen: Boolean = false,
259+
scheduler: CoroutineDispatcher = Dispatchers.Default,
257260
noinline function: suspend SafeContext.(T) -> Unit = {},
258261
): SafeListener<T> {
259262
val listener = SafeListener<T>(priority, this, alwaysListen) { event ->
260-
runConcurrent {
263+
runConcurrent(scheduler) {
261264
function(event)
262265
}
263266
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ class UnsafeListener<T : Event>(
8181
* The [function] is executed on the same thread where the [Event] was dispatched.
8282
* The execution of the [function] is independent of the safety conditions of the context.
8383
* Use this function when you need to listen to an [Event] in a context that is not in-game.
84-
* For only in-game related contexts, use the [SafeListener.listener] function instead.
84+
* For only in-game related contexts, use the [SafeListener.listen] function instead.
8585
*
8686
* Usage:
8787
* ```kotlin
88-
* unsafeListener<MyEvent> { event ->
88+
* listenUnsafe<MyEvent> { event ->
8989
* println("Unsafe event received: $event")
9090
* // no safe access to player or world
9191
* }
9292
*
93-
* unsafeListener<MyEvent>(priority = 1) { event ->
93+
* listenUnsafe<MyEvent>(priority = 1) { event ->
9494
* println("Unsafe event received before the previous listener: $event")
9595
* }
9696
* ```
@@ -101,7 +101,7 @@ class UnsafeListener<T : Event>(
101101
* @param function The function to be executed when the event is posted. This function should take an event of type T as a parameter.
102102
* @return The newly created and registered [UnsafeListener].
103103
*/
104-
inline fun <reified T : Event> Any.unsafeListener(
104+
inline fun <reified T : Event> Any.listenUnsafe(
105105
priority: Int = 0,
106106
alwaysListen: Boolean = false,
107107
noinline function: (T) -> Unit = {},
@@ -123,7 +123,7 @@ class UnsafeListener<T : Event>(
123123
*
124124
* Usage:
125125
* ```kotlin
126-
* private val event by unsafeListenOnce<MyEvent> { event ->
126+
* private val event by listenUnsafeOnce<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.unsafeListenOnce(
142+
inline fun <reified T : Event, reified E> Any.listenUnsafeOnce(
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@unsafeListenOnce, alwaysListen) { event ->
151+
UnsafeListener(priority, this@listenUnsafeOnce, alwaysListen) { event ->
152152
pointer.value = transform(event)
153153

154154
if (predicate(event) &&
@@ -169,19 +169,19 @@ class UnsafeListener<T : Event>(
169169
* Registers a new [UnsafeListener] for a generic [Event] type [T].
170170
* The [function] is executed on a new thread running asynchronously to the game thread.
171171
* This function should only be used when the [function] performs read actions on the game data.
172-
* For only in-game related contexts, use the [SafeListener.concurrentListener] function instead.
172+
* For only in-game related contexts, use the [SafeListener.listenConcurrently] function instead.
173173
*
174174
* Caution: Using this function to write to the game data can lead to race conditions. Therefore, it is recommended
175175
* to use this function only for read operations to avoid potential concurrency issues.
176176
*
177177
* Usage:
178178
* ```kotlin
179-
* concurrentListener<MyEvent> { event ->
179+
* listenUnsafeConcurrently<MyEvent> { event ->
180180
* println("Concurrent event received: $event")
181181
* // no safe access to player or world
182182
* }
183183
*
184-
* concurrentListener<MyEvent>(priority = 1) { event ->
184+
* listenUnsafeConcurrently<MyEvent>(priority = 1) { event ->
185185
* println("Concurrent event received before the previous listener: $event")
186186
* }
187187
* ```
@@ -191,7 +191,7 @@ class UnsafeListener<T : Event>(
191191
* @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.
192192
* @return The newly created and registered [UnsafeListener].
193193
*/
194-
inline fun <reified T : Event> Any.unsafeConcurrentListener(
194+
inline fun <reified T : Event> Any.listenUnsafeConcurrently(
195195
priority: Int = 0,
196196
alwaysListen: Boolean = false,
197197
noinline function: (T) -> Unit = {},

common/src/main/kotlin/com/lambda/graphics/RenderMain.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.lambda.Lambda.mc
2121
import com.lambda.event.EventFlow.post
2222
import com.lambda.event.events.RenderEvent
2323
import com.lambda.event.events.TickEvent
24-
import com.lambda.event.listener.SafeListener.Companion.listener
24+
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.graphics.animation.Animation.Companion.exp
2626
import com.lambda.graphics.animation.AnimationTicker
2727
import com.lambda.graphics.buffer.FrameBuffer
@@ -46,7 +46,7 @@ object RenderMain {
4646
private val showHud get() = mc.currentScreen == null || LambdaHudGui.isOpen
4747

4848
private val hudAnimation0 = with(AnimationTicker()) {
49-
listener<TickEvent.Pre> {
49+
listen<TickEvent.Pre> {
5050
tick()
5151
}
5252

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ package com.lambda.graphics.renderer.esp
2020
import com.lambda.event.events.RenderEvent
2121
import com.lambda.event.events.TickEvent
2222
import com.lambda.event.events.WorldEvent
23-
import com.lambda.event.listener.SafeListener.Companion.concurrentListener
24-
import com.lambda.event.listener.SafeListener.Companion.listener
23+
import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
24+
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.graphics.renderer.esp.impl.ESPRenderer
2626
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
2727
import com.lambda.module.modules.client.RenderSettings
@@ -53,19 +53,19 @@ class ChunkedESP private constructor(
5353
}
5454

5555
init {
56-
concurrentListener<WorldEvent.BlockUpdate> { event ->
56+
listenConcurrently<WorldEvent.BlockUpdate> { event ->
5757
world.getWorldChunk(event.pos).renderer.notifyChunks()
5858
}
5959

60-
concurrentListener<WorldEvent.ChunkEvent.Load> { event ->
60+
listenConcurrently<WorldEvent.ChunkEvent.Load> { event ->
6161
event.chunk.renderer.notifyChunks()
6262
}
6363

64-
concurrentListener<WorldEvent.ChunkEvent.Unload> { event ->
64+
listenConcurrently<WorldEvent.ChunkEvent.Unload> { event ->
6565
rendererMap.remove(event.chunk.pos.toLong())?.notifyChunks()
6666
}
6767

68-
owner.concurrentListener<TickEvent.Pre> {
68+
owner.listenConcurrently<TickEvent.Pre> {
6969
if (++ticks % RenderSettings.updateFrequency == 0) {
7070
val polls = minOf(RenderSettings.rebuildsPerTick, rebuildQueue.size)
7171

@@ -76,8 +76,8 @@ class ChunkedESP private constructor(
7676
}
7777
}
7878

79-
owner.listener<TickEvent.Pre> {
80-
if (uploadQueue.isEmpty()) return@listener
79+
owner.listen<TickEvent.Pre> {
80+
if (uploadQueue.isEmpty()) return@listen
8181

8282
val polls = minOf(RenderSettings.uploadsPerTick, uploadQueue.size)
8383

@@ -86,7 +86,7 @@ class ChunkedESP private constructor(
8686
}
8787
}
8888

89-
owner.listener<RenderEvent.World> {
89+
owner.listen<RenderEvent.World> {
9090
rendererMap.values.forEach {
9191
it.renderer?.render()
9292
}

common/src/main/kotlin/com/lambda/graphics/renderer/esp/global/DynamicESP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ package com.lambda.graphics.renderer.esp.global
2020
import com.lambda.event.EventFlow.post
2121
import com.lambda.event.events.RenderEvent
2222
import com.lambda.event.events.TickEvent
23-
import com.lambda.event.listener.SafeListener.Companion.listener
23+
import com.lambda.event.listener.SafeListener.Companion.listen
2424
import com.lambda.graphics.renderer.esp.impl.DynamicESPRenderer
2525

2626
object DynamicESP : DynamicESPRenderer() {
2727
init {
28-
listener<TickEvent.Post> {
28+
listen<TickEvent.Post> {
2929
clear()
3030
RenderEvent.DynamicESP().post()
3131
upload()

common/src/main/kotlin/com/lambda/graphics/renderer/esp/global/StaticESP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ package com.lambda.graphics.renderer.esp.global
2020
import com.lambda.event.EventFlow.post
2121
import com.lambda.event.events.RenderEvent
2222
import com.lambda.event.events.TickEvent
23-
import com.lambda.event.listener.SafeListener.Companion.listener
23+
import com.lambda.event.listener.SafeListener.Companion.listen
2424
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
2525

2626
object StaticESP : StaticESPRenderer(false) {
2727
init {
28-
listener<TickEvent.Post> {
28+
listen<TickEvent.Post> {
2929
clear()
3030
RenderEvent.StaticESP().post()
3131
upload()

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.lambda.Lambda.mc
2121
import com.lambda.event.Muteable
2222
import com.lambda.event.events.RenderEvent
2323
import com.lambda.event.events.TickEvent
24-
import com.lambda.event.listener.SafeListener.Companion.listener
24+
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.graphics.animation.AnimationTicker
2626
import com.lambda.gui.api.component.core.IComponent
2727
import com.lambda.gui.impl.AbstractClickGui
@@ -51,12 +51,12 @@ abstract class LambdaGui(
5151
val animation = AnimationTicker()
5252

5353
init {
54-
listener<RenderEvent.GUI.Scaled> { event ->
54+
listen<RenderEvent.GUI.Scaled> { event ->
5555
screenSize = event.screenSize
5656
onEvent(GuiEvent.Render())
5757
}
5858

59-
listener<TickEvent.Pre> {
59+
listen<TickEvent.Pre> {
6060
animation.tick()
6161
onEvent(GuiEvent.Tick())
6262
}

0 commit comments

Comments
 (0)