moonflow

    A lightweight event bus and message abstraction layer for MoonBit.

    event-bus
    message
    pubsub
    moonbit
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    2 months ago
    Downloads
    28

    No dependencies

    #username/moonflow

    BusConfig

    pub(all) struct BusConfig {
    mode : PublishMode
    keep_history : Bool
    keep_dead_letters : Bool
    } derive(Eq,
    Debug
    )

    BusConfig controls dispatch behavior.

    BusConfig::BusConfig

    fn BusConfig::BusConfig(mode? : PublishMode, keep_history? : Bool, keep_dead_letters? : Bool) -> BusConfig

    DeadLetter

    pub(all) struct DeadLetter {
    message : Message
    subscriber_id : String
    reason : String
    } derive(Eq,
    Debug
    )

    DeadLetter keeps messages that were rejected by at least one subscriber.

    DeliveryReport

    pub(all) struct DeliveryReport {
    subscriber_id : String
    topic : String
    status : DeliveryStatus
    } derive(Eq,
    Debug
    )

    DeliveryReport is one subscriber's dispatch result.

    DeliveryStatus

    pub(all) enum DeliveryStatus {
    Ack
    Rejected(String)
    Skipped(String)
    } derive(Eq,
    Debug
    )

    DeliveryStatus describes how a subscriber handled a message.

    EventBus

    pub(all) struct EventBus {
    config : BusConfig
    subscribers : Array[Subscriber]
    history : Array[Message]
    dead_letters : Array[DeadLetter]
    } derive(
    Debug
    )

    EventBus is an in-memory event hub for tests, tools, and embedded apps.

    EventBus::EventBus

    fn EventBus::EventBus(config? : BusConfig) -> EventBus

    EventBus::clear_dead_letters

    fn EventBus::clear_dead_letters(self : EventBus) -> Unit

    EventBus::clear_history

    fn EventBus::clear_history(self : EventBus) -> Unit

    EventBus::dead_letter_count

    fn EventBus::dead_letter_count(self : EventBus) -> Int

    EventBus::dispatch

    fn EventBus::dispatch(self : EventBus, message : Message) -> PublishReport

    EventBus::history_count

    fn EventBus::history_count(self : EventBus) -> Int

    EventBus::publish

    fn EventBus::publish(self : EventBus, topic : StringView, payload? : StringView, source? : StringView, trace_id? : StringView, tags? : ArrayView[String], headers? : Map[String, String]) -> PublishReport

    EventBus::sort_subscribers

    fn EventBus::sort_subscribers(self : EventBus) -> Unit

    EventBus::subscribe

    fn EventBus::subscribe(self : EventBus, id : StringView, pattern : StringView, handler : (Message) -> DeliveryStatus, priority? : Int, once? : Bool) -> Unit

    EventBus::subscriber_count

    fn EventBus::subscriber_count(self : EventBus) -> Int

    EventBus::unsubscribe

    fn EventBus::unsubscribe(self : EventBus, id : StringView) -> Bool

    EventRecorder

    pub(all) struct EventRecorder {
    name : String
    messages : Array[Message]
    } derive(
    Debug
    )

    EventRecorder is a small in-memory probe for tests.

    EventRecorder::EventRecorder

    fn EventRecorder::EventRecorder(name? : StringView) -> EventRecorder

    EventRecorder::clear

    fn EventRecorder::clear(self : EventRecorder) -> Unit

    EventRecorder::contains_topic

    fn EventRecorder::contains_topic(self : EventRecorder, topic : StringView) -> Bool

    EventRecorder::count

    fn EventRecorder::count(self : EventRecorder) -> Int

    EventRecorder::count_topic

    fn EventRecorder::count_topic(self : EventRecorder, topic : StringView) -> Int

    EventRecorder::handle

    fn EventRecorder::handle(self : EventRecorder, message : Message) -> DeliveryStatus

    EventRecorder::handler

    fn EventRecorder::handler(self : EventRecorder) -> ((Message) -> DeliveryStatus)

    EventRecorder::last

    fn EventRecorder::last(self : EventRecorder) -> Message?

    FakePublisher

    pub(all) struct FakePublisher {
    bus : EventBus
    source : String
    } derive(
    Debug
    )

    FakePublisher::FakePublisher

    fn FakePublisher::FakePublisher(bus : EventBus, source? : StringView) -> FakePublisher

    FakePublisher::emit

    fn FakePublisher::emit(self : FakePublisher, topic : StringView) -> PublishReport

    FakePublisher::emit_payload

    fn FakePublisher::emit_payload(self : FakePublisher, topic : StringView, payload : StringView) -> PublishReport

    Message

    pub(all) struct Message {
    id : String
    topic : String
    payload : String
    source : String
    trace_id : String
    tags : Array[String]
    headers : Map[String, String]
    } derive(Eq,
    Debug
    )

    Message carries a topic, payload, and lightweight metadata.

    Message::Message

    fn Message::Message(id : StringView, topic : StringView, payload? : StringView, source? : StringView, trace_id? : StringView, tags? : ArrayView[String], headers? : Map[String, String]) -> Message

    PublishMode

    pub(all) enum PublishMode {
    ContinueOnError
    StopOnError
    } derive(Eq,
    Debug
    )

    PublishMode controls whether dispatch continues after a handler failure.

    PublishReport

    pub(all) struct PublishReport {
    message_id : String
    topic : String
    matched : Int
    delivered : Int
    rejected : Int
    skipped : Int
    reports : Array[DeliveryReport]
    } derive(Eq,
    Debug
    )

    PublishReport summarizes one publish operation.

    Subscriber

    pub(all) struct Subscriber {
    id : String
    pattern : TopicPattern
    priority : Int
    once : Bool
    active : Bool
    handler : (Message) -> DeliveryStatus
    } derive(
    Debug
    )

    Subscriber records routing information and the callback to invoke.

    Subscriber::Subscriber

    fn Subscriber::Subscriber(id : StringView, pattern : StringView, handler : (Message) -> DeliveryStatus, priority? : Int, once? : Bool) -> Subscriber

    TopicPattern

    pub(all) struct TopicPattern {
    pattern : String
    } derive(Eq,
    Debug
    )

    TopicPattern is the route expression used by subscribers.

    TopicPattern::TopicPattern

    fn TopicPattern::TopicPattern(pattern : StringView) -> TopicPattern

    TopicPattern::matches

    fn TopicPattern::matches(self : TopicPattern, topic : StringView) -> Bool

    assert_delivered

    fn assert_delivered(report : PublishReport, expected : Int) -> Unit

    assert_matched

    fn assert_matched(report : PublishReport, expected : Int) -> Unit

    assert_rejected

    fn assert_rejected(report : PublishReport, expected : Int) -> Unit

    collect_topics

    fn collect_topics(messages : ArrayView[Message]) -> Array[String]

    escape_json

    fn escape_json(input : StringView) -> String

    new_event_bus

    fn new_event_bus(config? : BusConfig) -> EventBus

    report_to_json

    fn report_to_json(report : PublishReport) -> String

    standard_catalog_size

    fn standard_catalog_size() -> Int

    standard_messages

    fn standard_messages() -> Array[Message]

    standard_route_cases

    fn standard_route_cases() -> Array[(String, String, Bool)]

    standard_topic_names

    fn standard_topic_names() -> Array[String]

    Built-in topic and message catalog for examples, smoke tests, and demos.

    standard_topic_patterns

    fn standard_topic_patterns() -> Array[TopicPattern]

    status_name

    fn status_name(status : DeliveryStatus) -> String

    topic_matches

    fn topic_matches(pattern : StringView, topic : StringView) -> Bool