lifetime_js

    Cancellable callback and resource handoff adapters for MoonBit's official JS async runtime

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    7 hours ago
    Downloads
    10

    Dependencies

    #Hosi121/lifetime_js

    Typed callback and resource handoff adapters for moonbitlang/async on JS. Uses Hosi121/lifetime for immediate synchronous cleanup. No separate scheduler, dynamic value type, unchecked generic cast, or npm runtime dependency.

    #API

    • wait(life, register, discard?) turns a one-shot callback into a cancellable async operation. register receives (Result[T, PortError]) -> Unit and returns a synchronous unregister callback. It may complete synchronously.
    • acquire(life, register, release~) uses the same release function for both discarded values and values adopted by the lifetime. Ownership is registered before returning, with no intervening async suspension.
    • run(life, task, failed) enters official async from a synchronous host callback. task receives TaskGroup[Unit]; its children stay in that group. Root completion or failure closes life. Failure is reported after cleanup; cancellation is observed without an unhandled Promise rejection.
    • guard_sync(fn) converts a synchronous host exception to checked PortError. Async callback exceptions need their own boundary conversion.
    • check(life) raises Stopped when closed. This is distinct from official async's cancellation signal.
    • Inbox[T](life, limit~) is bounded ingress from JS callbacks to one async reader. offer returns false when full or closed; next waits via the official Promise bridge and raises ConcurrentRead on overlapping reads. limit must be positive (invalid programmer input aborts). Queued values must not require disposal; closing drops them, and rejected values remain with the producer.

    #Ownership

    Successful callbacks transfer fresh values. Calling success again with the same owned handle is invalid: duplicate success values go to discard/release. Only one result reaches the caller. Failure results after completion are ignored. The host unregister callback runs once on completion, failure or cancellation. If registration throws before returning a callback, it must clean up any partial registration itself; the adapter cannot discover that resource.

    An unabortable operation may still finish after close; the adapter stops waiting and disposes its eventual successful value. A value ready before close but not yet delivered is also disposed. If unregister reentrantly closes the parent, the value is disposed instead of returned. wait callers own delivered values and must register cleanup before their next suspension. Prefer acquire for resources; wait is convenient for data.

    Cleanup, discard and failed callbacks must not throw. Lifetime close releases registered resources synchronously and requests root cancellation; it does not wait for asynchronous finalizers. Use protected official async cleanup for operations such as a database rollback. Do not wrap arbitrary throwing host I/O in an async task and expect JS exceptions to become MoonBit checked errors.

    T and Result remain in MoonBit. The FFI Promise carries only Unit to wake official async, avoiding exposure of MoonBit's generic representation to JS.

    For normal abortable Promise APIs, first consider official js_async.run_promise. For lexical cleanup, use defer / errdefer. This adapter addresses host-driven lifetime closure and resource delivery races beyond those simple cases.

    JS only, tested with official async 0.22.1 and MoonBit 0.10.14. The synchronous Hosi121/lifetime module is separately usable on native and Wasm GC.

    Runnable image preview and design. Apache-2.0; see LICENSE and NOTICE in this package.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io