@@ -203,11 +203,12 @@ proc on*(proxy: ThreadProxy, action: string, handler: ThreadActionHandler) =
203
203
proxy.actions[action] = handler
204
204
205
205
proc onDefault*(proxy: ThreadProxy, handler: ThreadDefaultActionHandler) =
206
- ## Set default `handler`
206
+ ## Set default `handler` for all unhandled action
207
207
proxy.defaultAction = handler
208
208
209
209
template onData*(proxy: ThreadProxy, action: string , body: untyped ): void =
210
- ## Template version of `on`
210
+ ## Template version of `on`, saving you from typing `proc(data: JsonNode): Future[JsonNode] {.gcsafe,async.} = ...` every time.
211
+ ## The argument `data` is injected and so the name *onData*.
211
212
proxy.on (
212
213
action,
213
214
proc(json: JsonNode): Future[JsonNode] {.gcsafe,async.} =
@@ -222,9 +223,7 @@ template onDefaultData*(proxy: ThreadProxy, body: untyped) =
222
223
let data {.inject.} = j
223
224
`body`
224
225
225
- proc send*(proxy: ThreadProxy, target: ThreadChannelPtr, action: string , data: JsonNode): Future[void ] =
226
- ## Send `data` to `target` channel and then complete.
227
- ## Raise MessageUndeliveredError if cannot put on to target channel.
226
+ proc send(proxy: ThreadProxy, target: ThreadChannelPtr, action: string , data: JsonNode): Future[void ] =
228
227
result = newFuture[void ]("send")
229
228
let sent = target[].trySend proxy.newMessage(EMIT, action, data)
230
229
if sent:
@@ -237,8 +236,7 @@ proc send*(proxy: ThreadProxy, target: ThreadChannelPtr, action: string, data: J
237
236
err.sender = proxy.name
238
237
result .fail(err)
239
238
240
- proc ask*(proxy: ThreadProxy, target: ThreadChannelPtr, action: string , data: JsonNode = nil): Future[JsonNode] =
241
- ## Send `data` to `target` channel and then wait for reply
239
+ proc ask(proxy: ThreadProxy, target: ThreadChannelPtr, action: string , data: JsonNode = nil): Future[JsonNode] =
242
240
let id = proxy.nextCallbackId()
243
241
result = newFuture[JsonNode]("ask")
244
242
proxy.jsonCallbacks[id] = result
@@ -284,12 +282,17 @@ proc getChannel*(proxy: ThreadProxy, name: string): Future[ThreadChannelPtr] =
284
282
285
283
286
284
proc send*(proxy: ThreadProxy, target: string , action: string , data: JsonNode = nil): Future[void ] {.async.} =
287
- ## put one message to `target`'s channel
285
+ ## Send `data` to `target` channel.
286
+ ##
287
+ ## The returned future complete once the message is successfully placed on target channel.
288
+ ## otherwise fail with MessageUndeliveredError.
288
289
let ch = await proxy.getChannel(target)
289
290
await proxy.send(ch, action, data)
290
291
291
292
proc ask*(proxy: ThreadProxy, target: string , action: string , data: JsonNode = nil): Future[JsonNode] {.async.} =
292
- ## put one message to target's channel and complete until `target`'s response
293
+ ## Send `data` to `target` and wait for the return.
294
+ ##
295
+ ## The returned future will complete with the result from target is received. It may fail with many kind of errors.
293
296
let ch = await proxy.getChannel(target)
294
297
result = await proxy.ask(ch, action, data)
295
298
0 commit comments