#mizchi/js_web/event

    Event API for event-driven programming.

    #Installation

    Add to your moon.mod:

    import { "mizchi/js_web@0.13.0", }

    ...and to the moon.pkg of the package that uses it:

    import { "mizchi/js_web/event", }

    #Overview

    Provides bindings for Event, EventTarget and EventSource (server-sent events).

    #Usage Example

    ///|
    pub fn dispatch(target : @event.EventTarget) -> Bool {
    // Create an event
    let event = @event.Event("myevent", bubbles=true, cancelable=true)

    // Event properties
    let _ = event.type_()
    let _ = event.bubbles()
    let _ = event.cancelable()

    // Register a listener, then dispatch
    target.addEventListener("myevent", _ev => ())
    target.dispatchEvent(event)
    }

    Server-sent events go through EventSource:

    ///|
    pub fn subscribe() -> @event.EventSource {
    let source = @event.EventSource("/stream")
    source.set_onmessage(ev => println(@event.get_event_data(ev)))
    source
    }

    #Available Types

    • Event - Base event type
    • EventTarget - Event listener registration and dispatch
    • EventSource - Server-sent events

    CustomEvent and MessageEvent are not bound in this package. A message event's payload is readable from the raw value with the get_event_data / get_event_origin / get_last_event_id helpers above; @message covers MessageChannel and MessagePort.

    #Reference

    Event

    #external
    pub type Event

    Event - Base interface for all DOM events https://developer.mozilla.org/en-US/docs/Web/API/Event

    Event::Event

    fn Event::Event(event_type : String, bubbles? : Bool, cancelable? : Bool) -> Event

    Create a new Event

    Event::as_any

    fn Event::as_any(self : Event) ->
    Any

    Event::bubbles

    fn Event::bubbles(self : Event) -> Bool

    Check if the event bubbles

    Event::cancelable

    fn Event::cancelable(self : Event) -> Bool

    Check if the event is cancelable

    Event::currentTarget

    fn Event::currentTarget(self : Event) -> EventTarget?

    Get the current target

    Event::defaultPrevented

    fn Event::defaultPrevented(self : Event) -> Bool

    Check if default was prevented

    Event::new

    #deprecated("use `Event(..)` instead")
    fn Event::new(event_type : String, bubbles? : Bool, cancelable? : Bool) -> Event

    Deprecated alias for the Event(..) constructor.

    Event::preventDefault

    fn Event::preventDefault(self : Event) -> Unit

    Prevent the default action

    Event::stopImmediatePropagation

    fn Event::stopImmediatePropagation(self : Event) -> Unit

    Stop immediate propagation

    Event::stopPropagation

    fn Event::stopPropagation(self : Event) -> Unit

    Stop event propagation

    Event::target

    fn Event::target(self : Event) -> EventTarget?

    Get the event target

    Event::type_

    fn Event::type_(self : Event) -> String

    Get the event type

    EventSource

    pub(all) struct EventSource {
    url : String
    readyState : Int
    withCredentials : Bool
    }

    EventSource::EventSource

    fn EventSource::EventSource(url : String, withCredentials? : Bool) -> EventSource

    Create a new EventSource JS: new EventSource(url, options?)

    EventSource::as_any

    EventSource::as_event_target

    fn EventSource::as_event_target(self : EventSource) -> EventTarget

    Cast to EventTarget for event handling

    EventSource::close

    fn EventSource::close(self : EventSource) -> Unit

    Close the connection JS: eventSource.close()

    EventSource::new

    #deprecated("use `EventSource(..)` instead")
    fn EventSource::new(url : String, withCredentials? : Bool) -> EventSource

    Deprecated alias for the EventSource(..) constructor.

    EventSource::set_onerror

    fn EventSource::set_onerror(self : EventSource, handler : (
    Any
    ) -> Unit) -> Unit

    Set onerror event handler JS: eventSource.onerror = handler

    EventSource::set_onmessage

    fn EventSource::set_onmessage(self : EventSource, handler : (
    Any
    ) -> Unit) -> Unit

    Set onmessage event handler JS: eventSource.onmessage = handler

    EventSource::set_onopen

    fn EventSource::set_onopen(self : EventSource, handler : (
    Any
    ) -> Unit) -> Unit

    Set onopen event handler JS: eventSource.onopen = handler

    EventTarget

    #external
    pub type EventTarget

    https://developer.mozilla.org/en-US/docs/Web/API/Event/target EventTarget is the base interface for objects that can receive events. Types that can be event targets should provide as_event_target() method.

    EventTarget::addEventListener

    #alias(add_event_listener)
    fn EventTarget::addEventListener(self : EventTarget, event_type : String, handler : (
    Any
    ) -> Unit, capture? : Bool, once? : Bool, passive? : Bool, signal? :
    AbortSignal
    ) -> Unit

    Add an event listener for the specified event type This is the primary method for registering event handlers

    EventTarget::as_any

    EventTarget::dispatchEvent

    #alias(dispatch_event)
    fn EventTarget::dispatchEvent(self : EventTarget, event : Event) -> Bool

    Dispatch an event to this target

    EventTarget::off

    fn EventTarget::off(self : EventTarget, event_type : String, handler : (
    Any
    ) -> Unit, capture? : Bool) -> Unit

    Short alias for removeEventListener

    EventTarget::on

    fn EventTarget::on(self : EventTarget, event_type : String, handler : (
    Any
    ) -> Unit, capture? : Bool, once? : Bool, passive? : Bool, signal? :
    AbortSignal
    ) -> Unit

    Short alias for addEventListener

    EventTarget::removeEventListener

    #alias(remove_event_listener)
    fn EventTarget::removeEventListener(self : EventTarget, event_type : String, handler : (
    Any
    ) -> Unit, capture? : Bool) -> Unit

    Remove a previously registered event listener

    event_source_closed

    fn event_source_closed() -> Int

    EventSource ready state: CLOSED

    event_source_connecting

    fn event_source_connecting() -> Int

    EventSource ready state: CONNECTING

    event_source_open

    fn event_source_open() -> Int

    EventSource ready state: OPEN

    get_event_data

    fn get_event_data(event :
    Any
    ) -> String

    MessageEvent helper to get data JS: event.data

    get_event_origin

    fn get_event_origin(event :
    Any
    ) -> String

    MessageEvent helper to get origin JS: event.origin

    get_last_event_id

    fn get_last_event_id(event :
    Any
    ) -> String

    MessageEvent helper to get lastEventId JS: event.lastEventId

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io