I’m trying to update an image in an asynchronously loop in a Genie (Stipple) app. I’ve tried different approaches, none successful. I’ve therefore created the following MWE (based on your solution to my last question, @hhaensel):
# ] add Genie, StippleUI#master, Stipple
using Stipple, StippleUI
using Genie.Renderer.Html
const IMGPATH = "img/demo.png" # make sure there is an image in public/img/demo.png
Base.@kwdef mutable struct Dashboard1 <: ReactiveModel
imageurl::R{String} = IMGPATH
restart::R{Bool} = false # restart the updating of the images
end
keep_updating = Ref(true) # a container for stopping the previous task
function restart()
global model
cd(@__DIR__)
model = Stipple.init(Dashboard1(), debounce=1)
on(model.restart) do x
keep_updating[] = false # this stops the previous task
sleep(2) # wait for enough time so that the previous task can iterate at least once to register the difference in the value of `keep_updating`
keep_updating[] = true # prepare for the next task
@async while keep_updating[] # start a new task for updating the image url
model.imageurl[] = "/$IMGPATH#$(Base.time())"
sleep(1) # sleep to allow the image to load at the client's end
end
end
end
function ui()
dashboard(vm(model), [
heading("Image Demo"),
row(cell(class="st-module", [
btn("New Image", "", @on("click", "restart=true")),
quasar(:img, src=:imageurl, spinner__color="white", style="height: 140px; max-width: 150px")
]))
], title = "Image Demo") |> html
end
route("/", ui)
Genie.config.server_host = "127.0.0.1"
restart()
up(open_browser = true)
But this (and many of my other attempts) does not work and just report that
┌ Error: Base.IOError("stream is closed or unusable", 0)
└ @ Genie.WebChannels ~/.julia/packages/Genie/SN5kE/src/WebChannels.jl:223
So I’m obviously doing something wrong.
I feel there is an important distinction to be made here:
Normal UI updates the state of the webpage (e.g. update an image) as a result of an action by the user (e.g. pressing a button). What I’m trying to achieve is a loop of updates (albeit, that starts from pressing some “start” button), each update event (iteration) elicited by the server, not the user. Any ideas on what the Genie/Stipple way of achieving that is?