Skip to content

Commit f1c1526

Browse files
committed
Fix when not working for non-ref source when immediate
1 parent 45d257a commit f1c1526

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

src/components/cylc/Drawer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import Header from '@/components/cylc/Header.vue'
8787
import Workflows from '@/views/Workflows.vue'
8888
import { mdiHome, mdiInformationOutline } from '@mdi/js'
8989
import pkg from '@/../package.json'
90-
import { when } from '@/utils'
90+
import { when } from '@/utils/reactivity'
9191
import { useDrawer } from '@/utils/toolbar'
9292
9393
export const initialWidth = 260

src/components/cylc/log/Log.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4242
<script>
4343
import { useTemplateRef, watch, onBeforeUnmount, nextTick } from 'vue'
4444
import { useScroll, useVModel, whenever } from '@vueuse/core'
45-
import { when } from '@/utils'
45+
import { when } from '@/utils/reactivity'
4646
import {
4747
mdiMouseMoveUp
4848
} from '@mdi/js'

src/components/cylc/tree/TreeItem.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ import {
176176
isFlowNone,
177177
} from '@/utils/tasks'
178178
import { getIndent, getNodeChildren } from '@/components/cylc/tree/util'
179-
import { once } from '@/utils'
179+
import { once } from '@/utils/reactivity'
180180
import { useToggle } from '@vueuse/core'
181181
import FlowNumsChip from '@/components/cylc/common/FlowNumsChip.vue'
182182

src/components/cylc/workspace/Toolbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ import {
210210
mdiAccount
211211
} from '@mdi/js'
212212
import { startCase } from 'lodash'
213-
import { until } from '@/utils'
213+
import { until } from '@/utils/reactivity'
214214
import { useDrawer, useNavBtn, toolbarHeight } from '@/utils/toolbar'
215215
import WorkflowState from '@/model/WorkflowState.model'
216216
import graphql from '@/mixins/graphql'

src/utils/index.js renamed to src/utils/reactivity.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { ref, watch } from 'vue'
18+
import { ref, toValue, watch } from 'vue'
1919

2020
/**
2121
* Watch source until it is truthy, then call the callback (and stop watching).
@@ -27,7 +27,8 @@ import { ref, watch } from 'vue'
2727
* @param {import('vue').WatchOptions?} options
2828
*/
2929
export function when (source, callback, options = {}) {
30-
if (source.value) {
30+
const { immediate = true } = options
31+
if (immediate && toValue(source)) {
3132
callback()
3233
return
3334
}
@@ -39,7 +40,7 @@ export function when (source, callback, options = {}) {
3940
callback()
4041
}
4142
},
42-
options
43+
{ ...options, immediate: false }
4344
)
4445
}
4546

tests/unit/utils/index.spec.js renamed to tests/unit/utils/reactivity.spec.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { nextTick, ref } from 'vue'
19-
import { once, when, until } from '@/utils/index'
18+
import { nextTick, ref, computed } from 'vue'
19+
import { once, when, until } from '@/utils/reactivity'
20+
21+
const truthySources = () => [
22+
ref(true),
23+
computed(() => true),
24+
() => true,
25+
]
2026

2127
describe.each([
2228
{ func: when, description: 'watches source until true and then stops watching' },
@@ -44,16 +50,15 @@ describe.each([
4450
})
4551

4652
describe('when()', () => {
47-
it('works for a source that is already truthy', () => {
48-
const source = ref(true)
53+
it.for(truthySources())('works for already-truthy source %s', (source) => {
4954
let counter = 0
5055
when(source, () => counter++)
5156
expect(counter).toEqual(1)
5257
})
5358
})
5459

5560
describe('once()', () => {
56-
it('returns a ref that permanently toggles to true when the source bevomes truthy', async () => {
61+
it('returns a ref that permanently toggles to true when the source becomes truthy', async () => {
5762
const source = ref(false)
5863
const myRef = once(source)
5964
expect(myRef.value).toEqual(false)
@@ -65,8 +70,7 @@ describe('once()', () => {
6570
expect(myRef.value).toEqual(true)
6671
})
6772

68-
it('works for a source that is already truthy', () => {
69-
const source = ref(true)
73+
it.for(truthySources())('works for already-truthy source %s', (source) => {
7074
const myRef = once(source)
7175
expect(myRef.value).toEqual(true)
7276
})

0 commit comments

Comments
 (0)