Skip to content

Commit e2f29f7

Browse files
feat: add property delegation support for event declarations
1 parent beae8ff commit e2f29f7

File tree

3 files changed

+42
-10
lines changed
  • lib/common/src
    • main/kotlin/dev/robocode/tankroyale/common
    • test/kotlin/dev/robocode/tankroyale/common
  • openspec/changes/improve-event-system-kotlin

3 files changed

+42
-10
lines changed

lib/common/src/main/kotlin/dev/robocode/tankroyale/common/Event.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import java.util.WeakHashMap
1212
* // Declare event
1313
* val onMyEvent = Event<MyEvent>()
1414
*
15+
* // Declare event using property delegation (optional)
16+
* val onMyDelegatedEvent by event<MyEvent>()
17+
*
1518
* // Subscribe to event with an event handler to handle event when it occurs
1619
* onMyEvent.subscribe(this) { event -> handle(event) }
1720
*
@@ -121,6 +124,21 @@ open class Event<T> {
121124
}
122125
}
123126

127+
/**
128+
* Delegate for creating an [Event] instance using property delegation.
129+
*/
130+
class EventDelegate<T> {
131+
132+
private val event = Event<T>()
133+
134+
operator fun getValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>): Event<T> = event
135+
}
136+
137+
/**
138+
* Creates an [EventDelegate] for property delegation: `val onEvent by event<T>()`.
139+
*/
140+
fun <T> event() = EventDelegate<T>()
141+
124142
/**
125143
* Wrapper for subscribing with the once-flag using operator syntax.
126144
*/

lib/common/src/test/kotlin/dev/robocode/tankroyale/common/EventTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ class EventTest : FunSpec({
6767
result shouldBe listOf("test1")
6868
}
6969

70+
test("property delegation") {
71+
val events = object {
72+
val onMessage by event<String>()
73+
}
74+
75+
val result = mutableListOf<String>()
76+
77+
events.onMessage.subscribe(this) { result.add(it) }
78+
events.onMessage.fire("test")
79+
80+
(events.onMessage === events.onMessage) shouldBe true
81+
result shouldBe listOf("test")
82+
}
83+
7084
test("subscribe once") {
7185
val event = Event<String>()
7286
val result = mutableListOf<String>()

openspec/changes/improve-event-system-kotlin/tasks.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,29 @@
4141

4242
### 3.1 Event Delegate Implementation
4343

44-
- [ ] 3.1.1 Create `EventDelegate<T>` class with `getValue()` operator
45-
- [ ] 3.1.2 Create `event()` factory function returning `EventDelegate<T>`
46-
- [ ] 3.1.3 Test property delegation in event object declarations
47-
- [ ] 3.1.4 Update KDoc with delegation examples
44+
- [x] 3.1.1 Create `EventDelegate<T>` class with `getValue()` operator
45+
- [x] 3.1.2 Create `event()` factory function returning `EventDelegate<T>`
46+
- [x] 3.1.3 Test property delegation in event object declarations
47+
- [x] 3.1.4 Update KDoc with delegation examples
4848

4949
### 3.2 Documentation Updates
5050

51-
- [ ] 3.2.1 Document that delegation is optional (not required)
52-
- [ ] 3.2.2 Update Event.kt KDoc header with all usage patterns
51+
- [x] 3.2.1 Document that delegation is optional (not required)
52+
- [x] 3.2.2 Update Event.kt KDoc header with all usage patterns
5353

5454
## Phase 4: Testing and Validation
5555

5656
### 4.1 Existing Behavior Verification
5757

58-
- [ ] 4.1.1 Run existing `EventTest.kt` without modifications
58+
- [x] 4.1.1 Run existing `EventTest.kt` without modifications
5959
- [ ] 4.1.2 Manual GUI smoke test — verify all event handlers work
6060
- [ ] 4.1.3 Verify `WebSocketClientEvents` work unchanged
61-
- [ ] 4.1.4 Test EDT.enqueue() extension function compatibility
61+
- [x] 4.1.4 Test EDT.enqueue() extension function compatibility
6262

6363
### 4.2 New Pattern Tests
6464

65-
- [ ] 4.2.1 Add tests for operator `+=` and `-=` syntax
66-
- [ ] 4.2.2 Add tests for property delegation syntax
65+
- [x] 4.2.1 Add tests for operator `+=` and `-=` syntax
66+
- [x] 4.2.2 Add tests for property delegation syntax
6767

6868
## Phase 5: Documentation
6969

0 commit comments

Comments
 (0)