@umituz/web-hono 1.16.0 → 1.17.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/package.json +2 -1
- package/src/resource-react.ts +39 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), better-auth server integration (tables/getter/mount/session helpers), weak-compare If-None-Match matcher (matchesEtag), cookie channels + request scope/ETag/R2-stream server helpers, TTL resource cache, and db lifecycle scripts (reset / snapshot / seed)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"./http": "./src/http.ts",
|
|
23
23
|
"./r2": "./src/r2.ts",
|
|
24
24
|
"./resource": "./src/resource.ts",
|
|
25
|
+
"./resource/react": "./src/resource-react.ts",
|
|
25
26
|
"./package.json": "./package.json"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/* React binding for a createResource instance. The resource is
|
|
2
|
+
framework-free; apps that don't use React never import this entry.
|
|
3
|
+
Binding pattern:
|
|
4
|
+
|
|
5
|
+
const plans = createResource({ load, storageKey: null, ttlMs: RESOURCE_TTL_MS });
|
|
6
|
+
const { value, loading } = useResource(plans); */
|
|
7
|
+
|
|
8
|
+
import { useEffect, useState, useSyncExternalStore } from 'react';
|
|
9
|
+
import type { Resource } from './resource';
|
|
10
|
+
|
|
11
|
+
export interface ResourceResult<T> {
|
|
12
|
+
value: T | null;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function useResource<T>(resource: Resource<T>): ResourceResult<T> {
|
|
17
|
+
const value = useSyncExternalStore(
|
|
18
|
+
(onChange) => resource.subscribe(onChange),
|
|
19
|
+
() => resource.read(),
|
|
20
|
+
() => resource.read(),
|
|
21
|
+
);
|
|
22
|
+
const [loading, setLoading] = useState(resource.read() === null);
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (resource.read() !== null) setLoading(false);
|
|
26
|
+
let alive = true;
|
|
27
|
+
resource
|
|
28
|
+
.ensure()
|
|
29
|
+
.catch(() => null)
|
|
30
|
+
.finally(() => {
|
|
31
|
+
if (alive) setLoading(false);
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
alive = false;
|
|
35
|
+
};
|
|
36
|
+
}, [resource]);
|
|
37
|
+
|
|
38
|
+
return { value, loading };
|
|
39
|
+
}
|