@@ -2,6 +2,7 @@ import omit from 'lodash.omit'
2
2
import { print } from 'graphql-tag/printer'
3
3
import { SmartQuery , SmartSubscription } from './smart-apollo'
4
4
5
+ let Vue
5
6
let apolloClient = null
6
7
7
8
let defineReactive = function ( ) { }
@@ -23,6 +24,9 @@ function addGraphQLSubscriptions (networkInterface, wsClient) {
23
24
24
25
class DollarApollo {
25
26
constructor ( vm ) {
27
+ this . _apolloSubscriptions = [ ]
28
+ this . _watchers = [ ]
29
+
26
30
this . vm = vm
27
31
this . queries = { }
28
32
this . subscriptions = { }
@@ -37,12 +41,11 @@ class DollarApollo {
37
41
}
38
42
39
43
watchQuery ( options ) {
40
- const vm = this . vm
41
44
const observable = this . client . watchQuery ( options )
42
45
const _subscribe = observable . subscribe . bind ( observable )
43
- observable . subscribe = function ( options ) {
46
+ observable . subscribe = ( options ) => {
44
47
let sub = _subscribe ( options )
45
- vm . _apolloSubscriptions . push ( sub )
48
+ this . _apolloSubscriptions . push ( sub )
46
49
return sub
47
50
}
48
51
return observable
@@ -53,12 +56,11 @@ class DollarApollo {
53
56
}
54
57
55
58
subscribe ( options ) {
56
- const vm = this . vm
57
59
const observable = this . client . subscribe ( options )
58
60
const _subscribe = observable . subscribe . bind ( observable )
59
- observable . subscribe = function ( options ) {
61
+ observable . subscribe = ( options ) => {
60
62
let sub = _subscribe ( options )
61
- vm . _apolloSubscriptions . push ( sub )
63
+ this . _apolloSubscriptions . push ( sub )
62
64
return sub
63
65
}
64
66
return observable
@@ -71,11 +73,52 @@ class DollarApollo {
71
73
subscribeOption ( key , options ) {
72
74
this . subscriptions [ key ] = new SmartSubscription ( this . vm , key , options )
73
75
}
76
+
77
+ defineReactiveSetter ( key , func ) {
78
+ this . _watchers . push ( this . vm . $watch ( func , value => {
79
+ console . log ( value )
80
+ this [ key ] = value
81
+ } , {
82
+ immediate : true ,
83
+ } ) )
84
+ }
85
+
86
+ set skipAllQueries ( value ) {
87
+ for ( let key in this . queries ) {
88
+ this . queries [ key ] . skip = value
89
+ }
90
+ }
91
+
92
+ set skipAllSubscriptions ( value ) {
93
+ for ( let key in this . subscriptions ) {
94
+ this . subscriptions [ key ] . skip = value
95
+ }
96
+ }
97
+
98
+ set skipAll ( value ) {
99
+ this . skipAllQueries = value
100
+ this . skipAllSubscriptions = value
101
+ }
102
+
103
+ destroy ( ) {
104
+ for ( const unwatch of this . _watchers ) {
105
+ unwatch ( )
106
+ }
107
+ for ( let key in this . queries ) {
108
+ this . queries [ key ] . destroy ( )
109
+ }
110
+ for ( let key in this . subscriptions ) {
111
+ this . subscriptions [ key ] . destroy ( )
112
+ }
113
+ this . _apolloSubscriptions . forEach ( ( sub ) => {
114
+ sub . unsubscribe ( )
115
+ } )
116
+ this . _apolloSubscriptions = null
117
+ this . vm = null
118
+ }
74
119
}
75
120
76
121
const prepare = function prepare ( ) {
77
- this . _apolloSubscriptions = [ ]
78
-
79
122
// Lazy creation
80
123
Object . defineProperty ( this , '$apollo' , {
81
124
get : ( ) => {
@@ -91,6 +134,10 @@ const prepare = function prepare () {
91
134
if ( apollo ) {
92
135
this . _apolloQueries = omit ( apollo , [
93
136
'subscribe' ,
137
+ '$subscribe' ,
138
+ '$skipAll' ,
139
+ '$skipAllQueries' ,
140
+ '$skipAllSubscriptions' ,
94
141
] )
95
142
96
143
// watchQuery
@@ -110,19 +157,42 @@ const launch = function launch () {
110
157
}
111
158
112
159
let apollo = this . $options . apollo
113
- if ( apollo && apollo . subscribe ) {
114
- for ( let key in apollo . subscribe ) {
115
- this . $apollo . subscribeOption ( key , apollo . subscribe [ key ] )
160
+ if ( apollo ) {
161
+ if ( apollo . subscribe ) {
162
+ Vue . util . warn ( 'vue-apollo -> `subscribe` option is deprecated. Use the `$subscribe` option instead.' )
163
+ for ( let key in apollo . subscribe ) {
164
+ this . $apollo . subscribeOption ( key , apollo . subscribe [ key ] )
165
+ }
166
+ }
167
+
168
+ if ( apollo . $subscribe ) {
169
+ for ( let key in apollo . $subscribe ) {
170
+ this . $apollo . subscribeOption ( key , apollo . $subscribe [ key ] )
171
+ }
172
+ }
173
+
174
+ defineReactiveSetter ( this . $apollo , 'skipAll' , apollo . $skipAll )
175
+ defineReactiveSetter ( this . $apollo , 'skipAllQueries' , apollo . $skipAllQueries )
176
+ defineReactiveSetter ( this . $apollo , 'skipAllSubscriptions' , apollo . $skipAllSubscriptions )
177
+ }
178
+ }
179
+
180
+ function defineReactiveSetter ( $apollo , key , value ) {
181
+ if ( typeof value !== 'undefined' ) {
182
+ if ( typeof value === 'function' ) {
183
+ $apollo . defineReactiveSetter ( key , value )
184
+ } else {
185
+ $apollo [ key ] = value
116
186
}
117
187
}
118
188
}
119
189
120
190
module . exports = {
121
191
addGraphQLSubscriptions,
122
192
123
- install ( Vue , options ) {
193
+ install ( pVue , options ) {
194
+ Vue = pVue
124
195
defineReactive = Vue . util . defineReactive
125
-
126
196
apolloClient = options . apolloClient
127
197
128
198
Vue . mixin ( {
@@ -135,11 +205,8 @@ module.exports = {
135
205
created : launch ,
136
206
137
207
destroyed : function ( ) {
138
- this . _apolloSubscriptions . forEach ( ( sub ) => {
139
- sub . unsubscribe ( )
140
- } )
141
- this . _apolloSubscriptions = null
142
208
if ( this . _apollo ) {
209
+ this . _apollo . destroy ( )
143
210
this . _apollo = null
144
211
}
145
212
} ,
0 commit comments