@@ -2,6 +2,7 @@ import omit from 'lodash.omit'
22import { print } from 'graphql-tag/printer'
33import { SmartQuery , SmartSubscription } from './smart-apollo'
44
5+ let Vue
56let apolloClient = null
67
78let defineReactive = function ( ) { }
@@ -23,6 +24,9 @@ function addGraphQLSubscriptions (networkInterface, wsClient) {
2324
2425class DollarApollo {
2526 constructor ( vm ) {
27+ this . _apolloSubscriptions = [ ]
28+ this . _watchers = [ ]
29+
2630 this . vm = vm
2731 this . queries = { }
2832 this . subscriptions = { }
@@ -37,12 +41,11 @@ class DollarApollo {
3741 }
3842
3943 watchQuery ( options ) {
40- const vm = this . vm
4144 const observable = this . client . watchQuery ( options )
4245 const _subscribe = observable . subscribe . bind ( observable )
43- observable . subscribe = function ( options ) {
46+ observable . subscribe = ( options ) => {
4447 let sub = _subscribe ( options )
45- vm . _apolloSubscriptions . push ( sub )
48+ this . _apolloSubscriptions . push ( sub )
4649 return sub
4750 }
4851 return observable
@@ -53,12 +56,11 @@ class DollarApollo {
5356 }
5457
5558 subscribe ( options ) {
56- const vm = this . vm
5759 const observable = this . client . subscribe ( options )
5860 const _subscribe = observable . subscribe . bind ( observable )
59- observable . subscribe = function ( options ) {
61+ observable . subscribe = ( options ) => {
6062 let sub = _subscribe ( options )
61- vm . _apolloSubscriptions . push ( sub )
63+ this . _apolloSubscriptions . push ( sub )
6264 return sub
6365 }
6466 return observable
@@ -71,11 +73,52 @@ class DollarApollo {
7173 subscribeOption ( key , options ) {
7274 this . subscriptions [ key ] = new SmartSubscription ( this . vm , key , options )
7375 }
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+ }
74119}
75120
76121const prepare = function prepare ( ) {
77- this . _apolloSubscriptions = [ ]
78-
79122 // Lazy creation
80123 Object . defineProperty ( this , '$apollo' , {
81124 get : ( ) => {
@@ -91,6 +134,10 @@ const prepare = function prepare () {
91134 if ( apollo ) {
92135 this . _apolloQueries = omit ( apollo , [
93136 'subscribe' ,
137+ '$subscribe' ,
138+ '$skipAll' ,
139+ '$skipAllQueries' ,
140+ '$skipAllSubscriptions' ,
94141 ] )
95142
96143 // watchQuery
@@ -110,19 +157,42 @@ const launch = function launch () {
110157 }
111158
112159 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
116186 }
117187 }
118188}
119189
120190module . exports = {
121191 addGraphQLSubscriptions,
122192
123- install ( Vue , options ) {
193+ install ( pVue , options ) {
194+ Vue = pVue
124195 defineReactive = Vue . util . defineReactive
125-
126196 apolloClient = options . apolloClient
127197
128198 Vue . mixin ( {
@@ -135,11 +205,8 @@ module.exports = {
135205 created : launch ,
136206
137207 destroyed : function ( ) {
138- this . _apolloSubscriptions . forEach ( ( sub ) => {
139- sub . unsubscribe ( )
140- } )
141- this . _apolloSubscriptions = null
142208 if ( this . _apollo ) {
209+ this . _apollo . destroy ( )
143210 this . _apollo = null
144211 }
145212 } ,
0 commit comments