Skip to content

Commit d3003c2

Browse files
committed
fix problem when overwriting TSPlot with same key
1 parent f0ccbf0 commit d3003c2

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

NetworkDynamicsInspector/src/timeseries.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,30 @@ function timeseries_col(app, session)
4444
end
4545

4646
function timeseries_cards(app, session)
47-
cards = OrderedDict{String,Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
48-
observables = OrderedDict{String, Vector{Observables.ObserverFunction}}()
47+
# we store the cards based on objid of TSPlot, otherwise overwrite with same key
48+
# leads to problems
49+
cards = OrderedDict{UInt, Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
50+
observables = OrderedDict{UInt, Vector{Observables.ObserverFunction}}()
4951
container = Observable{Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
5052

5153
on(app.tsplots; update=true) do _tsplots
5254
@debug "TS: app.tsplots => update timeseries cards"
53-
newkeys = collect(keys(_tsplots)) # collect to preserv order on setdiff
54-
knownkeys = keys(cards)
55-
56-
for delkey in setdiff(knownkeys, newkeys)
57-
Observables.off.(observables[delkey]) # deactivate allobservables from card
58-
delete!(observables, delkey)
59-
delete!(cards, delkey)
55+
id_to_key = Dict(Base.objectid(val) => key for (key, val) in _tsplots)
56+
newids = Base.objectid.(values(_tsplots)) # collect to preserv order on setdiff
57+
knownids = keys(cards)
58+
59+
for delid in setdiff(knownids, newids)
60+
Observables.off.(observables[delid]) # deactivate allobservables from card
61+
delete!(observables, delid)
62+
delete!(cards, delid)
6063
end
61-
for newkey in setdiff(newkeys, knownkeys)
62-
card, obsf = timeseries_card(app, newkey, session)
63-
cards[newkey] = card
64-
observables[newkey] = obsf
64+
for newid in setdiff(newids, knownids)
65+
card, obsf = timeseries_card(app, id_to_key[newid], session)
66+
cards[newid] = card
67+
observables[newid] = obsf
6568
end
66-
if keys(cards) != keys(_tsplots)
67-
@warn "The keys do not match: $(keys(cards)) vs $(keys(_tsplots))"
69+
if collect(keys(cards)) != newids
70+
@warn "The keys do not match: $(keys(cards)) vs $(newids)"
6871
end
6972

7073
container[] = DOM.div(values(cards)...; class="timeseries-stack")

NetworkDynamicsInspector/test/runtests.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ end
9595

9696
@testset "track changes in tsplots" begin
9797
sol = get_sol()
98-
inspect(sol)
98+
inspect(sol; display=BrowserDisp())
9999
NDI.APPSTATE[].tsplots[]["ts-1"] = NDI.TimeseriesPlot(selcomp=[EIndex(3), EIndex(2), EIndex(1)], states=[:P])
100-
notify(NDI.APPSTATE[].tsplots)
101-
# does not change because i thinks i knows the key!
102-
@test false
100+
notify(NDI.APPSTATE[].tsplots) # should update
103101
end
104102

105103
@testest "Test different display types" begin

0 commit comments

Comments
 (0)