Skip to content

Commit

Permalink
forward init-kwargs debounce, transport and core_theme from `Pa…
Browse files Browse the repository at this point in the history
…ge()`, `@page` and `@init` to `initfn()`
  • Loading branch information
hhaensel committed Sep 8, 2023
1 parent e1a11f2 commit 0ab7770
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/Pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ function Page( route::Union{Route,String};
model::Union{M,Function,Nothing,Expr,Module} = Stipple.init(EmptyModel),
layout::Union{Genie.Renderers.FilePath,<:AbstractString,ParsedHTMLString,Nothing,Function} = nothing,
context::Module = @__MODULE__,
debounce::Int = Stipple.JS_DEBOUNCE_TIME,
transport::Module = Genie.WebChannels,
core_theme::Bool = true,
kwargs...
) where {M<:ReactiveModel}

model = if isa(model, Expr)
Core.eval(context, model)
elseif isa(model, Module)
context = model
@eval(context, @init())
@eval(context, @init(debounce = debounce, transport = transport, core_theme = core_theme))
else
model
end
Expand Down
53 changes: 42 additions & 11 deletions src/ReactiveTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,44 @@ function init_handlers(m::Module)
get!(Vector{Expr}, HANDLERS, m)
end

macro init(modeltype)
"""
@init(kwargs...)
Create a new app with the following kwargs supported:
- `debounce::Int = JS_DEBOUNCE_TIME`
- `transport::Module = Genie.WebChannels`
- `core_theme::Bool = true`
### Example
```
@app begin
@in n = 10
@out s = "Hello"
end
model = @init(debounce = 50)
```
------------
@init(App, kwargs...)
Create a new app of type `App` with the same kwargs as above
### Example
```
@app MyApp begin
@in n = 10
@out s = "Hello"
end
model = @init(MyApp, debounce = 50)
```
"""
macro init(args...)
called_without_type = length(args) == 0 || args[1] isa Expr && args[1].head == :(=)
init_args = Stipple.expressions_to_args(args)
called_without_type && pushfirst!(init_args, :(Stipple.@type()))
quote
local new_handlers = false
local initfn = if isdefined($__module__, :init_from_storage)
Expand All @@ -538,23 +575,17 @@ macro init(modeltype)
else
identity
end

instance = new_handlers ? Base.invokelatest(handlersfn, $modeltype |> initfn) : $modeltype |> initfn |> handlersfn

let model = initfn($(init_args...))
instance = new_handlers ? Base.invokelatest(handlersfn, model) : handlersfn(model)
end
for p in Stipple.Pages._pages
p.context == $__module__ && (p.model = instance)
end
instance
end |> esc
end

macro init()
quote
let type = Stipple.@type
Stipple.ReactiveTools.@init(type)
end
end |> esc
end

macro handlers()
handlers = init_handlers(__module__)

Expand Down

0 comments on commit 0ab7770

Please sign in to comment.