Skip to content

Commit

Permalink
Merge pull request #346 from PrefectHQ/fix-use-subscription-with-depe…
Browse files Browse the repository at this point in the history
…ndency-reactivity

Fix subscription actions being called even when the args have not changed
  • Loading branch information
pleek91 authored Nov 16, 2023
2 parents 2da1c36 + a37a9cb commit 3f2e73c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/useSubscription/useSubscriptionWithDependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test, vi } from 'vitest'
import { computed, ref } from 'vue'
import { useSubscriptionWithDependencies } from '@/useSubscription/useSubscriptionWithDependencies'
import { timeout } from '@/utilities/tests'

test('it does not execute the action when args have not changed', async () => {
const object = ref({ value: 0 })
const action = vi.fn()

const parameters = computed(() => [object.value])

useSubscriptionWithDependencies(action, parameters)

await timeout()

expect(action).toBeCalledTimes(1)

object.value = { value: 1 }

await timeout()

expect(action).toBeCalledTimes(2)

object.value = { value: 1 }

await timeout()

expect(action).toBeCalledTimes(2)
})
5 changes: 5 additions & 0 deletions src/useSubscription/useSubscriptionWithDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isEqual from 'lodash.isequal'
import { reactive, ref, Ref, toRaw, watch } from 'vue'
import { MaybeRef } from '@/types/maybe'
import { Action, SubscriptionOptions, UseSubscription, ActionArguments, MappedSubscription } from '@/useSubscription/types'
Expand Down Expand Up @@ -33,6 +34,10 @@ export function useSubscriptionWithDependencies<T extends Action>(...[action, ar
return
}

if (isEqual(value, previousValue)) {
return
}

if (subscription.isSubscribed()) {
subscription.unsubscribe()
}
Expand Down

0 comments on commit 3f2e73c

Please sign in to comment.