JuliaGizmos / Escher.jl

Composable Web UIs in Julia
https://juliagizmos.github.io/Escher.jl
Other
337 stars 63 forks source link

Should nested map(...) do work? #190

Open izaid opened 8 years ago

izaid commented 8 years ago

Okay, so I have a question about how to modularize signals. Consider the following code:

import Escher

main(window) =  begin
    push!(window.assets,"widgets")
    push!(window.assets, "icons")
    push!(window.assets, "layout2")
    push!(window.assets, "animation")

    sl = Signal(1)
    connected_slider = subscribe(sl, slider(1:10))

    ch = Signal(false)
    connected_checkbox = subscribe(ch, checkbox("Checked?"))

    map(ch, sl) do b, n
        hbox(vbox(connected_checkbox, connected_slider), "Checkbox: $b, Slider: $n")
    end
end

This simply shows the status of a slider and a checkbox, and it works fine. Now what about the following:

import Escher

main(window) =  begin
    push!(window.assets,"widgets")
    push!(window.assets, "icons")
    push!(window.assets, "layout2")
    push!(window.assets, "animation")

    sl = Signal(1)
    connected_slider = subscribe(sl, slider(1:10))

    ch = Signal(false)
    connected_checkbox = subscribe(ch, checkbox("Checked?"))

    map(ch) do b
        map(sl) do n
            hbox(vbox(connected_checkbox, connected_slider), "Checkbox: $b, Slider: $n")
        end
    end
end

I'd imagine this would do the same thing, but instead I get weird effects like duplication of all the widgets. Is this supposed to work? If not, how can I separate the ch and sl signals?

Of course, this is a toy example -- in a real example, I want to do some complicated computation involving the value from one of the signals, or I simply want to modularize one of the signals in a function so that users can call that function but not need to put it in the map.

izaid commented 8 years ago

Okay, so my understanding from talking to @shashi is that this should work. I've been debugging it a bit, and the problem seems to lie with this line https://github.com/shashi/Escher.jl/blob/master/src/cli/serve.jl#L119 being used wrongly when there are nested signals -- I can see differences in the diffs there. No fix yet.

izaid commented 8 years ago

Partial progress: That line is indeed the problem. It is diffing against Escher.empty at each recursion, whereas it should diff against the last rendering. I've fixed that up with a global dict. This prevents the duplication of widgets, but it leaves the signals not functioning. :/

shashi commented 7 years ago

I have made the fix where it won't create a new slider. But for some reason, when things get redrawn the watch-states listeners do not get set up. So when the checkbox is checked for the first time, the slider stops working

izaid commented 7 years ago

Any idea why that is?