diff --git a/src/useSubscription/useSubscriptionWithDependencies.spec.ts b/src/useSubscription/useSubscriptionWithDependencies.spec.ts new file mode 100644 index 00000000..59d8f60c --- /dev/null +++ b/src/useSubscription/useSubscriptionWithDependencies.spec.ts @@ -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) +}) \ No newline at end of file diff --git a/src/useSubscription/useSubscriptionWithDependencies.ts b/src/useSubscription/useSubscriptionWithDependencies.ts index 57c0f443..5c7d7c8e 100644 --- a/src/useSubscription/useSubscriptionWithDependencies.ts +++ b/src/useSubscription/useSubscriptionWithDependencies.ts @@ -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' @@ -33,6 +34,10 @@ export function useSubscriptionWithDependencies(...[action, ar return } + if (isEqual(value, previousValue)) { + return + } + if (subscription.isSubscribed()) { subscription.unsubscribe() }