|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import type { MaybeRefOrGetter, WatchCallback, WatchOptions, WatchStopHandle } from 'vue' |
| 6 | +import type { MaybeRefOrGetter, WatchCallback, WatchStopHandle } from 'vue' |
7 | 7 |
|
8 | | -import { createSharedComposable } from '@vueuse/core' |
9 | | -import { onBeforeMount, onBeforeUnmount, readonly, ref, toValue, watch } from 'vue' |
| 8 | +import { createSharedComposable, whenever } from '@vueuse/core' |
| 9 | +import { onBeforeMount, onBeforeUnmount, readonly, ref, toValue } from 'vue' |
10 | 10 | import { EventBus } from '../services/EventBus.ts' |
11 | 11 | import SessionStorage from '../services/SessionStorage.js' |
12 | 12 |
|
@@ -39,24 +39,24 @@ export const useJoinedConversation = createSharedComposable(useJoinedConversatio |
39 | 39 |
|
40 | 40 | /** |
41 | 41 | * Watch for the current joined conversation matching the provided token. |
| 42 | + * Fires immediately if already matching, and stops after the first match |
42 | 43 | * |
43 | 44 | * @param token token to match against the joined conversation |
44 | 45 | * @param callback callback triggered when the joined conversation matches the token |
45 | | - * @param options watch options |
46 | 46 | */ |
47 | 47 | export function watchJoinedConversation( |
48 | 48 | token: MaybeRefOrGetter<string | null>, |
49 | | - callback: WatchCallback<string, string | null | undefined>, |
50 | | - options?: WatchOptions, |
| 49 | + callback: WatchCallback<string, string | undefined>, |
51 | 50 | ): WatchStopHandle { |
52 | 51 | const currentJoinedConversation = useJoinedConversation() |
53 | 52 |
|
54 | | - return watch(currentJoinedConversation, (newToken, oldToken, onCleanup) => { |
| 53 | + // Getter resolves to the matching token / undefined |
| 54 | + // `whenever()` only invokes the callback and stops watching on a truthy value. |
| 55 | + return whenever<string | undefined>(() => { |
55 | 56 | const targetToken = toValue(token) |
56 | | - if (!targetToken || newToken !== targetToken) { |
| 57 | + if (!targetToken || currentJoinedConversation.value !== targetToken) { |
57 | 58 | return |
58 | 59 | } |
59 | | - |
60 | | - callback(newToken, oldToken, onCleanup) |
61 | | - }, options) |
| 60 | + return targetToken |
| 61 | + }, callback, { immediate: true, once: true }) |
62 | 62 | } |
0 commit comments