Skip to content

Commit

Permalink
fix precompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaensel committed Dec 13, 2024
1 parent 533b3b5 commit cdfbe37
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
39 changes: 19 additions & 20 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function init(t::Type{M};
# add a timer that checks if the model is outdated and if so prepare the model to be garbage collected
LAST_ACTIVITY[Symbol(getchannel(model))] = now()

# PRECOMPILE[] || Timer(setup_purge_checker(model), PURGE_CHECK_DELAY[], interval = PURGE_CHECK_DELAY[])
PRECOMPILE[] || Timer(setup_purge_checker(model), PURGE_CHECK_DELAY[], interval = PURGE_CHECK_DELAY[])

# register channels and routes only if within a request
if haskey(Genie.Router.params(), :CHANNEL) || haskey(Genie.Router.params(), :ROUTE) || always_register_channels
Expand Down Expand Up @@ -1274,27 +1274,26 @@ using Stipple.ReactiveTools

# precompilation ...

# using Stipple.ReactiveTools
# @stipple_precompile begin
# ui() = [cell("hello"), row("world"), htmldiv("Hello World")]
@app PrecompileApp begin
@in demo_i = 1
@out demo_s = "Hi"

# @app PrecompileApp begin
# @in demo_i = 1
# @out demo_s = "Hi"

# @onchange demo_i begin
# println(demo_i)
# end
# end
@onchange demo_i begin
println(demo_i)
end
end

# route("/") do
# model = Stipple.ReactiveTools.@init PrecompileApp
# page(model, ui) |> html
# end
using Stipple.ReactiveTools
@stipple_precompile begin
ui() = [cell("hello"), row("world"), htmldiv("Hello World")]

# precompile_get("/")
# deps_routes(core_theme = true)
# precompile_get(Genie.Assets.asset_path(assets_config, :js, file = "stipplecore"))
# end
route("/") do
model = Stipple.ReactiveTools.@init PrecompileApp
page(model, ui) |> html
end
precompile_get("/")
deps_routes(core_theme = true)
precompile_get(Genie.Assets.asset_path(assets_config, :js, file = "stipplecore"))
end

end
54 changes: 44 additions & 10 deletions src/Tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,38 @@ end
```
"""
macro stipple_precompile(setup, workload)
quote
# wrap @app calls in @eval to avoid precompilation errors
# and escape all expressions except those starting with precompile_
for (i, ex) in enumerate(workload.args)
ex isa Expr && ex.head == :call && startswith("$(ex.args[1])", "precompile_") && continue
if ex isa Expr && ex.head == :macrocall && ex.args[1] == Symbol("@app")
println("found!")
workload.args[i] = :(@eval $__module__ $(ex))#Expr(:macrocall, Symbol("@eval"), ex.args)
else
workload.args[i] = :(esc($ex))
end
end

for (i, ex) in enumerate(setup.args)
ex isa Expr && ex.head == :call && startswith("$(ex.args[1])", "precompile_") && continue
if ex isa Expr && ex.head == :macrocall && ex.args[1] == Symbol("@app")
setup.args[i] = :(@eval $(ex))#Expr(:macrocall, Symbol("@eval"), ex.args)
end
setup.args[i] = :(esc($ex))
end

expr = quote
@setup_workload begin
# Putting some things in `setup` can reduce the size of the
# precompile file and potentially make loading faster.
using Genie.HTTPUtils.HTTP
HTTP = Stipple.Genie.HTTPUtils.HTTP
PRECOMPILE[] = true
dot_theme_watcher = Layout.DOTTHEME_WATCHER[]
user_theme_watcher = Layout.USER_THEME_WATCHER[]
Layout.DOTTHEME_WATCHER[] = true
Layout.USER_THEME_WATCHER[] = true

esc($setup)
:__setup__

@compile_workload begin
# all calls in this block will be precompiled, regardless of whether
Expand All @@ -194,25 +218,35 @@ macro stipple_precompile(setup, workload)
Logging.with_logger(Logging.SimpleLogger(stdout, Logging.Error)) do
up(port)

esc($workload)
:__workload__

down()
end
# reset secret back to empty string
Genie.Secrets.secret_token!("")
end
PRECOMPILE[] = false
Layout.DOTTHEME_WATCHER[] = dot_theme_watcher
Layout.USER_THEME_WATCHER[] = user_theme_watcher
end
end
end

macro stipple_precompile(workload)
# wrap @app calls in @eval to avoid precompilation errors
for (i, ex) in enumerate(workload.args)
if ex isa Expr && ex.head == :macrocall && ex.args[1] == Symbol("@app")
workload.args[i] = :(@eval $(ex))#Expr(:macrocall, Symbol("@eval"), ex.args)
MacroTools.postwalk(expr) do ex
if ex isa QuoteNode
if ex.value === :__setup__
setup
elseif ex.value === :__workload__
workload
else
ex
end
else
ex
end
end
end

macro stipple_precompile(workload)
quote
@stipple_precompile begin end begin
$workload
Expand Down
1 change: 0 additions & 1 deletion src/stipple/reactivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ end
macro type(modelname, storage)
modelname isa DataType && (modelname = modelname.name.name)
modelconst = Symbol(modelname, '!')
modelconst_qn = QuoteNode(modelconst)

output = quote end
output.args = @eval __module__ collect(values($storage))
Expand Down

0 comments on commit cdfbe37

Please sign in to comment.