Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce the GraphQL subscription request call in 0.5 second #373

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/services/workflow.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import store from '@/store/'
import Alert from '@/model/Alert.model'
import { createApolloClient } from '@/utils/graphql'
import gql from 'graphql-tag'
import debounce from 'lodash.debounce'

const HOLD_WORKFLOW = gql`
mutation HoldWorkflowMutation($workflow: String!) {
Expand All @@ -29,25 +30,28 @@ mutation StopWorkflowMutation($workflow: String!) {
`

class SubscriptionWorkflowService extends GQuery {
constructor () {
constructor (client = null) {
super()
// TODO: revisit this and evaluate other ways to build the GraphQL URL - not safe to rely on window.location (?)
const baseUrl = `${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}${window.location.pathname}`
const httpUrl = `${window.location.protocol}//${baseUrl}graphql`
const wsUrl = `${window.location.protocol.startsWith('https') ? 'wss' : 'ws'}://${baseUrl}subscriptions`
this.apolloClient = createApolloClient(httpUrl, wsUrl)
if (client === null) {
// TODO: revisit this and evaluate other ways to build the GraphQL URL - not safe to rely on window.location (?)
const baseUrl = `${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}${window.location.pathname}`
const httpUrl = `${window.location.protocol}//${baseUrl}graphql`
const wsUrl = `${window.location.protocol.startsWith('https') ? 'wss' : 'ws'}://${baseUrl}subscriptions`
this.apolloClient = createApolloClient(httpUrl, wsUrl)
}
/**
* @type {object}
*/
this.observable = null
this.request = debounce(this.request_, 500)
}

recompute () {
super.recompute()
this.request()
}

request () {
request_ () {
/**
* Perform a REST GraphQL request for all subscriptions.
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/services/workflow.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from 'chai'
// need the polyfill as otherwise ApolloClient fails to be imported as it checks for a global fetch object on import...
import 'cross-fetch/polyfill'
import sinon from 'sinon'
import SubscriptionWorkflowService from '@/services/workflow.service'

describe('SubscriptionWorkflowService', () => {
describe('request', () => {
it('should debounce the request function', () => {
// mocked createApolloClient because it will try to create a new ApolloClient and use a websocket
const fakeClient = sinon.mock()
const service = new SubscriptionWorkflowService(fakeClient)
// because we don't want to call or test the actual request_ implementation, which would try to use a websocket
const requestSpy = sinon.spy(service, 'request_')
for (let i = 0; i < 10; i++) {
service.request()
}
// now after 1 second the function should have been debounced
setTimeout(() => {
expect(requestSpy.callCount).to.equal(1)
requestSpy.restore()
}, 700)
})
})
})