Skip to content

Commit d72bc86

Browse files
committed
enh(sync): add useDelayedFlag
Allow changing a flag with different delays: * Switch it on slow. * Switch it off fast. Useful for errors - so they only show after a while but are resolved quickly. Example: const input = ref(false) const { delayed } = useDelayedFlag(input) ... input.value = true // 5 seconds later delayed.value will be true ... input.value = false // 200 ms later delayed.value will be true Signed-off-by: Max <max@nextcloud.com>
1 parent 7d6c69b commit d72bc86

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { ref, watch, type Ref } from 'vue'
7+
8+
/**
9+
* Delay the changing of the boolean
10+
* @param input - ref to react to
11+
*/
12+
export function useDelayedFlag(input: Ref<boolean>): { delayed: Ref<boolean> } {
13+
14+
let timeout: ReturnType<typeof setTimeout> | undefined
15+
const delayed = ref(input.value)
16+
17+
watch(input, (val) => {
18+
if (timeout) {
19+
clearTimeout(timeout)
20+
}
21+
const delay = val ? 5000 : 200
22+
timeout = setTimeout(() => {
23+
delayed.value = val
24+
}, delay)
25+
26+
})
27+
28+
return { delayed }
29+
}

0 commit comments

Comments
 (0)