Skip to content

Commit

Permalink
feat(composition): debounce + throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Dec 1, 2019
1 parent f289394 commit 30267b2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/docs/src/api/use-query.md
Expand Up @@ -12,6 +12,8 @@

- `context`: Context to be passed to link execution chain.

- `debounce`: Debounce interval in ms.

- `enabled`: A boolean `Ref` to enable or disable the query.

- `errorPolicy`: Customize error behavior. See [error handling](../guide-composable/error-handling).
Expand All @@ -36,6 +38,8 @@

- `returnPartialData`: Allow returning incomplete data from the cache when a larger query cannot be fully satisfied by the cache, instead of returning nothing.

- `throttle`: Throttle interval in ms.

## Return

- `result`: result data object.
Expand Down
4 changes: 4 additions & 0 deletions packages/docs/src/api/use-subscription.md
Expand Up @@ -10,6 +10,8 @@

- `clientId`: Id of the client that should be used for this subscription if you have provided multiple clients.

- `debounce`: Debounce interval in ms.

- `enabled`: A boolean `Ref` to enable or disable the subscription.

- `fetchPolicy`: Customize cache behavior.
Expand All @@ -19,6 +21,8 @@
- `network-only`: return result from network, fail if network call doesn't succeed, save to cache.
- `no-cache`: return result from network, fail if network call doesn't succeed, don't save to cache.

- `throttle`: Throttle interval in ms.

## Return

- `result`: result data object.
Expand Down
4 changes: 4 additions & 0 deletions packages/vue-apollo-composable/package.json
Expand Up @@ -31,6 +31,9 @@
"build": "tsc --outDir dist -d",
"prepublishOnly": "yarn build"
},
"dependencies": {
"throttle-debounce": "^2.1.0"
},
"peerDependencies": {
"@vue/composition-api": "^0.3.2",
"apollo-client": "^2.0.0",
Expand All @@ -40,6 +43,7 @@
"vue": "^2.6.10"
},
"devDependencies": {
"@types/throttle-debounce": "^2.1.0",
"@vue/composition-api": "^0.3.2",
"typescript": "^3.7.2"
}
Expand Down
27 changes: 26 additions & 1 deletion packages/vue-apollo-composable/src/useQuery.ts
Expand Up @@ -11,6 +11,7 @@ import {
FetchMoreOptions,
} from 'apollo-client'
import { Subscription } from 'apollo-client/util/Observable'
import { throttle, debounce } from 'throttle-debounce'
import { useApolloClient } from './useApolloClient'
import { ReactiveFunction } from './util/ReactiveFunction'
import { paramToRef } from './util/paramToRef'
Expand All @@ -24,6 +25,8 @@ export interface UseQueryOptions<
> extends Omit<WatchQueryOptions<TVariables>, 'query' | 'variables'> {
clientId?: string
enabled?: boolean
throttle?: number
debounce?: number
}

interface SubscribeToMoreItem {
Expand Down Expand Up @@ -181,7 +184,7 @@ export function useQuery<
/**
* Queue a restart of the query (on next tick) if it is already active
*/
function restart () {
function baseRestart () {
if (!started || restarting) return
restarting = true
Vue.nextTick(() => {
Expand All @@ -193,6 +196,22 @@ export function useQuery<
})
}

let debouncedRestart: Function
function updateRestartFn () {
if (currentOptions.value.throttle) {
debouncedRestart = throttle(currentOptions.value.throttle, baseRestart)
} else if (currentOptions.value.debounce) {
debouncedRestart = debounce(currentOptions.value.debounce, baseRestart)
} else {
debouncedRestart = baseRestart
}
}

function restart () {
if (!debouncedRestart) updateRestartFn()
debouncedRestart()
}

// Applying document
let currentDocument: DocumentNode
watch(documentRef, value => {
Expand All @@ -212,6 +231,12 @@ export function useQuery<
// Applying options
const currentOptions = ref<UseQueryOptions<TResult, TVariables>>()
watch(() => isRef(optionsRef) ? optionsRef.value : optionsRef, value => {
if (currentOptions.value && (
currentOptions.value.throttle !== value.throttle ||
currentOptions.value.debounce !== value.debounce
)) {
updateRestartFn()
}
currentOptions.value = value
restart()
}, {
Expand Down
27 changes: 26 additions & 1 deletion packages/vue-apollo-composable/src/useSubscription.ts
Expand Up @@ -4,6 +4,7 @@ import { Ref, ref, watch, isRef, onUnmounted, computed } from '@vue/composition-
import { OperationVariables, SubscriptionOptions } from 'apollo-client'
import { Observable, Subscription } from 'apollo-client/util/Observable'
import { FetchResult } from 'apollo-link'
import { throttle, debounce } from 'throttle-debounce'
import { ReactiveFunction } from './util/ReactiveFunction'
import { paramToRef } from './util/paramToRef'
import { paramToReactive } from './util/paramToReactive'
Expand All @@ -16,6 +17,8 @@ export interface UseSubscriptionOptions <
> extends Omit<SubscriptionOptions<TVariables>, 'query' | 'variables'> {
clientId?: string
enabled?: boolean
throttle?: number
debounce?: number
}

export function useSubscription <
Expand Down Expand Up @@ -97,7 +100,7 @@ export function useSubscription <
/**
* Queue a restart of the query (on next tick) if it is already active
*/
function restart () {
function baseRestart () {
if (!started || restarting) return
restarting = true
Vue.nextTick(() => {
Expand All @@ -109,6 +112,22 @@ export function useSubscription <
})
}

let debouncedRestart: Function
function updateRestartFn () {
if (currentOptions.value.throttle) {
debouncedRestart = throttle(currentOptions.value.throttle, baseRestart)
} else if (currentOptions.value.debounce) {
debouncedRestart = debounce(currentOptions.value.debounce, baseRestart)
} else {
debouncedRestart = baseRestart
}
}

function restart () {
if (!debouncedRestart) updateRestartFn()
debouncedRestart()
}

// Applying document
let currentDocument: DocumentNode
watch(documentRef, value => {
Expand All @@ -128,6 +147,12 @@ export function useSubscription <
// Applying options
const currentOptions = ref<UseSubscriptionOptions<TResult, TVariables>>()
watch(() => isRef(optionsRef) ? optionsRef.value : optionsRef, value => {
if (currentOptions.value && (
currentOptions.value.throttle !== value.throttle ||
currentOptions.value.debounce !== value.debounce
)) {
updateRestartFn()
}
currentOptions.value = value
restart()
}, {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -2393,6 +2393,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==

"@types/throttle-debounce@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/throttle-debounce/-/throttle-debounce-2.1.0.tgz#1c3df624bfc4b62f992d3012b84c56d41eab3776"
integrity sha512-5eQEtSCoESnh2FsiLTxE121IiE60hnMqcb435fShf4bpLRjEu1Eoekht23y6zXS9Ts3l+Szu3TARnTsA0GkOkQ==

"@types/ws@^6.0.0":
version "6.0.4"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-6.0.4.tgz#7797707c8acce8f76d8c34b370d4645b70421ff1"
Expand Down

0 comments on commit 30267b2

Please sign in to comment.