Skip to content

Commit c9a4107

Browse files
committed
Skip all feature & better destroying
1 parent d9671a6 commit c9a4107

File tree

4 files changed

+138
-30
lines changed

4 files changed

+138
-30
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Integrates [apollo](http://www.apollostack.com/) in your [Vue](http://vuejs.org)
3333
- [Subscriptions](#subscriptions)
3434
- [Skipping the subscription](#skipping-the-subscription)
3535
- [Pagination with `fetchMore`](#pagination-with-fetchmore)
36+
- [Skip all](#skip-all)
3637

3738
## Installation
3839

@@ -723,12 +724,12 @@ mounted() {
723724
},
724725
```
725726

726-
You can declare subscriptions in the `apollo` option with the `subscribe` keyword:
727+
You can declare subscriptions in the `apollo` option with the `$subscribe` keyword:
727728

728729
```javascript
729730
apollo: {
730731
// Subscriptions
731-
subscribe: {
732+
$subscribe: {
732733
// When a tag is added
733734
tags: {
734735
query: gql`subscription tags($type: String!) {
@@ -772,7 +773,7 @@ If the subscription is skipped, it will disable it and it will not be updated an
772773
// Apollo-specific options
773774
apollo: {
774775
// Subscriptions
775-
subscribe: {
776+
$subscribe: {
776777
// When a tag is added
777778
tags: {
778779
query: gql`subscription tags($type: String!) {
@@ -895,6 +896,34 @@ export default {
895896

896897
[Here](https://github.com/Akryum/apollo-server-example/blob/master/schema.js#L21) is a simple example for the server.
897898

899+
## Skip all
900+
901+
You can disable all the queries for the component with `skipAllQueries`, all the subscriptions with `skipAllSubscriptions` and both with `skipAll`:
902+
903+
```javascript
904+
this.$apollo.skipAllQueries = true
905+
this.$apollo.skipAllSubscriptions = true
906+
this.$apollo.skipAll = true
907+
```
908+
909+
You can also declare these properties in the `apollo` option of the component. It can be booleans:
910+
911+
```javascript
912+
apollo: {
913+
$skipAll: true
914+
}
915+
```
916+
917+
Or reactive functions:
918+
919+
```javascript
920+
apollo: {
921+
$skipAll () {
922+
return this.foo === 42
923+
}
924+
}
925+
```
926+
898927
---
899928

900929
LICENCE ISC - Created by Guillaume CHAU (@Akryum)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "vue-apollo",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "Vue apollo integration",
55
"main": "index.js",
66
"scripts": {
77
"compile": "babel --presets es2015,stage-0 -d lib/ src/",
88
"prepublish": "npm run compile",
9-
"ev": "npm-watch"
9+
"dev": "npm-watch"
1010
},
1111
"watch": {
1212
"compile": "src/*.js"

src/smart-apollo.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,26 @@ class SmartApollo {
1010
this.key = key
1111
this.options = options
1212
this._skip = false
13+
this._watchers = []
1314

1415
// Query callback
1516
if (typeof this.options.query === 'function') {
1617
const queryCb = this.options.query.bind(this.vm)
1718
this.options.query = queryCb()
18-
this.vm.$watch(queryCb, query => {
19+
this._watchers.push(this.vm.$watch(queryCb, query => {
1920
this.options.query = query
2021
this.refresh()
21-
})
22+
}))
2223
}
2324

2425
this.autostart()
2526
}
2627

2728
autostart () {
2829
if (typeof this.options.skip === 'function') {
29-
this.vm.$watch(this.options.skip.bind(this.vm), this.skipChanged.bind(this), {
30+
this._watchers.push(this.vm.$watch(this.options.skip.bind(this.vm), this.skipChanged.bind(this), {
3031
immediate: true,
31-
})
32+
}))
3233
} else if (!this.options.skip) {
3334
this.start()
3435
} else {
@@ -56,8 +57,10 @@ class SmartApollo {
5657
}
5758

5859
refresh () {
59-
this.stop()
60-
this.start()
60+
if (!this._skip) {
61+
this.stop()
62+
this.start()
63+
}
6164
}
6265

6366
start () {
@@ -116,6 +119,13 @@ class SmartApollo {
116119
this.options.error.call(this.vm, error)
117120
}
118121
}
122+
123+
destroy () {
124+
this.stop()
125+
for (const unwatch of this._watchers) {
126+
unwatch()
127+
}
128+
}
119129
}
120130

121131
export class SmartQuery extends SmartApollo {
@@ -237,8 +247,10 @@ export class SmartQuery extends SmartApollo {
237247
this.loading = false
238248
}
239249

240-
get fetchMore () {
241-
return this.observer.fetchMore.bind(this.observer)
250+
fetchMore (...args) {
251+
if (this.observer) {
252+
this.observer.fetchMore(...args)
253+
}
242254
}
243255
}
244256

src/vue-plugin.js

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import omit from 'lodash.omit'
22
import { print } from 'graphql-tag/printer'
33
import { SmartQuery, SmartSubscription } from './smart-apollo'
44

5+
let Vue
56
let apolloClient = null
67

78
let defineReactive = function () {}
@@ -23,6 +24,9 @@ function addGraphQLSubscriptions (networkInterface, wsClient) {
2324

2425
class 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

76121
const 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

120190
module.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

Comments
 (0)