Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: BetterTyped/hyper-fetch
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 508b7510196d56ee460eda5e145eaac12562fd30
Choose a base ref
...
head repository: BetterTyped/hyper-fetch
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3f01c37e0788ea00eae9456e0746a468da91d5ac
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Apr 11, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    08e0c19 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    crazy-max CrazyMax
    Copy the full SHA
    3f01c37 View commit details

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { act } from "@testing-library/react";

import { startServer, resetInterceptors, stopServer, createRequestInterceptor } from "../../../server";
import { createRequest, renderUseRequestEvents } from "../../../utils";

describe("useRequestEvents [ Utils ]", () => {
let request = createRequest();

beforeAll(() => {
startServer();
});

afterEach(() => {
resetInterceptors();
});

afterAll(() => {
stopServer();
});

beforeEach(() => {
jest.resetModules();
request = createRequest();
});

describe("When handling lifecycle events", () => {
it("should not throw when removing non existing event", async () => {
const response = renderUseRequestEvents(request);

expect(response.result.current[1].removeLifecycleListener).not.toThrow();
});
it("should unmount lifecycle events when handling requests by queue/cache keys", async () => {
const spy = jest.fn();
createRequestInterceptor(request);
const response = renderUseRequestEvents(request);

await act(async () => {
response.result.current[0].onRequestStart(spy);
response.result.current[1].addLifecycleListeners(request);
response.result.current[1].addLifecycleListeners(request);
response.result.current[1].addLifecycleListeners(request);

await request.send();
});

expect(spy).toBeCalledTimes(1);
});
it("should listen to every request id events", async () => {
const spy = jest.fn();
createRequestInterceptor(request);
const response = renderUseRequestEvents(request);

await act(async () => {
await request.send({
onSettle: (requestId) => {
response.result.current[0].onRequestStart(spy);
response.result.current[1].addLifecycleListeners(request, requestId);
response.result.current[1].addLifecycleListeners(request, requestId);
response.result.current[1].addLifecycleListeners(request, requestId);
},
});
});

expect(spy).toBeCalledTimes(3);
});
});
});
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ describe("useFetch [ Cancel ]", () => {
act(() => {
const params = { page: 1 };
response.result.current.onAbort(spy);
response.rerender({ request: request.setQueryParams(params), dependencies: [params] });
response.rerender({ request, dependencies: [params] });
});
await waitForRender();

Original file line number Diff line number Diff line change
@@ -175,7 +175,14 @@ export const useRequestEvents = <T extends RequestInstance>({
// ******************

const addLifecycleListeners = (cmd: T, requestId?: string) => {
/**
* useFetch handles requesting by general keys
* This makes it possible to deduplicate requests from different places and share data
*/
if (!requestId) {
// It's important to clear previously attached listeners to not cause some additional response/request
// events to be triggered during lifecycle
clearLifecycleListeners();
const { queueKey, cacheKey } = cmd;
const requestStartUnmount = requestManager.events.onRequestStart(queueKey, handleRequestStart(cmd));
const responseStartUnmount = requestManager.events.onResponseStart(queueKey, handleResponseStart(cmd));
@@ -195,6 +202,9 @@ export const useRequestEvents = <T extends RequestInstance>({

return unmount;
}
/**
* useSubmit handles requesting by requestIds, this makes it possible to track single requests
*/
const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart(cmd));
const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(cmd));
7 changes: 2 additions & 5 deletions packages/react/src/hooks/use-fetch/use-fetch.hooks.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { useDebounce, useThrottle } from "@better-hooks/performance";
import { RequestInstance, Request, getRequestKey } from "@hyper-fetch/core";

import { useRequestEvents, useTrackedState } from "helpers";
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType, getRefreshTime } from "hooks/use-fetch";
import { UseFetchOptionsType, useFetchDefaultOptions, UseFetchReturnType } from "hooks/use-fetch";
import { useConfigProvider } from "config-provider";
import { getBounceData } from "utils";
import { InvalidationKeyType } from "types";
@@ -99,9 +99,6 @@ export const useFetch = <RequestType extends RequestInstance>(

function handleRefresh() {
if (!refresh) return;
const time = getRefreshTime(refreshTime, state.timestamp);

logger.debug(`Starting refresh counter, request will be send in ${time}ms`);

refreshDebounce.debounce(() => {
const isBlurred = !appManager.isFocused;
@@ -120,7 +117,7 @@ export const useFetch = <RequestType extends RequestInstance>(

// Start new refresh counter
handleRefresh();
}, time);
});
}

const handleRevalidation = (invalidateKey: InvalidationKeyType) => {