Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port fixes from 0.28 to 0.30 #287

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name = "Stipple"
uuid = "4acbeb90-81a0-11ea-1966-bdaff8155998"
authors = ["Adrian <[email protected]>"]

version = "0.30.7"

[deps]
Expand Down
22 changes: 14 additions & 8 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,16 +24,19 @@ 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
# restore fields only if a stored model exists, if the field is not part of the internal fields and is not write protected
(
isnothing(stored_model) || f ∈ [Stipple.CHANNELFIELDNAME, Stipple.AUTOFIELDS...] || Stipple.isreadonly(f, model) || Stipple.isprivate(f, model) ||
! hasproperty(stored_model, f) || ! hasproperty(model, f) || (field[!] = getfield(stored_model, f)[])
)
if isnothing(stored_model) || f ∈ [Stipple.CHANNELFIELDNAME, Stipple.AUTOFIELDS...] ||
Stipple.isprivate(f, model) || ! hasproperty(stored_model, f) || ! hasproperty(model, f)
else
# restore field value from stored model
(field[!] = getfield(stored_model, f)[])
end

# register reactive handlers to automatically save model on session when model changes
if f ∉ [Stipple.AUTOFIELDS...]
Expand All @@ -40,7 +45,8 @@ function init_from_storage( t::Type{M};
end
end
else
isnothing(stored_model) || Stipple.isprivate(f, model) || Stipple.isreadonly(f, model) || ! hasproperty(stored_model, f) || ! hasproperty(model, f) || setfield!(model, f, getfield(stored_model, f))
isnothing(stored_model) || Stipple.isprivate(f, model) || Stipple.isreadonly(f, model) ||
! hasproperty(stored_model, f) || ! hasproperty(model, f) || setfield!(model, f, getfield(stored_model, f))
end
end

Expand Down
60 changes: 33 additions & 27 deletions src/ReactiveTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const TYPES = LittleDict{Module,Union{<:DataType,Nothing}}()
const HANDLERS_FUNCTIONS = LittleDict{Type{<:ReactiveModel},Function}()

function DEFAULT_LAYOUT(; title::String = "Genie App",
meta::D = Dict(), head_content::Union{AbstractString, Vector} = "") where {D <:AbstractDict}
meta::D = Dict(),
head_content::Union{AbstractString, Vector} = "") where {D <:AbstractDict}
tags = Genie.Renderers.Html.for_each(x -> """<meta name="$(string(x.first))" content="$(string(x.second))">\n""", meta)
"""
<!DOCTYPE html>
Expand All @@ -52,8 +53,8 @@ function DEFAULT_LAYOUT(; title::String = "Genie App",
$tags
<% Stipple.sesstoken() %>
<title>$title</title>
<% if isfile(joinpath(Genie.config.server_document_root, "css", "genieapp.css")) %>
<link rel='stylesheet' href='$(Genie.Configuration.basepath())/css/genieapp.css'>
<% if isfile(joinpath(Stipple.Genie.config.server_document_root, "css", "genieapp.css")) %>
<link rel='stylesheet' href='$(Stipple.Genie.Configuration.basepath())/css/genieapp.css'>
<% else %>
<% end %>
<style>
Expand All @@ -77,7 +78,7 @@ function DEFAULT_LAYOUT(; title::String = "Genie App",
</div>
</div>
</div>
<% if isfile(joinpath(Genie.config.server_document_root, "js", "genieapp.js")) %>
<% if isfile(joinpath(Stipple.Genie.config.server_document_root, "js", "genieapp.js")) %>
<script src='$(Stipple.Genie.Configuration.basepath())/js/genieapp.js'></script>
<% else %>
<% end %>
Expand All @@ -90,12 +91,12 @@ function DEFAULT_LAYOUT(; title::String = "Genie App",
</div>
</div>
</footer>
<% if isfile(joinpath(Genie.config.server_document_root, "css", "theme.css")) %>
<link rel='stylesheet' href='$(Genie.Configuration.basepath())/css/theme.css'>
<% if isfile(joinpath(Stipple.Genie.config.server_document_root, "css", "theme.css")) %>
<link rel='stylesheet' href='$(Stipple.Genie.Configuration.basepath())/css/theme.css'>
<% else %>
<% end %>
<% if isfile(joinpath(Genie.config.server_document_root, "css", "autogenerated.css")) %>
<link rel='stylesheet' href='$(Genie.Configuration.basepath())/css/autogenerated.css'>
<% if isfile(joinpath(Stipple.Genie.config.server_document_root, "css", "autogenerated.css")) %>
<link rel='stylesheet' href='$(Stipple.Genie.Configuration.basepath())/css/autogenerated.css'>
<% else %>
<% end %>
</body>
Expand Down Expand Up @@ -734,30 +735,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
18 changes: 12 additions & 6 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 @@ -384,6 +388,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 @@ -408,7 +414,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 @@ -511,7 +517,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 @@ -533,7 +539,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 @@ -1271,14 +1277,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
Loading