proton_client

    Typed asynchronous frontend client for Proton applications.

    proton
    client
    ipc
    Download zip
    Version
    0.2.10
    License
    Apache-2.0
    Last updated
    2 hours ago
    Downloads
    147

    #Proton Client

    moonbit-community/proton_client invokes typed Proton contracts from the active renderer page. It uses the bridge installed by Proton before application scripts run; the default client needs no configuration.

    ///|
    async fn load_todos {
    let snapshot = @proton_client.invoke(@shared.list_todos, {})
    render(snapshot)
    }

    Requests are encoded and responses are decoded at the client boundary. Transport, timeout, cancellation, remote execution, and decoding failures are raised as ClientFailure.

    #Isolated clients and cancellation

    Construct a Client with callback-based JSON invocation and listener functions for browser previews or deterministic tests. Each instance is independent; tests do not need to replace the global desktop bridge. Use client.invoke, client.invoke_with_callbacks, and client.subscribe with the same shared descriptors as the desktop convenience functions.

    The cancellation function returned by invoke_with_callbacks is idempotent and suppresses late success/failure callbacks even if the transport ignores abort. Async invocation requires a moonbitlang/async task context; adapters whose runtime owns scheduling should use invoke_with_callbacks. Async task cancellation also cancels the pending invocation. Cancellation ends observation and requests backend cancellation; it never promises to undo a completed write. Closing a subscription suppresses queued notifications. Malformed events report EventDecode without removing the listener.

    Expected business failures belong in the response enum, not in string matching against ClientFailure. Events are live notifications with no replay or delivery acknowledgement. State consumers should subscribe before querying and reject outdated query results.

    ClientFailure

    pub(all) suberror ClientFailure {
    BridgeUnavailable
    RequestCancelled
    InvalidContract(message~ : String)
    RemoteFailure(code~ : String, message~ : String, detail~ : String?)
    TransportFailure(message~ : String)
    ResponseDecode(message~ : String)
    EventDecode(message~ : String)
    } derive(Eq,
    Debug
    )

    Failures raised by the typed frontend client.

    ClientFailure::equal

    ClientFailure::message

    fn ClientFailure::message(self : ClientFailure) -> String

    Returns a user-facing description of this client failure.

    ClientFailure::not_equal

    fn ClientFailure::not_equal(x : ClientFailure, y : ClientFailure) -> Bool

    ClientFailure::output

    fn ClientFailure::output(self : ClientFailure, logger : &Logger) -> Unit

    ClientFailure::to_string

    fn ClientFailure::to_string(self : ClientFailure) -> String

    Client

    pub struct Client {
    start_request : (String, String, (String) -> Unit, (ClientFailure) -> Unit) -> (() -> Unit)
    start_listener : (String, (String) -> Unit) -> (() -> Unit) raise ClientFailure
    }

    A client boundary independent of the active renderer. Implementations receive JSON text and must return idempotent cancellation/unsubscription functions. Cancelling stops observation; it does not roll back backend side effects.

    Client::Client

    fn Client::Client(invoke~ : (String, String, (String) -> Unit, (ClientFailure) -> Unit) -> (() -> Unit), subscribe~ : (String, (String) -> Unit) -> (() -> Unit) raise ClientFailure) -> Client

    Creates an isolated client, for example for browser previews and tests.

    Client::invoke

    async fn[Request : ToJson, Response :
    FromJson
    ] Client::invoke(self : Client, command :
    Command
    [Request, Response], request : Request) -> Response raise ClientFailure

    Invokes a typed command using this client's boundary. Task cancellation cancels the pending transport request.

    Client::invoke_with_callbacks

    fn[Request : ToJson, Response :
    FromJson
    ] Client::invoke_with_callbacks(self : Client, command :
    Command
    [Request, Response], request : Request, success : (Response) -> Unit, failure : (ClientFailure) -> Unit) -> (() -> Unit)

    Starts a typed request without entering an async scheduler. Late responses after cancellation and repeated completions are ignored.

    Client::subscribe

    fn[Payload :
    FromJson
    ] Client::subscribe(self : Client, event :
    Event
    [Payload], received : (Payload) -> Unit, failure : (ClientFailure) -> Unit) -> Subscription raise ClientFailure

    Installs an independent listener. Closing it also suppresses queued events.

    Subscription

    pub struct Subscription {
    closed : Bool
    close_listener : () -> Unit
    }

    A live event subscription installed on the active renderer page.

    Subscription::close

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

    Removes this subscription. Repeated calls have no effect.

    desktop

    let desktop : Client

    The default client resolves Proton's bridge when an operation starts.

    invoke

    async fn[Request : ToJson, Response :
    FromJson
    ] invoke(command :
    Command
    [Request, Response], request : Request) -> Response raise ClientFailure

    Invokes a typed command through the bridge installed on the active page.

    invoke_with_callbacks

    fn[Request : ToJson, Response :
    FromJson
    ] invoke_with_callbacks(command :
    Command
    [Request, Response], request : Request, success : (Response) -> Unit, failure : (ClientFailure) -> Unit) -> (() -> Unit)

    Starts a typed request on the desktop client. The returned function cancels observation and requests transport cancellation exactly once.

    subscribe

    fn[Payload :
    FromJson
    ] subscribe(event :
    Event
    [Payload], received : (Payload) -> Unit, failure : (ClientFailure) -> Unit) -> Subscription raise ClientFailure

    Subscribes to a typed live event on the active renderer page.

    A malformed payload reports EventDecode through failure without removing the subscription.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io