diff --git a/src/ReactiveTools.jl b/src/ReactiveTools.jl index ea2c082e..3dd6e018 100644 --- a/src/ReactiveTools.jl +++ b/src/ReactiveTools.jl @@ -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 @@ -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 diff --git a/src/Stipple.jl b/src/Stipple.jl index a9fa4040..56c3a1ce 100644 --- a/src/Stipple.jl +++ b/src/Stipple.jl @@ -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 "__" + 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 @@ -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 """