Skip to content

Commit

Permalink
fix: remove abort handler from fetch util
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Sep 19, 2024
1 parent 72833b2 commit 8d7f97c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-camels-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@frigade/js": patch
"@frigade/react": patch
---

Fixes an issue where anchor hashes causes network calls with state updates to be cancelled
142 changes: 142 additions & 0 deletions apps/smithy/src/stories/Tour/TourInteractionTests.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { Box, Button, Tour, TourProps, useFlow } from "@frigade/react";
import { type Meta, type StoryObj } from "@storybook/react";
import { expect, userEvent, waitFor, within } from "@storybook/test";
import { useEffect, useRef } from "react";

type TourStory = StoryObj<typeof Tour>;

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const StoryMeta: Meta<typeof Tour> = {
title: "Components/Tour",
component: Tour,
};

export default StoryMeta;

export const InteractionTests: TourStory = {
args: {
flowId: "flow_U63A5pndRrvCwxNs",
},

decorators: [
(Story, { args }) => {
const { flow } = useFlow(args.flowId);

const lateAnchorRef = useRef(null);

useEffect(() => {
// @ts-expect-error Shush TypeScript, it's a debug ref in a story, it's fine
lateAnchorRef.current = (
<Box
borderRadius="10px"
id="tooltip-storybook-0"
p={4}
style={{ background: "pink", width: "200px" }}
>
<Button.Primary
title="Anchor here"
onClick={() => {
// no-op
}}
/>
</Box>
);
}, []);

return (
<>
<Story {...args} />
<button
id="reset-flow"
onClick={() => {
flow.restart();
}}
style={{ marginTop: "36px" }}
>
Reset Flow
</button>
<Box
style={{
alignItems: "center",
display: "flex",
flexDirection: "column",
justifyContent: "center",
height: "calc(100vh - 32px)",
// height: "1500px",
}}
>
<input
value="Restart Flow"
type="button"
onClick={() => {
// Do nothing, this is just to see if you can interact outside the overlay
}}
/>

{lateAnchorRef.current}

<Box
id="tooltip-storybook-1"
p={4}
style={{ background: "fuchsia", width: "200px" }}
>
Second anchor
</Box>
</Box>

<Box
id="tooltip-storybook-2"
p={4}
style={{
background: "lightgreen",
width: "200px",
marginTop: "1400px", // Add significant margin to force scrolling
}}
>
Scroll to see this anchor
</Box>
</>
);
},
],

play: async ({ step }) => {
await step("Test paginating through the Tour", async () => {
const canvas = within(document.body);
let TourElement = await canvas.findByRole("dialog");
let Tour = within(TourElement);

await userEvent.click(Tour.getByRole("button", { name: "Let's go!" }));
await sleep(100);

TourElement = await canvas.findByRole("dialog");
Tour = within(TourElement);

await userEvent.click(
Tour.getByRole("button", { name: "Scroll to the next step" })
);

await sleep(100);

TourElement = await canvas.findByRole("dialog");
Tour = within(TourElement);

await userEvent.click(Tour.getByRole("button", { name: "Continue" }));

await sleep(100);

await waitFor(async () => {
await expect(TourElement).not.toBeInTheDocument();
});

await sleep(1000);

await userEvent.click(canvas.getByText("Reset Flow"));

await sleep(1000);
});
},
};
8 changes: 8 additions & 0 deletions packages/js-api/src/core/frigade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export class Frigade extends Fetchable {
if (this.getGlobalState().currentUrl === event.destination.url) {
return
}
// if the new base url is the same but with a hashtag, don't refresh the state as the page has not changed.
if (
event.destination.url &&
this.getGlobalState().currentUrl &&
event.destination.url.split('#')[0] === this.getGlobalState().currentUrl.split('#')[0]
) {
return
}

this.getGlobalState().currentUrl = event.destination.url
this.refreshStateFromAPI(true)
Expand Down
16 changes: 4 additions & 12 deletions packages/js-api/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class CallQueue {
}[] = []
private readonly ttlInMS = 250
private readonly cacheSize = 10
private controller: AbortController = new AbortController()

public push(call: string, response?: Promise<Response>) {
const now = new Date()
Expand All @@ -106,14 +105,7 @@ class CallQueue {
}

public cancelAllPendingRequests() {
// abort all requests
this.controller.abort(REDUNDANT_CALL_MESSAGE)
this.queue = []
this.controller = new AbortController()
}

public getController() {
return this.controller
}
}

Expand Down Expand Up @@ -151,10 +143,7 @@ export async function gracefulFetch(
callQueue.cancelAllPendingRequests()
}

const pendingResponse = fetch(url, {
...(options ?? {}),
signal: callQueue.getController().signal,
})
const pendingResponse = fetch(url, options)

if (isWebPostRequest) {
callQueue.push(
Expand All @@ -165,6 +154,9 @@ export async function gracefulFetch(
}

response = await pendingResponse
if (isWebPostRequest && !callQueue.hasCall(lastCallDataKey)) {
response = getEmptyResponse(REDUNDANT_CALL_MESSAGE)
}
} catch (error) {
return getEmptyResponse(error)
}
Expand Down

0 comments on commit 8d7f97c

Please sign in to comment.