Skip to content

Commit

Permalink
add client info to events on request
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaensel committed Nov 27, 2023
1 parent 3c2a4aa commit ecafe01
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion assets/js/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ const reviveMixin = {

const eventMixin = {
methods: {
handle_event: function (event_data, event_handler) {
handle_event: function (event_data, event_handler, mode) {
console.debug('event: ' + JSON.stringify(event_data) + ":" + event_handler)
if (mode=='addclient') { event_data._addclient = true}
Genie.WebChannels.sendMessageTo(window.CHANNEL, 'events', {
'event': {
'name': event_handler,
Expand Down
3 changes: 2 additions & 1 deletion src/Elements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ Sometimes preprocessing of the events is necessary, e.g. to add or skip informat
```
"""
macro on(arg, expr, preprocess = nothing)
preprocess isa QuoteNode && preprocess == :(:addclient) && (preprocess = "event._addclient = true")
kw = Symbol("v-on:", arg isa String ? arg : arg isa QuoteNode ? arg.value : arg.head == :vect ? join(lstrip.(string.(arg.args), ':'), '.') :
throw("Value '$arg' for `arg` not supported. `arg` should be of type Symbol, String, or Vector{Union{String, Symbol}}"))

Expand All @@ -370,7 +371,7 @@ macro on(arg, expr, preprocess = nothing)
:(replace("""function(event) {
const preprocess = (event) => { """ * replace($preprocess, '"' => "\\\"") * """; return event }
handle_event(preprocess(event), '$($(esc(expr)))')
}'""", '\n' => ';'))
}""", '\n' => ';'))
end
else
esc_expr(expr)
Expand Down
7 changes: 7 additions & 0 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,13 @@ function init(::Type{M};
# form handler parameter & call event notifier
handler = Symbol(get(event, "name", nothing))
event_info = get(event, "event", nothing)

# add client id if requested
if event_info isa Dict && get(event_info, "_addclient", false)
client = transport == Genie.WebChannels ? Genie.WebChannels.id(Genie.Requests.wsclient()) : Genie.Requests.wtclient()
push!(event_info, "_client" => client)
end

isempty(methods(notify, (M, Val{handler}))) || notify(model, Val(handler))
isempty(methods(notify, (M, Val{handler}, Any))) || notify(model, Val(handler), event_info)
LAST_ACTIVITY[Symbol(channel)] = now()
Expand Down

0 comments on commit ecafe01

Please sign in to comment.