-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing reactive variable value from a loop running in a handler #300
Comments
I suppose this happens because The |
Yes, we need to do another iteration for the reactive macros. There are a few issues: To address use cases like
|
I understand where this comes from. The syntax of the on(x) do value_of_x
println(value_of_x)
end I chose to use that syntax and use the original names for the function part, so that I use the values instead of their full references in order to save the lookup time. EDIT: Most probably the lookup times are small versus typical computational tasks. |
Just tested. There's no significant difference in performance, let's always replace then ... |
Found out that with explicit models local variables already work, when they are written as references: julia> @app MyApp begin
@in x = 1
@in y = 2.0 + x[]
end
(MyApp, handlers)
julia> MyApp()
Instance of 'MyApp'
channel__ (internal): EVLJILCQALUITGSRUBMWAAOMJNNOPDIO
modes__ (internal): LittleDict{Symbol, Int64, Vector{Symbol}, Vector{Int64}}()
isready (autofield, in): false
isprocessing (autofield, in): false
fileuploads (autofield, in): Dict{AbstractString, AbstractString}()
ws_disconnected (autofield, in): false
x (in): 1
y (in): 3.0 |
So with a little trick you can also use that already for implicit models: @app Main_ReactiveModel begin
@in x = 1
@in y = 2.0 + x[]
end __GF_AUTO_HANDLERS__
RT.TYPES[@__MODULE__] = Main_ReactiveModel julia> @init
Instance of 'Main_ReactiveModel'
channel__ (internal): JCRJFVAVLCQINPCCFGHTMVBMXWHANYSA
modes__ (internal): LittleDict{Symbol, Int64, Vector{Symbol}, Vector{Int64}}()
isready (autofield, in): false
isprocessing (autofield, in): false
fileuploads (autofield, in): Dict{AbstractString, AbstractString}()
ws_disconnected (autofield, in): false
x (in): 1
y (in): 3.0 Actually, I think, it's a good idea to use that reference syntax to include model variables in the definition, because otherwise you might not be aware that the local definition is shadowing the module's definition. julia> x = 11
11
julia> @app Main_ReactiveModel begin
@in x = 1
@in y = 2.0 + Main.x
end __GF_AUTO_HANDLERS__
(Main_ReactiveModel, __GF_AUTO_HANDLERS__)
julia> @init
Instance of 'Main_ReactiveModel'
channel__ (internal): ZBLOSXJIPQKKZILUZRVPBAOCSCGIDDGU
modes__ (internal): LittleDict{Symbol, Int64, Vector{Symbol}, Vector{Int64}}()
isready (autofield, in): false
isprocessing (autofield, in): false
fileuploads (autofield, in): Dict{AbstractString, AbstractString}()
ws_disconnected (autofield, in): false
x (in): 1
y (in): 13.0 Unfortunately, neither |
This definition would immediately allow for variable references in implicit models: macro app2(expr)
modelname = Symbol(Stipple.ReactiveTools.model_typename(__module__))
quote
Stipple.ReactiveTools.TYPES[$__module__] = Stipple.@type
Stipple.ReactiveTools.@app $modelname $expr __GF_AUTO_HANDLERS__
end |> esc
end e.g. julia> Stipple.ReactiveTools.@app2 begin
@in x = 1
@in y = 2.0 + x[]
end
(Main_ReactiveModel, __GF_AUTO_HANDLERS__)
julia> @init
Instance of 'Main_ReactiveModel'
channel__ (internal): VKOFFIBIWOYUJPZIKMFNWOUCLIGRZXNV
modes__ (internal): LittleDict{Symbol, Int64, Vector{Symbol}, Vector{Int64}}()
isready (autofield, in): false
isprocessing (autofield, in): false
fileuploads (autofield, in): Dict{AbstractString, AbstractString}()
ws_disconnected (autofield, in): false
x (in): 1
y (in): 3.0 |
It also holds when the app has been renamed with |
We could redefine |
REACTIVE_STORAGE is "private" I would say... |
I have a functional version that builds the code without REACTIVE_STORAGE and which is much cleaner |
OK, we can merge if it works |
Needs some refinement, but will get it going the next days. |
Got it all working. The macros generate code that can be viewed by Shall we remove the old functionality then? That would mean removing all macro definitions |
However is interested in trying out can use the branch hh-appmacros, which is a larger refactoring of the |
Suppose we have a handler, and a loop conditioned on the handler's reactive variable like this one:
This loop will never break when
toggle
is set to false via an element in the UI or from another handler. To break the loop, you need to access the observable directly at the reactive model asMoreover, the updated values can be accessed from another handler. Here's a full MWE and a video:
toggle_A
will always run.toggle_B
stops when the variable is false.print_value
correctly prints the current values fortoggle_A
andtoggle_B
at any time.CleanShot.2024-11-14.at.18.48.20.mp4
The text was updated successfully, but these errors were encountered: