Skip to content

Commit

Permalink
support adding of model-specific depencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaensel committed Nov 14, 2023
1 parent 894567b commit 50fce4b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
68 changes: 68 additions & 0 deletions src/ReactiveTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export @readonly, @private, @in, @out, @jsfn, @readonly!, @private!, @in!, @out!
#definition of handlers/events
export @onchange, @onbutton, @event, @notify

# definition of dependencies
export @deps, @clear_deps

# deletion
export @clear, @clear_vars, @clear_handlers

Expand Down Expand Up @@ -1302,4 +1305,69 @@ macro modelstorage()
end |> esc
end

"""
@deps f
Add a function f to the dependencies of the current app.
------------------------
@deps M::Module
Add the dependencies of the module M to the dependencies of the current app.
"""
macro deps(expr)
quote
Stipple.deps!(Stipple.@type(), $expr)
end |> esc
end

"""
@deps(MyApp::ReactiveModel, f::Function)
Add a function f to the dependencies of the app MyApp.
The module needs to define a function `deps()`.
------------------------
@deps(MyApp::ReactiveModel, M::Module)
Add the dependencies of the module M to the dependencies of the app MyApp.
The module needs to define a function `deps()`.
"""
macro deps(M, expr)
quote
Stipple.deps!($M, $expr)
end |> esc
end

"""
@clear_deps
Delete all dependencies of the current app.
------------------------
@clear_deps MyApp
Delete all dependencies of the app MyApp.
"""
macro clear_deps()
quote
Stipple.clear_deps!(Stipple.@type())
end |> esc
end

macro clear_deps(M)
quote
Stipple.clear_deps!($M)
end |> esc
end

end
41 changes: 39 additions & 2 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,25 @@ end
function injectdeps(output::Vector{AbstractString}, M::Type{<:ReactiveModel}) :: Vector{AbstractString}
for (key, f) in DEPS
key isa DataType && key <: ReactiveModel && continue
# exclude keys starting with '_'
key isa Symbol && startswith("$key", '_') && continue
push!(output, f()...)
end
AM = get_abstract_type(M)
haskey(DEPS, AM) && push!(output, DEPS[AM]()...)

if haskey(DEPS, AM)
# DEPS[AM] contains the stipple-generated deps
push!(output, DEPS[AM]()...)
# furthermore, include deps who's keys start with "_<name of the ReactiveModel>_"
model_prefix = "_$(vm(AM))_"
@show model_prefix
for (key, f) in DEPS
println("hi 1")
@show key
key isa Symbol || continue
@show startswith("$key", model_prefix)
startswith("$key", model_prefix) && push!(output, f()...)
end
end
output
end

Expand Down Expand Up @@ -774,6 +788,29 @@ function deps!(m::Any, f::Function)
DEPS[m] = f
end

function deps!(m::Any, M::Module)
DEPS[m] = M.deps
end

function deps!(M::Type{<:ReactiveModel}, f::Function; extra_deps = true)
key = extra_deps ? Symbol("_$(vm(M))_$(nameof(f))") : M
DEPS[key] = f isa Function ? f : f.deps
end

deps!(M::Type{<:ReactiveModel}, modul::Module; extra_deps = true) = deps!(M, modul.deps; extra_deps)

deps!(m::Any, v::Vector{Union{Function, Module}}) = deps!.(Ref(m), v)
deps!(m::Any, t::Tuple) = [deps!(m, f) for f in t]
deps!(m, args...) = [deps!(m, f) for f in args]

function clear_deps!(M::Type{<:ReactiveModel})
delete!(DEPS, M)
model_prefix = "_$(vm(M))_"
for k in keys(Stipple.DEPS)
k isa Symbol && startswith("$k", model_prefix) && delete!(DEPS, k)
end
end

@specialize

"""
Expand Down

0 comments on commit 50fce4b

Please sign in to comment.