Skip to content

Commit 727db40

Browse files
committed
Throttle and debounce
1 parent 5a25b15 commit 727db40

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-apollo",
3-
"version": "1.2.5",
3+
"version": "1.3.0",
44
"description": "Vue apollo integration",
55
"main": "index.js",
66
"scripts": {
@@ -31,7 +31,9 @@
3131
},
3232
"dependencies": {
3333
"graphql-tag": "^0.1.15",
34-
"lodash.omit": "^4.5.0"
34+
"lodash.debounce": "^4.0.8",
35+
"lodash.omit": "^4.5.0",
36+
"lodash.throttle": "^4.1.1"
3537
},
3638
"devDependencies": {
3739
"babel-cli": "^6.14.0",

src/smart-apollo.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import omit from 'lodash.omit'
2+
import { throttle, debounce } from './utils'
23

34
class SmartApollo {
45
type = null
@@ -62,7 +63,10 @@ class SmartApollo {
6263
start () {
6364
this.starting = true
6465
if (typeof this.options.variables === 'function') {
65-
this.unwatchVariables = this.vm.$watch(this.options.variables.bind(this.vm), this.executeApollo.bind(this), {
66+
let cb = this.executeApollo.bind(this)
67+
cb = this.options.throttle ? throttle(cb, this.options.throttle) : cb
68+
cb = this.options.debounce ? debounce(cb, this.options.debounce) : cb
69+
this.unwatchVariables = this.vm.$watch(this.options.variables.bind(this.vm), cb, {
6670
immediate: true,
6771
})
6872
} else {
@@ -125,6 +129,8 @@ export class SmartQuery extends SmartApollo {
125129
'loadingKey',
126130
'watchLoading',
127131
'skip',
132+
'throttle',
133+
'debounce',
128134
]
129135

130136
constructor (vm, key, options) {
@@ -245,6 +251,8 @@ export class SmartSubscription extends SmartApollo {
245251
'variables',
246252
'result',
247253
'error',
254+
'throttle',
255+
'debounce',
248256
]
249257

250258
constructor (vm, key, options) {

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import loThrottle from 'lodash.throttle'
2+
import loDebounce from 'lodash.debounce'
3+
4+
function factory (action) {
5+
return (cb, options) => {
6+
if (typeof options === 'number') {
7+
return action(cb, options)
8+
} else {
9+
return action(cb, options.wait, options)
10+
}
11+
}
12+
}
13+
14+
export const throttle = factory(loThrottle)
15+
16+
export const debounce = factory(loDebounce)

0 commit comments

Comments
 (0)