@trigger.dev/react-hooks 0.0.0-v3-prerelease-20241118124944 → 0.0.0-v3-prerelease-20250108131948
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 +39 -2
- package/dist/commonjs/hooks/useApiClient.js +34 -6
- package/dist/commonjs/hooks/useApiClient.js.map +1 -1
- package/dist/commonjs/hooks/useRealtime.d.ts +117 -0
- package/dist/commonjs/hooks/useRealtime.js +409 -0
- package/dist/commonjs/hooks/useRealtime.js.map +1 -0
- package/dist/commonjs/hooks/useRun.js +7 -2
- package/dist/commonjs/hooks/useRun.js.map +1 -1
- package/dist/commonjs/hooks/useTaskTrigger.d.ts +101 -0
- package/dist/commonjs/hooks/useTaskTrigger.js +137 -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 +39 -2
- package/dist/esm/hooks/useApiClient.js +35 -7
- package/dist/esm/hooks/useApiClient.js.map +1 -1
- package/dist/esm/hooks/useRealtime.d.ts +117 -0
- package/dist/esm/hooks/useRealtime.js +403 -0
- package/dist/esm/hooks/useRealtime.js.map +1 -0
- package/dist/esm/hooks/useRun.js +7 -2
- package/dist/esm/hooks/useRun.js.map +1 -1
- package/dist/esm/hooks/useTaskTrigger.d.ts +101 -0
- package/dist/esm/hooks/useTaskTrigger.js +129 -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 +4 -4
- 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
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { makeIdempotencyKey, stringifyIO, } from "@trigger.dev/core/v3";
|
|
3
|
+
import useSWRMutation from "swr/mutation";
|
|
4
|
+
import { useApiClient } from "./useApiClient.js";
|
|
5
|
+
import { useRealtimeRun, useRealtimeRunWithStreams, } from "./useRealtime.js";
|
|
6
|
+
/**
|
|
7
|
+
* Hook to trigger a task and manage its initial execution state.
|
|
8
|
+
*
|
|
9
|
+
* @template TTask - The type of the task
|
|
10
|
+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
|
|
11
|
+
* @param {UseTaskTriggerOptions} [options] - Configuration options for the task trigger
|
|
12
|
+
* @returns {TriggerInstance<TTask>} An object containing the submit function, loading state, handle, and any errors
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import type { myTask } from './path/to/task';
|
|
17
|
+
* const { submit, isLoading, handle, error } = useTaskTrigger<typeof myTask>('my-task-id');
|
|
18
|
+
*
|
|
19
|
+
* // Submit the task with payload
|
|
20
|
+
* submit({ foo: 'bar' });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useTaskTrigger(id, options) {
|
|
24
|
+
const apiClient = useApiClient(options);
|
|
25
|
+
async function triggerTask(id, { arg: { payload, options }, }) {
|
|
26
|
+
if (!apiClient) {
|
|
27
|
+
throw new Error("Could not trigger task in useTaskTrigger: Missing access token");
|
|
28
|
+
}
|
|
29
|
+
const payloadPacket = await stringifyIO(payload);
|
|
30
|
+
const handle = await apiClient.triggerTask(id, {
|
|
31
|
+
payload: payloadPacket.data,
|
|
32
|
+
options: {
|
|
33
|
+
queue: options?.queue,
|
|
34
|
+
concurrencyKey: options?.concurrencyKey,
|
|
35
|
+
payloadType: payloadPacket.dataType,
|
|
36
|
+
idempotencyKey: await makeIdempotencyKey(options?.idempotencyKey),
|
|
37
|
+
delay: options?.delay,
|
|
38
|
+
ttl: options?.ttl,
|
|
39
|
+
tags: options?.tags,
|
|
40
|
+
maxAttempts: options?.maxAttempts,
|
|
41
|
+
metadata: options?.metadata,
|
|
42
|
+
maxDuration: options?.maxDuration,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return { ...handle, taskIdentifier: id };
|
|
46
|
+
}
|
|
47
|
+
const mutation = useSWRMutation(id, triggerTask);
|
|
48
|
+
return {
|
|
49
|
+
submit: (payload, options) => {
|
|
50
|
+
// trigger the task with the given payload
|
|
51
|
+
mutation.trigger({ payload, options });
|
|
52
|
+
},
|
|
53
|
+
isLoading: mutation.isMutating,
|
|
54
|
+
handle: mutation.data,
|
|
55
|
+
error: mutation.error,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Hook to trigger a task and subscribe to its realtime updates including stream data.
|
|
60
|
+
*
|
|
61
|
+
* @template TTask - The type of the task
|
|
62
|
+
* @template TStreams - The type of the streams data
|
|
63
|
+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
|
|
64
|
+
* @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
|
|
65
|
+
* @returns {RealtimeTriggerInstanceWithStreams<TTask, TStreams>} An object containing the submit function, loading state,
|
|
66
|
+
* handle, run state, streams data, and error handling
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import type { myTask } from './path/to/task';
|
|
71
|
+
* const { submit, run, streams, error } = useRealtimeTaskTriggerWithStreams<
|
|
72
|
+
* typeof myTask,
|
|
73
|
+
* { output: string }
|
|
74
|
+
* >('my-task-id');
|
|
75
|
+
*
|
|
76
|
+
* // Submit and monitor the task with streams
|
|
77
|
+
* submit({ foo: 'bar' });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function useRealtimeTaskTriggerWithStreams(id, options) {
|
|
81
|
+
const triggerInstance = useTaskTrigger(id, options);
|
|
82
|
+
const realtimeInstance = useRealtimeRunWithStreams(triggerInstance.handle?.id, {
|
|
83
|
+
...options,
|
|
84
|
+
id: triggerInstance.handle?.id,
|
|
85
|
+
accessToken: triggerInstance.handle?.publicAccessToken ?? options?.accessToken,
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
...realtimeInstance,
|
|
89
|
+
...triggerInstance,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Hook to trigger a task and subscribe to its realtime updates.
|
|
94
|
+
*
|
|
95
|
+
* @template TTask - The type of the task
|
|
96
|
+
* @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
|
|
97
|
+
* @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
|
|
98
|
+
* @returns {RealtimeTriggerInstance<TTask>} An object containing the submit function, loading state,
|
|
99
|
+
* handle, run state, and error handling
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import type { myTask } from './path/to/task';
|
|
104
|
+
* const { submit, run, error, stop } = useRealtimeTaskTrigger<typeof myTask>('my-task-id');
|
|
105
|
+
*
|
|
106
|
+
* // Submit and monitor the task
|
|
107
|
+
* submit({ foo: 'bar' });
|
|
108
|
+
*
|
|
109
|
+
* // Stop monitoring when needed
|
|
110
|
+
* stop();
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export function useRealtimeTaskTrigger(id, options) {
|
|
114
|
+
const triggerInstance = useTaskTrigger(id, options);
|
|
115
|
+
const realtimeInstance = useRealtimeRun(triggerInstance.handle?.id, {
|
|
116
|
+
...options,
|
|
117
|
+
id: triggerInstance.handle?.id,
|
|
118
|
+
accessToken: triggerInstance.handle?.publicAccessToken ?? options?.accessToken,
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
submit: triggerInstance.submit,
|
|
122
|
+
isLoading: triggerInstance.isLoading,
|
|
123
|
+
handle: triggerInstance.handle,
|
|
124
|
+
run: realtimeInstance.run,
|
|
125
|
+
error: realtimeInstance.error ?? triggerInstance.error,
|
|
126
|
+
stop: realtimeInstance.stop,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=useTaskTrigger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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,MAAM,EAAE,EAAE;QAC9B,WAAW,EAAE,eAAe,CAAC,MAAM,EAAE,iBAAiB,IAAI,OAAO,EAAE,WAAW;KAC/E,CAAC,CAAC;IAEH,OAAO;QACL,MAAM,EAAE,eAAe,CAAC,MAAM;QAC9B,SAAS,EAAE,eAAe,CAAC,SAAS;QACpC,MAAM,EAAE,eAAe,CAAC,MAAM;QAC9B,GAAG,EAAE,gBAAgB,CAAC,GAAG;QACzB,KAAK,EAAE,gBAAgB,CAAC,KAAK,IAAI,eAAe,CAAC,KAAK;QACtD,IAAI,EAAE,gBAAgB,CAAC,IAAI;KAC5B,CAAC;AACJ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
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";
|
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": "0.0.0-v3-prerelease-
|
|
3
|
+
"version": "0.0.0-v3-prerelease-20250108131948",
|
|
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": "0.0.0-v3-prerelease-
|
|
32
|
+
"@trigger.dev/core": "0.0.0-v3-prerelease-20250108131948",
|
|
33
33
|
"swr": "^2.2.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"typescript": "^5.5.4"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"react": "
|
|
47
|
-
"react-dom": "
|
|
46
|
+
"react": "^18.0 || ^19.0 || ^19.0.0-rc",
|
|
47
|
+
"react-dom": "^18.0 || ^19.0 || ^19.0.0-rc"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": ">=18.20.0"
|
|
@@ -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;IAC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AS,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,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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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,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"}
|