@methodacting/actor-kit 0.47.1 → 0.47.2
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/dist/browser.d.ts +8 -99
- package/dist/index.d.ts +14 -14
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +11 -101
- package/dist/react.js.map +1 -1
- package/dist/src/createActorFetch.d.ts +3 -3
- package/dist/src/createActorFetch.d.ts.map +1 -1
- package/dist/src/createActorKitContext.d.ts +3 -3
- package/dist/src/createActorKitContext.d.ts.map +1 -1
- package/dist/src/createMachineServer.d.ts +21 -14
- package/dist/src/createMachineServer.d.ts.map +1 -1
- package/dist/src/react.d.ts +1 -0
- package/dist/src/react.d.ts.map +1 -1
- package/dist/src/schemas.d.ts +44 -250
- package/dist/src/schemas.d.ts.map +1 -1
- package/dist/src/types.d.ts +12 -12
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/worker.d.ts +1 -0
- package/dist/src/worker.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/createActorFetch.ts +4 -4
- package/src/createActorKitContext.tsx +2 -3
- package/src/createMachineServer.ts +20 -30
- package/src/react.ts +8 -0
- package/src/schemas.ts +5 -2
- package/src/types.ts +35 -44
- package/src/worker.ts +11 -1
package/dist/react.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { ReactNode } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { AnyStateMachine, SnapshotFrom, EventFromLogic, StateValueFrom } from 'xstate';
|
|
3
3
|
import { DurableObject } from 'cloudflare:workers';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
|
|
@@ -21,7 +21,7 @@ declare const RequestInfoSchema: z.ZodObject<{
|
|
|
21
21
|
passed: z.ZodBoolean;
|
|
22
22
|
}, z.core.$strip>;
|
|
23
23
|
staticResource: z.ZodBoolean;
|
|
24
|
-
detectionIds: z.ZodRecord<z.
|
|
24
|
+
detectionIds: z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
25
25
|
score: z.ZodNumber;
|
|
26
26
|
}, z.core.$strip>;
|
|
27
27
|
}, z.core.$strip>;
|
|
@@ -33,60 +33,12 @@ declare const CallerSchema: z.ZodObject<{
|
|
|
33
33
|
service: "service";
|
|
34
34
|
}>;
|
|
35
35
|
}, z.core.$strip>;
|
|
36
|
-
declare const SystemEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
37
|
-
type: z.ZodLiteral<"INITIALIZE">;
|
|
38
|
-
caller: z.ZodObject<{
|
|
39
|
-
type: z.ZodLiteral<"system">;
|
|
40
|
-
id: z.ZodString;
|
|
41
|
-
}, z.core.$strip>;
|
|
42
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
43
|
-
type: z.ZodLiteral<"CONNECT">;
|
|
44
|
-
caller: z.ZodObject<{
|
|
45
|
-
type: z.ZodLiteral<"system">;
|
|
46
|
-
id: z.ZodString;
|
|
47
|
-
}, z.core.$strip>;
|
|
48
|
-
connectingCaller: z.ZodObject<{
|
|
49
|
-
id: z.ZodString;
|
|
50
|
-
type: z.ZodEnum<{
|
|
51
|
-
client: "client";
|
|
52
|
-
system: "system";
|
|
53
|
-
service: "service";
|
|
54
|
-
}>;
|
|
55
|
-
}, z.core.$strip>;
|
|
56
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
57
|
-
type: z.ZodLiteral<"DISCONNECT">;
|
|
58
|
-
caller: z.ZodObject<{
|
|
59
|
-
type: z.ZodLiteral<"system">;
|
|
60
|
-
id: z.ZodString;
|
|
61
|
-
}, z.core.$strip>;
|
|
62
|
-
disconnectingCaller: z.ZodObject<{
|
|
63
|
-
id: z.ZodString;
|
|
64
|
-
type: z.ZodEnum<{
|
|
65
|
-
client: "client";
|
|
66
|
-
system: "system";
|
|
67
|
-
service: "service";
|
|
68
|
-
}>;
|
|
69
|
-
}, z.core.$strip>;
|
|
70
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
71
|
-
type: z.ZodLiteral<"RESUME">;
|
|
72
|
-
caller: z.ZodObject<{
|
|
73
|
-
type: z.ZodLiteral<"system">;
|
|
74
|
-
id: z.ZodString;
|
|
75
|
-
}, z.core.$strip>;
|
|
76
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
77
|
-
type: z.ZodLiteral<"MIGRATE">;
|
|
78
|
-
caller: z.ZodObject<{
|
|
79
|
-
type: z.ZodLiteral<"system">;
|
|
80
|
-
id: z.ZodString;
|
|
81
|
-
}, z.core.$strip>;
|
|
82
|
-
operations: z.ZodArray<z.ZodAny>;
|
|
83
|
-
}, z.core.$strip>], "type">;
|
|
84
36
|
|
|
85
37
|
type EnvWithDurableObjects = {
|
|
86
38
|
ACTOR_KIT_SECRET: string;
|
|
87
39
|
[key: string]: DurableObjectNamespace<ActorServer<any>> | unknown;
|
|
88
40
|
};
|
|
89
|
-
interface ActorServerMethods<TMachine extends
|
|
41
|
+
interface ActorServerMethods<TMachine extends AnyStateMachine> {
|
|
90
42
|
fetch(request: Request): Promise<Response>;
|
|
91
43
|
spawn(props: {
|
|
92
44
|
actorType: string;
|
|
@@ -105,60 +57,17 @@ interface ActorServerMethods<TMachine extends BaseActorKitStateMachine> {
|
|
|
105
57
|
snapshot: CallerSnapshotFrom<TMachine>;
|
|
106
58
|
}>;
|
|
107
59
|
}
|
|
108
|
-
type ActorServer<TMachine extends
|
|
60
|
+
type ActorServer<TMachine extends AnyStateMachine> = DurableObject & ActorServerMethods<TMachine>;
|
|
109
61
|
type Caller = z.infer<typeof CallerSchema>;
|
|
110
62
|
type RequestInfo = z.infer<typeof RequestInfoSchema>;
|
|
111
63
|
type CallerType = "client" | "system" | "service";
|
|
112
|
-
type
|
|
113
|
-
type: string;
|
|
114
|
-
};
|
|
115
|
-
type BaseActorKitContext<TPublicProps extends {
|
|
116
|
-
[key: string]: unknown;
|
|
117
|
-
}, TPrivateProps extends {
|
|
118
|
-
[key: string]: unknown;
|
|
119
|
-
}> = {
|
|
120
|
-
public: TPublicProps;
|
|
121
|
-
private: Record<string, TPrivateProps>;
|
|
122
|
-
};
|
|
123
|
-
type ActorKitStateMachine<TEvent extends BaseActorKitEvent<EnvWithDurableObjects>, TInput extends {
|
|
124
|
-
id: string;
|
|
125
|
-
caller: Caller;
|
|
126
|
-
storage: DurableObjectStorage;
|
|
127
|
-
}, TContext extends BaseActorKitContext<any, any> & {
|
|
128
|
-
[key: string]: unknown;
|
|
129
|
-
}> = StateMachine<TContext, TEvent & EventObject, any, any, any, any, any, any, any, TInput, any, any, any, any>;
|
|
130
|
-
type BaseActorKitInput<TEnv = EnvWithDurableObjects> = {
|
|
131
|
-
id: string;
|
|
132
|
-
caller: Caller;
|
|
133
|
-
env: TEnv;
|
|
134
|
-
storage: DurableObjectStorage;
|
|
135
|
-
};
|
|
136
|
-
type WithActorKitInput<TInputProps extends {
|
|
137
|
-
[key: string]: unknown;
|
|
138
|
-
}, TEnv extends EnvWithDurableObjects> = TInputProps & BaseActorKitInput<TEnv>;
|
|
139
|
-
type AnyActorKitStateMachine = ActorKitStateMachine<any, any, any>;
|
|
140
|
-
type AnyActorKitEvent = (WithActorKitEvent<AnyEventObject, "client"> | WithActorKitEvent<AnyEventObject, "service"> | ActorKitSystemEvent) & BaseActorKitEvent<EnvWithDurableObjects>;
|
|
141
|
-
type AnyActorKitInput = WithActorKitInput<{
|
|
142
|
-
[key: string]: unknown;
|
|
143
|
-
}, EnvWithDurableObjects> & {
|
|
144
|
-
storage: DurableObjectStorage;
|
|
145
|
-
};
|
|
146
|
-
type AnyActorKitContext = {
|
|
147
|
-
public: {
|
|
148
|
-
[key: string]: unknown;
|
|
149
|
-
};
|
|
150
|
-
private: Record<string, {
|
|
151
|
-
[key: string]: unknown;
|
|
152
|
-
}>;
|
|
153
|
-
};
|
|
154
|
-
type BaseActorKitStateMachine = ActorKitStateMachine<AnyActorKitEvent, AnyActorKitInput, AnyActorKitContext>;
|
|
64
|
+
type AnyActorKitStateMachine = AnyStateMachine;
|
|
155
65
|
interface BaseActorKitEvent<TEnv extends EnvWithDurableObjects> {
|
|
156
66
|
caller: Caller;
|
|
157
67
|
storage: DurableObjectStorage;
|
|
158
68
|
requestInfo?: RequestInfo;
|
|
159
69
|
env: TEnv;
|
|
160
70
|
}
|
|
161
|
-
type ActorKitSystemEvent = z.infer<typeof SystemEventSchema>;
|
|
162
71
|
type WithActorKitEvent<T extends {
|
|
163
72
|
type: string;
|
|
164
73
|
}, C extends CallerType> = T & BaseActorKitEvent<EnvWithDurableObjects> & {
|
|
@@ -181,16 +90,16 @@ type CallerSnapshotFrom<TMachine extends AnyStateMachine> = {
|
|
|
181
90
|
value: infer V;
|
|
182
91
|
} ? V : unknown;
|
|
183
92
|
};
|
|
184
|
-
type ClientEventFrom<T extends
|
|
185
|
-
type ServiceEventFrom<T extends
|
|
186
|
-
interface MatchesProps<TMachine extends
|
|
93
|
+
type ClientEventFrom<T extends AnyStateMachine> = EventFromLogic<T> extends infer TEvent ? TEvent extends WithActorKitEvent<infer E, "client"> ? Omit<E, keyof BaseActorKitEvent<EnvWithDurableObjects>> : Omit<TEvent, keyof BaseActorKitEvent<EnvWithDurableObjects>> : never;
|
|
94
|
+
type ServiceEventFrom<T extends AnyStateMachine> = EventFromLogic<T> extends infer TEvent ? TEvent extends WithActorKitEvent<infer E, "service"> ? Omit<E, keyof BaseActorKitEvent<EnvWithDurableObjects>> : never : never;
|
|
95
|
+
interface MatchesProps<TMachine extends AnyStateMachine> {
|
|
187
96
|
state: StateValueFrom<TMachine>;
|
|
188
97
|
and?: StateValueFrom<TMachine>;
|
|
189
98
|
or?: StateValueFrom<TMachine>;
|
|
190
99
|
not?: boolean;
|
|
191
100
|
initialValueOverride?: boolean;
|
|
192
101
|
}
|
|
193
|
-
type ActorKitClient<TMachine extends
|
|
102
|
+
type ActorKitClient<TMachine extends AnyStateMachine> = {
|
|
194
103
|
connect: () => Promise<void>;
|
|
195
104
|
disconnect: () => void;
|
|
196
105
|
send: (event: ClientEventFrom<TMachine>) => void;
|
|
@@ -210,7 +119,7 @@ type ActorKitClientProps<TMachine extends AnyActorKitStateMachine> = {
|
|
|
210
119
|
onError?: (error: Error) => void;
|
|
211
120
|
};
|
|
212
121
|
|
|
213
|
-
declare function createActorKitContext<TMachine extends
|
|
122
|
+
declare function createActorKitContext<TMachine extends AnyStateMachine>(actorType: string): {
|
|
214
123
|
Provider: React.FC<{
|
|
215
124
|
children: ReactNode;
|
|
216
125
|
} & Omit<ActorKitClientProps<TMachine>, "actorType">>;
|
|
@@ -236,3 +145,4 @@ declare function createActorKitContext<TMachine extends AnyActorKitStateMachine>
|
|
|
236
145
|
};
|
|
237
146
|
|
|
238
147
|
export { createActorKitContext };
|
|
148
|
+
export type { ActorKitClient, BaseActorKitEvent, CallerSnapshotFrom, ClientEventFrom, MatchesProps, ServiceEventFrom };
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sources":["../src/createActorKitClient.ts","../src/createActorKitContext.tsx"],"sourcesContent":["import { applyPatch } from \"fast-json-patch\";\nimport { produce } from \"immer\";\n\nimport {\n ActorKitClient,\n ActorKitEmittedEvent,\n AnyActorKitStateMachine,\n CallerSnapshotFrom,\n ClientEventFrom,\n} from \"./types\";\n\nexport type ActorKitClientProps<TMachine extends AnyActorKitStateMachine> = {\n host: string;\n actorType: string;\n actorId: string;\n checksum: string;\n accessToken: string;\n initialSnapshot: CallerSnapshotFrom<TMachine>;\n onStateChange?: (newState: CallerSnapshotFrom<TMachine>) => void;\n onError?: (error: Error) => void;\n};\n\ntype Listener<T> = (state: T) => void;\n\n/**\n * Creates an Actor Kit client for managing state and communication with the server.\n *\n * @template TMachine - The type of the state machine.\n * @param {ActorKitClientProps<TMachine>} props - Configuration options for the client.\n * @returns {ActorKitClient<TMachine>} An object with methods to interact with the actor.\n */\nexport function createActorKitClient<TMachine extends AnyActorKitStateMachine>(\n props: ActorKitClientProps<TMachine>\n): ActorKitClient<TMachine> {\n let currentSnapshot = props.initialSnapshot;\n let socket: WebSocket | null = null;\n const listeners: Set<Listener<CallerSnapshotFrom<TMachine>>> = new Set();\n let reconnectAttempts = 0;\n const maxReconnectAttempts = 5;\n\n /**\n * Notifies all registered listeners with the current state.\n */\n const notifyListeners = () => {\n listeners.forEach((listener) => listener(currentSnapshot));\n };\n\n /**\n * Establishes a WebSocket connection to the Actor Kit server.\n * @returns {Promise<void>} A promise that resolves when the connection is established.\n */\n const connect = async () => {\n const url = getWebSocketUrl(props);\n\n socket = new WebSocket(url);\n\n socket.addEventListener(\"open\", () => {\n reconnectAttempts = 0;\n });\n\n socket.addEventListener(\"message\", (event: MessageEvent) => {\n try {\n const data = JSON.parse(\n typeof event.data === \"string\"\n ? event.data\n : new TextDecoder().decode(event.data)\n ) as ActorKitEmittedEvent;\n\n currentSnapshot = produce(currentSnapshot, (draft) => {\n applyPatch(draft, data.operations);\n });\n\n props.onStateChange?.(currentSnapshot);\n notifyListeners();\n } catch (error) {\n console.error(`[ActorKitClient] Error processing message:`, error);\n props.onError?.(error as Error);\n }\n });\n\n socket.addEventListener(\"error\", (error: any) => {\n console.error(`[ActorKitClient] WebSocket error:`, error);\n console.error(`[ActorKitClient] Error details:`, {\n message: error.message,\n type: error.type,\n target: error.target,\n eventPhase: error.eventPhase,\n });\n props.onError?.(new Error(`WebSocket error: ${JSON.stringify(error)}`));\n });\n\n // todo, how do we reconnect when a user returns to the tab\n // later after it's disconnected\n\n socket.addEventListener(\"close\", (event) => {\n // Implement reconnection logic\n if (reconnectAttempts < maxReconnectAttempts) {\n reconnectAttempts++;\n const delay = Math.min(1000 * Math.pow(2, reconnectAttempts), 30000);\n setTimeout(connect, delay);\n } else {\n console.error(`[ActorKitClient] Max reconnection attempts reached`);\n }\n });\n\n return new Promise<void>((resolve) => {\n socket!.addEventListener(\"open\", () => resolve());\n });\n };\n\n /**\n * Closes the WebSocket connection to the Actor Kit server.\n */\n const disconnect = () => {\n if (socket) {\n socket.close();\n socket = null;\n }\n };\n\n /**\n * Sends an event to the Actor Kit server.\n * @param {ClientEventFrom<TMachine>} event - The event to send.\n */\n const send = (event: ClientEventFrom<TMachine>) => {\n if (socket && socket.readyState === WebSocket.OPEN) {\n socket.send(JSON.stringify(event));\n } else {\n props.onError?.(\n new Error(\"Cannot send event: WebSocket is not connected\")\n );\n }\n };\n\n /**\n * Retrieves the current state of the actor.\n * @returns {CallerSnapshotFrom<TMachine>} The current state.\n */\n const getState = () => currentSnapshot;\n\n /**\n * Subscribes a listener to state changes.\n * @param {Listener<CallerSnapshotFrom<TMachine>>} listener - The listener function to be called on state changes.\n * @returns {() => void} A function to unsubscribe the listener.\n */\n const subscribe = (listener: Listener<CallerSnapshotFrom<TMachine>>) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n };\n\n /**\n * Waits for a state condition to be met.\n * @param {(state: CallerSnapshotFrom<TMachine>) => boolean} predicateFn - Function that returns true when condition is met\n * @param {number} [timeoutMs=5000] - Maximum time to wait in milliseconds\n * @returns {Promise<void>} Resolves when condition is met, rejects on timeout\n */\n const waitFor = async (\n predicateFn: (state: CallerSnapshotFrom<TMachine>) => boolean,\n timeoutMs: number = 5000\n ): Promise<void> => {\n // Check if condition is already met\n if (predicateFn(currentSnapshot)) {\n return Promise.resolve();\n }\n return new Promise((resolve, reject) => {\n let timeoutId: number | null = null;\n\n // Set up timeout to reject if condition isn't met in time\n if (timeoutMs > 0) {\n timeoutId = setTimeout(() => {\n unsubscribe();\n reject(\n new Error(`Timeout waiting for condition after ${timeoutMs}ms`)\n );\n }, timeoutMs);\n }\n\n // Subscribe to state changes\n const unsubscribe = subscribe((state) => {\n if (predicateFn(state)) {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n unsubscribe();\n resolve();\n }\n });\n });\n };\n\n return {\n connect,\n disconnect,\n send,\n getState,\n subscribe,\n waitFor,\n };\n}\n\nfunction getWebSocketUrl(props: ActorKitClientProps<any>): string {\n const { host, actorId, actorType, accessToken, checksum } = props;\n\n // Determine protocol (ws or wss)\n const protocol =\n /^(localhost|127\\.0\\.0\\.1|192\\.168\\.|10\\.|172\\.(1[6-9]|2[0-9]|3[0-1])\\.)/.test(\n host\n )\n ? \"ws\"\n : \"wss\";\n\n // Construct base URL\n const baseUrl = `${protocol}://${host}/api/${actorType}/${actorId}`;\n\n // Add query parameters\n const params = new URLSearchParams({ accessToken });\n if (checksum) params.append(\"checksum\", checksum);\n\n const finalUrl = `${baseUrl}?${params.toString()}`;\n\n return finalUrl;\n}\n","\"use client\";\n\nimport React, {\n createContext,\n memo,\n ReactNode,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from \"react\";\nimport { matchesState, StateValueFrom } from \"xstate\";\nimport type { ActorKitClientProps } from \"./createActorKitClient\";\nimport { createActorKitClient } from \"./createActorKitClient\";\nimport type {\n ActorKitClient,\n AnyActorKitStateMachine,\n CallerSnapshotFrom,\n ClientEventFrom,\n MatchesProps,\n} from \"./types\";\n\nexport function createActorKitContext<TMachine extends AnyActorKitStateMachine>(\n actorType: string\n) {\n const ActorKitContext = createContext<ActorKitClient<TMachine> | null>(null);\n\n const ProviderFromClient: React.FC<{\n children: ReactNode;\n client: ActorKitClient<TMachine>;\n }> = ({ children, client }) => {\n return (\n <ActorKitContext.Provider value={client}>\n {children}\n </ActorKitContext.Provider>\n );\n };\n\n const Provider: React.FC<\n {\n children: ReactNode;\n } & Omit<ActorKitClientProps<TMachine>, \"actorType\">\n > = memo((props) => {\n const clientRef = useRef(\n createActorKitClient<TMachine>({\n host: props.host,\n actorId: props.actorId,\n accessToken: props.accessToken,\n checksum: props.checksum,\n initialSnapshot: props.initialSnapshot,\n actorType,\n })\n );\n const initializedRef = useRef(false);\n\n useEffect(() => {\n if (!initializedRef.current) {\n initializedRef.current = true;\n clientRef.current.connect().then(() => {});\n }\n }, [initializedRef]);\n\n return (\n <ActorKitContext.Provider value={clientRef.current}>\n {props.children}\n </ActorKitContext.Provider>\n );\n });\n\n function useClient(): ActorKitClient<TMachine> {\n const client = useContext(ActorKitContext);\n if (!client) {\n throw new Error(\n \"useClient must be used within an ActorKitContext.Provider\"\n );\n }\n return client;\n }\n\n const useSelector = <T,>(\n selector: (snapshot: CallerSnapshotFrom<TMachine>) => T\n ) => {\n const client = useClient();\n\n return useSyncExternalStoreWithSelector(\n client.subscribe,\n client.getState,\n client.getState,\n selector,\n defaultCompare\n );\n };\n\n function useSend(): (event: ClientEventFrom<TMachine>) => void {\n const client = useClient();\n return client.send;\n }\n\n function useMatches(stateValue: StateValueFrom<TMachine>): boolean {\n return useSelector((state) => matchesState(stateValue, state.value as any));\n }\n\n const Matches: React.FC<MatchesProps<TMachine> & { children: ReactNode }> & {\n create: (\n state: StateValueFrom<TMachine>,\n options?: {\n and?: StateValueFrom<TMachine>;\n or?: StateValueFrom<TMachine>;\n not?: boolean;\n }\n ) => React.FC<\n Omit<MatchesProps<TMachine>, \"state\" | \"and\" | \"or\" | \"not\"> & {\n children: ReactNode;\n }\n >;\n } = (props) => {\n const active = useMatches(props.state);\n const matchesAnd = props.and ? useMatches(props.and) : true;\n const matchesOr = props.or ? useMatches(props.or) : false;\n const value =\n typeof props.initialValueOverride === \"boolean\"\n ? props.initialValueOverride\n : (active && matchesAnd) || matchesOr;\n const finalValue = props.not ? !value : value;\n return finalValue ? <>{props.children}</> : null;\n };\n\n Matches.create = (state, options = {}) => {\n const Component: React.FC<\n Omit<MatchesProps<TMachine>, \"state\" | \"and\" | \"or\" | \"not\"> & {\n children: ReactNode;\n }\n > = ({ children, initialValueOverride }) => (\n <Matches\n state={state}\n and={options.and}\n or={options.or}\n not={options.not}\n initialValueOverride={initialValueOverride}\n >\n {children}\n </Matches>\n );\n Component.displayName = `MatchesComponent(${state.toString()})`;\n return Component;\n };\n\n return {\n Provider,\n ProviderFromClient,\n useClient,\n useSelector,\n useSend,\n useMatches,\n Matches,\n };\n}\n\nfunction useSyncExternalStoreWithSelector<Snapshot, Selection>(\n subscribe: (onStoreChange: () => void) => () => void,\n getSnapshot: () => Snapshot,\n getServerSnapshot: undefined | null | (() => Snapshot),\n selector: (snapshot: Snapshot) => Selection,\n isEqual?: (a: Selection, b: Selection) => boolean\n): Selection {\n const [getSelection, getServerSelection] = useMemo(() => {\n let hasMemo = false;\n let memoizedSnapshot: Snapshot;\n let memoizedSelection: Selection;\n\n const memoizedSelector = (nextSnapshot: Snapshot) => {\n if (!hasMemo) {\n hasMemo = true;\n memoizedSnapshot = nextSnapshot;\n memoizedSelection = selector(nextSnapshot);\n return memoizedSelection;\n }\n\n if (Object.is(memoizedSnapshot, nextSnapshot)) {\n return memoizedSelection;\n }\n\n const nextSelection = selector(nextSnapshot);\n\n if (isEqual && isEqual(memoizedSelection, nextSelection)) {\n memoizedSnapshot = nextSnapshot;\n return memoizedSelection;\n }\n\n memoizedSnapshot = nextSnapshot;\n memoizedSelection = nextSelection;\n return nextSelection;\n };\n\n const getSnapshotWithSelector = () => memoizedSelector(getSnapshot());\n const getServerSnapshotWithSelector = getServerSnapshot\n ? () => memoizedSelector(getServerSnapshot())\n : undefined;\n\n return [getSnapshotWithSelector, getServerSnapshotWithSelector];\n }, [getSnapshot, getServerSnapshot, selector, isEqual]);\n\n const subscribeWithSelector = useCallback(\n (onStoreChange: () => void) => {\n let previousSelection = getSelection();\n return subscribe(() => {\n const nextSelection = getSelection();\n if (!isEqual || !isEqual(previousSelection, nextSelection)) {\n previousSelection = nextSelection;\n onStoreChange();\n }\n });\n },\n [subscribe, getSelection, isEqual]\n );\n\n return useSyncExternalStore(\n subscribeWithSelector,\n getSelection,\n getServerSelection\n );\n}\n\nfunction defaultCompare<T>(a: T, b: T) {\n return a === b;\n}\n"],"names":["createActorKitClient","props","currentSnapshot","initialSnapshot","socket","listeners","Set","reconnectAttempts","connect","async","url","host","actorId","actorType","accessToken","checksum","baseUrl","test","params","URLSearchParams","append","toString","getWebSocketUrl","WebSocket","addEventListener","event","data","JSON","parse","TextDecoder","decode","produce","draft","applyPatch","operations","onStateChange","forEach","listener","error","console","onError","message","type","target","eventPhase","Error","stringify","delay","Math","min","pow","setTimeout","Promise","resolve","subscribe","add","delete","disconnect","close","send","readyState","OPEN","getState","waitFor","predicateFn","timeoutMs","reject","timeoutId","unsubscribe","state","clearTimeout","createActorKitContext","ActorKitContext","createContext","Provider","memo","clientRef","useRef","initializedRef","useEffect","current","then","React","createElement","value","children","useClient","client","useContext","useSelector","selector","getSnapshot","getServerSnapshot","isEqual","getSelection","getServerSelection","useMemo","memoizedSnapshot","memoizedSelection","hasMemo","memoizedSelector","nextSnapshot","Object","is","nextSelection","undefined","subscribeWithSelector","useCallback","onStoreChange","previousSelection","useSyncExternalStore","useSyncExternalStoreWithSelector","defaultCompare","useMatches","stateValue","matchesState","Matches","active","matchesAnd","and","matchesOr","or","initialValueOverride","not","Fragment","create","options","Component","displayName","ProviderFromClient","useSend","a","b"],"mappings":"sRA+BM,SAAUA,EACdC,GAEA,IAAIC,EAAkBD,EAAME,gBACxBC,EAA2B,KAC/B,MAAMC,EAAyD,IAAIC,IACnE,IAAIC,EAAoB,EACxB,MAaMC,EAAUC,UACd,MAAMC,EAsJV,SAAyBT,GACvB,MAAMU,KAAEA,EAAIC,QAAEA,EAAOC,UAAEA,EAASC,YAAEA,EAAWC,SAAEA,GAAad,EAWtDe,EAAU,GAPd,0EAA0EC,KACxEN,GAEE,KACA,WAG2BA,SAAYE,KAAaD,IAGpDM,EAAS,IAAIC,gBAAgB,CAAEL,gBACjCC,GAAUG,EAAOE,OAAO,WAAYL,GAIxC,MAFiB,GAAGC,KAAWE,EAAOG,YAGxC,CA3KgBC,CAAgBrB,GAqD5B,OAnDAG,EAAS,IAAImB,UAAUb,GAEvBN,EAAOoB,iBAAiB,OAAQ,KAC9BjB,EAAoB,IAGtBH,EAAOoB,iBAAiB,UAAYC,IAClC,IACE,MAAMC,EAAOC,KAAKC,MACM,iBAAfH,EAAMC,KACTD,EAAMC,MACN,IAAIG,aAAcC,OAAOL,EAAMC,OAGrCxB,EAAkB6B,EAAQ7B,EAAkB8B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pBI,QAIN,CAACtD,EAAWwC,EAAcD,IAG5B,OAAOiB,EACLJ,EACAZ,EACAC,EAEJ,CAzIWgB,CACLxB,EAAOjC,UACPiC,EAAOzB,SACPyB,EAAOzB,SACP4B,EACAsB,IASJ,SAASC,EAAWC,GAClB,OAAOzB,EAAapB,GAAU8C,EAAaD,EAAY7C,EAAMe,OAC/D,CAEA,MAAMgC,EAaDnH,IACH,MAAMoH,EAASJ,EAAWhH,EAAMoE,OAC1BiD,GAAarH,EAAMsH,KAAMN,EAAWhH,EAAMsH,KAC1CC,IAAYvH,EAAMwH,IAAKR,EAAWhH,EAAMwH,IACxCrC,EACkC,kBAA/BnF,EAAMyH,qBACTzH,EAAMyH,qBACLL,GAAUC,GAAeE,EAEhC,OADmBvH,EAAM0H,KAAOvC,EAAQA,GACpBF,EAAAC,cAAAD,EAAA0C,SAAA,KAAG3H,EAAMoF,UAAe,MAuB9C,OApBA+B,EAAQS,OAAS,CAACxD,EAAOyD,EAAU,CAAA,KACjC,MAAMC,EAIF,EAAG1C,WAAUqC,0BACfxC,EAAAC,cAACiC,EAAO,CACN/C,MAAOA,EACPkD,IAAKO,EAAQP,IACbE,GAAIK,EAAQL,GACZE,IAAKG,EAAQH,IACbD,qBAAsBA,GAErBrC,GAIL,OADA0C,EAAUC,YAAc,oBAAoB3D,EAAMhD,cAC3C0G,GAGF,CACLrD,WACAuD,mBAvHG,EAAG5C,WAAUE,YAEdL,EAAAC,cAACX,EAAgBE,SAAQ,CAACU,MAAOG,GAC9BF,GAqHLC,YACAG,cACAyC,QA3DF,WAEE,OADe5C,IACD3B,IAChB,EAyDEsD,aACAG,UAEJ,CAmEA,SAASJ,EAAkBmB,EAAMC,GAC/B,OAAOD,IAAMC,CACf"}
|
|
1
|
+
{"version":3,"file":"react.js","sources":["../src/createActorKitClient.ts","../src/createActorKitContext.tsx"],"sourcesContent":["import { applyPatch } from \"fast-json-patch\";\nimport { produce } from \"immer\";\n\nimport {\n ActorKitClient,\n ActorKitEmittedEvent,\n AnyActorKitStateMachine,\n CallerSnapshotFrom,\n ClientEventFrom,\n} from \"./types\";\n\nexport type ActorKitClientProps<TMachine extends AnyActorKitStateMachine> = {\n host: string;\n actorType: string;\n actorId: string;\n checksum: string;\n accessToken: string;\n initialSnapshot: CallerSnapshotFrom<TMachine>;\n onStateChange?: (newState: CallerSnapshotFrom<TMachine>) => void;\n onError?: (error: Error) => void;\n};\n\ntype Listener<T> = (state: T) => void;\n\n/**\n * Creates an Actor Kit client for managing state and communication with the server.\n *\n * @template TMachine - The type of the state machine.\n * @param {ActorKitClientProps<TMachine>} props - Configuration options for the client.\n * @returns {ActorKitClient<TMachine>} An object with methods to interact with the actor.\n */\nexport function createActorKitClient<TMachine extends AnyActorKitStateMachine>(\n props: ActorKitClientProps<TMachine>\n): ActorKitClient<TMachine> {\n let currentSnapshot = props.initialSnapshot;\n let socket: WebSocket | null = null;\n const listeners: Set<Listener<CallerSnapshotFrom<TMachine>>> = new Set();\n let reconnectAttempts = 0;\n const maxReconnectAttempts = 5;\n\n /**\n * Notifies all registered listeners with the current state.\n */\n const notifyListeners = () => {\n listeners.forEach((listener) => listener(currentSnapshot));\n };\n\n /**\n * Establishes a WebSocket connection to the Actor Kit server.\n * @returns {Promise<void>} A promise that resolves when the connection is established.\n */\n const connect = async () => {\n const url = getWebSocketUrl(props);\n\n socket = new WebSocket(url);\n\n socket.addEventListener(\"open\", () => {\n reconnectAttempts = 0;\n });\n\n socket.addEventListener(\"message\", (event: MessageEvent) => {\n try {\n const data = JSON.parse(\n typeof event.data === \"string\"\n ? event.data\n : new TextDecoder().decode(event.data)\n ) as ActorKitEmittedEvent;\n\n currentSnapshot = produce(currentSnapshot, (draft) => {\n applyPatch(draft, data.operations);\n });\n\n props.onStateChange?.(currentSnapshot);\n notifyListeners();\n } catch (error) {\n console.error(`[ActorKitClient] Error processing message:`, error);\n props.onError?.(error as Error);\n }\n });\n\n socket.addEventListener(\"error\", (error: any) => {\n console.error(`[ActorKitClient] WebSocket error:`, error);\n console.error(`[ActorKitClient] Error details:`, {\n message: error.message,\n type: error.type,\n target: error.target,\n eventPhase: error.eventPhase,\n });\n props.onError?.(new Error(`WebSocket error: ${JSON.stringify(error)}`));\n });\n\n // todo, how do we reconnect when a user returns to the tab\n // later after it's disconnected\n\n socket.addEventListener(\"close\", (event) => {\n // Implement reconnection logic\n if (reconnectAttempts < maxReconnectAttempts) {\n reconnectAttempts++;\n const delay = Math.min(1000 * Math.pow(2, reconnectAttempts), 30000);\n setTimeout(connect, delay);\n } else {\n console.error(`[ActorKitClient] Max reconnection attempts reached`);\n }\n });\n\n return new Promise<void>((resolve) => {\n socket!.addEventListener(\"open\", () => resolve());\n });\n };\n\n /**\n * Closes the WebSocket connection to the Actor Kit server.\n */\n const disconnect = () => {\n if (socket) {\n socket.close();\n socket = null;\n }\n };\n\n /**\n * Sends an event to the Actor Kit server.\n * @param {ClientEventFrom<TMachine>} event - The event to send.\n */\n const send = (event: ClientEventFrom<TMachine>) => {\n if (socket && socket.readyState === WebSocket.OPEN) {\n socket.send(JSON.stringify(event));\n } else {\n props.onError?.(\n new Error(\"Cannot send event: WebSocket is not connected\")\n );\n }\n };\n\n /**\n * Retrieves the current state of the actor.\n * @returns {CallerSnapshotFrom<TMachine>} The current state.\n */\n const getState = () => currentSnapshot;\n\n /**\n * Subscribes a listener to state changes.\n * @param {Listener<CallerSnapshotFrom<TMachine>>} listener - The listener function to be called on state changes.\n * @returns {() => void} A function to unsubscribe the listener.\n */\n const subscribe = (listener: Listener<CallerSnapshotFrom<TMachine>>) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n };\n\n /**\n * Waits for a state condition to be met.\n * @param {(state: CallerSnapshotFrom<TMachine>) => boolean} predicateFn - Function that returns true when condition is met\n * @param {number} [timeoutMs=5000] - Maximum time to wait in milliseconds\n * @returns {Promise<void>} Resolves when condition is met, rejects on timeout\n */\n const waitFor = async (\n predicateFn: (state: CallerSnapshotFrom<TMachine>) => boolean,\n timeoutMs: number = 5000\n ): Promise<void> => {\n // Check if condition is already met\n if (predicateFn(currentSnapshot)) {\n return Promise.resolve();\n }\n return new Promise((resolve, reject) => {\n let timeoutId: number | null = null;\n\n // Set up timeout to reject if condition isn't met in time\n if (timeoutMs > 0) {\n timeoutId = setTimeout(() => {\n unsubscribe();\n reject(\n new Error(`Timeout waiting for condition after ${timeoutMs}ms`)\n );\n }, timeoutMs);\n }\n\n // Subscribe to state changes\n const unsubscribe = subscribe((state) => {\n if (predicateFn(state)) {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n unsubscribe();\n resolve();\n }\n });\n });\n };\n\n return {\n connect,\n disconnect,\n send,\n getState,\n subscribe,\n waitFor,\n };\n}\n\nfunction getWebSocketUrl(props: ActorKitClientProps<any>): string {\n const { host, actorId, actorType, accessToken, checksum } = props;\n\n // Determine protocol (ws or wss)\n const protocol =\n /^(localhost|127\\.0\\.0\\.1|192\\.168\\.|10\\.|172\\.(1[6-9]|2[0-9]|3[0-1])\\.)/.test(\n host\n )\n ? \"ws\"\n : \"wss\";\n\n // Construct base URL\n const baseUrl = `${protocol}://${host}/api/${actorType}/${actorId}`;\n\n // Add query parameters\n const params = new URLSearchParams({ accessToken });\n if (checksum) params.append(\"checksum\", checksum);\n\n const finalUrl = `${baseUrl}?${params.toString()}`;\n\n return finalUrl;\n}\n","\"use client\";\n\nimport React, {\n createContext,\n memo,\n ReactNode,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from \"react\";\nimport { AnyStateMachine, matchesState, StateValueFrom } from \"xstate\";\nimport type { ActorKitClientProps } from \"./createActorKitClient\";\nimport { createActorKitClient } from \"./createActorKitClient\";\nimport type {\n ActorKitClient,\n CallerSnapshotFrom,\n ClientEventFrom,\n MatchesProps,\n} from \"./types\";\n\nexport function createActorKitContext<TMachine extends AnyStateMachine>(\n actorType: string\n) {\n const ActorKitContext = createContext<ActorKitClient<TMachine> | null>(null);\n\n const ProviderFromClient: React.FC<{\n children: ReactNode;\n client: ActorKitClient<TMachine>;\n }> = ({ children, client }) => {\n return (\n <ActorKitContext.Provider value={client}>\n {children}\n </ActorKitContext.Provider>\n );\n };\n\n const Provider: React.FC<\n {\n children: ReactNode;\n } & Omit<ActorKitClientProps<TMachine>, \"actorType\">\n > = memo((props) => {\n const clientRef = useRef(\n createActorKitClient<TMachine>({\n host: props.host,\n actorId: props.actorId,\n accessToken: props.accessToken,\n checksum: props.checksum,\n initialSnapshot: props.initialSnapshot,\n actorType,\n })\n );\n const initializedRef = useRef(false);\n\n useEffect(() => {\n if (!initializedRef.current) {\n initializedRef.current = true;\n clientRef.current.connect().then(() => {});\n }\n }, [initializedRef]);\n\n return (\n <ActorKitContext.Provider value={clientRef.current}>\n {props.children}\n </ActorKitContext.Provider>\n );\n });\n\n function useClient(): ActorKitClient<TMachine> {\n const client = useContext(ActorKitContext);\n if (!client) {\n throw new Error(\n \"useClient must be used within an ActorKitContext.Provider\"\n );\n }\n return client;\n }\n\n const useSelector = <T,>(\n selector: (snapshot: CallerSnapshotFrom<TMachine>) => T\n ) => {\n const client = useClient();\n\n return useSyncExternalStoreWithSelector(\n client.subscribe,\n client.getState,\n client.getState,\n selector,\n defaultCompare\n );\n };\n\n function useSend(): (event: ClientEventFrom<TMachine>) => void {\n const client = useClient();\n return client.send;\n }\n\n function useMatches(stateValue: StateValueFrom<TMachine>): boolean {\n return useSelector((state) => matchesState(stateValue, state.value as any));\n }\n\n const Matches: React.FC<MatchesProps<TMachine> & { children: ReactNode }> & {\n create: (\n state: StateValueFrom<TMachine>,\n options?: {\n and?: StateValueFrom<TMachine>;\n or?: StateValueFrom<TMachine>;\n not?: boolean;\n }\n ) => React.FC<\n Omit<MatchesProps<TMachine>, \"state\" | \"and\" | \"or\" | \"not\"> & {\n children: ReactNode;\n }\n >;\n } = (props) => {\n const active = useMatches(props.state);\n const matchesAnd = props.and ? useMatches(props.and) : true;\n const matchesOr = props.or ? useMatches(props.or) : false;\n const value =\n typeof props.initialValueOverride === \"boolean\"\n ? props.initialValueOverride\n : (active && matchesAnd) || matchesOr;\n const finalValue = props.not ? !value : value;\n return finalValue ? <>{props.children}</> : null;\n };\n\n Matches.create = (state, options = {}) => {\n const Component: React.FC<\n Omit<MatchesProps<TMachine>, \"state\" | \"and\" | \"or\" | \"not\"> & {\n children: ReactNode;\n }\n > = ({ children, initialValueOverride }) => (\n <Matches\n state={state}\n and={options.and}\n or={options.or}\n not={options.not}\n initialValueOverride={initialValueOverride}\n >\n {children}\n </Matches>\n );\n Component.displayName = `MatchesComponent(${state.toString()})`;\n return Component;\n };\n\n return {\n Provider,\n ProviderFromClient,\n useClient,\n useSelector,\n useSend,\n useMatches,\n Matches,\n };\n}\n\nfunction useSyncExternalStoreWithSelector<Snapshot, Selection>(\n subscribe: (onStoreChange: () => void) => () => void,\n getSnapshot: () => Snapshot,\n getServerSnapshot: undefined | null | (() => Snapshot),\n selector: (snapshot: Snapshot) => Selection,\n isEqual?: (a: Selection, b: Selection) => boolean\n): Selection {\n const [getSelection, getServerSelection] = useMemo(() => {\n let hasMemo = false;\n let memoizedSnapshot: Snapshot;\n let memoizedSelection: Selection;\n\n const memoizedSelector = (nextSnapshot: Snapshot) => {\n if (!hasMemo) {\n hasMemo = true;\n memoizedSnapshot = nextSnapshot;\n memoizedSelection = selector(nextSnapshot);\n return memoizedSelection;\n }\n\n if (Object.is(memoizedSnapshot, nextSnapshot)) {\n return memoizedSelection;\n }\n\n const nextSelection = selector(nextSnapshot);\n\n if (isEqual && isEqual(memoizedSelection, nextSelection)) {\n memoizedSnapshot = nextSnapshot;\n return memoizedSelection;\n }\n\n memoizedSnapshot = nextSnapshot;\n memoizedSelection = nextSelection;\n return nextSelection;\n };\n\n const getSnapshotWithSelector = () => memoizedSelector(getSnapshot());\n const getServerSnapshotWithSelector = getServerSnapshot\n ? () => memoizedSelector(getServerSnapshot())\n : undefined;\n\n return [getSnapshotWithSelector, getServerSnapshotWithSelector];\n }, [getSnapshot, getServerSnapshot, selector, isEqual]);\n\n const subscribeWithSelector = useCallback(\n (onStoreChange: () => void) => {\n let previousSelection = getSelection();\n return subscribe(() => {\n const nextSelection = getSelection();\n if (!isEqual || !isEqual(previousSelection, nextSelection)) {\n previousSelection = nextSelection;\n onStoreChange();\n }\n });\n },\n [subscribe, getSelection, isEqual]\n );\n\n return useSyncExternalStore(\n subscribeWithSelector,\n getSelection,\n getServerSelection\n );\n}\n\nfunction defaultCompare<T>(a: T, b: T) {\n return a === b;\n}\n"],"names":["createActorKitClient","props","currentSnapshot","initialSnapshot","socket","listeners","Set","reconnectAttempts","connect","async","url","host","actorId","actorType","accessToken","checksum","baseUrl","test","params","URLSearchParams","append","toString","getWebSocketUrl","WebSocket","addEventListener","event","data","JSON","parse","TextDecoder","decode","produce","draft","applyPatch","operations","onStateChange","forEach","listener","error","console","onError","message","type","target","eventPhase","Error","stringify","delay","Math","min","pow","setTimeout","Promise","resolve","subscribe","add","delete","disconnect","close","send","readyState","OPEN","getState","waitFor","predicateFn","timeoutMs","reject","timeoutId","unsubscribe","state","clearTimeout","createActorKitContext","ActorKitContext","createContext","Provider","memo","clientRef","useRef","initializedRef","useEffect","current","then","React","createElement","value","children","useClient","client","useContext","useSelector","selector","getSnapshot","getServerSnapshot","isEqual","getSelection","getServerSelection","useMemo","memoizedSnapshot","memoizedSelection","hasMemo","memoizedSelector","nextSnapshot","Object","is","nextSelection","undefined","subscribeWithSelector","useCallback","onStoreChange","previousSelection","useSyncExternalStore","useSyncExternalStoreWithSelector","defaultCompare","useMatches","stateValue","matchesState","Matches","active","matchesAnd","and","matchesOr","or","initialValueOverride","not","Fragment","create","options","Component","displayName","ProviderFromClient","useSend","a","b"],"mappings":"sRA+BM,SAAUA,EACdC,GAEA,IAAIC,EAAkBD,EAAME,gBACxBC,EAA2B,KAC/B,MAAMC,EAAyD,IAAIC,IACnE,IAAIC,EAAoB,EACxB,MAaMC,EAAUC,UACd,MAAMC,EAsJV,SAAyBT,GACvB,MAAMU,KAAEA,EAA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EAAQP,IACbE,GAAIK,EAAQL,GACZE,IAAKG,EAAQH,IACbD,qBAAsBA,GAErBrC,GAIL,OADA0C,EAAUC,YAAc,oBAAoB3D,EAAMhD,cAC3C0G,GAGF,CACLrD,WACAuD,mBAvHG,EAAG5C,WAAUE,YAEdL,EAAAC,cAACX,EAAgBE,SAAQ,CAACU,MAAOG,GAC9BF,GAqHLC,YACAG,cACAyC,QA3DF,WAEE,OADe5C,IACD3B,IAChB,EAyDEsD,aACAG,UAEJ,CAmEA,SAASJ,EAAkBmB,EAAMC,GAC/B,OAAOD,IAAMC,CACf"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { StateValueFrom } from "xstate";
|
|
2
|
-
import {
|
|
3
|
-
export declare function createActorFetch<TMachine extends
|
|
1
|
+
import { AnyStateMachine, StateValueFrom } from "xstate";
|
|
2
|
+
import { CallerSnapshotFrom, ClientEventFrom } from "./types";
|
|
3
|
+
export declare function createActorFetch<TMachine extends AnyStateMachine>({ actorType, host, }: {
|
|
4
4
|
actorType: string;
|
|
5
5
|
host: string;
|
|
6
6
|
}): (props: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createActorFetch.d.ts","sourceRoot":"","sources":["../../src/createActorFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"createActorFetch.d.ts","sourceRoot":"","sources":["../../src/createActorFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAO9D,wBAAgB,gBAAgB,CAAC,QAAQ,SAAS,eAAe,EAAE,EACjE,SAAS,EACT,IAAI,GACL,EAAE;IACD,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd,IAEG,OAAO;IACL,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,YAAY,CAAC,EAAE,eAAe,CAAC,QAAQ,CAAC,CAAC;IACzC,YAAY,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,EACD,UAAU,WAAW,KACpB,OAAO,CAAC;IACT,QAAQ,EAAE,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IACvC,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CAgEH"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React, { ReactNode } from "react";
|
|
2
|
-
import { StateValueFrom } from "xstate";
|
|
2
|
+
import { AnyStateMachine, StateValueFrom } from "xstate";
|
|
3
3
|
import type { ActorKitClientProps } from "./createActorKitClient";
|
|
4
|
-
import type { ActorKitClient,
|
|
5
|
-
export declare function createActorKitContext<TMachine extends
|
|
4
|
+
import type { ActorKitClient, CallerSnapshotFrom, ClientEventFrom, MatchesProps } from "./types";
|
|
5
|
+
export declare function createActorKitContext<TMachine extends AnyStateMachine>(actorType: string): {
|
|
6
6
|
Provider: React.FC<{
|
|
7
7
|
children: ReactNode;
|
|
8
8
|
} & Omit<ActorKitClientProps<TMachine>, "actorType">>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createActorKitContext.d.ts","sourceRoot":"","sources":["../../src/createActorKitContext.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAGZ,SAAS,EAOV,MAAM,OAAO,CAAC;AACf,OAAO,EAAgB,cAAc,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"createActorKitContext.d.ts","sourceRoot":"","sources":["../../src/createActorKitContext.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,E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|
2
|
+
import { ActorServer, EnvFromMachine, MachineServerOptions } from "./types";
|
|
3
|
+
type EventSchema<T extends {
|
|
4
|
+
type: string;
|
|
5
|
+
} = {
|
|
6
|
+
type: string;
|
|
7
|
+
}> = {
|
|
8
|
+
parse(data: unknown): T;
|
|
9
|
+
};
|
|
10
|
+
type InputSchema<T extends Record<string, unknown> = Record<string, unknown>> = {
|
|
11
|
+
parse(data: unknown): T;
|
|
12
|
+
};
|
|
13
|
+
export declare const createMachineServer: <TClientEvent extends {
|
|
14
|
+
type: string;
|
|
15
|
+
}, TServiceEvent extends {
|
|
16
|
+
type: string;
|
|
17
|
+
}, TMachine extends AnyStateMachine>({ machine, schemas, options, }: {
|
|
12
18
|
machine: TMachine;
|
|
13
19
|
schemas: {
|
|
14
|
-
clientEvent:
|
|
15
|
-
serviceEvent:
|
|
16
|
-
inputProps:
|
|
20
|
+
clientEvent: EventSchema<TClientEvent>;
|
|
21
|
+
serviceEvent: EventSchema<TServiceEvent>;
|
|
22
|
+
inputProps: InputSchema;
|
|
17
23
|
};
|
|
18
24
|
options?: MachineServerOptions;
|
|
19
25
|
}) => new (state: DurableObjectState, env: EnvFromMachine<TMachine>, ctx: ExecutionContext) => ActorServer<TMachine>;
|
|
26
|
+
export {};
|
|
20
27
|
//# sourceMappingURL=createMachineServer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMachineServer.d.ts","sourceRoot":"","sources":["../../src/createMachineServer.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"createMachineServer.d.ts","sourceRoot":"","sources":["../../src/createMachineServer.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,eAAe,EAOhB,MAAM,QAAQ,CAAC;AAKhB,OAAO,EAEL,WAAW,EAIX,cAAc,EACd,oBAAoB,EAErB,MAAM,SAAS,CAAC;AA6BjB,KAAK,WAAW,CAAC,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,IAAI;IAChE,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,CAAC,CAAC;CACzB,CAAC;AAEF,KAAK,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;IAC9E,KAAK,CAAC,IAAI,EAAE,OAAO,GAAG,CAAC,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAC9B,YAAY,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EACrC,aAAa,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EACtC,QAAQ,SAAS,eAAe,EAChC,gCAIC;IACD,OAAO,EAAE,QAAQ,CAAC;IAClB,OAAO,EAAE;QACP,WAAW,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC;QACvC,YAAY,EAAE,WAAW,CAAC,aAAa,CAAC,CAAC;QACzC,UAAU,EAAE,WAAW,CAAC;KACzB,CAAC;IACF,OAAO,CAAC,EAAE,oBAAoB,CAAC;CAChC,KAAG,KACF,KAAK,EAAE,kBAAkB,EACzB,GAAG,EAAE,cAAc,CAAC,QAAQ,CAAC,EAC7B,GAAG,EAAE,gBAAgB,KAClB,WAAW,CAAC,QAAQ,CA6uBtB,CAAC"}
|
package/dist/src/react.d.ts
CHANGED
package/dist/src/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,YAAY,EACV,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,YAAY,EACZ,gBAAgB,GACjB,MAAM,SAAS,CAAC"}
|