gantry-web 0.3.3 → 0.3.5
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 +1 -1
- package/src/appinfo.ts +55 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
package/src/appinfo.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The app's own identity, served by the built-in "gantry" service the
|
|
2
|
+
// Go side registers in gantry.Run: name, title and the version stamped
|
|
3
|
+
// from gantry.json by gantry dev/build.
|
|
4
|
+
|
|
5
|
+
import { useEffect, useState } from "react";
|
|
6
|
+
import { callGo } from "./socket";
|
|
7
|
+
|
|
8
|
+
export interface AppInfo {
|
|
9
|
+
/** gantry.json "name" - the exe/module identity. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** gantry.json "title" - the human-facing name. */
|
|
12
|
+
title: string;
|
|
13
|
+
/** gantry.json "version", stamped into the exe by gantry dev/build. */
|
|
14
|
+
version: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let cached: AppInfo | null = null;
|
|
18
|
+
let pending: Promise<AppInfo> | null = null;
|
|
19
|
+
|
|
20
|
+
/** fetchAppInfo resolves the app's name/title/version once and caches
|
|
21
|
+
* it - the values cannot change while the app runs. */
|
|
22
|
+
export function fetchAppInfo(): Promise<AppInfo> {
|
|
23
|
+
if (cached) return Promise.resolve(cached);
|
|
24
|
+
pending ??= callGo("gantry", "appInfo")
|
|
25
|
+
.then((v) => (cached = v as AppInfo))
|
|
26
|
+
.catch((err) => {
|
|
27
|
+
pending = null; // retry on the next call
|
|
28
|
+
throw err;
|
|
29
|
+
});
|
|
30
|
+
return pending;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* useAppInfo returns { name, title, version } - null until the first
|
|
35
|
+
* fetch resolves. The obvious About-page one-liner:
|
|
36
|
+
*
|
|
37
|
+
* const info = useAppInfo();
|
|
38
|
+
* return <span>v{info?.version}</span>;
|
|
39
|
+
*/
|
|
40
|
+
export function useAppInfo(): AppInfo | null {
|
|
41
|
+
const [info, setInfo] = useState(cached);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (info) return;
|
|
44
|
+
let alive = true;
|
|
45
|
+
fetchAppInfo()
|
|
46
|
+
.then((v) => {
|
|
47
|
+
if (alive) setInfo(v);
|
|
48
|
+
})
|
|
49
|
+
.catch(() => {});
|
|
50
|
+
return () => {
|
|
51
|
+
alive = false;
|
|
52
|
+
};
|
|
53
|
+
}, [info]);
|
|
54
|
+
return info;
|
|
55
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,5 +16,7 @@ export type { Paired } from "./paired";
|
|
|
16
16
|
export { service, useService, useCall } from "./service";
|
|
17
17
|
export type { Service, CallResult } from "./service";
|
|
18
18
|
export { useGoState } from "./gostate";
|
|
19
|
+
export { useAppInfo, fetchAppInfo } from "./appinfo";
|
|
20
|
+
export type { AppInfo } from "./appinfo";
|
|
19
21
|
export { TeaView } from "./tea/Runtime";
|
|
20
22
|
export type { TeaComponentProps, ComponentRegistry } from "./tea/Runtime";
|