Skip to content

Commit

Permalink
Fix auto-model storage and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Salceanu committed Jun 19, 2024
1 parent db766c7 commit fc126c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Stipple"
uuid = "4acbeb90-81a0-11ea-1966-bdaff8155998"
authors = ["Adrian <[email protected]>"]
version = "0.28.11"
version = "0.28.12"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
8 changes: 5 additions & 3 deletions src/ModelStorage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ function model_id(::Type{M}) where M
Symbol(Stipple.routename(M))
end

function store(model::M) where M
GenieSession.set!(model_id(M), model)
function store(model::M, force::Bool = false) where M
# do not overwrite stored model
(GenieSession.get(model_id(M), nothing) === nothing || force) && GenieSession.set!(model_id(M), model)

nothing
end

Expand All @@ -22,8 +24,8 @@ function init_from_storage( t::Type{M};
kwargs...) where M
model = Stipple.init(M; channel, kwargs...)
stored_model = GenieSession.get(model_id(M), nothing)

CM = Stipple.get_concrete_type(M)

for f in fieldnames(CM)
field = getfield(model, f)
if field isa Reactive
Expand Down
43 changes: 24 additions & 19 deletions src/ReactiveTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -734,30 +734,35 @@ macro init(args...)
quote
local new_handlers = false

local initfn =
if isdefined($__module__, :init_from_storage) && Stipple.USE_MODEL_STORAGE[]
$__module__.init_from_storage
else
Stipple.init
local initfn = begin
if Stipple.use_model_storage() && $__module__ === Stipple
Stipple.ModelStorage.Sessions.init_from_storage
elseif isdefined($__module__, :Stipple) && isdefined($__module__.Stipple, :ModelStorage) && isdefined($__module__.Stipple.ModelStorage, :Sessions) && isdefined($__module__.Stipple.ModelStorage.Sessions, :init_from_storage) && Stipple.use_model_storage()
$__module__.Stipple.ModelStorage.Sessions.init_from_storage
elseif isdefined($__module__, :init_from_storage) && Stipple.use_model_storage()
$__module__.init_from_storage
else
Stipple.init
end
end

local handlersfn =
if !$called_without_type
# writing '$(init_kwargs[type_pos])' generates an error during a pre-evaluation
# possibly from Revise?
# we use 'get' instead of 'getindex'
Stipple.ReactiveTools.HANDLERS_FUNCTIONS[$(get(init_args, type_pos, "dummy"))]
else
if isdefined($__module__, :__GF_AUTO_HANDLERS__)
if length(methods($__module__.__GF_AUTO_HANDLERS__)) == 0
@eval(@handlers())
new_handlers = true
end
$__module__.__GF_AUTO_HANDLERS__
if !$called_without_type
# writing '$(init_kwargs[type_pos])' generates an error during a pre-evaluation
# possibly from Revise?
# we use 'get' instead of 'getindex'
Stipple.ReactiveTools.HANDLERS_FUNCTIONS[$(get(init_args, type_pos, "dummy"))]
else
identity
if isdefined($__module__, :__GF_AUTO_HANDLERS__)
if length(methods($__module__.__GF_AUTO_HANDLERS__)) == 0
@eval(@handlers())
new_handlers = true
end
$__module__.__GF_AUTO_HANDLERS__
else
identity
end
end
end

instance = let model = initfn($(init_args...))
new_handlers ? Base.invokelatest(handlersfn, model) : handlersfn(model)
Expand Down
20 changes: 13 additions & 7 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const PRECOMPILE = Ref(false)
const ALWAYS_REGISTER_CHANNELS = Ref(true)
const USE_MODEL_STORAGE = Ref(true)

function use_model_storage()
USE_MODEL_STORAGE[]
end

"""
Disables the automatic storage and retrieval of the models in the session.
Useful for large models.
Expand Down Expand Up @@ -98,7 +102,7 @@ export setchannel, getchannel
isempty(methods(notify, Observables)) && (Base.notify(observable::AbstractObservable) = Observables.notify!(observable))

include("ParsingTools.jl")
USE_MODEL_STORAGE[] && include("ModelStorage.jl")
use_model_storage() && include("ModelStorage.jl")
include("NamedTuples.jl")

include("stipple/reactivity.jl")
Expand Down Expand Up @@ -357,7 +361,7 @@ function watch(vue_app_name::String, fieldname::Symbol, channel::String, debounc
print(output, debounce == 0 ?
"""
$vue_app_name.\$watch(function(){return this.$fieldname}, function(newVal, oldVal){$jsfunction}, {deep: true});
""" :
""" :
"""
$vue_app_name.\$watch(function(){return this.$fieldname}, _.debounce(function(newVal, oldVal){$jsfunction}, $debounce), {deep: true});
"""
Expand Down Expand Up @@ -387,6 +391,8 @@ const CHANNELPARAM = :CHANNEL__


function sessionid(; encrypt::Bool = true) :: String
use_model_storage() || error("Model storage is disabled")

sessid = Stipple.ModelStorage.Sessions.GenieSession.session().id

encrypt ? Genie.Encryption.encrypt(sessid) : sessid
Expand All @@ -411,7 +417,7 @@ function channeldefault(::Type{M}) where M<:ReactiveModel

model_id = Symbol(Stipple.routename(M))

USE_MODEL_STORAGE[] || return nothing
use_model_storage() || return nothing

stored_model = Stipple.ModelStorage.Sessions.GenieSession.get(model_id, nothing)
stored_model === nothing ? nothing : getfield(stored_model, Stipple.CHANNELFIELDNAME)
Expand Down Expand Up @@ -514,7 +520,7 @@ function init(t::Type{M};
setchannel(model, channel)

# make sure we store the channel name in the model
USE_MODEL_STORAGE[] && Stipple.ModelStorage.Sessions.store(model)
use_model_storage() && Stipple.ModelStorage.Sessions.store(model)

# 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()
Expand All @@ -536,7 +542,7 @@ function init(t::Type{M};
client = transport == Genie.WebChannels ? Genie.WebChannels.id(Genie.Requests.wsclient()) : Genie.Requests.wtclient()

try
haskey(payload, "sesstoken") && ! isempty(payload["sesstoken"]) && USE_MODEL_STORAGE[] &&
haskey(payload, "sesstoken") && ! isempty(payload["sesstoken"]) && use_model_storage() &&
Genie.Router.params!(Stipple.ModelStorage.Sessions.GenieSession.PARAMS_SESSION_KEY,
Stipple.ModelStorage.Sessions.GenieSession.load(payload["sesstoken"] |> Genie.Encryption.decrypt))
catch ex
Expand Down Expand Up @@ -1309,14 +1315,14 @@ using Stipple.ReactiveTools
end
end

route("/") do
route("/") do
model = Stipple.ReactiveTools.@init PrecompileApp
page(model, ui) |> html
end
port = tryparse(Int, get(ENV, "STIPPLE_PRECOMPILE_PORT", ""))
port === nothing && (port = rand(8081:8999))
up(port)

precompile_get = tryparse(Bool, get(ENV, "STIPPLE_PRECOMPILE_GET", "1"))
precompile_get === true && HTTP.get("http://localhost:$port")
# The following lines (still) produce an error although
Expand Down

2 comments on commit fc126c2

@essenciary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109341

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.28.12 -m "<description of version>" fc126c2e0e9ccc64e063519432729660b5554c6e
git push origin v0.28.12

Please sign in to comment.