Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / external / lib_core / promise.lucb

#==============================================================================================
#
#   promise - Core::Promise: a result or an error that arrives later, and the handlers that
#   hear of it
#
#   DESCRIPTION:
#       Port of Libraries/LibCore/Promise.h (lines 27-186), the LibCore seam of DESIGN.md §7.1:
#       one generic struct (types_promise) and generic functions for the template's members,
#       written at the call site with their type arguments where nothing infers them
#       (`core_promise_construct[bool, ak.AkError]()`). A promise is managed memory, as a
#       RefPtr target is (DESIGN.md §3.3), so whatever holds it keeps it alive.
#
#       The handlers are AK Functions that answer nothing: the C++ ErrorOr-answering form of
#       when_resolved, whose error would reach on_rejection, has no caller that fails, so a
#       handler's failure path is not ported. Promise::after and Promise::map, templates no
#       code of the port instantiates, are not ported; add_child (which `after` uses) is.
#
#       Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
#       Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
#       Copyright (c) 2021-2023, Ali Mohammad Pur <mpfard@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibCore/Promise.h:33 Core::Promise::construct.
pub func core_promise_construct[TResult, TTError]() -> CorePromise[TResult, TTError]*:
    # adopt_ref(*new Promise())
    return new CorePromise[TResult, TTError]() else trap("out of memory")

## Ported from Libraries/LibCore/Promise.h:84 Core::Promise::resolve.
pub func core_promise_resolve[TResult, TTError](this: CorePromise[TResult, TTError]*, result: TResult):
    this.m_result_or_rejection = CorePromiseResultOrError[TResult, TTError].value(value = result)

    if ak.function1_is_set(&this.on_resolution):
        # on_resolution(m_result_or_rejection->value()): the handler gets the stored result.
        var value = result
        ak.function1_call(&this.on_resolution, &value)

## Ported from Libraries/LibCore/Promise.h:96 Core::Promise::reject.
pub func core_promise_reject[TResult, TTError](this: CorePromise[TResult, TTError]*, error_: TTError):
    this.m_result_or_rejection = CorePromiseResultOrError[TResult, TTError].error_(error_ = error_)
    core_promise_possibly_handle_rejection(this)

## Ported from Libraries/LibCore/Promise.h:103 Core::Promise::is_rejected.
pub func core_promise_is_rejected[TResult, TTError](this: const CorePromise[TResult, TTError]*) -> bool:
    let result_or_rejection = this.m_result_or_rejection else return false
    return match result_or_rejection:
        .value(_) => false
        .error_(_) => true

## Ported from Libraries/LibCore/Promise.h:108 Core::Promise::is_resolved.
pub func core_promise_is_resolved[TResult, TTError](this: const CorePromise[TResult, TTError]*) -> bool:
    let result_or_rejection = this.m_result_or_rejection else return false
    return match result_or_rejection:
        .value(_) => true
        .error_(_) => false

## Ported from Libraries/LibCore/Promise.h:113 Core::Promise::await: pumps the current event loop
## until the promise settles, then answers its result or error.
pub func core_promise_await[TResult, TTError](this: CorePromise[TResult, TTError]*) -> CorePromiseResultOrError[TResult, TTError]:
    while this.m_result_or_rejection == none:
        var event_loop = core_event_loop_current()
        _ = event_loop.pump(CoreEventLoopPumpMode.wait_for_events)

    # m_result_or_rejection.release_value()
    let result_or_rejection = this.m_result_or_rejection else trap("VERIFY_NOT_REACHED")
    this.m_result_or_rejection = none
    return result_or_rejection

## Ported from Libraries/LibCore/Promise.h:146 Core::Promise::when_resolved (the handler that
## answers ErrorOr<void>; the C++ overload for a void handler wraps its handler in one).
pub func core_promise_when_resolved[TResult, TTError](this: CorePromise[TResult, TTError]*, handler: ak.Function1[TResult*, unit]) -> CorePromise[TResult, TTError]*:
    this.on_resolution = handler
    if core_promise_is_resolved(this):
        let result_or_rejection = this.m_result_or_rejection else trap("VERIFY_NOT_REACHED")
        match result_or_rejection:
            .value(result):
                var value = result
                ak.function1_call(&this.on_resolution, &value)
            .error_(_): trap("VERIFY_NOT_REACHED")

    return this

## Ported from Libraries/LibCore/Promise.h:158 Core::Promise::when_rejected.
pub func core_promise_when_rejected[TResult, TTError](this: CorePromise[TResult, TTError]*, handler: ak.Function1[TTError*, unit]) -> CorePromise[TResult, TTError]*:
    this.on_rejection = handler
    if core_promise_is_rejected(this):
        let result_or_rejection = this.m_result_or_rejection else trap("VERIFY_NOT_REACHED")
        match result_or_rejection:
            .value(_): trap("VERIFY_NOT_REACHED")
            .error_(failure):
                var error_ = failure
                ak.function1_call(&this.on_rejection, &error_)

    return this

## Ported from Libraries/LibCore/Promise.h:168 Core::Promise::add_child.
pub func core_promise_add_child[TResult, TTError](this: CorePromise[TResult, TTError]*, child: void*):
    ak.vector_append_t(&this.m_children, child)

## Ported from Libraries/LibCore/Promise.h:171 Core::Promise::possibly_handle_rejection: the
## rejection handler hears the error the promise holds, if it holds one.
func core_promise_possibly_handle_rejection[TResult, TTError](this: CorePromise[TResult, TTError]*):
    let result_or_rejection = this.m_result_or_rejection else return
    match result_or_rejection:
        .value(_): return
        .error_(failure):
            if ak.function1_is_set(&this.on_rejection):
                var error_ = failure
                ak.function1_call(&this.on_rejection, &error_)