Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async shorthands #283

Open
granicz opened this issue Apr 16, 2024 · 0 comments
Open

Add async shorthands #283

granicz opened this issue Apr 16, 2024 · 0 comments

Comments

@granicz
Copy link
Member

granicz commented Apr 16, 2024

I propose to add the following shorthands to make working with reactive vars/views easier (which are best handled asynchronously, instead of reading their values directly):

  1. Var.GetAsync, as a counterpart to View.GetAsync:

    type Var<'T> with
        member this.GetAsync =
            this.View |> View.GetAsync
  2. Allowing Var<'T> and View<'T> to participate in async let! expressions:

    [<AutoOpen;JavaScript>]
    module Extensions =
        type AsyncBuilder with
            member this.Bind(var: Var<'T>, continuation: 'T -> Async<'U>) : Async<'U> =
                var.View |> View.GetAsync |> fun res -> async.Bind(res, continuation)
            member this.Bind(view: View<'T>, continuation: 'T -> Async<'U>) : Async<'U> =
                view |> View.GetAsync |> fun res -> async.Bind(res, continuation)

The latter would be especially useful, and would allow the following:

MainTemplate.ContactForm()
    .OnSend(fun e ->
        async {
            // Instead of reading e.Vars.Name.View |> View.GetAsync,
            // a much nicer way would be:
            let! name = e.Vars.Name
            // ... and same shorthand for views:
            let! email = e.Vars.Email.View
...
@Jooseppi12 Jooseppi12 self-assigned this Apr 16, 2024
Jooseppi12 added a commit that referenced this issue Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: v7.0
Development

No branches or pull requests

2 participants