@@ -7,46 +7,12 @@ export default class SmartApollo {
77 constructor ( vm , key , options , autostart = true ) {
88 this . vm = vm
99 this . key = key
10+ this . initialOptions = options
1011 this . options = Object . assign ( { } , options )
1112 this . _skip = false
1213 this . _watchers = [ ]
1314 this . _destroyed = false
1415
15- // Query callback
16- if ( typeof this . options . query === 'function' ) {
17- const queryCb = this . options . query . bind ( this . vm )
18- this . options . query = queryCb ( )
19- this . _watchers . push ( this . vm . $watch ( queryCb , query => {
20- this . options . query = query
21- this . refresh ( )
22- } , {
23- deep : this . options . deep ,
24- } ) )
25- }
26- // Query callback
27- if ( typeof this . options . document === 'function' ) {
28- const queryCb = this . options . document . bind ( this . vm )
29- this . options . document = queryCb ( )
30- this . _watchers . push ( this . vm . $watch ( queryCb , document => {
31- this . options . document = document
32- this . refresh ( )
33- } , {
34- deep : this . options . deep ,
35- } ) )
36- }
37-
38- // Apollo context
39- if ( typeof this . options . context === 'function' ) {
40- const cb = this . options . context . bind ( this . vm )
41- this . options . context = cb ( )
42- this . _watchers . push ( this . vm . $watch ( cb , context => {
43- this . options . context = context
44- this . refresh ( )
45- } , {
46- deep : this . options . deep ,
47- } ) )
48- }
49-
5016 if ( this . vm . $isServer ) {
5117 this . options . fetchPolicy = 'cache-first'
5218 }
@@ -58,10 +24,10 @@ export default class SmartApollo {
5824
5925 autostart ( ) {
6026 if ( typeof this . options . skip === 'function' ) {
61- this . _watchers . push ( this . vm . $watch ( this . options . skip . bind ( this . vm ) , this . skipChanged . bind ( this ) , {
27+ this . _skipWatcher = this . vm . $watch ( this . options . skip . bind ( this . vm ) , this . skipChanged . bind ( this ) , {
6228 immediate : true ,
6329 deep : this . options . deep ,
64- } ) )
30+ } )
6531 } else if ( ! this . options . skip ) {
6632 this . start ( )
6733 } else {
@@ -97,23 +63,59 @@ export default class SmartApollo {
9763
9864 start ( ) {
9965 this . starting = true
66+
67+ // Query callback
68+ if ( typeof this . initialOptions . query === 'function' ) {
69+ const queryCb = this . initialOptions . query . bind ( this . vm )
70+ this . options . query = queryCb ( )
71+ this . _watchers . push ( this . vm . $watch ( queryCb , query => {
72+ this . options . query = query
73+ this . refresh ( )
74+ } , {
75+ deep : this . options . deep ,
76+ } ) )
77+ }
78+ // Query callback
79+ if ( typeof this . initialOptions . document === 'function' ) {
80+ const queryCb = this . initialOptions . document . bind ( this . vm )
81+ this . options . document = queryCb ( )
82+ this . _watchers . push ( this . vm . $watch ( queryCb , document => {
83+ this . options . document = document
84+ this . refresh ( )
85+ } , {
86+ deep : this . options . deep ,
87+ } ) )
88+ }
89+
90+ // Apollo context
91+ if ( typeof this . initialOptions . context === 'function' ) {
92+ const cb = this . initialOptions . context . bind ( this . vm )
93+ this . options . context = cb ( )
94+ this . _watchers . push ( this . vm . $watch ( cb , context => {
95+ this . options . context = context
96+ this . refresh ( )
97+ } , {
98+ deep : this . options . deep ,
99+ } ) )
100+ }
101+
102+ // GraphQL Variables
100103 if ( typeof this . options . variables === 'function' ) {
101104 let cb = this . executeApollo . bind ( this )
102105 cb = this . options . throttle ? throttle ( cb , this . options . throttle ) : cb
103106 cb = this . options . debounce ? debounce ( cb , this . options . debounce ) : cb
104- this . unwatchVariables = this . vm . $watch ( ( ) => this . options . variables . call ( this . vm ) , cb , {
107+ this . _watchers . push ( this . vm . $watch ( ( ) => this . options . variables . call ( this . vm ) , cb , {
105108 immediate : true ,
106109 deep : this . options . deep ,
107- } )
110+ } ) )
108111 } else {
109112 this . executeApollo ( this . options . variables )
110113 }
111114 }
112115
113116 stop ( ) {
114- if ( this . unwatchVariables ) {
115- this . unwatchVariables ( )
116- this . unwatchVariables = null
117+ for ( const unwatch of this . _watchers ) {
118+ unwatch ( )
117119 }
118120
119121 if ( this . sub ) {
@@ -180,8 +182,8 @@ export default class SmartApollo {
180182
181183 this . _destroyed = true
182184 this . stop ( )
183- for ( const unwatch of this . _watchers ) {
184- unwatch ( )
185+ if ( this . _skipWatcher ) {
186+ this . _skipWatcher ( )
185187 }
186188 }
187189}
0 commit comments