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

Notifications.pudu

Pudu35 lines1.8 KB

GitHub ↗
1/** @Examples.Notifications.Program — one notification, several handlers, four publishing strategies */2module Notifications34import Std.Io as Io5import PuduLangMediator.Context as Context6import PuduLangMediator.Mediator as Mediator7import PuduLangMediator as Messaging8import PuduLangMediator.Notification as Notification9import PuduLangMediator.Publishing as Publishing10import PuduLangMediator.Registration as Registration1112/// Publishes the same sign-up through each publishing strategy and prints what each answered.13fn main() -> Int {14  let joined: Notification.Kind[Str, Str] = Notification.kind("users.joined")15  let registrations = [16    Registration.notificationHandler(&joined, "welcome email", |name: Str, _context: Context.Context| Ok(())),17    Registration.notificationHandler(&joined, "crm sync", |name: Str, _context: Context.Context| Messaging.raise("crm is down")),18    Registration.notificationHandler(&joined, "analytics", |name: Str, _context: Context.Context| Ok(()))19  ]20  let strategies: Array[(Str, dynamic Publishing.Strategy)] = [21    ("sequential", Publishing.Sequential{}), ("continuing", Publishing.Continuing{}), ("parallel", Publishing.Parallel{}), ("bounded", Publishing.Bounded{workers: 2})22  ]23  var outcomes: Array[Messaging.Outcome[(), Str]] = []24  for (name, strategy) in strategies {25    let mediator = match Mediator.buildWith(&Mediator.Options{..Mediator.defaults(), publisher: strategy}, registrations) {26      case Ok(found) => found27      case Err(invalid) => panic(Mediator.explain(&invalid))28    }29    let outcome = Mediator.publish(&mediator, &joined, "ada")30    let _said = Io.writeLine(name + ": " + Messaging.summarize(&outcome))31    outcomes = outcomes.push(outcome)32  }33  if outcomes.length() == 4 && outcomes.filter(|outcome: Messaging.Outcome[(), Str]| outcome == Err(Messaging.Raised("crm is down"))).length() == 4 { 0 } else { 1 }34}35