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
      * Sends the given command to the IRC server, and waits for a response back.
142
      * Sends the given command to the IRC server, and waits for a response back.
143
      *
143
      *
144
      * This should only be needed to send raw/custom commands; standard messages can be sent using the
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
      * This method will return immediately. The returned [Deferred] will eventually be populated with
147
      * This method will return immediately. The returned [Deferred] will eventually be populated with
148
      * the server's response. If the server supports the labeled-responses capability, a label will
148
      * the server's response. If the server supports the labeled-responses capability, a label will
162
      * Sends the given command to the IRC server, and waits for a response back.
162
      * Sends the given command to the IRC server, and waits for a response back.
163
      *
163
      *
164
      * This should only be needed to send raw/custom commands; standard messages can be sent using the
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
      * This method will return immediately. The returned [Deferred] will eventually be populated with
167
      * This method will return immediately. The returned [Deferred] will eventually be populated with
168
      * the server's response. If the server supports the labeled-responses capability, a label will
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
 
2
 
3
 import com.dmdirc.ktirc.IrcClient
3
 import com.dmdirc.ktirc.IrcClient
4
 import com.dmdirc.ktirc.events.IrcEvent
4
 import com.dmdirc.ktirc.events.IrcEvent
5
+import kotlinx.coroutines.CoroutineScope
5
 import kotlinx.coroutines.GlobalScope
6
 import kotlinx.coroutines.GlobalScope
6
 import kotlinx.coroutines.launch
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
     override fun processEvent(client: IrcClient, event: IrcEvent) {
11
     override fun processEvent(client: IrcClient, event: IrcEvent) {
11
         client.serverState.asyncResponseState.pendingResponses.values
12
         client.serverState.asyncResponseState.pendingResponses.values
12
                 .filter { it.second(event) }
13
                 .filter { it.second(event) }
13
                 .forEach {
14
                 .forEach {
14
-                    GlobalScope.launch {
15
+                    scope.launch {
15
                         it.first.send(event)
16
                         it.first.send(event)
16
                     }
17
                     }
17
                 }
18
                 }

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

16
         PingHandler(),
16
         PingHandler(),
17
         ServerStateHandler(),
17
         ServerStateHandler(),
18
         UserStateHandler(),
18
         UserStateHandler(),
19
-        LabelledResponseHandler()
19
+        AsyncResponseHandler()
20
 )
20
 )

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

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