Skip to content

Commit 094ff2d

Browse files
committed
disable ts card observables on card deletion
1 parent ecaf0bb commit 094ff2d

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using GraphMakie: GraphMakie, EdgeClickHandler, EdgeHoverHandler,
1919
using GraphMakie.NetworkLayout: Stress
2020

2121
using OrderedCollections: OrderedDict
22-
using Observables: Observable, on, onany
22+
using Observables: Observables, Observable, on, onany
2323
using Colors: Colors, @colorant_str, RGB, color
2424
using ColorSchemes: ColorSchemes, ColorScheme
2525
using SciMLBase: SciMLBase

NetworkDynamicsInspector/src/timeseries.jl

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ end
4545

4646
function timeseries_cards(app, session)
4747
cards = OrderedDict{String,Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
48+
observables = OrderedDict{String, Vector{Observables.ObserverFunction}}()
4849
container = Observable{Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
4950

5051
on(app.tsplots; update=true) do _tsplots
@@ -53,10 +54,14 @@ function timeseries_cards(app, session)
5354
knownkeys = keys(cards)
5455

5556
for delkey in setdiff(knownkeys, newkeys)
57+
Observables.off.(observables[delkey]) # deactivate allobservables from card
58+
delete!(observables, delkey)
5659
delete!(cards, delkey)
5760
end
5861
for newkey in setdiff(newkeys, knownkeys)
59-
cards[newkey] = timeseries_card(app, newkey, session)
62+
card, obsf = timeseries_card(app, newkey, session)
63+
cards[newkey] = card
64+
observables[newkey] = obsf
6065
end
6166
if keys(cards) != keys(_tsplots)
6267
@warn "The keys do not match: $(keys(cards)) vs $(keys(_tsplots))"
@@ -87,6 +92,16 @@ function add_timeseries_button(app)
8792
end
8893

8994
function timeseries_card(app, key, session)
95+
# store all observer functions to be able to remove them later
96+
obsf = Observables.ObserverFunction[]
97+
track_obsf = function (f)
98+
if f isa AbstractVector
99+
append!(obsf, f)
100+
else
101+
push!(obsf, f)
102+
end
103+
end
104+
90105
tsplot = app.tsplots[][key]
91106

92107
comp_options = Observable{Vector{OptionGroup{SymbolicCompIndex}}}()
@@ -97,15 +112,15 @@ function timeseries_card(app, key, session)
97112
eg = OptionGroup{SymbolicCompIndex}("Edges", EIndex.(1:ne(g)))
98113
comp_options[] = [vg, eg]
99114
nothing
100-
end
115+
end |> track_obsf
101116

102117
state_options = Observable{Vector{OptionGroup{Symbol}}}()
103118
onany(app.sol, tsplot.selcomp; update=true) do _sol, _sel
104119
@debug "TS: app.sol, tsplot.selcomp => state_options"
105120
_nw = extract_nw(_sol)
106121
state_options[] = gen_state_options(_nw, _sel)
107122
nothing
108-
end
123+
end |> track_obsf
109124

110125
comp_sel = TomSelect(comp_options, tsplot.selcomp;
111126
placeholder="Select components",
@@ -126,7 +141,7 @@ function timeseries_card(app, key, session)
126141
empty!(linestyle_cache)
127142
notify(tsplot.selcomp)
128143
notify(tsplot.states)
129-
end
144+
end |> track_obsf
130145

131146
reset_axis_button = Bonito.Button("Reset Axis", style=Styles("margin-left"=>"10px"))
132147

@@ -146,7 +161,7 @@ function timeseries_card(app, key, session)
146161
@debug "TS: comp selection => graphplot selection"
147162
app.graphplot._selcomp[] = _sel
148163
nothing
149-
end
164+
end |> track_obsf
150165

151166
####
152167
#### actual plot
@@ -173,7 +188,7 @@ function timeseries_card(app, key, session)
173188
color="#"*Colors.hex(getcycled(COLORS, v)))
174189
for (k,v) in color_cache]
175190
nothing
176-
end
191+
end |> track_obsf
177192
on(tsplot.states; update=true) do _states
178193
@debug "TS: state selection => update linestyle_cache"
179194
for unused in setdiff(keys(linestyle_cache), _states)
@@ -186,7 +201,7 @@ function timeseries_card(app, key, session)
186201
lstylepairs[] = [(; title=repr(s), linestyle=getcycled(LINESTYLES_STR, i))
187202
for (s,i) in linestyle_cache]
188203
nothing
189-
end
204+
end |> track_obsf
190205

191206
comp_ms_id = Bonito.JSString(comp_sel.id)
192207
state_ms_id = Bonito.JSString(state_sel.id)
@@ -274,7 +289,7 @@ function timeseries_card(app, key, session)
274289
@debug "TS: tim/tmax => update ax limits"
275290
xlims!(ax, (_tmin, _tmax))
276291
nothing
277-
end
292+
end |> track_obsf
278293

279294
ts = Observable(collect(range(app.sol[].t[begin], app.sol[].t[end], length=1000)))
280295
refined_xlims = Ref((NaN, NaN))
@@ -287,7 +302,7 @@ function timeseries_card(app, key, session)
287302
notify(ts)
288303
end
289304
nothing
290-
end
305+
end |> track_obsf
291306

292307
# collect all the states wie might want to plot
293308
valid_idxs = Observable(
@@ -302,7 +317,7 @@ function timeseries_card(app, key, session)
302317
isvalid(idx) && push!(valid_idxs[], idx)
303318
end
304319
notify(valid_idxs)
305-
end
320+
end |> track_obsf
306321

307322
# extract the data
308323
data = Observable{Vector{Vector{Float32}}}(Vector{Float32}[])
@@ -320,7 +335,7 @@ function timeseries_card(app, key, session)
320335
data.val[i] = _dat[i,:]
321336
end
322337
notify(data)
323-
end
338+
end |> track_obsf
324339

325340
replot = Observable{Nothing}(nothing)
326341

@@ -352,12 +367,12 @@ function timeseries_card(app, key, session)
352367
end
353368
end
354369
nothing
355-
end
370+
end |> track_obsf
356371

357372
on(reset_axis_button.value) do _
358373
autolimits!(ax)
359374
xlims!(ax, (app.tmin[], app.tmax[]))
360-
end
375+
end |> track_obsf
361376

362377
####
363378
#### Click interaction to set time

0 commit comments

Comments
 (0)