moonbitlang/core/async/js_async does not have a README file

    JsError

    #internal(unimplemented, "unimplemented in native backend")
    type JsError

    A JavaScript exception wrapped in MoonBit error
    impl Show for JsError
    impl Debug for JsError

    JsError::to_string

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

    Default implementation for Show::to_string, uses a StringBuilder

    AbortController

    #internal(unimplemented, "unimplemented in native backend")
    #external
    pub type AbortController

    A JavaScript AbortController used to cancel promises

    AbortController::AbortController

    #internal(unimplemented, "unimplemented in native backend")
    #alias(new, deprecated="Use `AbortController()` instead")
    fn AbortController::AbortController() -> AbortController

    Create a new abort controller

    AbortController::abort

    #internal(unimplemented, "unimplemented in native backend")
    fn AbortController::abort(self : AbortController) -> Unit

    Abort the abort controller and activate its abort signal. All promises tied to the signal will be cancelled.

    AbortController::signal

    #internal(unimplemented, "unimplemented in native backend")
    fn AbortController::signal(self : AbortController) -> AbortSignal

    Get the abort signal of an abort controller. The abort signal will be triggered when .abort() is called on the controller.

    AbortSignal

    #internal(unimplemented, "unimplemented in native backend")
    #external
    pub type AbortSignal

    A JavaScript AbortSignal used to cancel promises

    JsReadableStream

    #internal(unimplemented, "unimplemented in native backend")
    type JsReadableStream

    The ReadableStream type in Web API, see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream for more details.

    Use this type on JavaScript FFI boundary to interact with API that make use of ReadableStream, such as fetch.

    JsReadableStream::new_pipe

    #internal(unimplemented, "unimplemented in native backend")
    fn JsReadableStream::new_pipe() -> (JsReadableStream,
    MemoryPipeWrite
    )

    Create an anonymous pipe and wrap the read end into a JsReadableStream. This allows writing data from MoonBit code to JavaScript. Usually users should pass the read end of the pipe (JsReadableStream) to JavaScript code, and write to the write end of the pipe (@io.MemoryPipeWrite) from MoonBit code.

    Closing the write end of the pipe will signal EOF on the read end. If the read end of the stream is cancelled from the JavaScript side, further write operations on the write end will fail with error.

    Promise

    #internal(unimplemented, "unimplemented in native backend")
    #external
    pub type Promise[X]

    A JavaScript promise that resolves to a value of type X

    Promise::from_async

    #internal(unimplemented, "unimplemented in native backend")
    fn[X] Promise::from_async(f : async () -> X, abort_signal? : AbortSignal) -> Promise[X]

    Convert a MoonBit async function into a JavaScript promise. Promise::from_async(f) returns a promise that:

    • resolve to the result of f when f return
    • reject with the error that f raise if f fail

    If abort_signal is present, f will be automatically cancelled when the signal is activated, and the returned promise will reject with JavaScript AbortError.

    The async function f will be run in a global context, so there is no structured concurrency support for Promise::from_async, and this function should only be used for exporting MoonBit code to JavaScript.

    It is undefined whether f will actually start running immediately, or start at the next JavaScript event loop.

    Promise::wait

    #internal(unimplemented, "unimplemented in native backend")
    async fn[X] Promise::wait(promise : Promise[X], abort_controller? : AbortController) -> X

    Wait for a JavaScript promise to resolve and return the fulfilled value, or raise an error if the promise is rejected.

    If abort_controller is provided, it should be an abort controller that controls the promise, and .abort() will be called on the controller automatically when wait is cancelled, cancelling the promise automatically.

    If abort_controller is absent, wait is non-cancellable.

    ReadableStream

    #internal(unimplemented, "unimplemented in native backend")
    type ReadableStream

    A wrapper around JsReadableStream that allows reading the content via the @io.BinaryReader interface.

    This type SHOULD NOT be used on FFI directly: its ABI is NOT the same as JsReadableStream, and most JavaScript types won't understand it.

    ReadableStream::close

    #internal(unimplemented, "unimplemented in native backend")
    fn ReadableStream::close(self : ReadableStream) -> Unit

    Close a ReadableStream. The underlying JS stream will be cancelled.

    ReadableStream::drop_all

    Drop all remaining content from a binary reader, until EOF is reached.

    ReadableStream::drop_exactly

    async fn ReadableStream::drop_exactly(self : ReadableStream, len : Int) -> Unit raise
    ReaderError

    reader.drop_exactly(len) drops exactly len bytes from the reader. drop_exactly waits until enough bytes are available. If EOF is reached before enough data is dropped, UnexpectedEof will be raised.

    ReadableStream::from_js

    #internal(unimplemented, "unimplemented in native backend")
    fn ReadableStream::from_js(stream : JsReadableStream) -> ReadableStream

    Create a ReadableStream wrapper from a JsReadableStream object, in order to read the content of the stream from the MoonBit side. The ownership of the JS stream will be transferred to the ReadableStream: other code cannot read the stream anymore.

    ReadableStream::peek

    reader.peek() fetches and returns a chunk of data from the reader. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.peek() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek() is cancelled, no data will be lost.

    ReadableStream::peek_at_least

    async fn ReadableStream::peek_at_least(self : ReadableStream, min_len : Int) -> BytesView raise
    ReaderError

    reader.peek_at_least(min_len) fetches and returns a chunk containing at least min_len bytes without consuming it. The returned chunk may be larger than min_len.

    The returned view is valid only until another operation is invoked on the same reader. Use .to_owned() to keep the data persistently.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched by a later reader operation.

    reader.peek_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    ReadableStream::peek_exactly

    async fn ReadableStream::peek_exactly(self : ReadableStream, len : Int) -> BytesView raise
    ReaderError

    reader.peek_exactly(len) fetches exactly len bytes from the reader. It will wait until enough number of bytes is available. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_exactly() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If EOF is reached before enough data is read, UnexpectedEof will be raised. In this case remaining data remain buffered, and can be fetched by other reader operations later.

    reader.peek_exactly() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_exactly() is cancelled, no data will be lost.

    ReadableStream::peek_until

    async fn ReadableStream::peek_until(self : ReadableStream, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise
    ReaderError

    reader.peek_until(pat, include_delimiter? : Bool = false) fetches content from reader until the byte string pat is found in the reader stream. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_until() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If pat is found in the input stream, content before that occurrence is returned. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.peek_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.peek_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_until() is cancelled, no data will be lost.

    ReadableStream::read

    reader.read() fetches and returns a chunk of data from the reader. It will return as fast as possible when new data become available.

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.read() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read() is cancelled, no data will be lost.

    ReadableStream::read_all

    Read all remaining content from a binary reader, until EOF is reached.

    ReadableStream::read_at_least

    async fn ReadableStream::read_at_least(self : ReadableStream, min_len : Int) -> Bytes raise
    ReaderError

    reader.read_at_least(min_len) fetches and returns an owned chunk containing at least min_len bytes. The returned chunk may be larger than min_len.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched through another reader operation later.

    reader.read_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    ReadableStream::read_exactly

    async fn ReadableStream::read_exactly(self : ReadableStream, len : Int) -> Bytes raise
    ReaderError

    reader.read_exactly(len) reads exactly len bytes from the reader. It will wait until enough number of bytes is available. If EOF is reached before enough data is read, UnexpectedEof will be raised.

    ReadableStream::read_into

    reader.read_into(dst) reads at most dst.length() bytes from the reader into dst. The number of bytes actually read is returned. Note that the return value may be smaller than dst.length() if not enough data is immediately available. If EOF is reached and no more data is available, zero will be returned.

    ReadableStream::read_until

    async fn ReadableStream::read_until(self : ReadableStream, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise
    ReaderError

    reader.read_until(pat, include_delimiter? : Bool = false) reads content from reader until the byte string pat is found in the reader stream.

    If pat is found in the input stream, content before that occurrence is returned and the delimiter is consumed. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.read_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.read_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read_until() is cancelled, no data will be lost.

    run_promise

    #internal(unimplemented, "unimplemented in native backend")
    async fn[X] run_promise(f : (AbortSignal) -> Promise[X]) -> X

    Convenient helper for calling async JavaScript code from MoonBit. run_promise(f) create a fresh abort signal, passed it to f, and wait for the promise that f returns. If f resolve to a value, run_promise return that value. If f is rejected with an error, run_promise raise that error. If run_promise is cancelled, the abort signal passed to f is activated, and the promise returned by f should be cancelled automatically.

    run_promise should only be used for cancellable JavaScript code (i.e. the promised returned by f should properly handle the passed-in abort signal). For non-cancellable JavaScript code, use Promise::wait() directly.

    Source Files