Skip to content

Commit 7b7a102

Browse files
committed
fix: isolate session channels when model storage is off, add override SHARE_CHANNELS_ACROSS_WINDOWS
1 parent cde6623 commit 7b7a102

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

src/Stipple.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const ALWAYS_REGISTER_CHANNELS = RefValue(true)
2121
const USE_MODEL_STORAGE = RefValue(true)
2222
const PRECOMPILE = RefValue(false)
2323
const FIXED_TYPES = Dict{Module, Vector{Symbol}}()
24+
const SHARE_CHANNELS_ACROSS_WINDOWS = RefValue{Union{Bool, Nothing}}(nothing)
2425

2526
import MacroTools
2627
import Pkg.TOML
@@ -29,6 +30,17 @@ function use_model_storage()
2930
USE_MODEL_STORAGE[]
3031
end
3132

33+
"""
34+
Whether windows in the same session should reuse the same channel.
35+
36+
If set to `nothing`, the default follows `use_model_storage()`:
37+
- `true` => share channels across windows
38+
- `false` => keep channels isolated unless explicitly enabled
39+
"""
40+
function share_channels_across_windows()::Bool
41+
SHARE_CHANNELS_ACROSS_WINDOWS[] === nothing ? use_model_storage() : SHARE_CHANNELS_ACROSS_WINDOWS[]
42+
end
43+
3244
"""
3345
Disables the automatic storage and retrieval of the models in the session.
3446
Useful for large models.
@@ -458,7 +470,9 @@ function channeldefault(::Type{M}) where M<:ReactiveModel
458470

459471
model_id = Symbol(Stipple.routename(M))
460472

461-
# Look up channel from session - session persistence should work regardless of model storage
473+
# Default channel sharing follows the model storage policy, but may be overridden explicitly.
474+
share_channels_across_windows() || return nothing
475+
462476
if use_model_storage()
463477
stored_model = Stipple.ModelStorage.Sessions.GenieSession.get(model_id, nothing)
464478
stored_model === nothing ? nothing : getchannel(stored_model)
@@ -597,12 +611,12 @@ function init(t::Type{M};
597611
channel === nothing && (channel = channelfactory())
598612
setchannel(model, channel)
599613

600-
# Store model or channel in session based on model storage setting
614+
# Store model or channel in session based on model storage setting and explicit cross-window sync override.
601615
if use_model_storage()
602-
# Store full model when model storage is enabled
616+
# Store full model when model storage is enabled.
603617
Stipple.ModelStorage.Sessions.store(model)
604-
else
605-
# When model storage is disabled, only store the channel for session persistence
618+
elseif SHARE_CHANNELS_ACROSS_WINDOWS[] === true
619+
# When model storage is disabled but cross-window sync is explicitly on, store only the channel.
606620
model_id = Symbol(Stipple.routename(M))
607621
channel_key = Symbol(string(model_id) * "_channel")
608622
Stipple.ModelStorage.Sessions.GenieSession.set!(channel_key, channel)

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,32 @@ end
383383
@test get_channel(s3) != get_channel(s1)
384384
@test get_channel(s4) == get_channel(s5) == get_channel(s6)
385385

386+
current_storage = Stipple.use_model_storage()
387+
current_channel_sharing = Stipple.SHARE_CHANNELS_ACROSS_WINDOWS[]
388+
389+
Stipple.enable_model_storage(false)
390+
empty!(COOKIE_JAR.entries)
391+
392+
s7 = string_get("http://localhost:$port/")
393+
s8 = string_get("http://localhost:$port/")
394+
s9 = string_get("http://localhost:$port/", cookies = false)
395+
396+
@test get_channel(s8) != get_channel(s7)
397+
@test get_channel(s9) != get_channel(s7)
398+
399+
Stipple.SHARE_CHANNELS_ACROSS_WINDOWS[] = true
400+
empty!(COOKIE_JAR.entries)
401+
402+
s10 = string_get("http://localhost:$port/")
403+
s11 = string_get("http://localhost:$port/")
404+
s12 = string_get("http://localhost:$port/", cookies = false)
405+
406+
@test get_channel(s11) == get_channel(s10)
407+
@test get_channel(s12) != get_channel(s10)
408+
409+
Stipple.enable_model_storage(current_storage)
410+
Stipple.SHARE_CHANNELS_ACROSS_WINDOWS[] = current_channel_sharing
411+
386412
@clear_cache
387413
down()
388414
end

0 commit comments

Comments
 (0)