fix: isolate split idle scheduler
- commit
- b0266fa617d83e77f16c638aace473ecb90bfc92
- author
- brice · · 2026-07-15 08:15 UTC
- parent
- 7137fd1
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
2 files · +34 -20
M internal/ui/layout/split.go +4 -8
| @@ -21,7 +21,7 @@ type SplitView struct { | |||
| 21 | 21 | logger zerolog.Logger | |
| 22 | 22 | ||
| 23 | 23 | onRatioChanged func(ratio float64) | |
| 24 | - | pendingNotifyRatio float64 | |
| 24 | + | idleAdd func(*glib.SourceFunc, uintptr) uint | |
| 25 | 25 | notifyDebounceTimer *time.Timer | |
| 26 | 26 | notifyGeneration uint64 | |
| 27 | 27 | tickCallbackID uint | |
| @@ -41,9 +41,6 @@ const notifyPositionDebounceDelay = 100 * time.Millisecond | |||
| 41 | 41 | const notifyPositionSuppressAfterApplyDelay = 50 * time.Millisecond | |
| 42 | 42 | const splitAllocationSettledTolerance = 2 | |
| 43 | 43 | ||
| 44 | - | // idleAdd is a seam for scheduling work on GLib's main loop. | |
| 45 | - | var idleAdd = glib.IdleAdd | |
| 46 | - | ||
| 47 | 44 | func childAllocation(widget Widget) (int, int) { | |
| 48 | 45 | if widget == nil { | |
| 49 | 46 | return 0, 0 | |
| @@ -115,6 +112,7 @@ func NewSplitView( | |||
| 115 | 112 | endChild: endChild, | |
| 116 | 113 | ratio: clampRatio(ratio), | |
| 117 | 114 | logger: log.With().Str("component", "split-view").Logger(), | |
| 115 | + | idleAdd: glib.IdleAdd, | |
| 118 | 116 | } | |
| 119 | 117 | ||
| 120 | 118 | // Set children | |
| @@ -162,7 +160,6 @@ func NewSplitView( | |||
| 162 | 160 | return | |
| 163 | 161 | } | |
| 164 | 162 | sv.ratio = ratio | |
| 165 | - | sv.pendingNotifyRatio = ratio | |
| 166 | 163 | sv.notifyGeneration++ | |
| 167 | 164 | generation := sv.notifyGeneration | |
| 168 | 165 | ||
| @@ -184,15 +181,14 @@ func NewSplitView( | |||
| 184 | 181 | return false | |
| 185 | 182 | } | |
| 186 | 183 | onRatioChanged := sv.onRatioChanged | |
| 187 | - | pending := sv.pendingNotifyRatio | |
| 188 | 184 | sv.mu.RUnlock() | |
| 189 | 185 | ||
| 190 | 186 | if onRatioChanged != nil { | |
| 191 | - | onRatioChanged(pending) | |
| 187 | + | onRatioChanged(ratio) | |
| 192 | 188 | } | |
| 193 | 189 | return false | |
| 194 | 190 | }) | |
| 195 | - | idleAdd(&cb, 0) | |
| 191 | + | sv.idleAdd(&cb, 0) | |
| 196 | 192 | }) | |
| 197 | 193 | ||
| 198 | 194 | sv.mu.Unlock() | |
| @@ -21,7 +21,7 @@ type SplitView struct { | |||||
| 21 | logger zerolog.Logger | 21 | logger zerolog.Logger | ||
| 22 | 22 | ||||
| 23 | onRatioChanged func(ratio float64) | 23 | onRatioChanged func(ratio float64) | ||
| 24 | - | pendingNotifyRatio float64 | 24 | + | idleAdd func(*glib.SourceFunc, uintptr) uint |
| 25 | notifyDebounceTimer *time.Timer | 25 | notifyDebounceTimer *time.Timer | ||
| 26 | notifyGeneration uint64 | 26 | notifyGeneration uint64 | ||
| 27 | tickCallbackID uint | 27 | tickCallbackID uint | ||
| @@ -41,9 +41,6 @@ const notifyPositionDebounceDelay = 100 * time.Millisecond | |||||
| 41 | const notifyPositionSuppressAfterApplyDelay = 50 * time.Millisecond | 41 | const notifyPositionSuppressAfterApplyDelay = 50 * time.Millisecond | ||
| 42 | const splitAllocationSettledTolerance = 2 | 42 | const splitAllocationSettledTolerance = 2 | ||
| 43 | 43 | ||||
| 44 | - | // idleAdd is a seam for scheduling work on GLib's main loop. | |||
| 45 | - | var idleAdd = glib.IdleAdd | |||
| 46 | - | ||||
| 47 | func childAllocation(widget Widget) (int, int) { | 44 | func childAllocation(widget Widget) (int, int) { | ||
| 48 | if widget == nil { | 45 | if widget == nil { | ||
| 49 | return 0, 0 | 46 | return 0, 0 | ||
| @@ -115,6 +112,7 @@ func NewSplitView( | |||||
| 115 | endChild: endChild, | 112 | endChild: endChild, | ||
| 116 | ratio: clampRatio(ratio), | 113 | ratio: clampRatio(ratio), | ||
| 117 | logger: log.With().Str("component", "split-view").Logger(), | 114 | logger: log.With().Str("component", "split-view").Logger(), | ||
| 115 | + | idleAdd: glib.IdleAdd, | |||
| 118 | } | 116 | } | ||
| 119 | 117 | ||||
| 120 | // Set children | 118 | // Set children | ||
| @@ -162,7 +160,6 @@ func NewSplitView( | |||||
| 162 | return | 160 | return | ||
| 163 | } | 161 | } | ||
| 164 | sv.ratio = ratio | 162 | sv.ratio = ratio | ||
| 165 | - | sv.pendingNotifyRatio = ratio | |||
| 166 | sv.notifyGeneration++ | 163 | sv.notifyGeneration++ | ||
| 167 | generation := sv.notifyGeneration | 164 | generation := sv.notifyGeneration | ||
| 168 | 165 | ||||
| @@ -184,15 +181,14 @@ func NewSplitView( | |||||
| 184 | return false | 181 | return false | ||
| 185 | } | 182 | } | ||
| 186 | onRatioChanged := sv.onRatioChanged | 183 | onRatioChanged := sv.onRatioChanged | ||
| 187 | - | pending := sv.pendingNotifyRatio | |||
| 188 | sv.mu.RUnlock() | 184 | sv.mu.RUnlock() | ||
| 189 | 185 | ||||
| 190 | if onRatioChanged != nil { | 186 | if onRatioChanged != nil { | ||
| 191 | - | onRatioChanged(pending) | 187 | + | onRatioChanged(ratio) |
| 192 | } | 188 | } | ||
| 193 | return false | 189 | return false | ||
| 194 | }) | 190 | }) | ||
| 195 | - | idleAdd(&cb, 0) | 191 | + | sv.idleAdd(&cb, 0) |
| 196 | }) | 192 | }) | ||
| 197 | 193 | ||||
| 198 | sv.mu.Unlock() | 194 | sv.mu.Unlock() | ||
M internal/ui/layout/split_notify_cleanup_test.go +30 -12
| @@ -60,16 +60,17 @@ func receiveQueuedIdle(t *testing.T, queued <-chan *glib.SourceFunc) *glib.Sourc | |||
| 60 | 60 | } | |
| 61 | 61 | } | |
| 62 | 62 | ||
| 63 | - | func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | |
| 64 | - | queued := make(chan *glib.SourceFunc, 1) | |
| 65 | - | previousIdleAdd := idleAdd | |
| 66 | - | idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { | |
| 63 | + | func setIdleScheduler(sv *SplitView, queued chan<- *glib.SourceFunc) { | |
| 64 | + | sv.idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { | |
| 67 | 65 | queued <- source | |
| 68 | 66 | return 1 | |
| 69 | 67 | } | |
| 70 | - | t.Cleanup(func() { idleAdd = previousIdleAdd }) | |
| 68 | + | } | |
| 71 | 69 | ||
| 70 | + | func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | |
| 71 | + | queued := make(chan *glib.SourceFunc, 1) | |
| 72 | 72 | sv, notify := newNotifyingSplitView(t) | |
| 73 | + | setIdleScheduler(sv, queued) | |
| 73 | 74 | var calls atomic.Int32 | |
| 74 | 75 | sv.SetOnRatioChanged(func(float64) { calls.Add(1) }) | |
| 75 | 76 | ||
| @@ -83,14 +84,8 @@ func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | |||
| 83 | 84 | ||
| 84 | 85 | func TestSplitViewSupersededRatioCallbackDeliversOnlyLatestValueOnce(t *testing.T) { | |
| 85 | 86 | queued := make(chan *glib.SourceFunc, 2) | |
| 86 | - | previousIdleAdd := idleAdd | |
| 87 | - | idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { | |
| 88 | - | queued <- source | |
| 89 | - | return 1 | |
| 90 | - | } | |
| 91 | - | t.Cleanup(func() { idleAdd = previousIdleAdd }) | |
| 92 | - | ||
| 93 | 87 | sv, notify := newNotifyingSplitView(t) | |
| 88 | + | setIdleScheduler(sv, queued) | |
| 94 | 89 | var ratios []float64 | |
| 95 | 90 | sv.SetOnRatioChanged(func(ratio float64) { ratios = append(ratios, ratio) }) | |
| 96 | 91 | ||
| @@ -103,3 +98,26 @@ func TestSplitViewSupersededRatioCallbackDeliversOnlyLatestValueOnce(t *testing. | |||
| 103 | 98 | ||
| 104 | 99 | require.Equal(t, []float64{0.8}, ratios) | |
| 105 | 100 | } | |
| 101 | + | ||
| 102 | + | func TestSplitViewIdleSchedulersAreInstanceScoped(t *testing.T) { | |
| 103 | + | firstQueued := make(chan *glib.SourceFunc, 1) | |
| 104 | + | secondQueued := make(chan *glib.SourceFunc, 1) | |
| 105 | + | first, firstNotify := newNotifyingSplitView(t) | |
| 106 | + | second, secondNotify := newNotifyingSplitView(t) | |
| 107 | + | setIdleScheduler(first, firstQueued) | |
| 108 | + | setIdleScheduler(second, secondQueued) | |
| 109 | + | ||
| 110 | + | firstNotify(20) | |
| 111 | + | secondNotify(80) | |
| 112 | + | ||
| 113 | + | firstSource := receiveQueuedIdle(t, firstQueued) | |
| 114 | + | secondSource := receiveQueuedIdle(t, secondQueued) | |
| 115 | + | var firstCalls, secondCalls atomic.Int32 | |
| 116 | + | first.SetOnRatioChanged(func(float64) { firstCalls.Add(1) }) | |
| 117 | + | second.SetOnRatioChanged(func(float64) { secondCalls.Add(1) }) | |
| 118 | + | (*firstSource)(0) | |
| 119 | + | (*secondSource)(0) | |
| 120 | + | ||
| 121 | + | require.Equal(t, int32(1), firstCalls.Load()) | |
| 122 | + | require.Equal(t, int32(1), secondCalls.Load()) | |
| 123 | + | } | |
| @@ -60,16 +60,17 @@ func receiveQueuedIdle(t *testing.T, queued <-chan *glib.SourceFunc) *glib.Sourc | |||||
| 60 | } | 60 | } | ||
| 61 | } | 61 | } | ||
| 62 | 62 | ||||
| 63 | - | func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | 63 | + | func setIdleScheduler(sv *SplitView, queued chan<- *glib.SourceFunc) { |
| 64 | - | queued := make(chan *glib.SourceFunc, 1) | 64 | + | sv.idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { |
| 65 | - | previousIdleAdd := idleAdd | |||
| 66 | - | idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { | |||
| 67 | queued <- source | 65 | queued <- source | ||
| 68 | return 1 | 66 | return 1 | ||
| 69 | } | 67 | } | ||
| 70 | - | t.Cleanup(func() { idleAdd = previousIdleAdd }) | 68 | + | } |
| 71 | 69 | ||||
| 70 | + | func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | |||
| 71 | + | queued := make(chan *glib.SourceFunc, 1) | |||
| 72 | sv, notify := newNotifyingSplitView(t) | 72 | sv, notify := newNotifyingSplitView(t) | ||
| 73 | + | setIdleScheduler(sv, queued) | |||
| 73 | var calls atomic.Int32 | 74 | var calls atomic.Int32 | ||
| 74 | sv.SetOnRatioChanged(func(float64) { calls.Add(1) }) | 75 | sv.SetOnRatioChanged(func(float64) { calls.Add(1) }) | ||
| 75 | 76 | ||||
| @@ -83,14 +84,8 @@ func TestSplitViewQueuedRatioCallbackDoesNotRunAfterCleanup(t *testing.T) { | |||||
| 83 | 84 | ||||
| 84 | func TestSplitViewSupersededRatioCallbackDeliversOnlyLatestValueOnce(t *testing.T) { | 85 | func TestSplitViewSupersededRatioCallbackDeliversOnlyLatestValueOnce(t *testing.T) { | ||
| 85 | queued := make(chan *glib.SourceFunc, 2) | 86 | queued := make(chan *glib.SourceFunc, 2) | ||
| 86 | - | previousIdleAdd := idleAdd | |||
| 87 | - | idleAdd = func(source *glib.SourceFunc, _ uintptr) uint { | |||
| 88 | - | queued <- source | |||
| 89 | - | return 1 | |||
| 90 | - | } | |||
| 91 | - | t.Cleanup(func() { idleAdd = previousIdleAdd }) | |||
| 92 | - | ||||
| 93 | sv, notify := newNotifyingSplitView(t) | 87 | sv, notify := newNotifyingSplitView(t) | ||
| 88 | + | setIdleScheduler(sv, queued) | |||
| 94 | var ratios []float64 | 89 | var ratios []float64 | ||
| 95 | sv.SetOnRatioChanged(func(ratio float64) { ratios = append(ratios, ratio) }) | 90 | sv.SetOnRatioChanged(func(ratio float64) { ratios = append(ratios, ratio) }) | ||
| 96 | 91 | ||||
| @@ -103,3 +98,26 @@ func TestSplitViewSupersededRatioCallbackDeliversOnlyLatestValueOnce(t *testing. | |||||
| 103 | 98 | ||||
| 104 | require.Equal(t, []float64{0.8}, ratios) | 99 | require.Equal(t, []float64{0.8}, ratios) | ||
| 105 | } | 100 | } | ||
| 101 | + | ||||
| 102 | + | func TestSplitViewIdleSchedulersAreInstanceScoped(t *testing.T) { | |||
| 103 | + | firstQueued := make(chan *glib.SourceFunc, 1) | |||
| 104 | + | secondQueued := make(chan *glib.SourceFunc, 1) | |||
| 105 | + | first, firstNotify := newNotifyingSplitView(t) | |||
| 106 | + | second, secondNotify := newNotifyingSplitView(t) | |||
| 107 | + | setIdleScheduler(first, firstQueued) | |||
| 108 | + | setIdleScheduler(second, secondQueued) | |||
| 109 | + | ||||
| 110 | + | firstNotify(20) | |||
| 111 | + | secondNotify(80) | |||
| 112 | + | ||||
| 113 | + | firstSource := receiveQueuedIdle(t, firstQueued) | |||
| 114 | + | secondSource := receiveQueuedIdle(t, secondQueued) | |||
| 115 | + | var firstCalls, secondCalls atomic.Int32 | |||
| 116 | + | first.SetOnRatioChanged(func(float64) { firstCalls.Add(1) }) | |||
| 117 | + | second.SetOnRatioChanged(func(float64) { secondCalls.Add(1) }) | |||
| 118 | + | (*firstSource)(0) | |||
| 119 | + | (*secondSource)(0) | |||
| 120 | + | ||||
| 121 | + | require.Equal(t, int32(1), firstCalls.Load()) | |||
| 122 | + | require.Equal(t, int32(1), secondCalls.Load()) | |||
| 123 | + | } | |||