Skip to content

Commit

Permalink
fix: cancel client requests on hook cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Dec 3, 2021
1 parent 4858113 commit 2c91305
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 22 deletions.
31 changes: 25 additions & 6 deletions src/useStatefulPrismicClientMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,43 @@ export const useStatefulPrismicClientMethod = <

React.useEffect(
() => {
// Used to prevent dispatching an action if the hook was cleaned up.
let didCancel = false;

if (!skip) {
dispatch(["start"]);
if (!didCancel) {
dispatch(["start"]);
}

client[methodName]
.call(
client,
// @ts-expect-error - Merging method arg types is too complex
...argsWithoutParams,
params,
)
.then((result) => dispatch(["succeed", result as TData]))
.catch((error) => dispatch(["fail", error]));
.then((result) => {
if (!didCancel) {
dispatch(["succeed", result as TData]);
}
})
.catch((error) => {
if (!didCancel) {
dispatch(["fail", error]);
}
});
}

// Ensure we don't dispatch an action if the hook is cleaned up.
() => {
didCancel = true;
};
},
// We must disable exhaustive-deps since we are using
// JSON.stringify on args (effectively a deep equality check).
// We want this effect to run again anytime args changes.
// JSON.stringify on params (effectively a deep equality check).
// We want this effect to run again anytime params change.
// eslint-disable-next-line react-hooks/exhaustive-deps
[client, methodName, JSON.stringify(args)],
[client, methodName, skip, JSON.stringify(params)],
);

return React.useMemo(
Expand Down
3 changes: 1 addition & 2 deletions test/useAllPrismicDocumentsByEveryTag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const tags = ["tag", "tag2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -148,7 +147,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => useAllPrismicDocumentsByEveryTag(tags),
() => useAllPrismicDocumentsByEveryTag(["tag", "tag2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/useAllPrismicDocumentsByIDs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const documentIDs = ["id", "id2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -148,7 +147,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => useAllPrismicDocumentsByIDs(documentIDs),
() => useAllPrismicDocumentsByIDs(["id", "id2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/useAllPrismicDocumentsBySomeTags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const tags = ["tag", "tag2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -148,7 +147,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => useAllPrismicDocumentsBySomeTags(tags),
() => useAllPrismicDocumentsBySomeTags(["tag", "tag2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/useAllPrismicDocumentsByUIDs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const documentUIDs = ["uid1", "uid2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -160,7 +159,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => useAllPrismicDocumentsByUIDs("type", documentUIDs),
() => useAllPrismicDocumentsByUIDs("type", ["uid1", "uid2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/usePrismicDocumentsByEveryTag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const tags = ["tag", "tag2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -133,7 +132,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => usePrismicDocumentsByEveryTag(tags),
() => usePrismicDocumentsByEveryTag(["tag", "tag2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/usePrismicDocumentsByIDs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const documentIDs = ["id", "id2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -133,7 +132,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => usePrismicDocumentsByIDs(documentIDs),
() => usePrismicDocumentsByIDs(["id", "id2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/usePrismicDocumentsBySomeTags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const tags = ["tag", "tag2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -133,7 +132,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => usePrismicDocumentsBySomeTags(tags),
() => usePrismicDocumentsBySomeTags(["tag", "tag2"]),
{ wrapper },
);

Expand Down
3 changes: 1 addition & 2 deletions test/usePrismicDocumentsByUIDs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ test.serial("returns failed state on error", async (t) => {
oauth_initiate: "oauth_initiate",
oauth_token: "oauth_token",
};
const documentUIDs = ["uid1", "uid2"];

server.use(
msw.rest.get(prismic.getEndpoint(md5(t.title)), (_req, res, ctx) => {
Expand All @@ -145,7 +144,7 @@ test.serial("returns failed state on error", async (t) => {
);

const { result, waitForValueToChange } = renderHook(
() => usePrismicDocumentsByUIDs("type", documentUIDs),
() => usePrismicDocumentsByUIDs("type", ["uid1", "uid2"]),
{ wrapper },
);

Expand Down

0 comments on commit 2c91305

Please sign in to comment.