@@ -3,7 +3,7 @@ import { Meteor } from 'meteor/meteor'
3
3
import { computed , ComputedRef , onUnmounted , ref , watch , watchEffect } from 'vue'
4
4
5
5
export interface AutorunEffect < TResult > {
6
- result : ComputedRef < TResult | undefined >
6
+ result : ComputedRef < TResult >
7
7
stop : ( ) => void
8
8
}
9
9
@@ -22,32 +22,51 @@ export function autorun<TResult = unknown> (callback: () => TResult): AutorunEff
22
22
} )
23
23
} )
24
24
return {
25
- result : computed < TResult | undefined > ( ( ) => result . value ) ,
25
+ result : computed < TResult > ( ( ) => result . value as TResult ) ,
26
26
stop,
27
27
}
28
28
}
29
29
30
- export function autoResult < TResult = unknown > ( callback : ( ) => TResult ) : ComputedRef < TResult | undefined > {
30
+ export function autoResult < TResult = unknown > ( callback : ( ) => TResult ) : ComputedRef < TResult > {
31
31
return autorun ( callback ) . result
32
32
}
33
33
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 {
35
40
const sub = Meteor . subscribe ( name , ...args )
36
- onUnmounted ( ( ) => {
41
+ const ready = autorun ( ( ) => sub . ready ( ) )
42
+
43
+ function stop ( ) : void {
44
+ ready . stop ( )
37
45
sub . stop ( )
46
+ }
47
+
48
+ onUnmounted ( ( ) => {
49
+ stop ( )
38
50
} )
39
- return sub
40
- }
41
51
42
- export interface AutoSubscribe {
43
- stop : ( ) => void
52
+ return {
53
+ stop,
54
+ ready : ready . result ,
55
+ }
44
56
}
45
57
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 )
47
60
const stop = watch ( callback , ( value , oldValue , onInvalidate ) => {
48
61
const sub = Meteor . subscribe ( ...value )
62
+
63
+ const computation = Tracker . autorun ( ( ) => {
64
+ ready . value = sub . ready ( )
65
+ } )
66
+
49
67
onInvalidate ( ( ) => {
50
68
sub . stop ( )
69
+ computation . stop ( )
51
70
} )
52
71
} , {
53
72
immediate : true ,
@@ -56,5 +75,6 @@ export function autoSubscribe (callback: () => [name: string, ...args: any[]]):
56
75
57
76
return {
58
77
stop,
78
+ ready : computed ( ( ) => ready . value ) ,
59
79
}
60
80
}
0 commit comments