From 7ec4b7f35740ac9dcdc93faf1980e57be4881063 Mon Sep 17 00:00:00 2001 From: Craig Harshbarger Date: Thu, 16 Nov 2023 13:30:19 -0600 Subject: [PATCH 1/2] Fix subscription actions being called even when the args have not changed --- .../useSubscriptionWithDependencies.spec.ts | 35 +++++++++++++++++++ .../useSubscriptionWithDependencies.ts | 5 +++ 2 files changed, 40 insertions(+) create mode 100644 src/useSubscription/useSubscriptionWithDependencies.spec.ts diff --git a/src/useSubscription/useSubscriptionWithDependencies.spec.ts b/src/useSubscription/useSubscriptionWithDependencies.spec.ts new file mode 100644 index 00000000..9e96bdef --- /dev/null +++ b/src/useSubscription/useSubscriptionWithDependencies.spec.ts @@ -0,0 +1,35 @@ +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 number = ref(0) + const object = ref({ value: 0 }) + const action = vi.fn() + + const parameters = computed(() => [ + number.value, + object.value, + ]) + + useSubscriptionWithDependencies(action, parameters) + + await timeout() + + expect(action).toBeCalledTimes(1) + + number.value = 1 + object.value = { value: 1 } + + await timeout() + + expect(action).toBeCalledTimes(2) + + number.value = 1 + 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() } From a37a9cbc30523fecac794ca907abb1807410af8d Mon Sep 17 00:00:00 2001 From: Craig Harshbarger Date: Thu, 16 Nov 2023 13:35:52 -0600 Subject: [PATCH 2/2] Simplify unit test --- .../useSubscriptionWithDependencies.spec.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/useSubscription/useSubscriptionWithDependencies.spec.ts b/src/useSubscription/useSubscriptionWithDependencies.spec.ts index 9e96bdef..59d8f60c 100644 --- a/src/useSubscription/useSubscriptionWithDependencies.spec.ts +++ b/src/useSubscription/useSubscriptionWithDependencies.spec.ts @@ -4,14 +4,10 @@ import { useSubscriptionWithDependencies } from '@/useSubscription/useSubscripti import { timeout } from '@/utilities/tests' test('it does not execute the action when args have not changed', async () => { - const number = ref(0) const object = ref({ value: 0 }) const action = vi.fn() - const parameters = computed(() => [ - number.value, - object.value, - ]) + const parameters = computed(() => [object.value]) useSubscriptionWithDependencies(action, parameters) @@ -19,14 +15,12 @@ test('it does not execute the action when args have not changed', async () => { expect(action).toBeCalledTimes(1) - number.value = 1 object.value = { value: 1 } await timeout() expect(action).toBeCalledTimes(2) - number.value = 1 object.value = { value: 1 } await timeout()