@phreshos/react 0.1.2 → 0.1.4
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 +13 -9
- package/dist/host-provider.d.ts +4 -4
- package/dist/host-provider.js +7 -7
- package/dist/main.d.ts +2 -1
- package/dist/main.js +2 -1
- package/dist/use-service-state.d.ts +7 -0
- package/dist/use-service-state.js +10 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -20,13 +20,14 @@ It provides two kinds of adapter:
|
|
|
20
20
|
`provide` prop, then exposes them synchronously through `useProgram()`,
|
|
21
21
|
`useProcess()`, and `useParent()`.
|
|
22
22
|
- `HostProvider` subscribes before reading exactly the host values named by its
|
|
23
|
-
required `provide` prop. `useHostTheme()`, `
|
|
23
|
+
required `provide` prop. `useHostTheme()`, `useDesktopSize()`, and
|
|
24
24
|
`usePointerPosition()` expose those values synchronously after resolution.
|
|
25
25
|
- `useSubscribe()` and `useObserve()` own persistent ordinary-event
|
|
26
26
|
registrations for one mounted React consumer.
|
|
27
|
-
- `useProgramState(program)`, `useProcessState(process)`,
|
|
28
|
-
`useWindowState(window)` explicitly compose
|
|
29
|
-
events for one mounted consumer. They add no
|
|
27
|
+
- `useProgramState(program)`, `useProcessState(process)`,
|
|
28
|
+
`useServiceState(service)`, and `useWindowState(window)` explicitly compose
|
|
29
|
+
existing reads with future live events for one mounted consumer. They add no
|
|
30
|
+
state operation to another SDK.
|
|
30
31
|
- `useObserveAsks()` adapts an Endpoint traffic surface's question observation,
|
|
31
32
|
while `useObserveAnswers()` adapts a Server traffic surface's answer
|
|
32
33
|
observation.
|
|
@@ -48,6 +49,9 @@ useProgramState(program)
|
|
|
48
49
|
useProcessState(process)
|
|
49
50
|
// { exited, serverExists, clientExists } | undefined
|
|
50
51
|
|
|
52
|
+
useServiceState(service)
|
|
53
|
+
// { disabled } | undefined
|
|
54
|
+
|
|
51
55
|
useWindowState(window)
|
|
52
56
|
// WindowState | undefined
|
|
53
57
|
```
|
|
@@ -86,7 +90,7 @@ The ordinary-event and traffic hooks retain the latest value returned by their p
|
|
|
86
90
|
import { host } from "@phreshos/client"
|
|
87
91
|
import { useSubscribe } from "@phreshos/react"
|
|
88
92
|
|
|
89
|
-
const
|
|
93
|
+
const desktop = useSubscribe(host.desktop, "resize")
|
|
90
94
|
```
|
|
91
95
|
|
|
92
96
|
Host reads are asynchronous while subscriptions are live-only. `HostProvider`
|
|
@@ -94,17 +98,17 @@ subscribes before requesting each selected snapshot and prevents an older read
|
|
|
94
98
|
from overwriting a newer event:
|
|
95
99
|
|
|
96
100
|
```tsx
|
|
97
|
-
import { HostProvider,
|
|
101
|
+
import { HostProvider, useDesktopSize, useHostTheme } from "@phreshos/react"
|
|
98
102
|
|
|
99
103
|
function Content() {
|
|
100
104
|
const theme = useHostTheme()
|
|
101
|
-
const
|
|
105
|
+
const desktop = useDesktopSize()
|
|
102
106
|
|
|
103
|
-
return <p style={{ color: theme.foreground }}>{
|
|
107
|
+
return <p style={{ color: theme.foreground }}>{desktop.width} × {desktop.height}</p>
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
function App() {
|
|
107
|
-
return <HostProvider provide={["theme", "
|
|
111
|
+
return <HostProvider provide={["theme", "desktopSize"]} fallback={<p>Loading…</p>}>
|
|
108
112
|
<Content />
|
|
109
113
|
</HostProvider>
|
|
110
114
|
}
|
package/dist/host-provider.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import { type
|
|
3
|
-
declare const provisionNames: readonly ["theme", "
|
|
2
|
+
import { type DesktopSize, type PointerPosition, type ThemeProperties } from "@phreshos/client";
|
|
3
|
+
declare const provisionNames: readonly ["theme", "desktopSize", "pointerPosition"];
|
|
4
4
|
/** Requests and follows only the host values explicitly selected by the program. */
|
|
5
5
|
export default function HostProvider(properties: HostProviderProperties): import("react").FunctionComponentElement<SelectedHostProviderProperties>;
|
|
6
6
|
/** Returns the selected Theme snapshot and follows future change events. */
|
|
7
7
|
export declare function useHostTheme(): Readonly<ThemeProperties>;
|
|
8
|
-
/** Returns the selected
|
|
9
|
-
export declare function
|
|
8
|
+
/** Returns the selected desktop size and follows future resize events. */
|
|
9
|
+
export declare function useDesktopSize(): DesktopSize;
|
|
10
10
|
/** Returns the selected Pointer position and follows future move events. */
|
|
11
11
|
export declare function usePointerPosition(): PointerPosition | null;
|
|
12
12
|
/** Values that HostProvider can request and follow. */
|
package/dist/host-provider.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createContext, createElement, useContext, useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
2
2
|
import { host } from "@phreshos/client";
|
|
3
3
|
import LiveSnapshot from "./live-snapshot.js";
|
|
4
|
-
const provisionNames = ["theme", "
|
|
4
|
+
const provisionNames = ["theme", "desktopSize", "pointerPosition"];
|
|
5
5
|
const HostContext = createContext(null);
|
|
6
6
|
/** Requests and follows only the host values explicitly selected by the program. */
|
|
7
7
|
export default function HostProvider(properties) {
|
|
@@ -41,9 +41,9 @@ function SelectedHostProvider({ children, fallback, selection }) {
|
|
|
41
41
|
export function useHostTheme() {
|
|
42
42
|
return useProvided("theme");
|
|
43
43
|
}
|
|
44
|
-
/** Returns the selected
|
|
45
|
-
export function
|
|
46
|
-
return useProvided("
|
|
44
|
+
/** Returns the selected desktop size and follows future resize events. */
|
|
45
|
+
export function useDesktopSize() {
|
|
46
|
+
return useProvided("desktopSize");
|
|
47
47
|
}
|
|
48
48
|
/** Returns the selected Pointer position and follows future move events. */
|
|
49
49
|
export function usePointerPosition() {
|
|
@@ -65,8 +65,8 @@ function createStores(selection) {
|
|
|
65
65
|
case "theme":
|
|
66
66
|
stores.theme = new LiveSnapshot(() => host.theme.snapshot(), subscriber => host.theme.subscribe("change", subscriber));
|
|
67
67
|
break;
|
|
68
|
-
case "
|
|
69
|
-
stores.
|
|
68
|
+
case "desktopSize":
|
|
69
|
+
stores.desktopSize = new LiveSnapshot(() => host.desktop.size(), subscriber => host.desktop.subscribe("resize", subscriber));
|
|
70
70
|
break;
|
|
71
71
|
case "pointerPosition":
|
|
72
72
|
stores.pointerPosition = new LiveSnapshot(() => host.pointer.position(), subscriber => host.pointer.subscribe("move", subscriber));
|
|
@@ -90,6 +90,6 @@ function normalizeProvision(provide) {
|
|
|
90
90
|
}
|
|
91
91
|
const hookNames = {
|
|
92
92
|
theme: "useHostTheme",
|
|
93
|
-
|
|
93
|
+
desktopSize: "useDesktopSize",
|
|
94
94
|
pointerPosition: "usePointerPosition"
|
|
95
95
|
};
|
package/dist/main.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { default as CurrentProvider, useParent, useProcess, useProgram, type CurrentProvision, type CurrentProvisionName, type CurrentProviderProperties } from "./current-provider.js";
|
|
2
|
-
export { default as HostProvider, usePointerPosition,
|
|
2
|
+
export { default as HostProvider, usePointerPosition, useDesktopSize, useHostTheme, type HostProvision, type HostProvisionName, type HostProviderProperties } from "./host-provider.js";
|
|
3
3
|
export { default as useSubscribe } from "./use-subscribe.js";
|
|
4
4
|
export { default as useObserve } from "./use-observe.js";
|
|
5
5
|
export { default as useScale } from "./use-scale.js";
|
|
6
6
|
export { default as useColor } from "./use-color.js";
|
|
7
7
|
export { default as useProgramState, type ProgramState } from "./use-program-state.js";
|
|
8
8
|
export { default as useProcessState, type ProcessState } from "./use-process-state.js";
|
|
9
|
+
export { default as useServiceState, type ServiceState } from "./use-service-state.js";
|
|
9
10
|
export { default as useWindowState } from "./use-window-state.js";
|
|
10
11
|
export { default as useObserveAsks, type AskObservable } from "./use-observe-asks.js";
|
|
11
12
|
export { default as useObserveAnswers, type AnswerObservable } from "./use-observe-answers.js";
|
package/dist/main.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { default as CurrentProvider, useParent, useProcess, useProgram } from "./current-provider.js";
|
|
2
|
-
export { default as HostProvider, usePointerPosition,
|
|
2
|
+
export { default as HostProvider, usePointerPosition, useDesktopSize, useHostTheme } from "./host-provider.js";
|
|
3
3
|
export { default as useSubscribe } from "./use-subscribe.js";
|
|
4
4
|
export { default as useObserve } from "./use-observe.js";
|
|
5
5
|
export { default as useScale } from "./use-scale.js";
|
|
6
6
|
export { default as useColor } from "./use-color.js";
|
|
7
7
|
export { default as useProgramState } from "./use-program-state.js";
|
|
8
8
|
export { default as useProcessState } from "./use-process-state.js";
|
|
9
|
+
export { default as useServiceState } from "./use-service-state.js";
|
|
9
10
|
export { default as useWindowState } from "./use-window-state.js";
|
|
10
11
|
export { default as useObserveAsks } from "./use-observe-asks.js";
|
|
11
12
|
export { default as useObserveAnswers } from "./use-observe-answers.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ServiceHandler } from "@phreshos/client";
|
|
2
|
+
/** Live availability of one exact service identity. */
|
|
3
|
+
export type ServiceState = Readonly<{
|
|
4
|
+
disabled: boolean;
|
|
5
|
+
}>;
|
|
6
|
+
/** Explicitly reads and follows one service while this hook is mounted. */
|
|
7
|
+
export default function useServiceState(service: ServiceHandler): ServiceState | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useMemo, useSyncExternalStore } from "react";
|
|
2
|
+
import LiveState, { combineCleanups } from "./live-state.js";
|
|
3
|
+
/** Explicitly reads and follows one service while this hook is mounted. */
|
|
4
|
+
export default function useServiceState(service) {
|
|
5
|
+
const state = useMemo(() => new LiveState(async () => ({ disabled: await service.disabled() }), reduce => combineCleanups(service.subscribe("enable", () => reduce(current => setDisabled(current, false))), service.subscribe("disable", () => reduce(current => setDisabled(current, true))))), [service]);
|
|
6
|
+
return useSyncExternalStore(state.subscribe, state.snapshot, state.snapshot);
|
|
7
|
+
}
|
|
8
|
+
function setDisabled(state, disabled) {
|
|
9
|
+
return state.disabled === disabled ? state : { disabled };
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phreshos/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "React adapters for the Client SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"prepack": "node --run test && node --run build"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@phreshos/client": "^0.1.
|
|
51
|
-
"@phreshos/core": "^0.1.
|
|
50
|
+
"@phreshos/client": "^0.1.7",
|
|
51
|
+
"@phreshos/core": "^0.1.6",
|
|
52
52
|
"react": "^19.2.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@phreshos/client": "^0.1.
|
|
56
|
-
"@phreshos/core": "^0.1.
|
|
55
|
+
"@phreshos/client": "^0.1.7",
|
|
56
|
+
"@phreshos/core": "^0.1.6",
|
|
57
57
|
"@testing-library/react": "^16.3.0",
|
|
58
58
|
"@types/react": "^19.2.18",
|
|
59
59
|
"@types/react-dom": "^19.2.4",
|