GenieFramework / Stipple.jl

The reactive UI library for interactive data applications with pure Julia.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does @mixin only work for named models?

PGimenez opened this issue · comments

I'm trying to use the @mixing macro to extend the reactive model with fields from a struct. However, this only works when using a named model. For instance, this code will throw ERROR: UndefVarError: field not defined

using GenieFramework
@genietools

@kwdef struct mixstruct
    field::R{Bool} = false
end

@app begin
    @out var1 = 0
    @mixin mixstruct
    @onbutton field begin
        println("button clicked")
    end
end

function ui()
    btn("TEST", @click("field = true"))
end
@page("/", ui)

If I remove the @OnButton handler then the code runs fine, and if I inspect the model with @initit does indeed have the fieldfrom mixstructappended.

On the other hand, this code works with no issues

using GenieFramework
@genietools

@kwdef struct mixstruct
    field::R{Bool} = false
end

@app Model begin
    @out var1 = 0
    @mixin mixstruct
    @onbutton field begin
        println("button clicked")
    end
end

function ui()
    btn("TEST", @click("field = true"))
end

route("/") do
   model = Model |> init |> handlers
   page(model, ui())
end

I've been looking at the code for @page, @handlerand @mixinbut I don't see it. Only thing I've found is this suspicious line

sum(ex_index) == 2 && ex.args[1] != Symbol("@mixin") && insert!(ex.args, pos, :_)

It has to do with when the macro is executed. I don't have the time to look into it directly. But there is an easy workaround; just split the handlers off with the @handlersmacro:

@app Model begin
    @out var1 = 0
    @mixin mixstruct
end

@handlers begin
    @onbutton field begin
        println("button clicked")
    end
end

That doesn't solve the issue when the model is unnamed 🥹, still getting ERROR: UndefVarError: field not defined

using GenieFramework
@genietools

@kwdef struct mixstruct
    field::R{Bool} = false
end

@app begin
    @out var1 = 0
    @mixin mixstruct
end

@handlers begin
    @onbutton field begin
        println("button clicked")
    end
end


function ui()
        btn("TEST", @click("field = true"))
end

@page("/", ui)

Edit: If I paste the code in the REPL and do up()to start a server it works. However when running the code from a file with includeit fails with the fieldnot defined error.

We have programmed the @in, @out and @mixin macros with many side effects. The reason for this was that we wanted to keep the syntax for the app macro identical to that of the script style for which the macros where initially designed for.
The only place where I didn't use Core.eval() is the one that now made the difficulties.
I think that this is successfully fixed in branch hh-mixin2. Please test before merging.