Genie Discord forum

Author Avataryakir12
8/25/2023, 7:54:07 AM

Is there a way to Observable.connect! a variable from an @app body to an Observable?

Something like this:

using Observables, MyModule

@app begin
    @in variable = 0
end

connect!(MyModule.some_observable, variable)
Author AvatarPere
8/25/2023, 8:34:41 AM

This is interesting, have you tried doing this inside a handler? Something like

@onchange isready begin connect!(observable, variable) end

Author AvatarPere
8/25/2023, 8:39:32 AM

Just to give some insight, we use Observable's onto attach callbacks to a variable. The @app macro generates a handlersfunction that attaches the callbacks.

julia> @macroexpand @app Model begin @out i = 10; @onchange i begin; print(i); end end
quote
    #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:585 =#
    begin
        #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:659 =#
        Stipple.ReactiveTools.delete_events(Model)
        #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:661 =#
        function handlers(__model__)
            #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:661 =#
            #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:662 =#
            #= REPL[57]:1 =#
            begin
                #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:842 =#
                on(__model__.i) do i
                    #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:843 =#
                    #= REPL[57]:1 =#
                    print(i)
                end
            end
            #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:664 =#
            __model__
        end
        #= /Users/pere/.julia/packages/Stipple/a9IBQ/src/ReactiveTools.jl:666 =#
        (Model, handlers)
    end
end

This handlersfunction is called at a later stage when the page's model is instantiated

Author Avataryakir12
8/25/2023, 8:43:18 AM

but would isready be updated only once? Because the connect!ion needs to happen only once..

Author AvatarPere
8/25/2023, 8:44:38 AM

yes, isready comes by default and changes to true when the page is finished loading. It'll be updated on page reload only

Author Avataryakir12
8/25/2023, 8:47:56 AM

ok!!! awesome. trying it

Author Avataryakir12
8/25/2023, 8:58:24 AM

nah, the observable variable is stripped off inside the scope of @onchange. So in my case it's no longer Observable{Bool} it's just Bool (e.g. false). Then the connect! fails because it connects two observables.

Author AvatarPere
8/25/2023, 9:06:32 AM

I see, that is right. I guess then we'd have to add the connect! call to the list of functions ( like on) executed when instantiating the model with handlers...I don't know how to do that yet, but I'm currently studying the Stipple source so I might be able to help soon

Author AvatarPere
8/25/2023, 9:07:10 AM

If this is something you need, please open an issue on the Stipple.jl repo with a MWE and we'll look into it