[GenieFramework] Persist some actions after refreshing on web browser

This would be my solution for a global counter:

using Stipple, Stipple.ReactiveTools
using StippleUI


gen_on::Bool = false
const ranval = Observable(0.0)

function startcounter(model = nothing)
    if gen_on
        msg = "Generation is already running!"
        @info msg
        model !== nothing && notify(model, type = "warning", msg)
    else
        @async begin
            global ranval, gen_on
            gen_on = true
            msg = "Generation starts!"
            @info msg
            model !== nothing && notify(model, type = "positive", msg)
            while gen_on
                ranval[] = rand()
                sleep(1)
            end
        end
    end
end

function stopcounter(model = nothing)
    global gen_on
    if gen_on
        gen_on = false
        msg = "Generation stops!"
        @info msg
        model !== nothing && notify(model, type = "negative", msg)
    else
        msg = "Generation is not running!"
        @info msg
        model !== nothing && notify(model, "warning", msg)
    end
end

@app begin
    @in mybutton = false
    @out ranval = 0.0

    @onchange isready begin
        synchronize!(__model__.ranval, @__MODULE__().ranval, bidirectional=false)
    end

    @onchange mybutton begin
        gen_on ? stopcounter(__model__) : startcounter(__model__)
    end    
end

function ui()
    htmldiv(card(class = "q-ma-lg", style = "max-width: 300px", [
        cardsection(row(class = "justify-center", btn(col = "auto", "Generate random number!", @click("mybutton = !mybutton"))))
        cardsection(bignumber(class = "column items-center", "Random value!", :ranval))
    ]))
end

@page("/", ui, post = x->(global model = x; nothing))
1 Like