Skip to content

Commit

Permalink
Prevent adding duplicate callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Sep 6, 2021
1 parent c1022fa commit 72d38a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/services/workflow.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import isEqual from 'lodash/isEqual'
import union from 'lodash/union'
import unionBy from 'lodash/unionBy'
import ViewState from '@/model/ViewState.model'
import Subscription from '@/model/Subscription.model'
import {
Expand Down Expand Up @@ -324,7 +324,9 @@ class WorkflowService {
throw new Error('Error recomputing subscription: Query variables do not match.')
}
this.mergeQueries(baseSubscriber.query.query, subscriber.query.query)
subscription.callbacks = union(subscription.callbacks, subscriber.query.callbacks)
// Combine the arrays of callbacks, creating an array of unique callbacks.
// The callbacks are compared by their class/constructor name.
subscription.callbacks = unionBy(subscription.callbacks, subscriber.query.callbacks, (callback) => callback.constructor.name)
}
const finalQuery = print(baseSubscriber.query.query)
// TODO: consider using a better approach than print(a) === print(b)
Expand Down
15 changes: 11 additions & 4 deletions tests/unit/services/workflow.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const sandbox = sinon.createSandbox()

Vue.use(Vuex)

if (!global.localStorage) {
global.localStorage = {
getItem () { return '{}' },
setItem () {}
}
}

describe('WorkflowService', () => {
/**
* @type {String}
Expand Down Expand Up @@ -264,7 +271,7 @@ describe('WorkflowService', () => {
const finalQuery = print(service.subscriptions.root.query.query)
expect(expectedQuery1).to.equal(finalQuery)
})
it('should not add duplicate action names', () => {
it('should not add duplicate callbacks', () => {
const newCallbacks = [
new WorkflowCallback(),
new TreeCallback()
Expand All @@ -281,10 +288,10 @@ describe('WorkflowService', () => {
query: newSubscriptionQuery
}
service.subscribe(anotherView)
// Same action names, Lodash's union should add to list like a set
// Same callbacks, Lodash's union should add to list like a set
expect(subscriptionQuery.callbacks).to.deep.equal(newCallbacks)
})
it('should add new action names', () => {
it('should add new callbacks', () => {
const baseCallbacks = [new WorkflowCallback()]
subscriptionQuery.callbacks.push(...baseCallbacks)
const newCallbacks = [new TreeCallback()]
Expand All @@ -299,7 +306,7 @@ describe('WorkflowService', () => {
query: newSubscriptionQuery
}
service.subscribe(anotherView)
// Same action names, Lodash's union should add to list like a set
// Same callbacks, Lodash's union should add to list like a set
expect(subscription.callbacks).to.deep.equal([...baseCallbacks, new TreeCallback()])
})
it('should throw an error if there are no subscribers', () => {
Expand Down

0 comments on commit 72d38a1

Please sign in to comment.