tlambda.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # bug 2007
  2. import asyncdispatch, asyncnet, logging, json, uri, strutils, future
  3. type
  4. Builder = ref object
  5. client: Client
  6. build: Build
  7. ProgressCB* = proc (message: string): Future[void] {.closure, gcsafe.}
  8. Build* = ref object
  9. onProgress*: ProgressCB
  10. Client = ref ClientObj
  11. ClientObj = object
  12. onMessage: proc (client: Client, msg: JsonNode): Future[void]
  13. proc newClient*(name: string,
  14. onMessage: (Client, JsonNode) -> Future[void]): Client =
  15. new result
  16. result.onMessage = onMessage
  17. proc newBuild*(onProgress: ProgressCB): Build =
  18. new result
  19. result.onProgress = onProgress
  20. proc start(build: Build, repo, hash: string) {.async.} =
  21. let path = repo.parseUri().path.toLower()
  22. proc onProgress(builder: Builder, message: string) {.async.} =
  23. debug($message)
  24. proc onMessage(builder: Builder, message: JsonNode) {.async.} =
  25. debug("onMessage")
  26. proc newBuilder(): Builder =
  27. var cres: Builder
  28. new cres
  29. cres.client = newClient("builder", (client, msg) => (onMessage(cres, msg)))
  30. cres.build = newBuild(
  31. proc (msg: string): Future[void] {.closure, gcsafe.} = onProgress(cres, msg))
  32. return cres
  33. proc main() =
  34. # Set up logging.
  35. var console = newConsoleLogger(fmtStr = verboseFmtStr)
  36. addHandler(console)
  37. var builder = newBuilder()
  38. # Test {.async.} pragma with do notation: #5995
  39. builder.client = newClient("builder") do(client: Client, msg: JsonNode) {.async.}:
  40. await onMessage(builder, msg)
  41. main()