diff --git a/package.json b/package.json index f397c0a5..bb4a4fa0 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ ], "dependencies": { "@babel/runtime": "^7.12.5", + "@typescript-eslint/parser": "^4.18.0", "immer": ">=8.0.0" }, "peerDependencies": { diff --git a/src/react-hooks/buildHooks.ts b/src/react-hooks/buildHooks.ts index 59d61248..80d7ebbf 100644 --- a/src/react-hooks/buildHooks.ts +++ b/src/react-hooks/buildHooks.ts @@ -205,7 +205,7 @@ export function buildHooks({ // arg did not change, but options probably did, update them lastPromise.updateSubscriptionOptions({ pollingInterval, refetchOnReconnect, refetchOnFocus }); } else { - if (lastPromise) lastPromise.unsubscribe(); + lastPromise?.unsubscribe(); const promise = dispatch( initiate(stableArg, { subscriptionOptions: { pollingInterval, refetchOnReconnect, refetchOnFocus }, @@ -227,7 +227,6 @@ export function buildHooks({ useEffect(() => { return () => { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions promiseRef.current?.unsubscribe(); promiseRef.current = undefined; }; @@ -255,42 +254,30 @@ export function buildHooks({ const promiseRef = useRef>(); const lastPromise = promiseRef.current; - const optionsRef = useRef(); - useEffect(() => { const options = { refetchOnReconnect, refetchOnFocus, pollingInterval, }; - if (optionsRef.current && !shallowEqual(options, optionsRef.current)) { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions + if (lastPromise && !shallowEqual(options, lastPromise.subscriptionOptions)) { lastPromise?.updateSubscriptionOptions(options); - optionsRef.current = options; } }, [lastPromise, refetchOnFocus, refetchOnReconnect, pollingInterval]); useEffect(() => { return () => { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions promiseRef.current?.unsubscribe(); promiseRef.current = undefined; - optionsRef.current = undefined; }; }, []); const trigger = useCallback( - function (args: any) { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions + function (arg: any) { lastPromise?.unsubscribe(); - // Set the subscription options on the initial query - if (!optionsRef.current) { - optionsRef.current = { pollingInterval, refetchOnReconnect, refetchOnFocus }; - } - promiseRef.current = dispatch( - initiate(args, { + initiate(arg, { subscriptionOptions: { pollingInterval, refetchOnReconnect, refetchOnFocus }, forceRefetch: true, }) @@ -378,7 +365,6 @@ export function buildHooks({ useEffect(() => { return () => { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions promiseRef.current?.unsubscribe(); promiseRef.current = undefined; }; @@ -388,7 +374,7 @@ export function buildHooks({ function (args) { let promise: MutationActionCreatorResult; batch(() => { - if (promiseRef.current) promiseRef.current.unsubscribe(); + promiseRef?.current?.unsubscribe(); promise = dispatch(initiate(args)); promiseRef.current = promise; setRequestId(promise.requestId); diff --git a/yarn.lock b/yarn.lock index 5141ecca..840719fe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1571,6 +1571,24 @@ "@typescript-eslint/typescript-estree" "4.7.0" debug "^4.1.1" +"@typescript-eslint/parser@^4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.18.0.tgz#a211edb14a69fc5177054bec04c95b185b4dde21" + integrity sha512-W3z5S0ZbecwX3PhJEAnq4mnjK5JJXvXUDBYIYGoweCyWyuvAKfGHvzmpUzgB5L4cRBb+cTu9U/ro66dx7dIimA== + dependencies: + "@typescript-eslint/scope-manager" "4.18.0" + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/typescript-estree" "4.18.0" + debug "^4.1.1" + +"@typescript-eslint/scope-manager@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.18.0.tgz#d75b55234c35d2ff6ac945758d6d9e53be84a427" + integrity sha512-olX4yN6rvHR2eyFOcb6E4vmhDPsfdMyfQ3qR+oQNkAv8emKKlfxTWUXU5Mqxs2Fwe3Pf1BoPvrwZtwngxDzYzQ== + dependencies: + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/visitor-keys" "4.18.0" + "@typescript-eslint/scope-manager@4.7.0": version "4.7.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.7.0.tgz#2115526085fb72723ccdc1eeae75dec7126220ed" @@ -1579,6 +1597,11 @@ "@typescript-eslint/types" "4.7.0" "@typescript-eslint/visitor-keys" "4.7.0" +"@typescript-eslint/types@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.18.0.tgz#bebe323f81f2a7e2e320fac9415e60856267584a" + integrity sha512-/BRociARpj5E+9yQ7cwCF/SNOWwXJ3qhjurMuK2hIFUbr9vTuDeu476Zpu+ptxY2kSxUHDGLLKy+qGq2sOg37A== + "@typescript-eslint/types@4.7.0": version "4.7.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.7.0.tgz#5e95ef5c740f43d942542b35811f87b62fccca69" @@ -1597,6 +1620,19 @@ semver "^7.3.2" tsutils "^3.17.1" +"@typescript-eslint/typescript-estree@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.18.0.tgz#756d3e61da8c16ab99185532c44872f4cd5538cb" + integrity sha512-wt4xvF6vvJI7epz+rEqxmoNQ4ZADArGQO9gDU+cM0U5fdVv7N+IAuVoVAoZSOZxzGHBfvE3XQMLdy+scsqFfeg== + dependencies: + "@typescript-eslint/types" "4.18.0" + "@typescript-eslint/visitor-keys" "4.18.0" + debug "^4.1.1" + globby "^11.0.1" + is-glob "^4.0.1" + semver "^7.3.2" + tsutils "^3.17.1" + "@typescript-eslint/typescript-estree@4.7.0": version "4.7.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.7.0.tgz#539531167f05ba20eb0b6785567076679e29d393" @@ -1611,6 +1647,14 @@ semver "^7.3.2" tsutils "^3.17.1" +"@typescript-eslint/visitor-keys@4.18.0": + version "4.18.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.18.0.tgz#4e6fe2a175ee33418318a029610845a81e2ff7b6" + integrity sha512-Q9t90JCvfYaN0OfFUgaLqByOfz8yPeTAdotn/XYNm5q9eHax90gzdb+RJ6E9T5s97Kv/UHWKERTmqA0jTKAEHw== + dependencies: + "@typescript-eslint/types" "4.18.0" + eslint-visitor-keys "^2.0.0" + "@typescript-eslint/visitor-keys@4.7.0": version "4.7.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.7.0.tgz#6783824f22acfc49e754970ed21b88ac03b80e6f"