@trigger.dev/react-hooks 3.1.2 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/contexts.d.ts +2 -2
- package/dist/commonjs/contexts.js +3 -2
- package/dist/commonjs/contexts.js.map +1 -1
- package/dist/commonjs/hooks/useApiClient.d.ts +33 -2
- package/dist/commonjs/hooks/useApiClient.js +31 -6
- package/dist/commonjs/hooks/useApiClient.js.map +1 -1
- package/dist/commonjs/hooks/useRealtime.d.ts +100 -0
- package/dist/commonjs/hooks/useRealtime.js +387 -0
- package/dist/commonjs/hooks/useRealtime.js.map +1 -0
- package/dist/commonjs/hooks/useRun.js +1 -1
- package/dist/commonjs/hooks/useRun.js.map +1 -1
- package/dist/commonjs/hooks/useTaskTrigger.d.ts +101 -0
- package/dist/commonjs/hooks/useTaskTrigger.js +134 -0
- package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
- package/dist/commonjs/index.d.ts +2 -3
- package/dist/commonjs/index.js +2 -3
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/utils/throttle.d.ts +6 -0
- package/dist/commonjs/utils/throttle.js +50 -0
- package/dist/commonjs/utils/throttle.js.map +1 -0
- package/dist/commonjs/utils/trigger-swr.d.ts +12 -0
- package/dist/commonjs/utils/trigger-swr.js.map +1 -1
- package/dist/esm/contexts.d.ts +2 -2
- package/dist/esm/contexts.js +2 -2
- package/dist/esm/contexts.js.map +1 -1
- package/dist/esm/hooks/useApiClient.d.ts +33 -2
- package/dist/esm/hooks/useApiClient.js +32 -7
- package/dist/esm/hooks/useApiClient.js.map +1 -1
- package/dist/esm/hooks/useRealtime.d.ts +100 -0
- package/dist/esm/hooks/useRealtime.js +381 -0
- package/dist/esm/hooks/useRealtime.js.map +1 -0
- package/dist/esm/hooks/useRun.js +1 -1
- package/dist/esm/hooks/useRun.js.map +1 -1
- package/dist/esm/hooks/useTaskTrigger.d.ts +101 -0
- package/dist/esm/hooks/useTaskTrigger.js +126 -0
- package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
- package/dist/esm/index.d.ts +2 -3
- package/dist/esm/index.js +2 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/throttle.d.ts +6 -0
- package/dist/esm/utils/throttle.js +47 -0
- package/dist/esm/utils/throttle.js.map +1 -0
- package/dist/esm/utils/trigger-swr.d.ts +12 -0
- package/dist/esm/utils/trigger-swr.js.map +1 -1
- package/package.json +2 -2
- package/dist/commonjs/hooks/useRealtimeBatch.d.ts +0 -19
- package/dist/commonjs/hooks/useRealtimeBatch.js +0 -56
- package/dist/commonjs/hooks/useRealtimeBatch.js.map +0 -1
- package/dist/commonjs/hooks/useRealtimeRun.d.ts +0 -18
- package/dist/commonjs/hooks/useRealtimeRun.js +0 -40
- package/dist/commonjs/hooks/useRealtimeRun.js.map +0 -1
- package/dist/commonjs/hooks/useRealtimeRunsWithTag.d.ts +0 -5
- package/dist/commonjs/hooks/useRealtimeRunsWithTag.js +0 -45
- package/dist/commonjs/hooks/useRealtimeRunsWithTag.js.map +0 -1
- package/dist/esm/hooks/useRealtimeBatch.d.ts +0 -19
- package/dist/esm/hooks/useRealtimeBatch.js +0 -53
- package/dist/esm/hooks/useRealtimeBatch.js.map +0 -1
- package/dist/esm/hooks/useRealtimeRun.d.ts +0 -18
- package/dist/esm/hooks/useRealtimeRun.js +0 -37
- package/dist/esm/hooks/useRealtimeRun.js.map +0 -1
- package/dist/esm/hooks/useRealtimeRunsWithTag.d.ts +0 -5
- package/dist/esm/hooks/useRealtimeRunsWithTag.js +0 -42
- package/dist/esm/hooks/useRealtimeRunsWithTag.js.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export * from "./contexts.js";
|
|
2
2
|
export * from "./hooks/useApiClient.js";
|
|
3
3
|
export * from "./hooks/useRun.js";
|
|
4
|
-
export * from "./hooks/
|
|
5
|
-
export * from "./hooks/
|
|
6
|
-
export * from "./hooks/useRealtimeBatch.js";
|
|
4
|
+
export * from "./hooks/useRealtime.js";
|
|
5
|
+
export * from "./hooks/useTaskTrigger.js";
|
|
7
6
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export function createThrottledQueue(onFlush, throttleInMs) {
|
|
2
|
+
let queue = [];
|
|
3
|
+
let lastFlushTime = 0;
|
|
4
|
+
let flushPromise = null;
|
|
5
|
+
const scheduleFlush = async () => {
|
|
6
|
+
// If no throttle specified or there's already a flush in progress, return
|
|
7
|
+
if (!throttleInMs) {
|
|
8
|
+
// Immediately flush when no throttling is specified
|
|
9
|
+
const itemsToFlush = [...queue];
|
|
10
|
+
queue = [];
|
|
11
|
+
await onFlush(itemsToFlush);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (queue.length === 0 || flushPromise)
|
|
15
|
+
return;
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
const timeUntilNextFlush = Math.max(0, lastFlushTime + throttleInMs - now);
|
|
18
|
+
if (timeUntilNextFlush === 0) {
|
|
19
|
+
const itemsToFlush = [...queue];
|
|
20
|
+
queue = [];
|
|
21
|
+
lastFlushTime = now;
|
|
22
|
+
flushPromise = onFlush(itemsToFlush).finally(() => {
|
|
23
|
+
flushPromise = null;
|
|
24
|
+
// Check if more items accumulated during flush
|
|
25
|
+
scheduleFlush();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
setTimeout(scheduleFlush, timeUntilNextFlush);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
add: (item) => {
|
|
34
|
+
queue.push(item);
|
|
35
|
+
scheduleFlush();
|
|
36
|
+
},
|
|
37
|
+
flush: async () => {
|
|
38
|
+
if (queue.length === 0)
|
|
39
|
+
return;
|
|
40
|
+
const itemsToFlush = [...queue];
|
|
41
|
+
queue = [];
|
|
42
|
+
await onFlush(itemsToFlush);
|
|
43
|
+
},
|
|
44
|
+
isEmpty: () => queue.length === 0,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../../src/utils/throttle.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,oBAAoB,CAClC,OAAsC,EACtC,YAAqB;IAErB,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,YAAY,GAAyB,IAAI,CAAC;IAE9C,MAAM,aAAa,GAAG,KAAK,IAAI,EAAE;QAC/B,0EAA0E;QAC1E,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,oDAAoD;YACpD,MAAM,YAAY,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;YAChC,KAAK,GAAG,EAAE,CAAC;YACX,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;YAC5B,OAAO;QACT,CAAC;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,YAAY;YAAE,OAAO;QAE/C,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,kBAAkB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,GAAG,YAAY,GAAG,GAAG,CAAC,CAAC;QAE3E,IAAI,kBAAkB,KAAK,CAAC,EAAE,CAAC;YAC7B,MAAM,YAAY,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;YAChC,KAAK,GAAG,EAAE,CAAC;YACX,aAAa,GAAG,GAAG,CAAC;YACpB,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE;gBAChD,YAAY,GAAG,IAAI,CAAC;gBACpB,+CAA+C;gBAC/C,aAAa,EAAE,CAAC;YAClB,CAAC,CAAC,CAAC;QACL,CAAC;aAAM,CAAC;YACN,UAAU,CAAC,aAAa,EAAE,kBAAkB,CAAC,CAAC;QAChD,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,GAAG,EAAE,CAAC,IAAO,EAAE,EAAE;YACf,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACjB,aAAa,EAAE,CAAC;QAClB,CAAC;QACD,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO;YAC/B,MAAM,YAAY,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;YAChC,KAAK,GAAG,EAAE,CAAC;YACX,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;QAC9B,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;KAClC,CAAC;AACJ,CAAC"}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import { ApiRequestOptions } from "@trigger.dev/core/v3";
|
|
1
2
|
export * from "swr";
|
|
2
3
|
export { default as useSWR, SWRConfig } from "swr";
|
|
3
4
|
export type CommonTriggerHookOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Poll for updates at the specified interval (in milliseconds). Polling is not recommended for most use-cases. Use the Realtime hooks instead.
|
|
7
|
+
*/
|
|
4
8
|
refreshInterval?: number;
|
|
9
|
+
/** Revalidate the data when the browser regains a network connection. */
|
|
5
10
|
revalidateOnReconnect?: boolean;
|
|
11
|
+
/** Revalidate the data when the window regains focus. */
|
|
6
12
|
revalidateOnFocus?: boolean;
|
|
13
|
+
/** Optional access token for authentication */
|
|
14
|
+
accessToken?: string;
|
|
15
|
+
/** Optional base URL for the API endpoints */
|
|
16
|
+
baseURL?: string;
|
|
17
|
+
/** Optional additional request configuration */
|
|
18
|
+
requestOptions?: ApiRequestOptions;
|
|
7
19
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trigger-swr.js","sourceRoot":"","sources":["../../../src/utils/trigger-swr.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"trigger-swr.js","sourceRoot":"","sources":["../../../src/utils/trigger-swr.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAIb,yCAAyC;AACzC,cAAc,KAAK,CAAC;AACpB,yCAAyC;AACzC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trigger.dev/react-hooks",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "trigger.dev react hooks",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
]
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@trigger.dev/core": "^3.1
|
|
32
|
+
"@trigger.dev/core": "^3.2.1",
|
|
33
33
|
"swr": "^2.2.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { AnyTask, TaskRunShape } from "@trigger.dev/core/v3";
|
|
2
|
-
/**
|
|
3
|
-
* hook to subscribe to realtime updates of a batch of task runs.
|
|
4
|
-
*
|
|
5
|
-
* @template TTask - The type of the task.
|
|
6
|
-
* @param {string} batchId - The unique identifier of the batch to subscribe to.
|
|
7
|
-
* @returns {{ runs: TaskRunShape<TTask>[], error: Error | null }} An object containing the current state of the runs and any error encountered.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* import type { myTask } from './path/to/task';
|
|
13
|
-
* const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare function useRealtimeBatch<TTask extends AnyTask>(batchId: string): {
|
|
17
|
-
runs: TaskRunShape<TTask>[];
|
|
18
|
-
error: Error | null;
|
|
19
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.useRealtimeBatch = useRealtimeBatch;
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const useApiClient_js_1 = require("./useApiClient.js");
|
|
7
|
-
/**
|
|
8
|
-
* hook to subscribe to realtime updates of a batch of task runs.
|
|
9
|
-
*
|
|
10
|
-
* @template TTask - The type of the task.
|
|
11
|
-
* @param {string} batchId - The unique identifier of the batch to subscribe to.
|
|
12
|
-
* @returns {{ runs: TaskRunShape<TTask>[], error: Error | null }} An object containing the current state of the runs and any error encountered.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* import type { myTask } from './path/to/task';
|
|
18
|
-
* const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
function useRealtimeBatch(batchId) {
|
|
22
|
-
const [runShapes, setRunShapes] = (0, react_1.useState)([]);
|
|
23
|
-
const [error, setError] = (0, react_1.useState)(null);
|
|
24
|
-
const apiClient = (0, useApiClient_js_1.useApiClient)();
|
|
25
|
-
(0, react_1.useEffect)(() => {
|
|
26
|
-
const subscription = apiClient.subscribeToBatch(batchId);
|
|
27
|
-
async function iterateUpdates() {
|
|
28
|
-
for await (const run of subscription) {
|
|
29
|
-
setRunShapes((prevRuns) => {
|
|
30
|
-
return insertRunShapeInOrder(prevRuns, run);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
iterateUpdates().catch((err) => {
|
|
35
|
-
setError(err);
|
|
36
|
-
});
|
|
37
|
-
return () => {
|
|
38
|
-
subscription.unsubscribe();
|
|
39
|
-
};
|
|
40
|
-
}, [batchId]);
|
|
41
|
-
return { runs: runShapes, error };
|
|
42
|
-
}
|
|
43
|
-
// Inserts and then orders by the run number, and ensures that the run is not duplicated
|
|
44
|
-
function insertRunShapeInOrder(previousRuns, run) {
|
|
45
|
-
const existingRun = previousRuns.find((r) => r.id === run.id);
|
|
46
|
-
if (existingRun) {
|
|
47
|
-
return previousRuns.map((r) => (r.id === run.id ? run : r));
|
|
48
|
-
}
|
|
49
|
-
const runNumber = run.number;
|
|
50
|
-
const index = previousRuns.findIndex((r) => r.number > runNumber);
|
|
51
|
-
if (index === -1) {
|
|
52
|
-
return [...previousRuns, run];
|
|
53
|
-
}
|
|
54
|
-
return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
|
|
55
|
-
}
|
|
56
|
-
//# sourceMappingURL=useRealtimeBatch.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRealtimeBatch.js","sourceRoot":"","sources":["../../../src/hooks/useRealtimeBatch.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAoBb,4CA0BC;AA3CD,iCAA4C;AAC5C,uDAAiD;AAEjD;;;;;;;;;;;;;GAaG;AACH,SAAgB,gBAAgB,CAAwB,OAAe;IACrE,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,IAAA,gBAAQ,EAAwB,EAAE,CAAC,CAAC;IACtE,MAAM,CAAC,KAAK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,MAAM,CAAC;IAC7B,MAAM,KAAK,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClE,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAED,OAAO,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { AnyTask, TaskRunShape } from "@trigger.dev/core/v3";
|
|
2
|
-
/**
|
|
3
|
-
* hook to subscribe to realtime updates of a task run.
|
|
4
|
-
*
|
|
5
|
-
* @template TTask - The type of the task.
|
|
6
|
-
* @param {string} runId - The unique identifier of the run to subscribe to.
|
|
7
|
-
* @returns {{ run: TaskRunShape<TTask> | undefined, error: Error | null }} An object containing the current state of the run and any error encountered.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import type { myTask } from './path/to/task';
|
|
12
|
-
* const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export declare function useRealtimeRun<TTask extends AnyTask>(runId: string): {
|
|
16
|
-
run: TaskRunShape<TTask> | undefined;
|
|
17
|
-
error: Error | null;
|
|
18
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.useRealtimeRun = useRealtimeRun;
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const useApiClient_js_1 = require("./useApiClient.js");
|
|
7
|
-
/**
|
|
8
|
-
* hook to subscribe to realtime updates of a task run.
|
|
9
|
-
*
|
|
10
|
-
* @template TTask - The type of the task.
|
|
11
|
-
* @param {string} runId - The unique identifier of the run to subscribe to.
|
|
12
|
-
* @returns {{ run: TaskRunShape<TTask> | undefined, error: Error | null }} An object containing the current state of the run and any error encountered.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* import type { myTask } from './path/to/task';
|
|
17
|
-
* const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
function useRealtimeRun(runId) {
|
|
21
|
-
const [runShape, setRunShape] = (0, react_1.useState)(undefined);
|
|
22
|
-
const [error, setError] = (0, react_1.useState)(null);
|
|
23
|
-
const apiClient = (0, useApiClient_js_1.useApiClient)();
|
|
24
|
-
(0, react_1.useEffect)(() => {
|
|
25
|
-
const subscription = apiClient.subscribeToRun(runId);
|
|
26
|
-
async function iterateUpdates() {
|
|
27
|
-
for await (const run of subscription) {
|
|
28
|
-
setRunShape(run);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
iterateUpdates().catch((err) => {
|
|
32
|
-
setError(err);
|
|
33
|
-
});
|
|
34
|
-
return () => {
|
|
35
|
-
subscription.unsubscribe();
|
|
36
|
-
};
|
|
37
|
-
}, [runId]);
|
|
38
|
-
return { run: runShape, error };
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=useRealtimeRun.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRealtimeRun.js","sourceRoot":"","sources":["../../../src/hooks/useRealtimeRun.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAmBb,wCA0BC;AA1CD,iCAA4C;AAC5C,uDAAiD;AAEjD;;;;;;;;;;;;GAYG;AACH,SAAgB,cAAc,CAC5B,KAAa;IAEb,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,IAAA,gBAAQ,EAAkC,SAAS,CAAC,CAAC;IACrF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAe,IAAI,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,IAAA,8BAAY,GAAE,CAAC;IAEjC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,SAAS,CAAC,cAAc,CAAuB,KAAK,CAAC,CAAC;QAE3E,KAAK,UAAU,cAAc;YAC3B,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;gBACrC,WAAW,CAAC,GAAG,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;QAED,cAAc,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YAC7B,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,YAAY,CAAC,WAAW,EAAE,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.useRealtimeRunsWithTag = useRealtimeRunsWithTag;
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const useApiClient_js_1 = require("./useApiClient.js");
|
|
7
|
-
function useRealtimeRunsWithTag(tag) {
|
|
8
|
-
const [runShapes, setRunShapes] = (0, react_1.useState)([]);
|
|
9
|
-
const [error, setError] = (0, react_1.useState)(null);
|
|
10
|
-
const apiClient = (0, useApiClient_js_1.useApiClient)();
|
|
11
|
-
(0, react_1.useEffect)(() => {
|
|
12
|
-
const subscription = apiClient.subscribeToRunsWithTag(tag);
|
|
13
|
-
async function iterateUpdates() {
|
|
14
|
-
for await (const run of subscription) {
|
|
15
|
-
setRunShapes((prevRuns) => {
|
|
16
|
-
return insertRunShape(prevRuns, run);
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
iterateUpdates().catch((err) => {
|
|
21
|
-
setError(err);
|
|
22
|
-
});
|
|
23
|
-
return () => {
|
|
24
|
-
subscription.unsubscribe();
|
|
25
|
-
};
|
|
26
|
-
}, [tag]);
|
|
27
|
-
return { runs: runShapes, error };
|
|
28
|
-
}
|
|
29
|
-
function stableSortTags(tag) {
|
|
30
|
-
return Array.isArray(tag) ? tag.slice().sort() : [tag];
|
|
31
|
-
}
|
|
32
|
-
// Replaces or inserts a run shape, ordered by the createdAt timestamp
|
|
33
|
-
function insertRunShape(previousRuns, run) {
|
|
34
|
-
const existingRun = previousRuns.find((r) => r.id === run.id);
|
|
35
|
-
if (existingRun) {
|
|
36
|
-
return previousRuns.map((r) => (r.id === run.id ? run : r));
|
|
37
|
-
}
|
|
38
|
-
const createdAt = run.createdAt;
|
|
39
|
-
const index = previousRuns.findIndex((r) => r.createdAt > createdAt);
|
|
40
|
-
if (index === -1) {
|
|
41
|
-
return [...previousRuns, run];
|
|
42
|
-
}
|
|
43
|
-
return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=useRealtimeRunsWithTag.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRealtimeRunsWithTag.js","sourceRoot":"","sources":["../../../src/hooks/useRealtimeRunsWithTag.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAMb,wDA0BC;AA7BD,iCAA4C;AAC5C,uDAAiD;AAEjD,SAAgB,sBAAsB,CAAwB,GAAsB;IAClF,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,IAAA,gBAAQ,EAAwB,EAAE,CAAC,CAAC;IACtE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAe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AE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,CAAC,SAAS,CAAC;IAEhC,MAAM,KAAK,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,GAAG,SAAS,CAAC,CAAC;IAErE,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAED,OAAO,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { AnyTask, TaskRunShape } from "@trigger.dev/core/v3";
|
|
2
|
-
/**
|
|
3
|
-
* hook to subscribe to realtime updates of a batch of task runs.
|
|
4
|
-
*
|
|
5
|
-
* @template TTask - The type of the task.
|
|
6
|
-
* @param {string} batchId - The unique identifier of the batch to subscribe to.
|
|
7
|
-
* @returns {{ runs: TaskRunShape<TTask>[], error: Error | null }} An object containing the current state of the runs and any error encountered.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* import type { myTask } from './path/to/task';
|
|
13
|
-
* const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare function useRealtimeBatch<TTask extends AnyTask>(batchId: string): {
|
|
17
|
-
runs: TaskRunShape<TTask>[];
|
|
18
|
-
error: Error | null;
|
|
19
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import { useApiClient } from "./useApiClient.js";
|
|
4
|
-
/**
|
|
5
|
-
* hook to subscribe to realtime updates of a batch of task runs.
|
|
6
|
-
*
|
|
7
|
-
* @template TTask - The type of the task.
|
|
8
|
-
* @param {string} batchId - The unique identifier of the batch to subscribe to.
|
|
9
|
-
* @returns {{ runs: TaskRunShape<TTask>[], error: Error | null }} An object containing the current state of the runs and any error encountered.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* import type { myTask } from './path/to/task';
|
|
15
|
-
* const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export function useRealtimeBatch(batchId) {
|
|
19
|
-
const [runShapes, setRunShapes] = useState([]);
|
|
20
|
-
const [error, setError] = useState(null);
|
|
21
|
-
const apiClient = useApiClient();
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
const subscription = apiClient.subscribeToBatch(batchId);
|
|
24
|
-
async function iterateUpdates() {
|
|
25
|
-
for await (const run of subscription) {
|
|
26
|
-
setRunShapes((prevRuns) => {
|
|
27
|
-
return insertRunShapeInOrder(prevRuns, run);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
iterateUpdates().catch((err) => {
|
|
32
|
-
setError(err);
|
|
33
|
-
});
|
|
34
|
-
return () => {
|
|
35
|
-
subscription.unsubscribe();
|
|
36
|
-
};
|
|
37
|
-
}, [batchId]);
|
|
38
|
-
return { runs: runShapes, error };
|
|
39
|
-
}
|
|
40
|
-
// Inserts and then orders by the run number, and ensures that the run is not duplicated
|
|
41
|
-
function insertRunShapeInOrder(previousRuns, run) {
|
|
42
|
-
const existingRun = previousRuns.find((r) => r.id === run.id);
|
|
43
|
-
if (existingRun) {
|
|
44
|
-
return previousRuns.map((r) => (r.id === run.id ? run : r));
|
|
45
|
-
}
|
|
46
|
-
const runNumber = run.number;
|
|
47
|
-
const index = previousRuns.findIndex((r) => r.number > runNumber);
|
|
48
|
-
if (index === -1) {
|
|
49
|
-
return [...previousRuns, run];
|
|
50
|
-
}
|
|
51
|
-
return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
|
|
52
|
-
}
|
|
53
|
-
//# sourceMappingURL=useRealtimeBatch.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRealtimeBatch.js","sourceRoot":"","sources":["../../../src/hooks/useRealtimeBatch.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,gBAAgB,CAAwB,OAAe;IACrE,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAwB,EAAE,CAAC,CAAC;IACtE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IAEjC,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,SAAS,CAAC,gBAAgB,CAAuB,OAAO,CAAC,CAAC;QAE/E,KAAK,UAAU,cAAc;YAC3B,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;gBACrC,YAAY,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACxB,OAAO,qBAAqB,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,cAAc,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YAC7B,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,YAAY,CAAC,WAAW,EAAE,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACpC,CAAC;AAED,wFAAwF;AACxF,SAAS,qBAAqB,CAC5B,YAAmC,EACnC,GAAwB;IAExB,MAAM,WAAW,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,CAAC,MAAM,CAAC;IAC7B,MAAM,KAAK,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClE,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAED,OAAO,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { AnyTask, TaskRunShape } from "@trigger.dev/core/v3";
|
|
2
|
-
/**
|
|
3
|
-
* hook to subscribe to realtime updates of a task run.
|
|
4
|
-
*
|
|
5
|
-
* @template TTask - The type of the task.
|
|
6
|
-
* @param {string} runId - The unique identifier of the run to subscribe to.
|
|
7
|
-
* @returns {{ run: TaskRunShape<TTask> | undefined, error: Error | null }} An object containing the current state of the run and any error encountered.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import type { myTask } from './path/to/task';
|
|
12
|
-
* const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export declare function useRealtimeRun<TTask extends AnyTask>(runId: string): {
|
|
16
|
-
run: TaskRunShape<TTask> | undefined;
|
|
17
|
-
error: Error | null;
|
|
18
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import { useApiClient } from "./useApiClient.js";
|
|
4
|
-
/**
|
|
5
|
-
* hook to subscribe to realtime updates of a task run.
|
|
6
|
-
*
|
|
7
|
-
* @template TTask - The type of the task.
|
|
8
|
-
* @param {string} runId - The unique identifier of the run to subscribe to.
|
|
9
|
-
* @returns {{ run: TaskRunShape<TTask> | undefined, error: Error | null }} An object containing the current state of the run and any error encountered.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import type { myTask } from './path/to/task';
|
|
14
|
-
* const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export function useRealtimeRun(runId) {
|
|
18
|
-
const [runShape, setRunShape] = useState(undefined);
|
|
19
|
-
const [error, setError] = useState(null);
|
|
20
|
-
const apiClient = useApiClient();
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
const subscription = apiClient.subscribeToRun(runId);
|
|
23
|
-
async function iterateUpdates() {
|
|
24
|
-
for await (const run of subscription) {
|
|
25
|
-
setRunShape(run);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
iterateUpdates().catch((err) => {
|
|
29
|
-
setError(err);
|
|
30
|
-
});
|
|
31
|
-
return () => {
|
|
32
|
-
subscription.unsubscribe();
|
|
33
|
-
};
|
|
34
|
-
}, [runId]);
|
|
35
|
-
return { run: runShape, error };
|
|
36
|
-
}
|
|
37
|
-
//# sourceMappingURL=useRealtimeRun.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRealtimeRun.js","sourceRoot":"","sources":["../../../src/hooks/useRealtimeRun.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAa;IAEb,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAkC,SAAS,CAAC,CAAC;IACrF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IAEjC,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,SAAS,CAAC,cAAc,CAAuB,KAAK,CAAC,CAAC;QAE3E,KAAK,UAAU,cAAc;YAC3B,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;gBACrC,WAAW,CAAC,GAAG,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;QAED,cAAc,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YAC7B,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE;YACV,YAAY,CAAC,WAAW,EAAE,CAAC;QAC7B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC"}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
3
|
-
import { useApiClient } from "./useApiClient.js";
|
|
4
|
-
export function useRealtimeRunsWithTag(tag) {
|
|
5
|
-
const [runShapes, setRunShapes] = useState([]);
|
|
6
|
-
const [error, setError] = useState(null);
|
|
7
|
-
const apiClient = useApiClient();
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
const subscription = apiClient.subscribeToRunsWithTag(tag);
|
|
10
|
-
async function iterateUpdates() {
|
|
11
|
-
for await (const run of subscription) {
|
|
12
|
-
setRunShapes((prevRuns) => {
|
|
13
|
-
return insertRunShape(prevRuns, run);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
iterateUpdates().catch((err) => {
|
|
18
|
-
setError(err);
|
|
19
|
-
});
|
|
20
|
-
return () => {
|
|
21
|
-
subscription.unsubscribe();
|
|
22
|
-
};
|
|
23
|
-
}, [tag]);
|
|
24
|
-
return { runs: runShapes, error };
|
|
25
|
-
}
|
|
26
|
-
function stableSortTags(tag) {
|
|
27
|
-
return Array.isArray(tag) ? tag.slice().sort() : [tag];
|
|
28
|
-
}
|
|
29
|
-
// Replaces or inserts a run shape, ordered by the createdAt timestamp
|
|
30
|
-
function insertRunShape(previousRuns, run) {
|
|
31
|
-
const existingRun = previousRuns.find((r) => r.id === run.id);
|
|
32
|
-
if (existingRun) {
|
|
33
|
-
return previousRuns.map((r) => (r.id === run.id ? run : r));
|
|
34
|
-
}
|
|
35
|
-
const createdAt = run.createdAt;
|
|
36
|
-
const index = previousRuns.findIndex((r) => r.createdAt > createdAt);
|
|
37
|
-
if (index === -1) {
|
|
38
|
-
return [...previousRuns, run];
|
|
39
|
-
}
|
|
40
|
-
return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
|
|
41
|
-
}
|
|
42
|
-
//# sourceMappingURL=useRealtimeRunsWithTag.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use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