Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	Project.toml
  • Loading branch information
essenciary committed Jan 31, 2024
2 parents b220590 + f02e3a9 commit 82c0e93
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ end
Genie.isrunning(:webserver) || up()
```
<img src="docs/content/img/Example2.png">

## Choosing the transport layer: WebSockets or HTTP


By default Stipple will attempt to use WebSockets for real time data sync between backend and frontend.
However, in some cases WebSockets support might not be available on the host. In this case, Stipple can be
switched to use regular HTTP for data sync, using frontend polling with AJAX (1s polling interval by default).
Expand Down
139 changes: 139 additions & 0 deletions src/ReactiveTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export @onchange, @onbutton, @event, @notify
# definition of dependencies
export @deps, @clear_deps

# definition of field-specific debounce times
export @debounce, @clear_debounce

# deletion
export @clear, @clear_vars, @clear_handlers

Expand Down Expand Up @@ -269,6 +272,142 @@ macro clear_route()
:(Stipple.clear_route(Stipple.@type)) |> esc
end

function _prepare(fieldname)
if fieldname isa Symbol
fieldname = QuoteNode(fieldname)
else
if fieldname isa Expr && fieldname.head == :tuple
for (i, x) in enumerate(fieldname.args)
x isa Symbol && (fieldname.args[i] = QuoteNode(x))
end
end
end
fieldname
end

"""
@debounce fieldname ms
@debounce App fieldname ms
Set field-specific debounce time in ms.
### Parameters
- `APP`: a subtype of ReactiveModel, e.g. `MyApp`
- `fieldname`: fieldname òr fieldnames as written in the declaration, e.g. `x`, `(x, y, z)`
- `ms`: debounce time in ms
### Example
#### Implicit apps
```
@app begin
@out quick = 12
@out slow = 12
@in s = "Hello"
end
# no debouncing for fast messaging
@debounce quick 0
# long debouncing for long-running tasks
@debounce (slow1, slow2) 1000
```
#### Explicit apps
```
@app MyApp begin
@out quick = 12
@out slow = 12
@in s = "Hello"
end
# no debouncing for fast messaging
@debounce MyApp quick 0
# long debouncing for long-running tasks
@debounce MyApp slow 1000
```
"""
macro debounce(M, fieldname, ms)
fieldname = _prepare(fieldname)
:(Stipple.debounce($M, $fieldname, $ms)) |> esc
end

macro debounce(fieldname, ms)
fieldname = _prepare(fieldname)
:(Stipple.debounce(Stipple.@type(),$fieldname, $ms)) |> esc
end

"""
@clear_debounce
@clear_debounce fieldname
@clear_debounce App
@clear_debounce App fieldname
Clear field-specific debounce time, for setting see `@debounce`.
After calling `@clear debounce` the field will be debounced by the value given in the
`@init` macro.
### Example
#### Implicit apps
```
@app begin
@out quick = 12
@out slow = 12
@in s = "Hello"
end
# no debouncing for fast messaging
@debounce quick 0
@debounce slow 1000
# reset to standard value of the app
@clear_debounce quick
# clear all field-specific debounce times
@clear_debounce
```
#### Explicit apps
```
@app MyApp begin
@out quick = 12
@out slow = 12
@in s = "Hello"
end
# no debouncing for fast messaging
@debounce MyApp quick 0
@clear_debounce MyApp quick
# clear all field-specific debounce times
@clear_debounce MyApp
```
"""
macro clear_debounce(M, fieldname)
fieldname = _prepare(fieldname)
:(Stipple.debounce($M, $fieldname, nothing)) |> esc
end

macro clear_debounce(expr)
quote
if $expr isa DataType && $expr <: Stipple.ReactiveModel
Stipple.debounce($expr, nothing)
else
Stipple.debounce(Stipple.@type(), $(_prepare(expr)), nothing)
end
end |> esc
end

macro clear_debounce()
:(Stipple.debounce(Stipple.@type(), nothing)) |> esc
end

function update_storage(m::Module)
clear_type(m)
# isempty(Stipple.Pages._pages) && return
Expand Down
57 changes: 50 additions & 7 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ const PURGE_TIME_LIMIT = Ref{Period}(Day(1))
const PURGE_NUMBER_LIMIT = Ref(1000)
const PURGE_CHECK_DELAY = Ref(60)

const DEBOUNCE = LittleDict{Type{<:ReactiveModel}, LittleDict{Symbol, Any}}()

"""
debounce(M::Type{<:ReactiveModel}, fieldnames::Union{Symbol, Vector{Symbol}}, debounce::Union{Int, Nothing} = nothing)
Add field-specific debounce times.
"""
function debounce(M::Type{<:ReactiveModel}, fieldnames::Union{Symbol, Vector{Symbol}, NTuple{N, Symbol} where N}, debounce::Union{Int, Nothing} = nothing)
if debounce === nothing
haskey(DEBOUNCE, M) || return
d = DEBOUNCE[M]
if fieldnames isa Symbol
delete!(d, fieldnames)
else
for v in fieldnames
delete!(d, v)
end
end
isempty(d) && delete!(DEBOUNCE, M)
else
d = get!(LittleDict{Symbol, Any}, DEBOUNCE, M)
if fieldnames isa Symbol
d[fieldnames] = debounce
else
for v in fieldnames
d[v] = debounce
end
end
end
return
end

debounce(M::Type{<:ReactiveModel}, ::Nothing) = delete!(DEBOUNCE, M)

"""
`function sorted_channels()`
Expand Down Expand Up @@ -311,20 +345,29 @@ function watch(vue_app_name::String, fieldname::Symbol, channel::String, debounc
isempty(jsfunction) &&
(jsfunction = "Genie.WebChannels.sendMessageTo($js_channel, 'watchers', {'payload': {'field':'$fieldname', 'newval': newVal, 'oldval': oldVal, 'sesstoken': document.querySelector(\"meta[name='sesstoken']\")?.getAttribute('content')}});")

output = IOBuffer()
if fieldname == :isready
output = """
print(output, """
$vue_app_name.\$watch(function(){return this.$fieldname}, function(newVal, oldVal){$jsfunction}, {deep: true});
"""
""")
else
output = """
$vue_app_name.\$watch(function(){return this.$fieldname}, _.debounce(function(newVal, oldVal){$jsfunction}, $debounce), {deep: true});
"""
AM = get_abstract_type(M)
debounce = get(get(DEBOUNCE, AM, Dict{Symbol, Any}()), fieldname, debounce)
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});
"""
)
end
# in production mode vue does not fill `this.expression` in the watcher, so we do it manually
Genie.Configuration.isprod() &&
(output *= "$vue_app_name._watchers[$vue_app_name._watchers.length - 1].expression = 'function(){return this.$fieldname}'")
print(output, "$vue_app_name._watchers[$vue_app_name._watchers.length - 1].expression = 'function(){return this.$fieldname}'")

output *= "\n\n"
print(output, "\n\n")
String(take!(output))
end

#===#
Expand Down
12 changes: 1 addition & 11 deletions src/stipple/print.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
function print_object(io, obj::T, omit = nothing, compact = false) where T
# currently no different printing for compact = true ...
fields = [p for p in propertynames(obj)]
omit !== nothing && setdiff!(fields, omit)

println(io, match(r"^#*([^!]+)", String(T.name.name)).captures[1])
for field in fields
println(io, " $field: ", Observables.to_value(getproperty(obj, field)))
end
end

function print_object(io, obj::T, omit = nothing, compact = false) where T <: ReactiveModel
# currently no different printing for compact = true ...
fields = [p for p in propertynames(obj)]
omit !== nothing && setdiff!(fields, omit)
internal_or_auto = true
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ end
# check reactivity
@eval model.i[] = 20
@test model.s[] == "20"

@eval @debounce TestApp i 101
@eval @debounce TestApp (a, b, c) 101
@test Stipple.DEBOUNCE[TestApp][:i] == 101

@eval @clear_debounce TestApp
@test haskey(Stipple.DEBOUNCE, TestApp) == false
end

@testset "Reactive API (implicit)" begin
Expand All @@ -142,6 +149,14 @@ end
# check reactivity
@eval model.i2[] = 20
@test model.s2[] == "20"

# check field-specific debouncing
@eval @debounce i3 101
@eval @debounce (a, b, c) 101
@test Stipple.DEBOUNCE[Stipple.@type()][:i3] == 101

@eval @clear_debounce
@test haskey(Stipple.DEBOUNCE, Stipple.@type()) == false
end

@testset "Reactive API (implicit) with mixins and handlers" begin
Expand Down

2 comments on commit 82c0e93

@essenciary
Copy link
Member Author

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/99973

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.0 -m "<description of version>" 82c0e934d1d2d233829ec582dbc3f54691c65807
git push origin v0.28.0

Please sign in to comment.