davedawkins / Sutil

Lightweight front-end framework for F# / Fable. No dependencies.
https://sutil.dev
MIT License
294 stars 18 forks source link

lazy loading/bindPromise #64

Closed alfonsogarciacaro closed 1 year ago

alfonsogarciacaro commented 1 year ago

Sorry @davedawkins, haven't checked the whole API but, is there something to bind promise/transform promises into SutilElements? I assume it shouldn't be very difficult as SutilElements are asynchronous by default. Binding promises would enable lazy loading and deferred elements (for fetching data, etc).

The helper should have 3 arguments:

davedawkins commented 1 year ago

Here's how I do it


[<AutoOpen>]
module SutilExtensions =
    open Sutil
    type JS.Promise<'T> with
        member self.ToObservable() : ObservablePromise<'T> =
            let op = ObservablePromise<'T>()
            op.Run(self)
            op

let view (server : Server) dispatchExternal (setlistId : string) =

    Bind.el(
         (findCreateSetlist server setlistId).ToObservable(),
         fun x ->
            match x with
            | Waiting ->
                Html.div "Loading..."
            | State.Error x ->
                Html.div (x.Message)
            | Result (setlist,songs) ->
                viewSetlist server dispatchExternal setlist songs
    )
davedawkins commented 1 year ago

We can make an overload in Bind.el that accepts a Promise, and does the .ToObservable() part internally

alfonsogarciacaro commented 1 year ago

This works, thank you @davedawkins!