Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-mediator

In-process messaging for Pudu: requests, notifications, streams, pipeline behaviors, processors, and exception handling

0.1.0Apache-2.01

InstallClose

QuickStart.pudu

Pudu48 lines2.1 KB

GitHub ↗
1/** @Examples.QuickStart.Program — one query, one command, and one notification */2module QuickStart34import Std.Io as Io5import PuduLangMediator.Context as Context6import PuduLangMediator.Mediator as Mediator7import PuduLangMediator as Messaging8import PuduLangMediator.Notification as Notification9import PuduLangMediator.Registration as Registration10import PuduLangMediator.Request as Request1112/** @Examples.QuickStart.Greeting — the query's request */13type Greeting = { name: Str }1415/// Builds a mediator, sends a query and a command, publishes a notification, and prints the outcomes.16fn main() -> Int {17  let greet: Request.Kind[Greeting, Str, Str] = Request.kind("greet")18  let wave: Request.Kind[Str, (), Str] = Request.command("wave")19  let greeted: Notification.Kind[Str, Str] = Notification.kind("greeted")20  let built = Mediator.build([21      Registration.handler(&greet, fn(request: Greeting, _context: Context.Context) -> Messaging.Outcome[Str, Str] {22          if request.name.isEmpty() { Messaging.raise("who should I greet?") } else { Ok("Hello, " + request.name + "!") }23        }),24      Registration.handler(&wave, fn(name: Str, _context: Context.Context) -> Messaging.Outcome[(), Str] {25          let _said = Io.writeLine("(waves at " + name + ")")26          Ok(())27        }),28      Registration.notificationHandler(&greeted, "audit", fn(name: Str, _context: Context.Context) -> Messaging.Outcome[(), Str] {29          let _said = Io.writeLine("audit: greeted " + name)30          Ok(())31        })32    ])33  let mediator = match built {34    case Ok(found) => found35    case Err(invalid) => {36      let _said = Io.writeErrorLine(Mediator.explain(&invalid))37      return 138    }39  }40  let hello = Mediator.send(&mediator, &greet, Greeting{name: "Ada"})41  let nobody = Mediator.send(&mediator, &greet, Greeting{name: ""})42  let waved = Mediator.send(&mediator, &wave, "Ada")43  let published = Mediator.publish(&mediator, &greeted, "Ada")44  let _first = Io.writeLine(Messaging.summarize(&hello))45  let _second = Io.writeLine(Messaging.summarize(&nobody))46  if hello == Ok("Hello, Ada!") && nobody == Err(Messaging.Raised("who should I greet?")) && waved == Ok(()) && published == Ok(()) { 0 } else { 1 }47}48