Skip to content

Commit 8da32b1

Browse files
committed
feat(subscription): reactive ready boolean
1 parent 5ff6b5a commit 8da32b1

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

demo-app/imports/ui/components/Info.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Links } from '/imports/api/links/links.js'
44
import { subscribe, autoResult } from 'meteor/vuejs:vue3'
55
6-
subscribe('links.all')
6+
const { ready } = subscribe('links.all')
77
88
const links = autoResult(() => Links.find({}))
99
@@ -32,6 +32,9 @@ function submit (form) {
3232
<input type="submit" name="submit" value="Add new link">
3333
</form>
3434
</li>
35+
36+
<div v-if="!ready">Loading...</div>
37+
3538
<li v-for="link of links" :key="link._id">
3639
<a :href="link.url" target="_blank">{{ link.title }}</a>
3740
</li>

packages/vue3/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import { subscribe } from 'meteor/vuejs:vue3'
1515

1616
export default {
1717
setup () {
18-
subscribe('links.all' /*, 'some', 'params', 'here' */)
18+
const { ready, stop } = subscribe('links.all' /*, 'some', 'params', 'here' */)
19+
20+
watch(ready, value => {
21+
console.log('loading:', !value)
22+
})
1923
}
2024
}
2125
```
@@ -75,11 +79,10 @@ Full example:
7579

7680
```vue
7781
<script setup>
78-
// @ts-nocheck
7982
import { Links } from '/imports/api/links/links.js'
8083
import { subscribe, autoResult } from 'meteor/vuejs:vue3'
8184
82-
subscribe('links.all')
85+
const { ready } = subscribe('links.all')
8386
8487
const links = autoResult(() => Links.find({}))
8588
@@ -108,6 +111,9 @@ function submit (form) {
108111
<input type="submit" name="submit" value="Add new link">
109112
</form>
110113
</li>
114+
115+
<div v-if="!ready">Loading...</div>
116+
111117
<li v-for="link of links" :key="link._id">
112118
<a :href="link.url" target="_blank">{{ link.title }}</a>
113119
</li>

packages/vue3/src/index.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Meteor } from 'meteor/meteor'
33
import { computed, ComputedRef, onUnmounted, ref, watch, watchEffect } from 'vue'
44

55
export interface AutorunEffect<TResult> {
6-
result: ComputedRef<TResult | undefined>
6+
result: ComputedRef<TResult>
77
stop: () => void
88
}
99

@@ -22,32 +22,51 @@ export function autorun<TResult = unknown> (callback: () => TResult): AutorunEff
2222
})
2323
})
2424
return {
25-
result: computed<TResult | undefined>(() => result.value),
25+
result: computed<TResult>(() => result.value as TResult),
2626
stop,
2727
}
2828
}
2929

30-
export function autoResult<TResult = unknown> (callback: () => TResult): ComputedRef<TResult | undefined> {
30+
export function autoResult<TResult = unknown> (callback: () => TResult): ComputedRef<TResult> {
3131
return autorun(callback).result
3232
}
3333

34-
export function subscribe (name: string, ...args: any[]): Meteor.SubscriptionHandle {
34+
export interface ReactiveMeteorSubscription {
35+
stop: () => void
36+
ready: ComputedRef<boolean>
37+
}
38+
39+
export function subscribe (name: string, ...args: any[]): ReactiveMeteorSubscription {
3540
const sub = Meteor.subscribe(name, ...args)
36-
onUnmounted(() => {
41+
const ready = autorun(() => sub.ready())
42+
43+
function stop (): void {
44+
ready.stop()
3745
sub.stop()
46+
}
47+
48+
onUnmounted(() => {
49+
stop()
3850
})
39-
return sub
40-
}
4151

42-
export interface AutoSubscribe {
43-
stop: () => void
52+
return {
53+
stop,
54+
ready: ready.result,
55+
}
4456
}
4557

46-
export function autoSubscribe (callback: () => [name: string, ...args: any[]]): AutoSubscribe {
58+
export function autoSubscribe (callback: () => [name: string, ...args: any[]]): ReactiveMeteorSubscription {
59+
const ready = ref(false)
4760
const stop = watch(callback, (value, oldValue, onInvalidate) => {
4861
const sub = Meteor.subscribe(...value)
62+
63+
const computation = Tracker.autorun(() => {
64+
ready.value = sub.ready()
65+
})
66+
4967
onInvalidate(() => {
5068
sub.stop()
69+
computation.stop()
5170
})
5271
}, {
5372
immediate: true,
@@ -56,5 +75,6 @@ export function autoSubscribe (callback: () => [name: string, ...args: any[]]):
5675

5776
return {
5877
stop,
78+
ready: computed(() => ready.value),
5979
}
6080
}

0 commit comments

Comments
 (0)