Skip to content

#2223 - elderberry #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions packages/react-hooks/src/hooks/useRealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@trigger.dev/core/v3";
import { useCallback, useEffect, useId, useRef, useState } from "react";
import { KeyedMutator, useSWR } from "../utils/trigger-swr.js";
import { logIfFetcherPresent } from "../utils/swrMiddleware.js";
import { useApiClient, UseApiClientOptions } from "./useApiClient.js";
import { createThrottledQueue } from "../utils/throttle.js";

Expand Down Expand Up @@ -78,17 +79,23 @@ export function useRealtimeRun<TTask extends AnyTask>(
const idKey = options?.id ?? hookId;

// Store the streams state in SWR, using the idKey as the key to share states.
const { data: run, mutate: mutateRun } = useSWR<RealtimeRun<TTask>>([idKey, "run"], null);
const { data: run, mutate: mutateRun } = useSWR<RealtimeRun<TTask>>(
[idKey, "run"],
null,
{ use: [logIfFetcherPresent("realtime:run")] }
);

const { data: error = undefined, mutate: setError } = useSWR<undefined | Error>(
[idKey, "error"],
null
null,
{ use: [logIfFetcherPresent("realtime:error")] }
);

// Add state to track when the subscription is complete
const { data: isComplete = false, mutate: setIsComplete } = useSWR<boolean>(
[idKey, "complete"],
null
null,
{ use: [logIfFetcherPresent("realtime:complete")] }
);

// Abort controller to cancel the current API call.
Expand Down Expand Up @@ -229,6 +236,7 @@ export function useRealtimeRunWithStreams<
null,
{
fallbackData: initialStreamsFallback,
use: [logIfFetcherPresent("realtime:streams")],
}
);

Expand All @@ -239,17 +247,23 @@ export function useRealtimeRunWithStreams<
}, [streams]);

// Store the streams state in SWR, using the idKey as the key to share states.
const { data: run, mutate: mutateRun } = useSWR<RealtimeRun<TTask>>([idKey, "run"], null);
const { data: run, mutate: mutateRun } = useSWR<RealtimeRun<TTask>>(
[idKey, "run"],
null,
{ use: [logIfFetcherPresent("realtime:run")] }
);

// Add state to track when the subscription is complete
const { data: isComplete = false, mutate: setIsComplete } = useSWR<boolean>(
[idKey, "complete"],
null
null,
{ use: [logIfFetcherPresent("realtime:complete")] }
);

const { data: error = undefined, mutate: setError } = useSWR<undefined | Error>(
[idKey, "error"],
null
null,
{ use: [logIfFetcherPresent("realtime:error")] }
);

// Abort controller to cancel the current API call.
Expand Down Expand Up @@ -403,6 +417,7 @@ export function useRealtimeRunsWithTag<TTask extends AnyTask>(
// Store the streams state in SWR, using the idKey as the key to share states.
const { data: runs, mutate: mutateRuns } = useSWR<RealtimeRun<TTask>[]>([idKey, "run"], null, {
fallbackData: [],
use: [logIfFetcherPresent("realtime:runs")],
});

// Keep the latest streams in a ref.
Expand All @@ -413,7 +428,8 @@ export function useRealtimeRunsWithTag<TTask extends AnyTask>(

const { data: error = undefined, mutate: setError } = useSWR<undefined | Error>(
[idKey, "error"],
null
null,
{ use: [logIfFetcherPresent("realtime:error")] }
);

// Abort controller to cancel the current API call.
Expand Down Expand Up @@ -501,6 +517,7 @@ export function useRealtimeBatch<TTask extends AnyTask>(
// Store the streams state in SWR, using the idKey as the key to share states.
const { data: runs, mutate: mutateRuns } = useSWR<RealtimeRun<TTask>[]>([idKey, "run"], null, {
fallbackData: [],
use: [logIfFetcherPresent("realtime:runs")],
});

// Keep the latest streams in a ref.
Expand All @@ -511,7 +528,8 @@ export function useRealtimeBatch<TTask extends AnyTask>(

const { data: error = undefined, mutate: setError } = useSWR<undefined | Error>(
[idKey, "error"],
null
null,
{ use: [logIfFetcherPresent("realtime:error")] }
);

// Abort controller to cancel the current API call.
Expand Down
27 changes: 27 additions & 0 deletions packages/react-hooks/src/utils/swrMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use client";

import type { Middleware } from "swr";

/**
* Middleware that logs if a fetcher is present. This helps detect cases where a global SWR fetcher
* might be injected into hooks that are intended to manage their own data (e.g. realtime hooks).
*
* This middleware is non-invasive: it does not modify the fetcher or behavior, it only logs in dev.
*/
export function logIfFetcherPresent(label: string): Middleware {
return (useSWRNext) => {
return (key, fetcher, config) => {
if (typeof fetcher === "function" && process.env.NODE_ENV !== "production") {
// eslint-disable-next-line no-console
console.warn(
`[trigger.dev][${label}] Detected a fetcher for SWR key. This hook is intended to manage its own data; an inherited global SWR fetcher may cause unintended requests. key:`,
key
);
}

return useSWRNext(key, fetcher, config);
};
};
}