Skip to content

Commit 9ac722e

Browse files
Merge pull request #2204 from MetRonnie/save-layout
Workspace layouts: avoid duplicate layout saving when resetting to default
2 parents d4dd9d3 + ef6e80b commit 9ac722e

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

src/components/cylc/workspace/Lumino.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,21 @@ async function restoreLayout () {
246246
* Reset the workspace layout to a single tab with the default view.
247247
*/
248248
async function resetToDefault () {
249-
layoutWatcher.pause()
250-
await closeAllViews()
249+
await layoutWatcher.ignore(closeAllViews)
251250
await addView({ name: defaultView.value })
252-
// (addView resumes the layout watcher)
253251
}
254252
255253
/**
256254
* React to a deleted event.
257255
*
258256
* @param {string} id - widget ID
259257
*/
260-
const onWidgetDeleted = (id) => {
261-
// layoutWatcher will be triggered by DockPanel.layoutModified, so pause to avoid duplicate trigger:
262-
layoutWatcher.pause()
263-
views.value.delete(id)
264-
layoutWatcher.resume()
265-
if (!views.value.size) {
266-
emit('emptied')
267-
}
258+
function onWidgetDeleted (id) {
259+
layoutWatcher.ignore(() => {
260+
views.value.delete(id)
261+
if (!views.value.size) {
262+
emit('emptied')
263+
}
264+
})
268265
}
269266
</script>

src/utils/reactivity.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function once (source, options = {}) {
7777
}
7878

7979
/**
80-
* Provides a controlled watcher with pause, resume, and manual trigger capabilities.
80+
* Provides a controlled watcher with pause, resume, ignore and manual trigger and capabilities.
8181
*
8282
* Unlike the standard Vue `watch()`, resuming a paused watcher will not trigger the callback immediately.
8383
* Using the `trigger()` method only works if the watcher is not paused.
@@ -96,6 +96,15 @@ export function watchWithControl (source, callback, options = {}) {
9696
resume () {
9797
watchHandle ??= doWatch()
9898
},
99+
async ignore (cb) {
100+
if (watchHandle) {
101+
this.pause()
102+
await cb()
103+
this.resume()
104+
} else {
105+
await cb()
106+
}
107+
},
99108
trigger () {
100109
if (watchHandle) { // Only trigger if not paused
101110
callback()

tests/unit/utils/reactivity.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,35 @@ describe('watchWithControl()', () => {
118118
watcher.trigger()
119119
expect(callback).toHaveBeenCalledTimes(2)
120120
})
121+
122+
it('allows ignoring changes during a callback', async () => {
123+
const source = ref(0)
124+
const callback = vi.fn()
125+
const watcher = watchWithControl(source, callback)
126+
127+
await watcher.ignore(() => {
128+
source.value++
129+
})
130+
expect(source.value).toEqual(1)
131+
await nextTick()
132+
expect(callback).toHaveBeenCalledTimes(0)
133+
134+
source.value++
135+
expect(source.value).toEqual(2)
136+
await nextTick()
137+
expect(callback).toHaveBeenCalledTimes(1)
138+
139+
watcher.pause()
140+
// Check ignore changes while paused does not inadvertently resume the watcher
141+
await watcher.ignore(() => {
142+
source.value++
143+
})
144+
expect(source.value).toEqual(3)
145+
await nextTick()
146+
expect(callback).toHaveBeenCalledTimes(1)
147+
source.value++
148+
expect(source.value).toEqual(4)
149+
await nextTick()
150+
expect(callback).toHaveBeenCalledTimes(1)
151+
})
121152
})

0 commit comments

Comments
 (0)