SimonDanisch / Bonito.jl

Serving JS to the browser
MIT License
204 stars 29 forks source link

Problem with promises and onjs #223

Closed tuncbkose closed 4 months ago

tuncbkose commented 4 months ago

In trying to get the second example here to work in a static page, I'm encountering a problem.

I want to make an interactive figure using WGLMakie, but whenever I include a promise in a function I give to onjs the figure ends up not being rendered, and I get errors in the browser console (in both Firefox and Chrome): image

The promises seem to work fine in on_document_load, so I wonder if this has something to do with things not being loaded yet, but I expect such a problem would be apparent when making the linked example too.

Example code I'm having problem with (simplified from the linked example)

using Bonito, WGLMakie
Page(exportable=true, offline=true)

App() do session::Session
    s1 = Slider(1:100)
    slider_val = DOM.p(s1[])
    fig, ax, splot = scatter(1:4)

    on_document_load(session, js"""
        $(splot).then(plots=>{
            const scatter_plot = plots[0]
            console.log(scatter_plot)
        })
    """)

    onjs(session, s1.value, js"""function on_update(new_value) {
        $(splot).then(plots=>{
            const scatter_plot = plots[0]
            scatter_plot.geometry.attributes.pos.array[0] = (new_value/100) * 4
            scatter_plot.geometry.attributes.pos.array[1] = (new_value/100) * 4
            scatter_plot.geometry.attributes.pos.needsUpdate = true
        })
    }
    """)
    return DOM.div(s1, fig)
end

In the above, if I remove the onjs function, everything works as expected.

SimonDanisch commented 4 months ago

Should be fixed by #224. Btw, I'd rather use: export_static("index.html", app) instead of Page(exportable=true, offline=true), which is a bit fragile.

tuncbkose commented 4 months ago

Thank you for the quick response!

As for the export_static advice, my impression is that using this would give me HTML files themselves, whereas I'd like to embed these plots in other things. Similar to the WGLMakie documentation but using a different tool. So is there a right way to use export_static I'm not aware of, or would Page be fine in this case?