Skip to content

Commit 456a608

Browse files
authored
Support notification in rpc context for non-async method (#279)
Make this compile: ```nim srv.rpc(JrpcConv): proc empty(): void = echo "nothing" ```
1 parent e6e3545 commit 456a608

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

json_rpc/private/server_handler_wrapper.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func wrapServerHandler*(
345345

346346
let
347347
executeCall = newCall(handlerName, executeParams)
348+
handlerReturnType = handler.params[0]
348349

349350
result = newStmtList()
350351
result.add handler
@@ -353,5 +354,8 @@ func wrapServerHandler*(
353354
# Avoid 'yield in expr not lowered' with an intermediate variable.
354355
# See: https://github.com/nim-lang/Nim/issues/17849
355356
`setup`
356-
let handlerRes = `executeCall`
357-
maybeWrapServerResult(`formatType`, handlerRes)
357+
when `handlerReturnType` is void:
358+
`executeCall`
359+
else:
360+
let handlerRes = `executeCall`
361+
maybeWrapServerResult(`formatType`, handlerRes)

tests/testserverclient.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,32 @@ template notifyTest(router, client: untyped) =
206206
var
207207
notified = newAsyncEvent()
208208
notified2 = newAsyncEvent()
209+
notified3 = newAsyncEvent()
210+
notified4 = newAsyncEvent()
209211

210212
router[].rpc("some_notify") do() -> void:
211213
notified.fire()
212214
router[].rpc("some_notify2") do() -> void:
213215
notified2.fire()
214216

217+
router[].rpc(JrpcFlavor):
218+
proc some_notify3(): void =
219+
notified3.fire()
220+
221+
proc some_notify4(): void {.async: (raises: []).} =
222+
notified4.fire()
223+
215224
await srv.notify("some_notify", default(RequestParamsTx))
216225
await srv.notify("doesnt_exist", default(RequestParamsTx))
217226
await srv.notify("some_notify2", default(RequestParamsTx))
227+
await srv.notify("some_notify3", default(RequestParamsTx))
228+
await srv.notify("some_notify4", default(RequestParamsTx))
218229

219230
check:
220231
await notified.wait().withTimeout(1.seconds)
221232
await notified2.wait().withTimeout(1.seconds)
233+
await notified3.wait().withTimeout(1.seconds)
234+
await notified4.wait().withTimeout(1.seconds)
222235

223236
suite "Socket Bidirectional":
224237
setup:

0 commit comments

Comments
 (0)