Browse Source

Implement first async message builder

tags/v0.11.0
Chris Smith 5 years ago
parent
commit
205b934766

+ 2
- 2
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt View File

@@ -142,7 +142,7 @@ internal interface ExperimentalIrcClient : IrcClient {
142 142
      * Sends the given command to the IRC server, and waits for a response back.
143 143
      *
144 144
      * This should only be needed to send raw/custom commands; standard messages can be sent using the
145
-     * extension methods in [com.dmdirc.ktirc.messages] such as TODO: sendJoinAsync.
145
+     * extension methods in [com.dmdirc.ktirc.messages] such as [com.dmdirc.ktirc.messages.sendPartAsync].
146 146
      *
147 147
      * This method will return immediately. The returned [Deferred] will eventually be populated with
148 148
      * the server's response. If the server supports the labeled-responses capability, a label will
@@ -162,7 +162,7 @@ internal interface ExperimentalIrcClient : IrcClient {
162 162
      * Sends the given command to the IRC server, and waits for a response back.
163 163
      *
164 164
      * This should only be needed to send raw/custom commands; standard messages can be sent using the
165
-     * extension methods in [com.dmdirc.ktirc.messages] such as TODO: sendJoinAsync.
165
+     * extension methods in [com.dmdirc.ktirc.messages] such as [com.dmdirc.ktirc.messages.sendPartAsync].
166 166
      *
167 167
      * This method will return immediately. The returned [Deferred] will eventually be populated with
168 168
      * the server's response. If the server supports the labeled-responses capability, a label will

src/main/kotlin/com/dmdirc/ktirc/events/handlers/LabelledResponseHandler.kt → src/main/kotlin/com/dmdirc/ktirc/events/handlers/AsyncResponseHandler.kt View File

@@ -2,16 +2,17 @@ package com.dmdirc.ktirc.events.handlers
2 2
 
3 3
 import com.dmdirc.ktirc.IrcClient
4 4
 import com.dmdirc.ktirc.events.IrcEvent
5
+import kotlinx.coroutines.CoroutineScope
5 6
 import kotlinx.coroutines.GlobalScope
6 7
 import kotlinx.coroutines.launch
7 8
 
8
-internal class LabelledResponseHandler : EventHandler {
9
+internal class AsyncResponseHandler(private val scope: CoroutineScope = GlobalScope) : EventHandler {
9 10
 
10 11
     override fun processEvent(client: IrcClient, event: IrcEvent) {
11 12
         client.serverState.asyncResponseState.pendingResponses.values
12 13
                 .filter { it.second(event) }
13 14
                 .forEach {
14
-                    GlobalScope.launch {
15
+                    scope.launch {
15 16
                         it.first.send(event)
16 17
                     }
17 18
                 }

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/events/handlers/EventHandler.kt View File

@@ -16,5 +16,5 @@ internal val eventHandlers = listOf(
16 16
         PingHandler(),
17 17
         ServerStateHandler(),
18 18
         UserStateHandler(),
19
-        LabelledResponseHandler()
19
+        AsyncResponseHandler()
20 20
 )

+ 10
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/MessageBuildersAsync.kt View File

@@ -0,0 +1,10 @@
1
+package com.dmdirc.ktirc.messages
2
+
3
+import com.dmdirc.ktirc.ExperimentalIrcClient
4
+import com.dmdirc.ktirc.events.ChannelParted
5
+
6
+/** Sends a request to part the given channel. */
7
+internal fun ExperimentalIrcClient.sendPartAsync(channel: String, reason: String? = null) =
8
+        sendAsync("PART", reason?.let { arrayOf(channel, reason) } ?: arrayOf(channel)) {
9
+            it is ChannelParted && isLocalUser(it.user) && it.target == channel
10
+        }

Loading…
Cancel
Save