@archboard/codex-realtime-voice 0.0.0-stage → 0.1.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.
@@ -0,0 +1,34 @@
1
+ import { a as VoiceClientSnapshot, r as VoiceClient } from "./contract-Cupd2jDS.mjs";
2
+ import { JSX, ReactNode } from "react";
3
+ /** Inputs for the provider. */
4
+ interface VoiceClientProviderProps {
5
+ /** The client from `createVoiceClient`. The host connects and disposes it. */
6
+ readonly client: VoiceClient;
7
+ readonly children: ReactNode;
8
+ }
9
+ /**
10
+ * The client, for `useVoiceClient` and `useVoiceState` below.
11
+ * @param props The client and what is drawn.
12
+ * @returns What is drawn, with the client offered to it.
13
+ */
14
+ declare function VoiceClientProvider(props: VoiceClientProviderProps): JSX.Element;
15
+ /**
16
+ * The voice client of the nearest `VoiceClientProvider`, for its actions.
17
+ * @returns The client.
18
+ * @throws {Error} When no provider is above the component.
19
+ */
20
+ declare function useVoiceClient(): VoiceClient;
21
+ /**
22
+ * The client's whole state, re-rendering on each publication.
23
+ * @returns The state.
24
+ */
25
+ declare function useVoiceState(): VoiceClientSnapshot;
26
+ /**
27
+ * Part of the client's state, re-rendering only when that part changes.
28
+ * @param select Picks part of the state. It runs once per publication. Return
29
+ * a part of the state or a primitive: a new object is a change, so the
30
+ * component then re-renders on every publication.
31
+ * @returns The selected part.
32
+ */
33
+ declare function useVoiceState<Selected>(select: (state: VoiceClientSnapshot) => Selected): Selected;
34
+ export { VoiceClientProvider, useVoiceClient, useVoiceState };
package/dist/react.mjs ADDED
@@ -0,0 +1,49 @@
1
+ import { createContext, useContext, useMemo, useSyncExternalStore } from "react";
2
+ import { jsx } from "react/jsx-runtime";
3
+ // src/ui/voice-client/hooks/use-voice-client.ts
4
+ /** The client a `VoiceClientProvider` offers, or null outside one. */
5
+ const VoiceClientContext = createContext(null);
6
+ /**
7
+ * The voice client of the nearest `VoiceClientProvider`, for its actions.
8
+ * @returns The client.
9
+ * @throws {Error} When no provider is above the component.
10
+ */
11
+ function useVoiceClient() {
12
+ const client = useContext(VoiceClientContext);
13
+ if (client === null) throw new Error("useVoiceClient needs a VoiceClientProvider above it.");
14
+ return client;
15
+ }
16
+ /**
17
+ * The client's state, whole or by a selector.
18
+ * @param select Picks part of the state, when given.
19
+ * @returns The selected part, or the whole state.
20
+ */
21
+ function useVoiceState(select) {
22
+ const client = useVoiceClient();
23
+ const read = useMemo(() => {
24
+ if (select === void 0) return client.snapshot;
25
+ let last = null;
26
+ return () => {
27
+ const state = client.snapshot();
28
+ if (last?.state !== state) last = {
29
+ state,
30
+ selected: select(state)
31
+ };
32
+ return last.selected;
33
+ };
34
+ }, [client, select]);
35
+ return useSyncExternalStore(client.subscribe, read, read);
36
+ }
37
+ // src/ui/voice-client/components/VoiceClientProvider.tsx
38
+ /**
39
+ * The client, for `useVoiceClient` and `useVoiceState` below.
40
+ * @param props The client and what is drawn.
41
+ * @returns What is drawn, with the client offered to it.
42
+ */
43
+ function VoiceClientProvider(props) {
44
+ return /* @__PURE__ */ jsx(VoiceClientContext.Provider, {
45
+ value: props.client,
46
+ children: props.children
47
+ });
48
+ }
49
+ export { VoiceClientProvider, useVoiceClient, useVoiceState };