-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.d.ts
67 lines (62 loc) · 1.5 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Ref } from 'vue'
import { LoguxVuexStore } from '../index.js'
export type Channel =
| string
| {
channel: string
[key: string]: any
}
export type Channels = Channel[] | Ref<Channel[]>
export interface useSubscriptionOptions {
/**
* Logux Vuex store.
*/
store?: LoguxVuexStore
/**
* Delay in milliseconds to avoid returning `true` when switching between `channels`.
*/
debounce?: number
}
/**
* Composable function that subscribes
* for channels during component initialization
* and unsubscribes on unmount.
*
* It watches for `channels` changes
* and returns `isSubscribing` flag that indicates loading state.
*
* ```html
* <template>
* <div v-if="isSubscribing">Loading</div>
* <h1 v-else>{{ user.name }}</h1>
* </template>
*
* <script>
* import { computed, toRefs } from 'vue'
* import { useStore, useSubscription } from '@logux/vuex'
*
* export default {
* props: ['userId'],
* setup (props) {
* let store = useStore()
* let { userId } = toRefs(props)
*
* let channels = computed(() => [`user/${userId.value}`])
* let isSubscribing = useSubscription(channels)
*
* let user = computed(() => store.state.users[userId.value])
*
* return { isSubscribing, user }
* }
* })
* </script>
*
* ```
* @param channels Channels to subscribe.
* @param options Options.
* @return `true` during data loading.
*/
export function useSubscription(
channels: Channels,
options?: useSubscriptionOptions
): Ref<boolean>