@polyphron/viper-react 0.2.1 → 0.4.0
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/README.md +70 -0
- package/dist/auth.d.ts +66 -4
- package/dist/auth.js +102 -5
- package/dist/db-fetch.d.ts +121 -0
- package/dist/db-fetch.js +398 -0
- package/dist/db-page.js +38 -74
- package/dist/db-query.d.ts +0 -6
- package/dist/db-query.js +8 -8
- package/dist/db-shared.d.ts +50 -0
- package/dist/db-shared.js +82 -0
- package/dist/db.d.ts +1 -0
- package/dist/db.js +40 -71
- package/dist/identity.d.ts +21 -0
- package/dist/identity.js +54 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -2
- package/dist/loaded.d.ts +30 -0
- package/dist/loaded.js +64 -0
- package/dist/page-plan.js +1 -4
- package/dist/records.d.ts +1 -14
- package/dist/records.js +4 -39
- package/dist/runs.d.ts +23 -0
- package/dist/runs.js +59 -0
- package/package.json +3 -2
package/dist/runs.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useViper } from "./provider.js";
|
|
3
|
+
/** A run is still going while it runs or waits; done, failed and cancelled are final. */
|
|
4
|
+
function unfinished(run) {
|
|
5
|
+
return run.status === "running" || run.status === "waiting";
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Watches a background workflow's run until it's done, failed or cancelled, reading
|
|
9
|
+
* it every `every` milliseconds (a second by default). Pass the
|
|
10
|
+
* `StartedRun` its call returned; nothing is read while it's empty.
|
|
11
|
+
*
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const [started, setStarted] = useState<StartedRun>()
|
|
14
|
+
* const { data, pending } = useRun<{ text: string }>("forge", started)
|
|
15
|
+
* // on click: setStarted(await viper.workflows.call<StartedRun>("forge", input))
|
|
16
|
+
* // when done: data.response.body.text
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function useRun(name, started, options = {}) {
|
|
20
|
+
const viper = useViper();
|
|
21
|
+
const every = options.every ?? 1000;
|
|
22
|
+
const run = started?.run;
|
|
23
|
+
const token = started?.token;
|
|
24
|
+
// The answer carries the run it's for, so another run's never shows.
|
|
25
|
+
const [state, setState] = useState({ key: "", data: null, error: null });
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (run === undefined || token === undefined)
|
|
28
|
+
return;
|
|
29
|
+
const key = `${name}/${run}`;
|
|
30
|
+
const controller = new AbortController();
|
|
31
|
+
let timer;
|
|
32
|
+
const read = async () => {
|
|
33
|
+
try {
|
|
34
|
+
const data = await viper.workflows.run(name, { run, token }, { signal: controller.signal });
|
|
35
|
+
setState({ key, data, error: null });
|
|
36
|
+
if (unfinished(data)) {
|
|
37
|
+
timer = setTimeout(() => void read(), every);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (!controller.signal.aborted)
|
|
42
|
+
setState({ key, data: null, error });
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
void read();
|
|
46
|
+
return () => {
|
|
47
|
+
controller.abort();
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
};
|
|
50
|
+
}, [viper, name, run, token, every]);
|
|
51
|
+
const mine = run !== undefined && state.key === `${name}/${run}`;
|
|
52
|
+
const data = mine ? state.data : null;
|
|
53
|
+
const error = mine ? state.error : null;
|
|
54
|
+
return {
|
|
55
|
+
data,
|
|
56
|
+
error,
|
|
57
|
+
pending: run !== undefined && !error && (data === null || unfinished(data)),
|
|
58
|
+
};
|
|
59
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polyphron/viper-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "React provider and hooks for the Viper app API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"typecheck": "tsc --noEmit"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@polyphron/viper-client": "^0.
|
|
33
|
+
"@polyphron/viper-client": "^0.4.0",
|
|
34
34
|
"@tanstack/db": "0.9.2",
|
|
35
35
|
"@tanstack/react-db": "0.4.1",
|
|
36
36
|
"react": "^18.3.0 || ^19.0.0"
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"@tanstack/react-db": "0.4.1",
|
|
43
43
|
"@types/node": "^24",
|
|
44
44
|
"@types/react": "^19",
|
|
45
|
+
"@viper/admin": "*",
|
|
45
46
|
"eslint": "^10",
|
|
46
47
|
"eslint-plugin-react-hooks": "^7.1.1",
|
|
47
48
|
"react": "^19.2.8",
|