@@ -5,10 +5,18 @@ import { SmartQuery, SmartSubscription } from './smart-apollo'
55let Vue
66let apolloClient = null
77
8+ const keywords = [
9+ 'subscribe' ,
10+ '$subscribe' ,
11+ '$skipAll' ,
12+ '$skipAllQueries' ,
13+ '$skipAllSubscriptions' ,
14+ ]
15+
816let defineReactive = function ( ) { }
917
1018// quick way to add the subscribe and unsubscribe functions to the network interface
11- function addGraphQLSubscriptions ( networkInterface , wsClient ) {
19+ export function addGraphQLSubscriptions ( networkInterface , wsClient ) {
1220 return Object . assign ( networkInterface , {
1321 subscribe ( request , handler ) {
1422 return wsClient . subscribe ( {
@@ -76,7 +84,6 @@ class DollarApollo {
7684
7785 defineReactiveSetter ( key , func ) {
7886 this . _watchers . push ( this . vm . $watch ( func , value => {
79- console . log ( value )
8087 this [ key ] = value
8188 } , {
8289 immediate : true ,
@@ -119,6 +126,9 @@ class DollarApollo {
119126}
120127
121128const prepare = function prepare ( ) {
129+ if ( this . _apolloPrepared ) return
130+ this . _apolloPrepared = true
131+
122132 // Lazy creation
123133 Object . defineProperty ( this , '$apollo' , {
124134 get : ( ) => {
@@ -132,13 +142,7 @@ const prepare = function prepare () {
132142 // Prepare properties
133143 let apollo = this . $options . apollo
134144 if ( apollo ) {
135- this . _apolloQueries = omit ( apollo , [
136- 'subscribe' ,
137- '$subscribe' ,
138- '$skipAll' ,
139- '$skipAllQueries' ,
140- '$skipAllSubscriptions' ,
141- ] )
145+ this . _apolloQueries = omit ( apollo , keywords )
142146
143147 // watchQuery
144148 for ( let key in this . _apolloQueries ) {
@@ -149,6 +153,9 @@ const prepare = function prepare () {
149153}
150154
151155const launch = function launch ( ) {
156+ if ( this . _apolloLaunched ) return
157+ this . _apolloLaunched = true
158+
152159 if ( this . _apolloQueries ) {
153160 // watchQuery
154161 for ( let key in this . _apolloQueries ) {
@@ -187,30 +194,46 @@ function defineReactiveSetter ($apollo, key, value) {
187194 }
188195}
189196
190- module . exports = {
191- addGraphQLSubscriptions,
197+ export default function install ( pVue , options ) {
198+ if ( install . installed ) return
199+ install . installed = true
192200
193- install ( pVue , options ) {
194- Vue = pVue
195- defineReactive = Vue . util . defineReactive
196- apolloClient = options . apolloClient
201+ Vue = pVue
202+ defineReactive = Vue . util . defineReactive
203+ apolloClient = options . apolloClient
197204
198- Vue . mixin ( {
205+ const merge = Vue . config . optionMergeStrategies . methods
206+ Vue . config . optionMergeStrategies . apollo = function ( toVal , fromVal , vm ) {
207+ if ( ! toVal ) return fromVal
208+ if ( ! fromVal ) return toVal
199209
200- // Vue 1.x
201- init : prepare ,
202- // Vue 2.x
203- beforeCreate : prepare ,
210+ const toData = Object . assign ( { } , omit ( toVal , keywords ) , toVal . data )
211+ const fromData = Object . assign ( { } , omit ( fromVal , keywords ) , fromVal . data )
204212
205- created : launch ,
213+ const map = { }
214+ for ( let i = 0 ; i < keywords . length ; i ++ ) {
215+ const key = keywords [ i ]
216+ map [ key ] = merge ( toVal [ key ] , fromVal [ key ] )
217+ }
206218
207- destroyed : function ( ) {
208- if ( this . _apollo ) {
209- this . _apollo . destroy ( )
210- this . _apollo = null
211- }
212- } ,
219+ return Object . assign ( map , merge ( toData , fromData ) )
220+ }
213221
214- } )
215- } ,
222+ Vue . mixin ( {
223+
224+ // Vue 1.x
225+ init : prepare ,
226+ // Vue 2.x
227+ beforeCreate : prepare ,
228+
229+ created : launch ,
230+
231+ destroyed : function ( ) {
232+ if ( this . _apollo ) {
233+ this . _apollo . destroy ( )
234+ this . _apollo = null
235+ }
236+ } ,
237+
238+ } )
216239}
0 commit comments