statewire 0.0.1 → 0.0.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/README.md +14 -13
- package/dist/client/changes.d.ts +2 -2
- package/dist/client/changes.d.ts.map +1 -1
- package/dist/client/changes.js +3 -10
- package/dist/client/changes.js.map +1 -1
- package/dist/client/connection.d.ts +18 -39
- package/dist/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +61 -54
- package/dist/client/connection.js.map +1 -1
- package/dist/client/core.d.ts +7 -11
- package/dist/client/core.d.ts.map +1 -1
- package/dist/client/core.js +58 -55
- package/dist/client/core.js.map +1 -1
- package/dist/client/session.d.ts +8 -25
- package/dist/client/session.d.ts.map +1 -1
- package/dist/client/session.js +56 -36
- package/dist/client/session.js.map +1 -1
- package/dist/client/sessionStore.d.ts +1 -1
- package/dist/client/sessionStore.js.map +1 -1
- package/dist/client/timing.d.ts +2 -1
- package/dist/client/timing.d.ts.map +1 -1
- package/dist/client/timing.js +5 -1
- package/dist/client/timing.js.map +1 -1
- package/dist/host/adapter.d.ts +8 -0
- package/dist/host/adapter.d.ts.map +1 -0
- package/dist/host/adapter.js +23 -0
- package/dist/host/adapter.js.map +1 -0
- package/dist/host/draft.d.ts +21 -7
- package/dist/host/draft.d.ts.map +1 -1
- package/dist/host/draft.js +190 -103
- package/dist/host/draft.js.map +1 -1
- package/dist/host/host.d.ts +18 -35
- package/dist/host/host.d.ts.map +1 -1
- package/dist/host/host.js +81 -53
- package/dist/host/host.js.map +1 -1
- package/dist/host/socket.d.ts +14 -0
- package/dist/host/socket.d.ts.map +1 -0
- package/dist/host/socket.js +459 -0
- package/dist/host/socket.js.map +1 -0
- package/dist/host-internal.d.ts +27 -0
- package/dist/host-internal.d.ts.map +1 -0
- package/dist/host-internal.js +3 -0
- package/dist/host.d.ts +44 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +5 -28
- package/dist/index.js +2 -3
- package/dist/statewire.d.ts +94 -10
- package/dist/statewire.d.ts.map +1 -1
- package/dist/statewire.js +23 -35
- package/dist/statewire.js.map +1 -1
- package/dist/transports/fake-sse.d.ts +12 -0
- package/dist/transports/fake-sse.d.ts.map +1 -0
- package/dist/transports/fake-sse.js +39 -0
- package/dist/transports/fake-sse.js.map +1 -0
- package/dist/transports/fixtures.d.ts +33 -0
- package/dist/transports/fixtures.d.ts.map +1 -0
- package/dist/transports/fixtures.js +50 -0
- package/dist/transports/fixtures.js.map +1 -0
- package/dist/transports/remote.d.ts +6 -13
- package/dist/transports/remote.d.ts.map +1 -1
- package/dist/transports/remote.js +5 -24
- package/dist/transports/remote.js.map +1 -1
- package/dist/transports/sse.d.ts +10 -20
- package/dist/transports/sse.d.ts.map +1 -1
- package/dist/transports/sse.js +42 -41
- package/dist/transports/sse.js.map +1 -1
- package/dist/transports/testing.d.ts +4 -3
- package/dist/transports/testing.d.ts.map +1 -1
- package/dist/transports/testing.js +19 -46
- package/dist/transports/testing.js.map +1 -1
- package/dist/transports/ws.d.ts +4 -13
- package/dist/transports/ws.d.ts.map +1 -1
- package/dist/transports/ws.js +57 -60
- package/dist/transports/ws.js.map +1 -1
- package/dist/types.d.ts +53 -100
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/util/draftTraps.d.ts +6 -0
- package/dist/util/draftTraps.d.ts.map +1 -0
- package/dist/util/draftTraps.js +25 -0
- package/dist/util/draftTraps.js.map +1 -0
- package/dist/util/macrotask.d.ts +5 -0
- package/dist/util/macrotask.d.ts.map +1 -0
- package/dist/util/macrotask.js +25 -0
- package/dist/util/macrotask.js.map +1 -0
- package/dist/util/methodProxy.d.ts +5 -0
- package/dist/util/methodProxy.d.ts.map +1 -0
- package/dist/util/methodProxy.js +17 -0
- package/dist/util/methodProxy.js.map +1 -0
- package/dist/util/randomId.d.ts +5 -0
- package/dist/util/randomId.d.ts.map +1 -0
- package/dist/util/randomId.js +6 -0
- package/dist/util/randomId.js.map +1 -0
- package/dist/{client/optimistic.d.ts → util/recordMutations.d.ts} +2 -2
- package/dist/util/recordMutations.d.ts.map +1 -0
- package/dist/{client/optimistic.js → util/recordMutations.js} +5 -24
- package/dist/util/recordMutations.js.map +1 -0
- package/dist/util/statePath.d.ts +14 -0
- package/dist/util/statePath.d.ts.map +1 -0
- package/dist/util/statePath.js +31 -0
- package/dist/util/statePath.js.map +1 -0
- package/dist/wire/meta.d.ts +6 -1
- package/dist/wire/meta.d.ts.map +1 -1
- package/dist/wire/meta.js +30 -28
- package/dist/wire/meta.js.map +1 -1
- package/dist/wire/ops.d.ts +5 -26
- package/dist/wire/ops.d.ts.map +1 -1
- package/dist/wire/ops.js +67 -80
- package/dist/wire/ops.js.map +1 -1
- package/package.json +15 -11
- package/src/client/changes.ts +5 -15
- package/src/client/connection.ts +87 -113
- package/src/client/core.ts +57 -91
- package/src/client/session.ts +87 -70
- package/src/client/sessionStore.ts +1 -1
- package/src/client/timing.ts +6 -0
- package/src/host/adapter.ts +33 -0
- package/src/host/draft.ts +293 -125
- package/src/host/host.ts +107 -106
- package/src/host/socket.ts +542 -0
- package/src/host-internal.ts +28 -0
- package/src/host.ts +61 -0
- package/src/index.ts +2 -62
- package/src/statewire.ts +154 -61
- package/src/transports/fake-sse.ts +51 -0
- package/src/transports/fixtures.ts +60 -0
- package/src/transports/remote.ts +12 -54
- package/src/transports/sse.ts +68 -81
- package/src/transports/testing.ts +27 -59
- package/src/transports/ws.ts +76 -92
- package/src/types.ts +50 -131
- package/src/util/draftTraps.ts +32 -0
- package/src/util/macrotask.ts +25 -0
- package/src/util/methodProxy.ts +16 -0
- package/src/util/randomId.ts +4 -0
- package/src/{client/optimistic.ts → util/recordMutations.ts} +0 -0
- package/src/util/statePath.ts +61 -0
- package/src/wire/meta.ts +42 -47
- package/src/wire/ops.ts +107 -106
- package/LICENSE +0 -21
- package/dist/client/optimistic.d.ts.map +0 -1
- package/dist/client/optimistic.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/server.d.ts +0 -15
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -3
- package/src/server.ts +0 -32
package/src/statewire.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
2
|
import {
|
|
3
3
|
createTapRoot,
|
|
4
4
|
flushTapSync,
|
|
@@ -6,102 +6,195 @@ import {
|
|
|
6
6
|
useResource,
|
|
7
7
|
} from "@assistant-ui/tap";
|
|
8
8
|
import type { ResourceElement } from "@assistant-ui/tap";
|
|
9
|
-
import {
|
|
10
|
-
import type {
|
|
11
|
-
import type {
|
|
12
|
-
|
|
13
|
-
StatewireCommandsProxy,
|
|
14
|
-
StatewireHelpers,
|
|
15
|
-
StatewireOptions,
|
|
16
|
-
} from "./types";
|
|
17
|
-
import type { StatewireConnection } from "./transports/sse";
|
|
9
|
+
import { LIVE_CONNECTION, useStatewireCore } from "./client/core";
|
|
10
|
+
import type { Statewire } from "./types";
|
|
11
|
+
import type { ConnectionState } from "./transports/sse";
|
|
12
|
+
import type { StatewireSessionStoreOptions } from "./client/sessionStore";
|
|
18
13
|
|
|
19
14
|
export const useStatewire = <
|
|
20
15
|
TState,
|
|
21
|
-
TCommands extends
|
|
16
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
22
17
|
TEvent = any,
|
|
23
18
|
>(
|
|
24
|
-
options:
|
|
25
|
-
):
|
|
19
|
+
options: StatewireClient.Options<TState, TCommands, TEvent>,
|
|
20
|
+
): StatewireClient.Helpers<TState, TCommands> => {
|
|
26
21
|
const latest = useRef(options);
|
|
27
22
|
latest.current = options;
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
23
|
+
|
|
24
|
+
const value = useResource(options.transport) as Statewire<unknown>;
|
|
25
|
+
const core = useStatewireCore(() => latest.current, value);
|
|
26
|
+
const [subscribe] = useState(
|
|
27
|
+
() => (listener: () => void) =>
|
|
28
|
+
core.subscribe(() => flushTapSync(listener)),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const state = useSyncExternalStore(
|
|
32
|
+
subscribe,
|
|
33
|
+
core.getState,
|
|
34
|
+
core.getState,
|
|
35
|
+
) as TState;
|
|
36
|
+
const connection = value.connection ?? LIVE_CONNECTION;
|
|
37
|
+
|
|
38
|
+
return useMemo(
|
|
39
|
+
() => ({
|
|
40
|
+
state,
|
|
41
|
+
getState: core.getState as () => TState,
|
|
42
|
+
connection,
|
|
43
|
+
commands: core.commands as Statewire.CommandsProxy<TCommands>,
|
|
44
|
+
}),
|
|
45
|
+
[state, connection, core],
|
|
46
|
+
);
|
|
52
47
|
};
|
|
53
48
|
|
|
54
49
|
export const StatewireResource = <
|
|
55
50
|
TState,
|
|
56
|
-
TCommands extends
|
|
51
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
57
52
|
TEvent = any,
|
|
58
53
|
>(
|
|
59
|
-
options:
|
|
60
|
-
): ResourceElement<
|
|
54
|
+
options: StatewireClient.Options<TState, TCommands, TEvent>,
|
|
55
|
+
): ResourceElement<StatewireClient.Helpers<TState, TCommands>> =>
|
|
61
56
|
resource(useStatewire<TState, TCommands, TEvent>)(options);
|
|
62
57
|
|
|
63
|
-
export class
|
|
58
|
+
export class StatewireClient<
|
|
64
59
|
TState,
|
|
65
|
-
TCommands extends
|
|
60
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
66
61
|
TEvent = any,
|
|
67
62
|
> {
|
|
68
|
-
private readonly core;
|
|
69
63
|
private readonly root;
|
|
70
64
|
private disposed = false;
|
|
71
65
|
|
|
72
|
-
constructor(options:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.root = createTapRoot(() => {
|
|
76
|
-
const value = useResource(
|
|
77
|
-
options.transport,
|
|
78
|
-
) as StatewireTransport<unknown>;
|
|
79
|
-
core.attach(value);
|
|
80
|
-
return value;
|
|
81
|
-
});
|
|
82
|
-
flushTapSync(() => {});
|
|
66
|
+
constructor(options: StatewireClient.Options<TState, TCommands, TEvent>) {
|
|
67
|
+
const ClientRoot = () => useStatewire<TState, TCommands, TEvent>(options);
|
|
68
|
+
this.root = createTapRoot(ClientRoot);
|
|
83
69
|
}
|
|
84
70
|
|
|
85
71
|
get state(): TState {
|
|
86
|
-
return this.
|
|
72
|
+
return this.root.getValue().getState();
|
|
87
73
|
}
|
|
88
74
|
|
|
89
|
-
get connection():
|
|
90
|
-
return this.root.getValue().connection
|
|
75
|
+
get connection(): StatewireClient.Connection {
|
|
76
|
+
return this.root.getValue().connection;
|
|
91
77
|
}
|
|
92
78
|
|
|
93
|
-
get commands():
|
|
94
|
-
return this.
|
|
79
|
+
get commands(): Statewire.CommandsProxy<TCommands> {
|
|
80
|
+
return this.root.getValue().commands;
|
|
95
81
|
}
|
|
96
82
|
|
|
97
83
|
subscribe(listener: () => void): () => void {
|
|
98
|
-
return this.
|
|
84
|
+
return this.root.subscribe(listener);
|
|
99
85
|
}
|
|
100
86
|
|
|
101
87
|
dispose(): void {
|
|
102
88
|
if (this.disposed) return;
|
|
103
89
|
this.disposed = true;
|
|
104
90
|
this.root.unmount();
|
|
105
|
-
this.core.dispose();
|
|
106
91
|
}
|
|
107
92
|
}
|
|
93
|
+
|
|
94
|
+
export namespace StatewireClient {
|
|
95
|
+
export type Options<
|
|
96
|
+
TState,
|
|
97
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
98
|
+
TEvent = any,
|
|
99
|
+
> = {
|
|
100
|
+
transport: ResourceElement<Statewire<TState, TCommands, TEvent>>;
|
|
101
|
+
optimistic?: Optimistic<NonNullable<TState>, TCommands>;
|
|
102
|
+
onEvent?: (event: TEvent, ctx: { state: NonNullable<TState> }) => void;
|
|
103
|
+
onStateChange?: (
|
|
104
|
+
state: NonNullable<TState>,
|
|
105
|
+
ctx: { changes: Statewire.Changes },
|
|
106
|
+
) => void;
|
|
107
|
+
onCommandChange?: (
|
|
108
|
+
update: Statewire.CommandUpdate<TCommands>,
|
|
109
|
+
ctx: { state: NonNullable<TState> },
|
|
110
|
+
) => void;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type Helpers<
|
|
114
|
+
TState,
|
|
115
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
116
|
+
> = {
|
|
117
|
+
/** The statewire's state type — remote statewires include `undefined` until the first attach snapshot applies. */
|
|
118
|
+
state: TState;
|
|
119
|
+
/** Identity-stable across renders; reads the current canonical state without subscribing the caller. */
|
|
120
|
+
getState: () => TState;
|
|
121
|
+
connection: Connection;
|
|
122
|
+
commands: Statewire.CommandsProxy<TCommands>;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export type Connection = ConnectionState & { reconnect: () => void };
|
|
126
|
+
export type ConnectionStatus = Connection["status"];
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Mutate the draft in place; the client records the next state plus the exact
|
|
130
|
+
* changed paths. Re-run against the new base on every rebase.
|
|
131
|
+
*/
|
|
132
|
+
export type Optimistic<
|
|
133
|
+
TState,
|
|
134
|
+
TCommands extends Statewire.Commands = Statewire.Commands,
|
|
135
|
+
> = (
|
|
136
|
+
draft: TState,
|
|
137
|
+
command: Statewire.Command<TCommands>,
|
|
138
|
+
ctx: OptimisticContext,
|
|
139
|
+
) => void;
|
|
140
|
+
|
|
141
|
+
export type OptimisticContext = {
|
|
142
|
+
ts: number;
|
|
143
|
+
seq: number;
|
|
144
|
+
clientId?: string;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* fate: "aborted" = never sent, "rejected" = the handler refused,
|
|
149
|
+
* "unknown-command" = no such command, "invalid-params" = the args did not
|
|
150
|
+
* match the command's schema, "lost" = sent but never covered by an ack,
|
|
151
|
+
* "crashed" = acked but the processing died, "result-unavailable" = acked
|
|
152
|
+
* but the result is gone.
|
|
153
|
+
* cause is which channel settled it: "server" = the server's answer;
|
|
154
|
+
* the rest are channel deaths the client settled locally.
|
|
155
|
+
*/
|
|
156
|
+
export type SendFailure =
|
|
157
|
+
| { fate: "rejected"; cause: "server"; message: string; payload?: unknown }
|
|
158
|
+
| { fate: "unknown-command"; cause: "server" }
|
|
159
|
+
| { fate: "invalid-params"; cause: "server"; message: string }
|
|
160
|
+
| { fate: "crashed"; cause: "server" }
|
|
161
|
+
| {
|
|
162
|
+
fate: "result-unavailable";
|
|
163
|
+
cause:
|
|
164
|
+
| "server"
|
|
165
|
+
| "unmount"
|
|
166
|
+
| "stream-gone"
|
|
167
|
+
| "session-lost"
|
|
168
|
+
| "superseded";
|
|
169
|
+
}
|
|
170
|
+
| {
|
|
171
|
+
fate: "aborted";
|
|
172
|
+
cause:
|
|
173
|
+
| "cascade"
|
|
174
|
+
| "unmount"
|
|
175
|
+
| "stream-gone"
|
|
176
|
+
| "session-lost"
|
|
177
|
+
| "superseded";
|
|
178
|
+
}
|
|
179
|
+
| {
|
|
180
|
+
fate: "lost";
|
|
181
|
+
cause: "unmount" | "stream-gone" | "session-lost" | "superseded";
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export type SSEOptions = StatewireSessionStoreOptions & {
|
|
185
|
+
url: string;
|
|
186
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
187
|
+
fetch?: typeof globalThis.fetch;
|
|
188
|
+
onChange?: (connection: Connection) => void;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export type WSOptions = StatewireSessionStoreOptions & {
|
|
192
|
+
url: string;
|
|
193
|
+
protocols?: string | string[];
|
|
194
|
+
query?:
|
|
195
|
+
| Record<string, string>
|
|
196
|
+
| (() => Record<string, string> | Promise<Record<string, string>>);
|
|
197
|
+
webSocket?: new (url: string, protocols?: string | string[]) => WebSocket;
|
|
198
|
+
onChange?: (connection: Connection) => void;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { StatewireEnvelope } from "../client/connection";
|
|
2
|
+
|
|
3
|
+
export type FakeStreamHandle = {
|
|
4
|
+
clientId: string;
|
|
5
|
+
send: (text: string) => void;
|
|
6
|
+
close: () => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const sseFrame = (envelope: StatewireEnvelope) =>
|
|
10
|
+
`data: ${JSON.stringify(envelope)}\n\n`;
|
|
11
|
+
|
|
12
|
+
export const openSSEStream = (
|
|
13
|
+
streams: Set<FakeStreamHandle>,
|
|
14
|
+
clientId: string,
|
|
15
|
+
onOpen: (handle: FakeStreamHandle) => void,
|
|
16
|
+
signal?: AbortSignal,
|
|
17
|
+
): Response => {
|
|
18
|
+
const encoder = new TextEncoder();
|
|
19
|
+
let handle: FakeStreamHandle;
|
|
20
|
+
const body = new ReadableStream<Uint8Array>({
|
|
21
|
+
start: (controller) => {
|
|
22
|
+
let open = true;
|
|
23
|
+
handle = {
|
|
24
|
+
clientId,
|
|
25
|
+
send: (text) => {
|
|
26
|
+
if (open) controller.enqueue(encoder.encode(text));
|
|
27
|
+
},
|
|
28
|
+
close: () => {
|
|
29
|
+
if (!open) return;
|
|
30
|
+
open = false;
|
|
31
|
+
streams.delete(handle);
|
|
32
|
+
controller.close();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
streams.add(handle);
|
|
36
|
+
onOpen(handle);
|
|
37
|
+
},
|
|
38
|
+
cancel: () => {
|
|
39
|
+
streams.delete(handle);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
signal?.addEventListener("abort", () => {
|
|
43
|
+
for (const stream of [...streams]) {
|
|
44
|
+
if (stream.clientId === clientId) stream.close();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return new Response(body, {
|
|
48
|
+
status: 200,
|
|
49
|
+
headers: { "content-type": "text/event-stream" },
|
|
50
|
+
});
|
|
51
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { resource } from "@assistant-ui/tap";
|
|
2
|
+
import {
|
|
3
|
+
useStatewireCommands,
|
|
4
|
+
useStatewireEmit,
|
|
5
|
+
useStatewireState,
|
|
6
|
+
} from "../host";
|
|
7
|
+
import { tick } from "./testing";
|
|
8
|
+
|
|
9
|
+
/** Let several rounds of queued microtasks and op batches drain. */
|
|
10
|
+
export const settle = async () => {
|
|
11
|
+
for (let i = 0; i < 3; i++) await tick();
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** A minimal statewire host element: `{ count }` state with an `increment` command. */
|
|
15
|
+
export const createCounterElement = () =>
|
|
16
|
+
resource(() => {
|
|
17
|
+
const [state] = useStatewireState(() => ({ count: 0 }));
|
|
18
|
+
const emit = useStatewireEmit();
|
|
19
|
+
const commands = useStatewireCommands({
|
|
20
|
+
increment: () => {
|
|
21
|
+
state.count += 1;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
return { state, commands, emit };
|
|
25
|
+
})();
|
|
26
|
+
|
|
27
|
+
/** Serialize a command frame as a client would send it over a socket. */
|
|
28
|
+
export const commandFrame = (
|
|
29
|
+
method: string,
|
|
30
|
+
seq: number,
|
|
31
|
+
params: unknown[] = [],
|
|
32
|
+
) => JSON.stringify({ method, params, seq });
|
|
33
|
+
|
|
34
|
+
export type SocketRecorder = {
|
|
35
|
+
sent: string[];
|
|
36
|
+
closes: { code?: number; reason?: string }[];
|
|
37
|
+
last: () => any;
|
|
38
|
+
send: (data: string) => void;
|
|
39
|
+
close: (code?: number, reason?: string) => void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Record sent frames and close calls; `last()` parses the latest frame. */
|
|
43
|
+
export const createSocketRecorder = (): SocketRecorder => {
|
|
44
|
+
const sent: string[] = [];
|
|
45
|
+
const closes: { code?: number; reason?: string }[] = [];
|
|
46
|
+
return {
|
|
47
|
+
sent,
|
|
48
|
+
closes,
|
|
49
|
+
last: () => JSON.parse(sent.at(-1)!),
|
|
50
|
+
send: (data) => {
|
|
51
|
+
sent.push(data);
|
|
52
|
+
},
|
|
53
|
+
close: (code, reason) => {
|
|
54
|
+
closes.push({
|
|
55
|
+
...(code !== undefined && { code }),
|
|
56
|
+
...(reason !== undefined && { reason }),
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
package/src/transports/remote.ts
CHANGED
|
@@ -1,65 +1,23 @@
|
|
|
1
|
-
import { useEffect
|
|
2
|
-
import { useStatewireConn } from "../client/connection";
|
|
3
|
-
import type { ConnBase } from "../client/connection";
|
|
1
|
+
import { useEffect } from "react";
|
|
4
2
|
import { useStatewireEmit } from "../host/host";
|
|
5
|
-
import type {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
handleSnapshot,
|
|
11
|
-
setTransport,
|
|
12
|
-
start,
|
|
13
|
-
stop,
|
|
14
|
-
} from "../client/session";
|
|
15
|
-
import type { SessionOptions } from "../client/session";
|
|
16
|
-
import type {
|
|
17
|
-
RemoteLink,
|
|
18
|
-
StatewireConnection,
|
|
19
|
-
StatewireTransportProps,
|
|
20
|
-
} from "./sse";
|
|
3
|
+
import type { Statewire } from "../types";
|
|
4
|
+
import { setTransport, useStatewireSession } from "../client/session";
|
|
5
|
+
import type { StatewireSessionStoreOptions } from "../client/sessionStore";
|
|
6
|
+
import type { StatewireClient } from "../statewire";
|
|
7
|
+
import type { RemoteLink, StatewireRemoteProps } from "./sse";
|
|
21
8
|
|
|
22
9
|
export const useStatewireRemote = <
|
|
23
10
|
TOptions extends {
|
|
24
11
|
url: string;
|
|
25
|
-
onChange?: (connection:
|
|
26
|
-
} &
|
|
12
|
+
onChange?: (connection: StatewireClient.Connection) => void;
|
|
13
|
+
} & StatewireSessionStoreOptions,
|
|
27
14
|
>(
|
|
28
15
|
options: TOptions,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
readonly current: { options: TOptions; ctx: StatewireTransportProps };
|
|
32
|
-
},
|
|
33
|
-
notify: (connection: StatewireConnection) => void,
|
|
34
|
-
) => ConnBase & { send: RemoteLink["send"] },
|
|
35
|
-
): StatewireTransport<any, any, any> => {
|
|
16
|
+
useLink: (options: TOptions, ctx: StatewireRemoteProps) => RemoteLink,
|
|
17
|
+
): Statewire<any, any, any> => {
|
|
36
18
|
const emit = useStatewireEmit<any>();
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
s.emitEvent = emit;
|
|
43
|
-
return () => {
|
|
44
|
-
s.emitEvent = () => {};
|
|
45
|
-
};
|
|
46
|
-
}, [s, emit]);
|
|
47
|
-
useEffect(() => {
|
|
48
|
-
start(s);
|
|
49
|
-
return () => stop(s);
|
|
50
|
-
}, [s]);
|
|
51
|
-
|
|
52
|
-
const [ctx] = useState(
|
|
53
|
-
(): StatewireTransportProps => ({
|
|
54
|
-
get clientId() {
|
|
55
|
-
return s.record.clientId;
|
|
56
|
-
},
|
|
57
|
-
onSnapshot: (args) => handleSnapshot(s, args),
|
|
58
|
-
onOps: (args) => handleOps(s, args),
|
|
59
|
-
onFinish: (args) => handleFinish(s, args),
|
|
60
|
-
}),
|
|
61
|
-
);
|
|
62
|
-
const conn = useStatewireConn(options, ctx, create);
|
|
19
|
+
const s = useStatewireSession({ sessionStore: options.sessionStore }, emit);
|
|
20
|
+
const conn = useLink(options, s.ctx);
|
|
63
21
|
useEffect(() => setTransport(s, conn), [s, conn]);
|
|
64
22
|
|
|
65
23
|
return {
|