statewire 0.19.0 → 0.19.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/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +2 -1
- package/dist/client/connection.js.map +1 -1
- package/dist/client/core.d.ts +2 -0
- package/dist/client/core.d.ts.map +1 -1
- package/dist/client/core.js +8 -3
- package/dist/client/core.js.map +1 -1
- package/dist/client/session.d.ts.map +1 -1
- package/dist/client/session.js +11 -15
- package/dist/client/session.js.map +1 -1
- package/dist/client/sessionStore.d.ts +5 -1
- package/dist/client/sessionStore.d.ts.map +1 -1
- package/dist/client/sessionStore.js +3 -1
- package/dist/client/sessionStore.js.map +1 -1
- package/dist/client/timing.d.ts +1 -0
- package/dist/client/timing.d.ts.map +1 -1
- package/dist/client/timing.js +1 -0
- package/dist/client/timing.js.map +1 -1
- package/dist/host/socket.d.ts +1 -0
- package/dist/host/socket.d.ts.map +1 -1
- package/dist/host/socket.js +26 -16
- package/dist/host/socket.js.map +1 -1
- package/dist/host-internal.d.ts +2 -0
- package/dist/host-internal.d.ts.map +1 -1
- package/dist/statewire.d.ts +8 -3
- package/dist/statewire.d.ts.map +1 -1
- package/dist/statewire.js +21 -13
- package/dist/statewire.js.map +1 -1
- package/dist/transports/remote.js +13 -9
- package/dist/transports/remote.js.map +1 -1
- package/dist/transports/testing.js +2 -1
- package/dist/transports/testing.js.map +1 -1
- package/dist/transports/ws.d.ts.map +1 -1
- package/dist/transports/ws.js +4 -0
- package/dist/transports/ws.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/client/connection.ts +6 -1
- package/src/client/core.ts +10 -3
- package/src/client/session.ts +20 -10
- package/src/client/sessionStore.ts +3 -0
- package/src/client/timing.ts +1 -0
- package/src/host/socket.ts +27 -27
- package/src/host-internal.ts +2 -0
- package/src/statewire.ts +63 -22
- package/src/transports/remote.ts +22 -12
- package/src/transports/testing.ts +4 -3
- package/src/transports/ws.ts +6 -0
- package/src/types.ts +2 -0
package/dist/statewire.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"statewire.d.ts","names":[],"sources":["../src/statewire.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"statewire.d.ts","names":[],"sources":["../src/statewire.ts"],"mappings":";;;;;;;cA8Da,eACX,QACA,kBAAkB,UAAU,WAAW,UAAU,UAEjD,SAAS,gBAAgB,QAAQ,QAAQ,eACxC,gBAAgB,QAAQ,QAAQ;cA+GtB,oBACX,QACA,kBAAkB,UAAU,WAAW,UAAU,UAEjD,SAAS,gBAAgB,QAAQ,QAAQ,eACxC,gBAAgB,gBAAgB,QAAQ,QAAQ;cAGtC,gBACX,QACA,kBAAkB,UAAU,WAAW,UAAU;mBAEhC;UACT;EAEI,YAAA,SAAS,gBAAgB,QAAQ,QAAQ;MAKjD,SAAS;MAIT,cAAc,gBAAgB;MAI9B,YAAY,UAAU,cAAc;MAIpC,aAAa,mBAAmB;MAGhC,aAAa,kBAAkB;EAGnC,OACE,kBACA,gBACA,6BACC;MAGC;MAIA,mBAAmB,gBAAgB;EAIvC,YAAY,OAAO,gBAAgB,gBAAgB;EAInD,cAAc,OAAO,gBAAgB;EAIrC,UAAU;EAIV;;kBAOe;OACH,QACV,QACA,kBAAkB,UAAU,WAAW,UAAU;IAEjD;IACA,cAAc,SAAS;IACvB,cAAc,kBAAkB;IAChC,gCAAgC,kBAAkB;IAClD,WAAW,gBAAgB,UAAU,QAAQ;;IAE7C;;IAEA;MAEM,UAAU,gBAAgB;MAC1B,YAAY,gBAAgB,mBAAmB;;;IAIrD,eAAe;IACf,aAAa,WAAW,YAAY,SAAS;IAC7C,cAAc,YAAY,YAAY;;IAEtC;IACA,iBACE,OAAO,YAAY,SACnB;MAAO,SAAS,UAAU;;IAE5B,mBACE,QAAQ,UAAU,cAAc,YAChC;MAAO,O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IAEL,QAAQ,GAAG,UAAU,IAAI,QAAQ,OAAO,QAAQ;;IAEhD,aAAa;;;OAIH,kBAAkB;IAAU,MAAM,cAAc;;OAEhD,gBACR,gBACE;IAAO;QAAyB,cAAc,QAAQ;OAEhD,cAAc,kBAAkB;IAC1C,eAAe;IACf;;IAEA;IACA,UAAU;IACV,eAAe,WAAW;IAC1B,gBAAgB;IAChB,YAAY,YAAY;IACxB,WAAW,OAAO;;IAElB;;OAGU,mBAAmB,kBAAkB;IAC/C,eAAe;IACf;;IAEA;IACA,UAAU;IACV;IACA,QACI,gCACO,yBAAyB,QAAQ;IAC5C,iBAAiB,aAAa,kCAAkC;IAChE,gBAAgB;IAChB,YAAY,YAAY;IACxB,WAAW,OAAO;;IAElB"}
|
package/dist/statewire.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CONNECTED_CONNECTION, useStatewireCore } from "./client/core.js";
|
|
2
|
-
import { DocumentStorageContext, ProtocolContext, SessionStorageContext } from "./client/sessionStore.js";
|
|
3
|
-
import { useMemo, useRef, useState, useSyncExternalStore } from "@assistant-ui/tap/react-shim";
|
|
2
|
+
import { DocumentStorageContext, LinkContext, ProtocolContext, SessionStorageContext } from "./client/sessionStore.js";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "@assistant-ui/tap/react-shim";
|
|
4
4
|
import { createTapRoot, flushTapSync, resource, useContextProvider, useResource, useResources, withKey } from "@assistant-ui/tap";
|
|
5
5
|
//#region src/statewire.ts
|
|
6
|
-
const useSessionTransport = ({ transport, session, documents, protocol }) => useContextProvider(SessionStorageContext, session, () => useContextProvider(DocumentStorageContext, documents, () => useContextProvider(ProtocolContext, protocol, () => useResource(transport))));
|
|
6
|
+
const useSessionTransport = ({ transport, session, documents, protocol, link }) => useContextProvider(SessionStorageContext, session, () => useContextProvider(DocumentStorageContext, documents, () => useContextProvider(ProtocolContext, protocol, () => useContextProvider(LinkContext, link, () => useResource(transport)))));
|
|
7
7
|
const SessionTransport = resource(useSessionTransport);
|
|
8
8
|
const useStatewire = (options) => {
|
|
9
9
|
const latest = useRef(options);
|
|
@@ -16,31 +16,36 @@ const useStatewire = (options) => {
|
|
|
16
16
|
...options.applicationProtocols !== void 0 && { applicationProtocols: options.applicationProtocols },
|
|
17
17
|
...options.keyPrefixes !== void 0 && { keyPrefixes: options.keyPrefixes }
|
|
18
18
|
};
|
|
19
|
+
const link = useMemo(() => options.isNew === void 0 ? {} : { isNew: options.isNew }, [options.isNew]);
|
|
19
20
|
const value = useResource(SessionTransport({
|
|
20
21
|
transport: options.transport,
|
|
21
22
|
session,
|
|
22
23
|
documents,
|
|
23
|
-
protocol
|
|
24
|
+
protocol,
|
|
25
|
+
link
|
|
24
26
|
}));
|
|
25
27
|
const core = useStatewireCore(() => latest.current, value);
|
|
28
|
+
useEffect(() => core.rematerialize(), [core, options.materializeKey]);
|
|
26
29
|
const [subscribe] = useState(() => (listener) => core.subscribe(() => flushTapSync(listener)));
|
|
27
30
|
const state = useSyncExternalStore(subscribe, core.getState, core.getState);
|
|
28
31
|
const isBusy = useSyncExternalStore(subscribe, core.getBusy, core.getBusy);
|
|
29
32
|
const failed = useSyncExternalStore(subscribe, core.getFailed, core.getFailed);
|
|
30
33
|
const connection = value.connection ?? CONNECTED_CONNECTION;
|
|
34
|
+
const { documents: source, selection, invoke: transportInvoke, failures: transportFailures } = value;
|
|
35
|
+
const invoke = useCallback((protocol_0, method, params) => {
|
|
36
|
+
if (!transportInvoke) throw new Error("statewire: transport does not support protocol commands");
|
|
37
|
+
return transportInvoke(protocol_0, method, params);
|
|
38
|
+
}, [transportInvoke]);
|
|
31
39
|
return useMemo(() => {
|
|
32
40
|
const failures = () => {
|
|
33
|
-
if (!
|
|
34
|
-
return
|
|
41
|
+
if (!transportFailures) throw new Error("statewire: this transport has no failed-send lane");
|
|
42
|
+
return transportFailures;
|
|
35
43
|
};
|
|
36
44
|
return {
|
|
37
45
|
state,
|
|
38
|
-
documents:
|
|
39
|
-
selection
|
|
40
|
-
invoke
|
|
41
|
-
if (!value.invoke) throw new Error("statewire: transport does not support protocol commands");
|
|
42
|
-
return value.invoke(protocol_0, method, params);
|
|
43
|
-
},
|
|
46
|
+
documents: source,
|
|
47
|
+
selection,
|
|
48
|
+
invoke,
|
|
44
49
|
getState: core.getState,
|
|
45
50
|
connection,
|
|
46
51
|
commands: core.commands,
|
|
@@ -55,7 +60,10 @@ const useStatewire = (options) => {
|
|
|
55
60
|
failed,
|
|
56
61
|
connection,
|
|
57
62
|
core,
|
|
58
|
-
|
|
63
|
+
source,
|
|
64
|
+
selection,
|
|
65
|
+
invoke,
|
|
66
|
+
transportFailures
|
|
59
67
|
]);
|
|
60
68
|
};
|
|
61
69
|
const StatewireResource = (options) => resource(useStatewire)(options);
|
package/dist/statewire.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"statewire.js","names":["StatewireDocuments","StatewireProtocol","StatewireHost","useMemo","useRef","useState","useSyncExternalStore","createTapRoot","flushTapSync","resource","useContextProvider","useResource","useResources","withKey","ResourceElement","CONNECTED_CONNECTION","useStatewireCore","Statewire","TERMINAL","DocumentStorageContext","ProtocolContext","SessionStorageContext","useSessionTransport","transport","session","documents","protocol","StatewireClient","SessionStorage","Storage","Options","SessionTransport","useStatewire","Commands","options","TState","TCommands","Helpers","latest","current","storage","undefined","wireVersion","applicationProtocols","keyPrefixes","value","core","subscribe","listener","state","getState","isBusy","getBusy","failed","getFailed","connection","failures","NonNullable","Error","selection","invoke","method","params","commands","CommandsProxy","retryFailed","entry","FailedCommand","retry","discardFailed","discard","StatewireResource","root","disposed","constructor","ClientRoot","getValue","Connection","Source","Selection","Promise","dispose","unmount","Readonly","Record","Range","Offer","initialState","optimistic","Optimistic","materialize","Materialize","onStateChange","ctx","changes","Changes","onCommandChange","update","CommandUpdate","onConnectionChange","onError","error","ConnectionError","failureCovered","ConnectionState","status","reason","degraded","attempt","cause","code","nextRetryAt","lastError","message","payload","reconnect","ConnectionStatus","draft","command","Command","OptimisticContext","ts","seq","clientId","SendFailure","fate","predecessorLost","OnSessionLost","pending","ReadonlyArray","produced","SessionCommand","meta","failure","wire","protocols","SessionLane","nextSeq","verdicts","SessionVerdict","SessionRecord","lanes","type","ready","flush","load","save","record","migrate","lock","fn","T","fin","Fin","HeadersOption","HeadersInit","staleAuth","HttpOptions","sessionStore","url","isNew","headers","fetch","globalThis","onSessionLost","onChange","onDone","WebsocketOptions","query","webSocket","WebSocket"],"sources":["../src/statewire.ts"],"sourcesContent":["import type { StatewireDocuments } from \"./wire/documents\";\nimport type { StatewireProtocol } from \"./wire/protocol\";\nimport type { StatewireHost } from \"./host\";\nimport { useMemo, useRef, useState, useSyncExternalStore } from \"react\";\nimport {\n createTapRoot,\n flushTapSync,\n resource,\n useContextProvider,\n useResource,\n useResources,\n withKey,\n} from \"@assistant-ui/tap\";\nimport type { ResourceElement } from \"@assistant-ui/tap\";\nimport { CONNECTED_CONNECTION, useStatewireCore } from \"./client/core\";\nimport type { Statewire } from \"./types\";\nimport type { TERMINAL } from \"./client/connection\";\nimport {\n DocumentStorageContext,\n ProtocolContext,\n SessionStorageContext,\n} from \"./client/sessionStore\";\n\nconst useSessionTransport = ({\n transport,\n session,\n documents,\n protocol,\n}: {\n transport: ResourceElement<Statewire<any, any>>;\n session: StatewireClient.SessionStorage | undefined;\n documents: StatewireDocuments.Storage | undefined;\n protocol: StatewireProtocol.Options;\n}): Statewire<unknown> =>\n useContextProvider(SessionStorageContext, session, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useContextProvider(DocumentStorageContext, documents, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useContextProvider(ProtocolContext, protocol, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useResource(transport),\n ),\n ),\n );\n\n// The transport owns the session; it reads the storage off the context this\n// wrapper's own fiber provides, so plain-React callers get a provider too.\nconst SessionTransport = resource(useSessionTransport);\n\nexport const useStatewire = <\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.Options<TState, TCommands>,\n): StatewireClient.Helpers<TState, TCommands> => {\n const latest = useRef(options);\n latest.current = options;\n\n const session = useResources(\n options.storage?.session !== undefined\n ? [withKey(\"session\", options.storage.session)]\n : [],\n )[0];\n const documents = useResources(\n options.storage?.documents === undefined\n ? []\n : [withKey(\"documents\", options.storage.documents)],\n )[0];\n const protocol: StatewireProtocol.Options = {\n ...(options.protocol !== undefined && { protocol: options.protocol }),\n ...(options.wireVersion !== undefined && {\n wireVersion: options.wireVersion,\n }),\n ...(options.applicationProtocols !== undefined && {\n applicationProtocols: options.applicationProtocols,\n }),\n ...(options.keyPrefixes !== undefined && {\n keyPrefixes: options.keyPrefixes,\n }),\n };\n const value = useResource(\n SessionTransport({\n transport: options.transport,\n session,\n documents,\n protocol,\n }),\n );\n const core = useStatewireCore(() => latest.current, value);\n const [subscribe] = useState(\n () => (listener: () => void) =>\n core.subscribe(() => flushTapSync(listener)),\n );\n\n const state = useSyncExternalStore(\n subscribe,\n core.getState,\n core.getState,\n ) as TState;\n const isBusy = useSyncExternalStore(subscribe, core.getBusy, core.getBusy);\n const failed = useSyncExternalStore(\n subscribe,\n core.getFailed,\n core.getFailed,\n );\n const connection = value.connection ?? CONNECTED_CONNECTION;\n\n return useMemo(() => {\n const failures = (): NonNullable<typeof value.failures> => {\n if (!value.failures) {\n throw new Error(\"statewire: this transport has no failed-send lane\");\n }\n return value.failures;\n };\n return {\n state,\n documents: value.documents,\n selection: value.selection,\n invoke: (\n protocol: string,\n method: string,\n params: readonly unknown[],\n ) => {\n if (!value.invoke)\n throw new Error(\n \"statewire: transport does not support protocol commands\",\n );\n return value.invoke(protocol, method, params);\n },\n getState: core.getState as () => TState,\n connection,\n commands: core.commands as Statewire.CommandsProxy<TCommands>,\n isBusy,\n failed,\n retryFailed: (entry: StatewireClient.FailedCommand) =>\n failures().retry(entry),\n discardFailed: (entry: StatewireClient.FailedCommand) =>\n failures().discard(entry),\n };\n }, [state, isBusy, failed, connection, core, value]);\n};\n\nexport const StatewireResource = <\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.Options<TState, TCommands>,\n): ResourceElement<StatewireClient.Helpers<TState, TCommands>> =>\n resource(useStatewire<TState, TCommands>)(options);\n\nexport class StatewireClient<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n> {\n private readonly root;\n private disposed = false;\n\n constructor(options: StatewireClient.Options<TState, TCommands>) {\n const ClientRoot = () => useStatewire<TState, TCommands>(options);\n this.root = createTapRoot(ClientRoot);\n }\n\n get state(): TState {\n return this.root.getValue().getState();\n }\n\n get connection(): StatewireClient.Connection {\n return this.root.getValue().connection;\n }\n\n get commands(): Statewire.CommandsProxy<TCommands> {\n return this.root.getValue().commands;\n }\n\n get documents(): StatewireDocuments.Source | undefined {\n return this.root.getValue().documents;\n }\n get selection(): StatewireProtocol.Selection | undefined {\n return this.root.getValue().selection;\n }\n invoke(\n protocol: string,\n method: string,\n params: readonly unknown[],\n ): Promise<unknown> {\n return this.root.getValue().invoke(protocol, method, params);\n }\n get isBusy(): boolean {\n return this.root.getValue().isBusy;\n }\n\n get failed(): readonly StatewireClient.FailedCommand[] {\n return this.root.getValue().failed;\n }\n\n retryFailed(entry: StatewireClient.FailedCommand): Promise<unknown> {\n return this.root.getValue().retryFailed(entry);\n }\n\n discardFailed(entry: StatewireClient.FailedCommand): void {\n this.root.getValue().discardFailed(entry);\n }\n\n subscribe(listener: () => void): () => void {\n return this.root.subscribe(listener);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n this.root.unmount();\n }\n}\n\nexport namespace StatewireClient {\n export type Options<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = {\n protocol?: string;\n keyPrefixes?: Readonly<Record<string, readonly string[]>>;\n wireVersion?: StatewireProtocol.Range;\n applicationProtocols?: readonly StatewireProtocol.Offer[] | \"*\";\n transport: ResourceElement<Statewire<TState, TCommands>>;\n /** Durable session: the mounted storage holds this client's lane identity and its un-durable commands, which resend on the next attach. */\n storage?:\n | {\n session?: ResourceElement<SessionStorage> | undefined;\n documents?: ResourceElement<StatewireDocuments.Storage> | undefined;\n }\n | undefined;\n /** Served as the canonical state until the attach snapshot; own commands fold over it. */\n initialState?: TState;\n optimistic?: Optimistic<NonNullable<TState>, TCommands>;\n materialize?: Materialize<NonNullable<TState>>;\n onStateChange?: (\n state: NonNullable<TState>,\n ctx: { changes: Statewire.Changes },\n ) => void;\n onCommandChange?: (\n update: Statewire.CommandUpdate<TCommands>,\n ctx: { state: NonNullable<TState> },\n ) => void;\n onConnectionChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** Drops a retained failed entry once a canonical snapshot shows its effect landed; checked against each new snapshot. */\n failureCovered?: (\n state: NonNullable<TState>,\n entry: FailedCommand,\n ) => boolean;\n };\n\n export type Helpers<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = {\n /** The statewire's state type — remote statewires include `undefined` until the first attach snapshot applies. */\n state: TState;\n documents: StatewireDocuments.Source | undefined;\n selection: StatewireProtocol.Selection | undefined;\n invoke: (\n protocol: string,\n method: string,\n params: readonly unknown[],\n ) => Promise<unknown>;\n /** Identity-stable across renders; reads the current canonical state without subscribing the caller. */\n getState: () => TState;\n connection: Connection;\n commands: Statewire.CommandsProxy<TCommands>;\n /** Negation of the server's broadcast idle level (a transport without idle is always busy). */\n isBusy: boolean;\n /** Non-accepted sends retained in the session record; reload with it. */\n failed: readonly FailedCommand[];\n /** Re-enqueues the entry's command on fresh clocks and removes the entry. */\n retryFailed: (entry: FailedCommand) => Promise<unknown>;\n /** Removes the entry. */\n discardFailed: (entry: FailedCommand) => void;\n };\n\n /** Connection lifecycle and whether its current state warrants a problem indicator. */\n export type ConnectionState =\n | { status: \"standby\"; reason: \"deferred\" | \"idle\"; degraded: false }\n | { status: \"connecting\"; degraded: false }\n | { status: \"connected\"; degraded: false }\n | {\n status: \"retrying\";\n degraded: true;\n attempt: number;\n /** Backoff details are absent while the retry dial is in flight. */\n cause?: \"dropped\" | \"reconnect\" | \"error\" | \"unauthorized\";\n code?: string;\n nextRetryAt?: number;\n lastError?: string;\n message?: string;\n }\n | {\n status: \"stopped\";\n degraded: true;\n reason:\n | \"gone\"\n | \"forbidden\"\n | \"protocol-error\"\n | \"unauthorized\"\n | \"storage-error\";\n message?: string;\n payload?: unknown;\n };\n export type Connection = ConnectionState & {\n error?: ConnectionError;\n reconnect: () => void;\n };\n export type ConnectionStatus = Connection[\"status\"];\n\n /**\n * Mutate the draft in place; the client records the next state plus the exact\n * changed paths. Re-run against the new base on every rebase.\n */\n export type Optimistic<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = (\n draft: TState,\n command: Statewire.Command<TCommands>,\n ctx: OptimisticContext,\n ) => void;\n\n export type OptimisticContext = {\n ts: number;\n seq: number;\n clientId?: string;\n };\n\n /**\n * Mutate the draft to make server-accepted, still-uncommitted intents in\n * the state concrete (e.g. dispatch records into messages). Runs after the\n * optimistic folds on every published state.\n */\n export type Materialize<TState> = (draft: TState) => void;\n\n /**\n * fate: \"aborted\" = never sent, \"rejected\" = the server refused it\n * (`code` \"unknown-command\" | \"invalid-params\" for the static rejects,\n * code-less for handler rejects), \"lost\" = sent but no verdict will ever\n * arrive — the record is gone (a proven seq gap) or the channel died: it\n * may or may not have run, \"failed\" = the handler ran and threw,\n * \"crashed\" = the handler died involuntarily, \"expired\" = settled —\n * result, rejected, failed, or crashed — but the verdict outlived the\n * server's retention.\n * cause is which channel settled it: \"server\" = the server's answer;\n * the rest are channel deaths the client settled locally.\n */\n export type SendFailure =\n | {\n fate: \"rejected\";\n cause: \"server\";\n code?: string;\n message?: string;\n payload?: unknown;\n }\n | { fate: \"failed\"; cause: \"server\"; message?: string }\n | { fate: \"crashed\"; cause: \"server\" }\n | { fate: \"expired\"; cause: \"server\" }\n | {\n fate: \"aborted\";\n cause: \"unmount\" | \"stream-gone\" | \"session-lost\";\n predecessorLost?: true;\n }\n | {\n fate: \"lost\";\n cause: \"unmount\" | \"stream-gone\" | \"session-lost\";\n };\n\n /** Held commands a proven record gap settled `{fate: \"lost\", cause: \"session-lost\"}`; return \"requeue\" to re-enqueue them on the reset clocks. */\n export type OnSessionLost = (\n pending: ReadonlyArray<{\n method: string;\n params: readonly unknown[];\n ts: number;\n seq: number;\n clientId?: string;\n }>,\n ) => \"requeue\" | \"drop\";\n\n /** A non-accepted send retained for the user: the wire statement, its failure, and its original clocks. */\n export type FailedCommand = {\n readonly protocol: string;\n readonly produced?: SessionCommand[\"produced\"];\n readonly command: string;\n readonly params: readonly unknown[];\n readonly meta?: unknown;\n readonly failure: SendFailure;\n readonly ts: number;\n readonly seq: number;\n };\n\n /** A command as persisted while un-durable: its lane seq plus the wire statement. */\n export type SessionCommand = {\n seq: number;\n produced?: {\n wire: StatewireProtocol.Range;\n protocols: readonly StatewireProtocol.Offer[] | \"*\";\n };\n method: string;\n params: unknown[];\n meta?: unknown;\n };\n\n /** A durable lane: its cmd clock and the commands the server has not marked durable. */\n export type SessionLane = {\n nextSeq: number;\n commands: readonly SessionCommand[];\n /** Terminal verdicts parked for the sharer that minted the seq (shared storage only). */\n verdicts?: readonly SessionVerdict[];\n /** Non-accepted sends retained for the user; survive record rebases and resets. */\n failed?: readonly FailedCommand[];\n };\n\n /** The persisted session: the client identity plus one lane per durable protocol. */\n export type SessionRecord = {\n clientId: string;\n lanes: Readonly<Record<string, SessionLane>>;\n };\n\n /** A terminal verdict one sharer's connection carried, parked for the sharer awaiting the seq. */\n export type SessionVerdict = {\n seq: number;\n type: (typeof TERMINAL)[number];\n code?: string;\n message?: string;\n payload?: unknown;\n meta?: unknown;\n };\n\n /** Session storage seam with optional asynchronous hydration and persistence. */\n export type SessionStorage = {\n /** Resolves when a synchronous load can return the hydrated record. */\n ready?: Promise<void>;\n /** Resolves after all queued saves are persisted; awaited before sending statements. */\n flush?: () => void | Promise<void>;\n load: () => SessionRecord | null;\n save: (record: SessionRecord) => void;\n /** Upgrade incompatible stored commands; null discards the old record and starts a new client identity. */\n migrate?: (\n record: SessionRecord,\n current: NonNullable<SessionCommand[\"produced\"]>,\n ) => SessionRecord | null;\n /** Serializes record mutations across sharers; presence marks the storage shared. */\n lock?: <T>(fn: () => T | Promise<T>) => Promise<T>;\n /** Notifies of record saves by other sharers; returns unsubscribe. Required alongside `lock`. */\n subscribe?: (listener: () => void) => () => void;\n };\n\n /** Static headers, or a resolver invoked per request/attach; `staleAuth` is true right after an auth rejection. */\n export type ConnectionError = Error & { fin?: StatewireHost.Fin };\n\n export type HeadersOption =\n | HeadersInit\n | ((ctx: { staleAuth: boolean }) => HeadersInit | Promise<HeadersInit>);\n\n export type HttpOptions = StatewireProtocol.Options & {\n sessionStore?: SessionStorage | undefined;\n url: string;\n /** The session does not exist on the server yet: the first attach waits for the first command send (or an explicit `reconnect()`). */\n isNew?: boolean;\n headers?: HeadersOption;\n fetch?: typeof globalThis.fetch;\n onSessionLost?: OnSessionLost;\n onChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** The broadcast idle flag's rising edge: the instance went quiet. */\n onDone?: () => void;\n };\n\n export type WebsocketOptions = StatewireProtocol.Options & {\n sessionStore?: SessionStorage | undefined;\n url: string;\n /** The session does not exist on the server yet: the first attach waits for the first command send (or an explicit `reconnect()`). */\n isNew?: boolean;\n headers?: HeadersOption;\n protocols?: string | string[];\n query?:\n | Record<string, string>\n | (() => Record<string, string> | Promise<Record<string, string>>);\n webSocket?: new (url: string, protocols?: string | string[]) => WebSocket;\n onSessionLost?: OnSessionLost;\n onChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** The broadcast idle flag's rising edge: the instance went quiet. */\n onDone?: () => void;\n };\n}\n"],"mappings":";;;;;AAuBA,MAAMsB,uBAAuB,EAC3BC,WACAC,SACAC,WACAC,eAOAhB,mBAAmBW,uBAAuBG,eAExCd,mBAAmBS,wBAAwBM,iBAEzCf,mBAAmBU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AKU,KAAKI,SAAS,CAAC,CAAChB,OAAOlC,UAAUmC,QAAQC,MAAM;CAC7D;CACA,IAAIX,SAAkB;EACpB,OAAO,KAAKqB,KAAKI,SAAS,CAAC,CAACzB;CAC9B;CAEA,IAAIE,SAAmD;EACrD,OAAO,KAAKmB,KAAKI,SAAS,CAAC,CAACvB;CAC9B;CAEAY,YAAYC,OAAwD;EAClE,OAAO,KAAKM,KAAKI,SAAS,CAAC,CAACX,YAAYC,KAAK;CAC/C;CAEAG,cAAcH,OAA4C;EACxD,KAAKM,KAAKI,SAAS,CAAC,CAACP,cAAcH,KAAK;CAC1C;CAEAnB,UAAUC,UAAkC;EAC1C,OAAO,KAAKwB,KAAKzB,UAAUC,QAAQ;CACrC;CAEAiC,UAAgB;EACd,IAAI,KAAKR,UAAU;EACnB,KAAKA,WAAW;EAChB,KAAKD,KAAKU,QAAQ;CACpB;AACF"}
|
|
1
|
+
{"version":3,"file":"statewire.js","names":["StatewireDocuments","StatewireProtocol","StatewireHost","useCallback","useEffect","useMemo","useRef","useState","useSyncExternalStore","createTapRoot","flushTapSync","resource","useContextProvider","useResource","useResources","withKey","ResourceElement","CONNECTED_CONNECTION","useStatewireCore","Statewire","TERMINAL","DocumentStorageContext","LinkContext","ProtocolContext","SessionStorageContext","useSessionTransport","transport","session","documents","protocol","link","StatewireClient","SessionStorage","Storage","Options","isNew","SessionTransport","useStatewire","Commands","options","TState","TCommands","Helpers","latest","current","storage","undefined","wireVersion","applicationProtocols","keyPrefixes","value","core","rematerialize","materializeKey","subscribe","listener","state","getState","isBusy","getBusy","failed","getFailed","connection","source","selection","invoke","transportInvoke","failures","transportFailures","method","params","Error","NonNullable","commands","CommandsProxy","retryFailed","entry","FailedCommand","retry","discardFailed","discard","StatewireResource","root","disposed","constructor","ClientRoot","getValue","Connection","Source","Selection","Promise","dispose","unmount","Readonly","Record","Range","Offer","initialState","optimistic","Optimistic","materialize","Materialize","onStateChange","ctx","changes","Changes","onCommandChange","update","CommandUpdate","onConnectionChange","onError","error","ConnectionError","failureCovered","ConnectionState","status","reason","degraded","attempt","cause","code","nextRetryAt","lastError","message","payload","reconnect","ConnectionStatus","draft","command","Command","OptimisticContext","ts","seq","clientId","SendFailure","fate","predecessorLost","OnSessionLost","pending","ReadonlyArray","produced","SessionCommand","meta","failure","wire","protocols","SessionLane","nextSeq","verdicts","SessionVerdict","SessionRecord","lanes","type","ready","flush","load","save","record","migrate","lock","fn","T","fin","Fin","HeadersOption","HeadersInit","staleAuth","HttpOptions","sessionStore","url","headers","fetch","globalThis","onSessionLost","onChange","onDone","WebsocketOptions","query","webSocket","WebSocket"],"sources":["../src/statewire.ts"],"sourcesContent":["import type { StatewireDocuments } from \"./wire/documents\";\nimport type { StatewireProtocol } from \"./wire/protocol\";\nimport type { StatewireHost } from \"./host\";\nimport {\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n useSyncExternalStore,\n} from \"react\";\nimport {\n createTapRoot,\n flushTapSync,\n resource,\n useContextProvider,\n useResource,\n useResources,\n withKey,\n} from \"@assistant-ui/tap\";\nimport type { ResourceElement } from \"@assistant-ui/tap\";\nimport { CONNECTED_CONNECTION, useStatewireCore } from \"./client/core\";\nimport type { Statewire } from \"./types\";\nimport type { TERMINAL } from \"./client/connection\";\nimport {\n DocumentStorageContext,\n LinkContext,\n ProtocolContext,\n SessionStorageContext,\n} from \"./client/sessionStore\";\n\nconst useSessionTransport = ({\n transport,\n session,\n documents,\n protocol,\n link,\n}: {\n transport: ResourceElement<Statewire<any, any>>;\n session: StatewireClient.SessionStorage | undefined;\n documents: StatewireDocuments.Storage | undefined;\n protocol: StatewireProtocol.Options;\n link: { isNew?: boolean | undefined };\n}): Statewire<unknown> =>\n useContextProvider(SessionStorageContext, session, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useContextProvider(DocumentStorageContext, documents, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useContextProvider(ProtocolContext, protocol, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useContextProvider(LinkContext, link, () =>\n // oxlint-disable-next-line react-hooks/rules-of-hooks\n useResource(transport),\n ),\n ),\n ),\n );\n\n// The transport owns the session; it reads the storage off the context this\n// wrapper's own fiber provides, so plain-React callers get a provider too.\nconst SessionTransport = resource(useSessionTransport);\n\nexport const useStatewire = <\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.Options<TState, TCommands>,\n): StatewireClient.Helpers<TState, TCommands> => {\n const latest = useRef(options);\n latest.current = options;\n\n const session = useResources(\n options.storage?.session !== undefined\n ? [withKey(\"session\", options.storage.session)]\n : [],\n )[0];\n const documents = useResources(\n options.storage?.documents === undefined\n ? []\n : [withKey(\"documents\", options.storage.documents)],\n )[0];\n const protocol: StatewireProtocol.Options = {\n ...(options.protocol !== undefined && { protocol: options.protocol }),\n ...(options.wireVersion !== undefined && {\n wireVersion: options.wireVersion,\n }),\n ...(options.applicationProtocols !== undefined && {\n applicationProtocols: options.applicationProtocols,\n }),\n ...(options.keyPrefixes !== undefined && {\n keyPrefixes: options.keyPrefixes,\n }),\n };\n const link = useMemo(\n () => (options.isNew === undefined ? {} : { isNew: options.isNew }),\n [options.isNew],\n );\n const value = useResource(\n SessionTransport({\n transport: options.transport,\n session,\n documents,\n protocol,\n link,\n }),\n );\n const core = useStatewireCore(() => latest.current, value);\n useEffect(() => core.rematerialize(), [core, options.materializeKey]);\n const [subscribe] = useState(\n () => (listener: () => void) =>\n core.subscribe(() => flushTapSync(listener)),\n );\n\n const state = useSyncExternalStore(\n subscribe,\n core.getState,\n core.getState,\n ) as TState;\n const isBusy = useSyncExternalStore(subscribe, core.getBusy, core.getBusy);\n const failed = useSyncExternalStore(\n subscribe,\n core.getFailed,\n core.getFailed,\n );\n const connection = value.connection ?? CONNECTED_CONNECTION;\n const {\n documents: source,\n selection,\n invoke: transportInvoke,\n failures: transportFailures,\n } = value;\n\n const invoke = useCallback(\n (protocol: string, method: string, params: readonly unknown[]) => {\n if (!transportInvoke)\n throw new Error(\n \"statewire: transport does not support protocol commands\",\n );\n return transportInvoke(protocol, method, params);\n },\n [transportInvoke],\n );\n\n return useMemo(() => {\n const failures = (): NonNullable<typeof transportFailures> => {\n if (!transportFailures) {\n throw new Error(\"statewire: this transport has no failed-send lane\");\n }\n return transportFailures;\n };\n return {\n state,\n documents: source,\n selection,\n invoke,\n getState: core.getState as () => TState,\n connection,\n commands: core.commands as Statewire.CommandsProxy<TCommands>,\n isBusy,\n failed,\n retryFailed: (entry: StatewireClient.FailedCommand) =>\n failures().retry(entry),\n discardFailed: (entry: StatewireClient.FailedCommand) =>\n failures().discard(entry),\n };\n }, [\n state,\n isBusy,\n failed,\n connection,\n core,\n source,\n selection,\n invoke,\n transportFailures,\n ]);\n};\n\nexport const StatewireResource = <\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.Options<TState, TCommands>,\n): ResourceElement<StatewireClient.Helpers<TState, TCommands>> =>\n resource(useStatewire<TState, TCommands>)(options);\n\nexport class StatewireClient<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n> {\n private readonly root;\n private disposed = false;\n\n constructor(options: StatewireClient.Options<TState, TCommands>) {\n const ClientRoot = () => useStatewire<TState, TCommands>(options);\n this.root = createTapRoot(ClientRoot);\n }\n\n get state(): TState {\n return this.root.getValue().getState();\n }\n\n get connection(): StatewireClient.Connection {\n return this.root.getValue().connection;\n }\n\n get commands(): Statewire.CommandsProxy<TCommands> {\n return this.root.getValue().commands;\n }\n\n get documents(): StatewireDocuments.Source | undefined {\n return this.root.getValue().documents;\n }\n get selection(): StatewireProtocol.Selection | undefined {\n return this.root.getValue().selection;\n }\n invoke(\n protocol: string,\n method: string,\n params: readonly unknown[],\n ): Promise<unknown> {\n return this.root.getValue().invoke(protocol, method, params);\n }\n get isBusy(): boolean {\n return this.root.getValue().isBusy;\n }\n\n get failed(): readonly StatewireClient.FailedCommand[] {\n return this.root.getValue().failed;\n }\n\n retryFailed(entry: StatewireClient.FailedCommand): Promise<unknown> {\n return this.root.getValue().retryFailed(entry);\n }\n\n discardFailed(entry: StatewireClient.FailedCommand): void {\n this.root.getValue().discardFailed(entry);\n }\n\n subscribe(listener: () => void): () => void {\n return this.root.subscribe(listener);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n this.root.unmount();\n }\n}\n\nexport namespace StatewireClient {\n export type Options<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = {\n protocol?: string;\n keyPrefixes?: Readonly<Record<string, readonly string[]>>;\n wireVersion?: StatewireProtocol.Range;\n applicationProtocols?: readonly StatewireProtocol.Offer[] | \"*\";\n transport: ResourceElement<Statewire<TState, TCommands>>;\n /** The session does not exist on the server yet: the transport's first attach waits for the first command send; the transport's own `isNew` wins. */\n isNew?: boolean;\n /** Durable session: the mounted storage holds this client's lane identity and its un-durable commands, which resend on the next attach. */\n storage?:\n | {\n session?: ResourceElement<SessionStorage> | undefined;\n documents?: ResourceElement<StatewireDocuments.Storage> | undefined;\n }\n | undefined;\n /** Served as the canonical state until the attach snapshot; own commands fold over it. */\n initialState?: TState;\n optimistic?: Optimistic<NonNullable<TState>, TCommands>;\n materialize?: Materialize<NonNullable<TState>>;\n /** Republishes through `materialize` when it changes (`Object.is`); `materialize` itself is read fresh on every publish. */\n materializeKey?: unknown;\n onStateChange?: (\n state: NonNullable<TState>,\n ctx: { changes: Statewire.Changes },\n ) => void;\n onCommandChange?: (\n update: Statewire.CommandUpdate<TCommands>,\n ctx: { state: NonNullable<TState> },\n ) => void;\n onConnectionChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** Drops a retained failed entry once a canonical snapshot shows its effect landed; checked against each new snapshot. */\n failureCovered?: (\n state: NonNullable<TState>,\n entry: FailedCommand,\n ) => boolean;\n };\n\n export type Helpers<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = {\n /** The statewire's state type — remote statewires include `undefined` until the first attach snapshot applies. */\n state: TState;\n documents: StatewireDocuments.Source | undefined;\n selection: StatewireProtocol.Selection | undefined;\n invoke: (\n protocol: string,\n method: string,\n params: readonly unknown[],\n ) => Promise<unknown>;\n /** Identity-stable across renders; reads the current canonical state without subscribing the caller. */\n getState: () => TState;\n connection: Connection;\n commands: Statewire.CommandsProxy<TCommands>;\n /** Negation of the server's broadcast idle level (a transport without idle is always busy). */\n isBusy: boolean;\n /** Non-accepted sends retained in the session record; reload with it. */\n failed: readonly FailedCommand[];\n /** Re-enqueues the entry's command on fresh clocks and removes the entry. */\n retryFailed: (entry: FailedCommand) => Promise<unknown>;\n /** Removes the entry. */\n discardFailed: (entry: FailedCommand) => void;\n };\n\n /** Connection lifecycle and whether its current state warrants a problem indicator. */\n export type ConnectionState =\n | { status: \"standby\"; reason: \"deferred\" | \"idle\"; degraded: false }\n | { status: \"connecting\"; degraded: false }\n | { status: \"connected\"; degraded: false }\n | {\n status: \"retrying\";\n degraded: true;\n attempt: number;\n /** Backoff details are absent while the retry dial is in flight. */\n cause?: \"dropped\" | \"reconnect\" | \"error\" | \"unauthorized\";\n code?: string;\n nextRetryAt?: number;\n lastError?: string;\n message?: string;\n }\n | {\n status: \"stopped\";\n degraded: true;\n reason:\n | \"gone\"\n | \"forbidden\"\n | \"protocol-error\"\n | \"unauthorized\"\n | \"storage-error\";\n message?: string;\n payload?: unknown;\n };\n export type Connection = ConnectionState & {\n error?: ConnectionError;\n reconnect: () => void;\n };\n export type ConnectionStatus = Connection[\"status\"];\n\n /**\n * Mutate the draft in place; the client records the next state plus the exact\n * changed paths. Re-run against the new base on every rebase.\n */\n export type Optimistic<\n TState,\n TCommands extends Statewire.Commands = Statewire.Commands,\n > = (\n draft: TState,\n command: Statewire.Command<TCommands>,\n ctx: OptimisticContext,\n ) => void;\n\n export type OptimisticContext = {\n ts: number;\n seq: number;\n clientId?: string;\n };\n\n /**\n * Mutate the draft to fold client-known facts into the state: server-accepted,\n * still-uncommitted intents (e.g. dispatch records into messages) or the live\n * state of a sibling statewire. Runs after the optimistic folds on every\n * published state; `materializeKey` republishes when the facts change.\n */\n export type Materialize<TState> = (draft: TState) => void;\n\n /**\n * fate: \"aborted\" = never sent, \"rejected\" = the server refused it\n * (`code` \"unknown-command\" | \"invalid-params\" for the static rejects,\n * code-less for handler rejects), \"lost\" = sent but no verdict will ever\n * arrive — the record is gone (a proven seq gap) or the channel died: it\n * may or may not have run, \"failed\" = the handler ran and threw,\n * \"crashed\" = the handler died involuntarily, \"expired\" = settled —\n * result, rejected, failed, or crashed — but the verdict outlived the\n * server's retention.\n * cause is which channel settled it: \"server\" = the server's answer;\n * the rest are channel deaths the client settled locally.\n */\n export type SendFailure =\n | {\n fate: \"rejected\";\n cause: \"server\";\n code?: string;\n message?: string;\n payload?: unknown;\n }\n | { fate: \"failed\"; cause: \"server\"; message?: string }\n | { fate: \"crashed\"; cause: \"server\" }\n | { fate: \"expired\"; cause: \"server\" }\n | {\n fate: \"aborted\";\n cause: \"unmount\" | \"stream-gone\" | \"session-lost\";\n predecessorLost?: true;\n }\n | {\n fate: \"lost\";\n cause: \"unmount\" | \"stream-gone\" | \"session-lost\";\n };\n\n /** Held commands a proven record gap settled `{fate: \"lost\", cause: \"session-lost\"}`; return \"requeue\" to re-enqueue them on the reset clocks. */\n export type OnSessionLost = (\n pending: ReadonlyArray<{\n method: string;\n params: readonly unknown[];\n ts: number;\n seq: number;\n clientId?: string;\n }>,\n ) => \"requeue\" | \"drop\";\n\n /** A non-accepted send retained for the user: the wire statement, its failure, and its original clocks. */\n export type FailedCommand = {\n readonly protocol: string;\n readonly produced?: SessionCommand[\"produced\"];\n readonly command: string;\n readonly params: readonly unknown[];\n readonly meta?: unknown;\n readonly failure: SendFailure;\n readonly ts: number;\n readonly seq: number;\n };\n\n /** A command as persisted while un-durable: its lane seq plus the wire statement. */\n export type SessionCommand = {\n seq: number;\n produced?: {\n wire: StatewireProtocol.Range;\n protocols: readonly StatewireProtocol.Offer[] | \"*\";\n };\n method: string;\n params: unknown[];\n meta?: unknown;\n };\n\n /** A durable lane: its cmd clock and the commands the server has not marked durable. */\n export type SessionLane = {\n nextSeq: number;\n commands: readonly SessionCommand[];\n /** Terminal verdicts parked for the sharer that minted the seq (shared storage only). */\n verdicts?: readonly SessionVerdict[];\n /** Non-accepted sends retained for the user; survive record rebases and resets. */\n failed?: readonly FailedCommand[];\n };\n\n /** The persisted session: the client identity plus one lane per durable protocol. */\n export type SessionRecord = {\n clientId: string;\n lanes: Readonly<Record<string, SessionLane>>;\n };\n\n /** A terminal verdict one sharer's connection carried, parked for the sharer awaiting the seq. */\n export type SessionVerdict = {\n seq: number;\n type: (typeof TERMINAL)[number];\n code?: string;\n message?: string;\n payload?: unknown;\n meta?: unknown;\n };\n\n /** Session storage seam with optional asynchronous hydration and persistence. */\n export type SessionStorage = {\n /** Resolves when a synchronous load can return the hydrated record. */\n ready?: Promise<void>;\n /** Resolves after all queued saves are persisted; awaited before sending statements. */\n flush?: () => void | Promise<void>;\n load: () => SessionRecord | null;\n save: (record: SessionRecord) => void;\n /** Upgrade incompatible stored commands; null discards the old record and starts a new client identity. */\n migrate?: (\n record: SessionRecord,\n current: NonNullable<SessionCommand[\"produced\"]>,\n ) => SessionRecord | null;\n /** Serializes record mutations across sharers; presence marks the storage shared. */\n lock?: <T>(fn: () => T | Promise<T>) => Promise<T>;\n /** Notifies of record saves by other sharers; returns unsubscribe. Required alongside `lock`. */\n subscribe?: (listener: () => void) => () => void;\n };\n\n /** Static headers, or a resolver invoked per request/attach; `staleAuth` is true right after an auth rejection. */\n export type ConnectionError = Error & { fin?: StatewireHost.Fin };\n\n export type HeadersOption =\n | HeadersInit\n | ((ctx: { staleAuth: boolean }) => HeadersInit | Promise<HeadersInit>);\n\n export type HttpOptions = StatewireProtocol.Options & {\n sessionStore?: SessionStorage | undefined;\n url: string;\n /** The session does not exist on the server yet: the first attach waits for the first command send (or an explicit `reconnect()`). */\n isNew?: boolean;\n headers?: HeadersOption;\n fetch?: typeof globalThis.fetch;\n onSessionLost?: OnSessionLost;\n onChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** The broadcast idle flag's rising edge: the instance went quiet. */\n onDone?: () => void;\n };\n\n export type WebsocketOptions = StatewireProtocol.Options & {\n sessionStore?: SessionStorage | undefined;\n url: string;\n /** The session does not exist on the server yet: the first attach waits for the first command send (or an explicit `reconnect()`). */\n isNew?: boolean;\n headers?: HeadersOption;\n protocols?: string | string[];\n query?:\n | Record<string, string>\n | (() => Record<string, string> | Promise<Record<string, string>>);\n webSocket?: new (url: string, protocols?: string | string[]) => WebSocket;\n onSessionLost?: OnSessionLost;\n onChange?: (connection: Connection) => void;\n onError?: (error: ConnectionError) => void;\n /** The broadcast idle flag's rising edge: the instance went quiet. */\n onDone?: () => void;\n };\n}\n"],"mappings":";;;;;AA+BA,MAAMyB,uBAAuB,EAC3BC,WACAC,SACAC,WACAC,UACAC,WAQAlB,mBAAmBY,uBAAuBG,eAExCf,mBAAmBS,wBAAwBO,iBAEzChB,mBAAmBW,iBAAiBM,gBAElCjB,mBAAmBU,aAAaQ,YAE9BjB,YAAYa,SAAS,CACvB,CACF,CACF,CACF;AAIF,MAAMU,mBAAmBzB,SAASc,mBAAmB;AAErD,MAAaY,gBAIXE,YAC+C;CAC/C,MAAMI,SAASrC,OAAOiC,OAAO;CAC7BI,OAAOC,UAAUL;CAEjB,MAAMZ,UAAUb,aACdyB,QAAQM,SAASlB,YAAYmB,KAAAA,IACzB,CAAC/B,QAAQ,WAAWwB,QAAQM,QAAQlB,OAAO,CAAC,IAC5C,CAAA,CACN,CAAC,CAAC;CACF,MAAMC,YAAYd,aAChByB,QAAQM,SAASjB,cAAckB,KAAAA,IAC3B,CAAA,IACA,CAAC/B,QAAQ,aAAawB,QAAQM,QAAQjB,SAAS,CAAC,CACtD,CAAC,CAAC;CACF,MAAMC,WAAsC;EAC1C,GAAIU,QAAQV,aAAaiB,KAAAA,KAAa,EAAEjB,UAAUU,QAAQV,SAAS;EACnE,GAAIU,QAAQQ,gBAAgB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,KAAK;CAC/C;CAEAG,cAAcH,OAA4C;EACxD,KAAKM,KAAKI,SAAS,CAAC,CAACP,cAAcH,KAAK;CAC1C;CAEAtB,UAAUC,UAAkC;EAC1C,OAAO,KAAK2B,KAAK5B,UAAUC,QAAQ;CACrC;CAEAoC,UAAgB;EACd,IAAI,KAAKR,UAAU;EACnB,KAAKA,WAAW;EAChB,KAAKD,KAAKU,QAAQ;CACpB;AACF"}
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
import { ProtocolContext } from "../client/sessionStore.js";
|
|
1
|
+
import { LinkContext, ProtocolContext } from "../client/sessionStore.js";
|
|
2
2
|
import { discardFailed, documentSource, failedCommands, invokeProtocol, retryFailed, setTransport, useStatewireSession } from "../client/session.js";
|
|
3
|
-
import { useContext, useEffect } from "@assistant-ui/tap/react-shim";
|
|
3
|
+
import { useContext, useEffect, useMemo } from "@assistant-ui/tap/react-shim";
|
|
4
4
|
//#region src/transports/remote.ts
|
|
5
5
|
const useStatewireRemote = (options, useLink) => {
|
|
6
6
|
const configured = {
|
|
7
7
|
...useContext(ProtocolContext),
|
|
8
|
+
...useContext(LinkContext),
|
|
8
9
|
...options
|
|
9
10
|
};
|
|
10
11
|
const s = useStatewireSession(configured);
|
|
11
12
|
const conn = useLink(configured, s.ctx);
|
|
12
13
|
useEffect(() => setTransport(s, conn), [s, conn]);
|
|
14
|
+
const lanes = useMemo(() => ({
|
|
15
|
+
documents: documentSource(s),
|
|
16
|
+
invoke: (protocol, method, params) => invokeProtocol(s, protocol, method, params),
|
|
17
|
+
failures: {
|
|
18
|
+
list: () => failedCommands(s),
|
|
19
|
+
retry: (entry) => retryFailed(s, entry),
|
|
20
|
+
discard: (entry_0) => discardFailed(s, entry_0)
|
|
21
|
+
}
|
|
22
|
+
}), [s]);
|
|
13
23
|
return {
|
|
14
24
|
state: s.canonical,
|
|
15
|
-
|
|
25
|
+
...lanes,
|
|
16
26
|
get selection() {
|
|
17
27
|
return s.selection;
|
|
18
28
|
},
|
|
19
|
-
invoke: (protocol, method, params) => invokeProtocol(s, protocol, method, params),
|
|
20
29
|
commands: s.commands,
|
|
21
30
|
connection: conn.connection,
|
|
22
31
|
source: s.source,
|
|
23
32
|
get isBusy() {
|
|
24
33
|
return !s.idle;
|
|
25
|
-
},
|
|
26
|
-
failures: {
|
|
27
|
-
list: () => failedCommands(s),
|
|
28
|
-
retry: (entry) => retryFailed(s, entry),
|
|
29
|
-
discard: (entry_0) => discardFailed(s, entry_0)
|
|
30
34
|
}
|
|
31
35
|
};
|
|
32
36
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","names":["useContext","useEffect","ProtocolContext","Statewire","documentSource","invokeProtocol","discardFailed","failedCommands","retryFailed","setTransport","useStatewireSession","StatewireSessionOptions","StatewireClient","RemoteLink","StatewireRemoteProps","useStatewireRemote","url","onChange","connection","Connection","onDone","options","TOptions","useLink","ctx","
|
|
1
|
+
{"version":3,"file":"remote.js","names":["useContext","useEffect","useMemo","LinkContext","ProtocolContext","Statewire","documentSource","invokeProtocol","discardFailed","failedCommands","retryFailed","setTransport","useStatewireSession","StatewireSessionOptions","StatewireClient","RemoteLink","StatewireRemoteProps","useStatewireRemote","url","onChange","connection","Connection","onDone","options","TOptions","useLink","ctx","configured","s","conn","lanes","documents","invoke","protocol","method","params","failures","list","retry","entry","FailedCommand","discard","state","canonical","selection","commands","source","isBusy","idle"],"sources":["../../src/transports/remote.ts"],"sourcesContent":["import { useContext, useEffect, useMemo } from \"react\";\nimport { LinkContext, ProtocolContext } from \"../client/sessionStore\";\nimport type { Statewire } from \"../types\";\nimport {\n documentSource,\n invokeProtocol,\n discardFailed,\n failedCommands,\n retryFailed,\n setTransport,\n useStatewireSession,\n} from \"../client/session\";\nimport type { StatewireSessionOptions } from \"../client/session\";\nimport type { StatewireClient } from \"../statewire\";\nimport type { RemoteLink, StatewireRemoteProps } from \"./sse\";\n\nexport const useStatewireRemote = <\n TOptions extends {\n url: string;\n onChange?: (connection: StatewireClient.Connection) => void;\n onDone?: () => void;\n } & StatewireSessionOptions,\n>(\n options: TOptions,\n useLink: (options: TOptions, ctx: StatewireRemoteProps) => RemoteLink,\n): Statewire<any, any> => {\n const configured = {\n ...useContext(ProtocolContext),\n ...useContext(LinkContext),\n ...options,\n };\n const s = useStatewireSession(configured);\n const conn = useLink(configured, s.ctx);\n useEffect(() => setTransport(s, conn), [s, conn]);\n const lanes = useMemo(\n () => ({\n documents: documentSource(s),\n invoke: (protocol: string, method: string, params: readonly unknown[]) =>\n invokeProtocol(s, protocol, method, params),\n failures: {\n list: () => failedCommands(s),\n retry: (entry: StatewireClient.FailedCommand) => retryFailed(s, entry),\n discard: (entry: StatewireClient.FailedCommand) =>\n discardFailed(s, entry),\n },\n }),\n [s],\n );\n\n return {\n state: s.canonical,\n ...lanes,\n get selection() {\n return s.selection;\n },\n commands: s.commands,\n connection: conn.connection,\n source: s.source,\n get isBusy() {\n return !s.idle;\n },\n };\n};\n"],"mappings":";;;;AAgBA,MAAaiB,sBAOXM,SACAE,YACwB;CACxB,MAAME,aAAa;EACjB,GAAG3B,WAAWI,eAAe;EAC7B,GAAGJ,WAAWG,WAAW;EACzB,GAAGoB;CACL;CACA,MAAMK,IAAIhB,oBAAoBe,UAAU;CACxC,MAAME,OAAOJ,QAAQE,YAAYC,EAAEF,GAAG;CACtCzB,gBAAgBU,aAAaiB,GAAGC,IAAI,GAAG,CAACD,GAAGC,IAAI,CAAC;CAChD,MAAMC,QAAQ5B,eACL;EACL6B,WAAWzB,eAAesB,CAAC;EAC3BI,SAASC,UAAkBC,QAAgBC,WACzC5B,eAAeqB,GAAGK,UAAUC,QAAQC,MAAM;EAC5CC,UAAU;GACRC,YAAY5B,eAAemB,CAAC;GAC5BU,QAAQC,UAAyC7B,YAAYkB,GAAGW,KAAK;GACrEE,UAAUF,YACR/B,cAAcoB,GAAGW,OAAK;EAC1B;CACF,IACA,CAACX,CAAC,CACJ;CAEA,OAAO;EACLc,OAAOd,EAAEe;EACT,GAAGb;EACH,IAAIc,YAAY;GACd,OAAOhB,EAAEgB;EACX;EACAC,UAAUjB,EAAEiB;EACZzB,YAAYS,KAAKT;EACjB0B,QAAQlB,EAAEkB;EACV,IAAIC,SAAS;GACX,OAAO,CAACnB,EAAEoB;EACZ;CACF;AACF"}
|
|
@@ -13,7 +13,7 @@ const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
|
13
13
|
*/
|
|
14
14
|
let mockCount = 0;
|
|
15
15
|
const createMockTransport = (initialState, protocol = "default", options = {}) => {
|
|
16
|
-
const
|
|
16
|
+
const ephemeralLanes = () => (options.ephemeral ?? []).map((child) => `${protocol}/${child}`);
|
|
17
17
|
let state = initialState;
|
|
18
18
|
let clientId = "";
|
|
19
19
|
let finished = false;
|
|
@@ -44,6 +44,7 @@ const createMockTransport = (initialState, protocol = "default", options = {}) =
|
|
|
44
44
|
clientId = id;
|
|
45
45
|
if (!clients.has(id)) clients.set(id, {});
|
|
46
46
|
const rec = clients.get(id);
|
|
47
|
+
const ephemeral = ephemeralLanes();
|
|
47
48
|
for (const lane of ephemeral) delete rec[lane];
|
|
48
49
|
const frame = JSON.parse(String(init?.body ?? "{}"));
|
|
49
50
|
admit(id, frame);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","names":["ResourceElement","StatewireEnvelope","StatewireHttp","StatewireFrame","Statewire","openSSEStream","sseFrame","FakeStreamHandle","WIRE_VERSION","tick","Promise","r","setTimeout","applyOptimistic","MockTransport","transport","TState","commands","protocol","method","params","setState","state","applied","seq","settle","payload","sendEnvelope","envelope","finish","reason","message","clientId","attached","mockCount","createMockTransport","initialState","options","ephemeral","map","child","finished","clients","Map","Record","streams","Set","stream","send","admit","id","frame","rec","get","statement","cmd","push","fakeFetch","input","RequestInfo","URL","init","RequestInit","Response","TypeError","url","String","headers","Headers","endsWith","has","set","lane","JSON","parse","body","handle","ops","op","main","value","syn","version","protocols","name","record","const","signal","undefined","json","status","fetch","next","path","type","fin","close","size"],"sources":["../../src/transports/testing.ts"],"sourcesContent":["import type { ResourceElement } from \"@assistant-ui/tap\";\nimport type { StatewireEnvelope } from \"../client/connection\";\nimport { StatewireHttp } from \"./sse\";\nimport type { StatewireFrame } from \"./sse\";\nimport type { Statewire } from \"../types\";\nimport { openSSEStream, sseFrame } from \"./fake-sse\";\nimport type { FakeStreamHandle } from \"./fake-sse\";\nimport { WIRE_VERSION } from \"../wire/protocol\";\n\nexport * from \"./fixtures\";\n\n/** Let queued microtasks and op batches drain through the statewire client. */\nexport const tick = () => new Promise<void>((r) => setTimeout(r, 0));\n\nexport { applyOptimistic } from \"../util/recordMutations\";\n\nexport type MockTransport<TState> = {\n /** Pass as the `transport:` option of useStatewire / Harness. */\n transport: ResourceElement<Statewire<TState | undefined, any>>;\n /** Captured statements in arrival order. */\n commands: { protocol: string; method: string; params: unknown[] }[];\n /** Replace the replicated state and broadcast it as a root snapshot op. */\n setState: (state: TState) => Promise<void>;\n /** Deliver a bare ack cmd packet on the stream. */\n applied: (seq: number) => Promise<void>;\n /** Deliver a terminal result cmd packet on the stream. */\n settle: (seq: number, payload?: unknown) => Promise<void>;\n /** Deliver an arbitrary envelope on the stream. */\n sendEnvelope: (envelope: StatewireEnvelope) => Promise<void>;\n /** End the stream with a fin; further attaches fail like a dead server. */\n finish: (reason: string, message?: string) => Promise<void>;\n readonly clientId: string;\n readonly attached: boolean;\n};\n\n/**\n * A mock statewire backend: serves `initialState` as the attach snapshot,\n * records submitted frames (receipt only — acks and results are delivered\n * from test code), and exposes the wire as a transport ResourceElement.\n */\nlet mockCount = 0;\n\nexport const createMockTransport = <TState>(\n initialState: TState,\n protocol = \"default\",\n options: { ephemeral?: readonly string[] } = {},\n): MockTransport<TState> => {\n const
|
|
1
|
+
{"version":3,"file":"testing.js","names":["ResourceElement","StatewireEnvelope","StatewireHttp","StatewireFrame","Statewire","openSSEStream","sseFrame","FakeStreamHandle","WIRE_VERSION","tick","Promise","r","setTimeout","applyOptimistic","MockTransport","transport","TState","commands","protocol","method","params","setState","state","applied","seq","settle","payload","sendEnvelope","envelope","finish","reason","message","clientId","attached","mockCount","createMockTransport","initialState","options","ephemeral","ephemeralLanes","map","child","finished","clients","Map","Record","streams","Set","stream","send","admit","id","frame","rec","get","statement","cmd","push","fakeFetch","input","RequestInfo","URL","init","RequestInit","Response","TypeError","url","String","headers","Headers","endsWith","has","set","lane","JSON","parse","body","handle","ops","op","main","value","syn","version","protocols","name","record","const","signal","undefined","json","status","fetch","next","path","type","fin","close","size"],"sources":["../../src/transports/testing.ts"],"sourcesContent":["import type { ResourceElement } from \"@assistant-ui/tap\";\nimport type { StatewireEnvelope } from \"../client/connection\";\nimport { StatewireHttp } from \"./sse\";\nimport type { StatewireFrame } from \"./sse\";\nimport type { Statewire } from \"../types\";\nimport { openSSEStream, sseFrame } from \"./fake-sse\";\nimport type { FakeStreamHandle } from \"./fake-sse\";\nimport { WIRE_VERSION } from \"../wire/protocol\";\n\nexport * from \"./fixtures\";\n\n/** Let queued microtasks and op batches drain through the statewire client. */\nexport const tick = () => new Promise<void>((r) => setTimeout(r, 0));\n\nexport { applyOptimistic } from \"../util/recordMutations\";\n\nexport type MockTransport<TState> = {\n /** Pass as the `transport:` option of useStatewire / Harness. */\n transport: ResourceElement<Statewire<TState | undefined, any>>;\n /** Captured statements in arrival order. */\n commands: { protocol: string; method: string; params: unknown[] }[];\n /** Replace the replicated state and broadcast it as a root snapshot op. */\n setState: (state: TState) => Promise<void>;\n /** Deliver a bare ack cmd packet on the stream. */\n applied: (seq: number) => Promise<void>;\n /** Deliver a terminal result cmd packet on the stream. */\n settle: (seq: number, payload?: unknown) => Promise<void>;\n /** Deliver an arbitrary envelope on the stream. */\n sendEnvelope: (envelope: StatewireEnvelope) => Promise<void>;\n /** End the stream with a fin; further attaches fail like a dead server. */\n finish: (reason: string, message?: string) => Promise<void>;\n readonly clientId: string;\n readonly attached: boolean;\n};\n\n/**\n * A mock statewire backend: serves `initialState` as the attach snapshot,\n * records submitted frames (receipt only — acks and results are delivered\n * from test code), and exposes the wire as a transport ResourceElement.\n */\nlet mockCount = 0;\n\nexport const createMockTransport = <TState>(\n initialState: TState,\n protocol = \"default\",\n options: { ephemeral?: readonly string[] } = {},\n): MockTransport<TState> => {\n // Read per attach: a test can grow the lane set between connects.\n const ephemeralLanes = () =>\n (options.ephemeral ?? []).map((child) => `${protocol}/${child}`);\n let state: unknown = initialState;\n let clientId = \"\";\n let finished = false;\n const clients = new Map<string, Record<string, number>>();\n const streams = new Set<FakeStreamHandle>();\n const commands: MockTransport<unknown>[\"commands\"] = [];\n\n const sendEnvelope = (envelope: StatewireEnvelope) => {\n for (const stream of streams) stream.send(sseFrame(envelope));\n return tick();\n };\n\n const admit = (id: string, frame: StatewireFrame) => {\n const rec = clients.get(id)!;\n for (const statement of frame.cmd ?? []) {\n if (statement.seq <= (rec[statement.protocol] ?? 0)) continue;\n rec[statement.protocol] = statement.seq;\n commands.push({\n protocol: statement.protocol,\n method: statement.method,\n params: statement.params,\n });\n }\n };\n\n const fakeFetch = (async (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> => {\n if (finished) throw new TypeError(\"fetch failed (finished)\");\n const url = String(input);\n const headers = new Headers(init?.headers);\n const id = headers.get(\"Statewire-Client-Id\") ?? \"\";\n if (url.endsWith(\"/stream\")) {\n clientId = id;\n if (!clients.has(id)) clients.set(id, {});\n const rec = clients.get(id)!;\n const ephemeral = ephemeralLanes();\n for (const lane of ephemeral) delete rec[lane];\n const frame = JSON.parse(String(init?.body ?? \"{}\")) as StatewireFrame;\n admit(id, frame);\n return openSSEStream(\n streams,\n id,\n (handle) => {\n handle.send(\n sseFrame({\n ops: [{ op: \"mount\", protocol, main: true, value: state }],\n syn: {\n version: WIRE_VERSION,\n protocols: [\n {\n name: protocol,\n version: WIRE_VERSION,\n seq: rec[protocol] ?? 0,\n },\n ...ephemeral.map((name) => ({\n name,\n version: WIRE_VERSION,\n record: \"ephemeral\" as const,\n seq: 0,\n })),\n ],\n },\n }),\n );\n },\n init?.signal ?? undefined,\n { \"Statewire-Lease\": \"mock-lease\" },\n );\n }\n if (url.endsWith(\"/frames\")) {\n admit(clientId, JSON.parse(String(init?.body ?? \"{}\")) as StatewireFrame);\n return Response.json({});\n }\n return new Response(null, { status: 404 });\n }) as typeof fetch;\n\n return {\n transport: StatewireHttp({\n url: `http://mock-${++mockCount}/api`,\n fetch: fakeFetch,\n }),\n commands,\n setState: (next) => {\n state = next;\n return sendEnvelope({ ops: [{ op: \"replace\", path: [], value: next }] });\n },\n applied: (seq) =>\n sendEnvelope({ cmd: [{ protocol, seq, type: \"applied\" }] }),\n settle: (seq, payload) =>\n sendEnvelope({\n cmd: [\n {\n protocol,\n seq,\n type: \"result\",\n ...(payload !== undefined && { payload }),\n },\n ],\n }),\n sendEnvelope,\n finish: async (reason, message) => {\n await sendEnvelope({\n fin: { reason, ...(message !== undefined && { message }) },\n });\n finished = true;\n for (const stream of [...streams]) stream.close();\n await tick();\n },\n get clientId() {\n return clientId;\n },\n get attached() {\n return streams.size > 0;\n },\n };\n};\n"],"mappings":";;;;;;;AAYA,MAAaS,aAAa,IAAIC,SAAeC,MAAMC,WAAWD,GAAG,CAAC,CAAC;;;;;;AA4BnE,IAAIuB,YAAY;AAEhB,MAAaC,uBACXC,cACAlB,WAAW,WACXmB,UAA6C,CAAC,MACpB;CAE1B,MAAME,wBACHF,QAAQC,aAAa,CAAA,EAAA,CAAIE,KAAKC,UAAU,GAAGvB,SAAQ,GAAIuB,OAAO;CACjE,IAAInB,QAAiBc;CACrB,IAAIJ,WAAW;CACf,IAAIU,WAAW;CACf,MAAMC,0BAAU,IAAIC,IAAoC;CACxD,MAAME,0BAAU,IAAIC,IAAsB;CAC1C,MAAM9B,WAA+C,CAAA;CAErD,MAAMU,gBAAgBC,aAAgC;EACpD,KAAK,MAAMoB,UAAUF,SAASE,OAAOC,KAAK3C,SAASsB,QAAQ,CAAC;EAC5D,OAAOnB,KAAK;CACd;CAEA,MAAMyC,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@
|
|
|
1
|
-
{"version":3,"file":"ws.d.ts","names":[],"sources":["../../src/transports/ws.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"ws.d.ts","names":[],"sources":["../../src/transports/ws.ts"],"mappings":";;;;;cA8La,qBACX,cACA,kBAAkB,UAAU,WAAW,UAAU,UAEjD,SAAS,gBAAgB,qBACtB,gBAAgB,UAAU,oBAAoB"}
|
package/dist/transports/ws.js
CHANGED
|
@@ -20,6 +20,7 @@ const socketUrl = async (options, clientId) => {
|
|
|
20
20
|
};
|
|
21
21
|
const runSocket = (c, ws, frame, abort) => new Promise((resolve, reject) => {
|
|
22
22
|
let timer;
|
|
23
|
+
let ping;
|
|
23
24
|
let timedOut = false;
|
|
24
25
|
let outcome = null;
|
|
25
26
|
let closed = false;
|
|
@@ -41,10 +42,12 @@ const runSocket = (c, ws, frame, abort) => new Promise((resolve, reject) => {
|
|
|
41
42
|
return;
|
|
42
43
|
}
|
|
43
44
|
ws.send(frame);
|
|
45
|
+
ping = setInterval(() => ws.send("{}"), timing.pingIntervalMs);
|
|
44
46
|
};
|
|
45
47
|
const stale = () => Boolean(outcome) || closed || abort.signal.aborted;
|
|
46
48
|
const fail = (error) => {
|
|
47
49
|
clearTimeout(timer);
|
|
50
|
+
clearInterval(ping);
|
|
48
51
|
reject(error instanceof Error ? error : /* @__PURE__ */ new Error("protocol"));
|
|
49
52
|
ws.close();
|
|
50
53
|
};
|
|
@@ -72,6 +75,7 @@ const runSocket = (c, ws, frame, abort) => new Promise((resolve, reject) => {
|
|
|
72
75
|
ws.onclose = () => {
|
|
73
76
|
closed = true;
|
|
74
77
|
clearTimeout(timer);
|
|
78
|
+
clearInterval(ping);
|
|
75
79
|
if (outcome) resolve(outcome);
|
|
76
80
|
else reject(/* @__PURE__ */ new Error(timedOut ? "timeout" : "network"));
|
|
77
81
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws.js","names":["useState","flushTapSync","resource","ResourceElement","timing","attachFrame","handleEnvelope","unsupportedVersion","useLink","AttemptOutcome","Conn","StatewireEnvelope","Statewire","StatewireClient","useStatewireRemote","CLIENT_QUERY_PARAM","WS_SUBPROTOCOL","RemoteLink","StatewireFrame","StatewireSubmitResult","StatewireRemoteProps","WsChannel","socket","WebSocket","OPEN","socketUrl","options","WebsocketOptions","clientId","url","query","params","URLSearchParams","set","resolved","URL","globalThis","location","href","protocol","replace","pathname","search","toString","runSocket","c","ws","frame","abort","AbortController","Promise","resolve","reject","timer","ReturnType","setTimeout","timedOut","outcome","closed","arm","clearTimeout","close","heartbeatTimeoutMs","signal","addEventListener","onerror","onopen","negotiated","trim","send","stale","Boolean","aborted","fail","error","Error","onmessage","event","env","JSON","parse","String","data","SyntaxError","Object","keys","length","done","onclose","attemptSocket","w","ctx","body","headers","throwIfAborted","protocols","webSocket","WS","extra","undefined","sendFrame","readyState","stringify","type","useWsLink","$","_c","_temp","t0","t1","_","t2","attemptOnce","useStatewireWebsocket","StatewireWebsocket","Commands","TState","TCommands"],"sources":["../../src/transports/ws.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { flushTapSync, resource } from \"@assistant-ui/tap\";\nimport type { ResourceElement } from \"@assistant-ui/tap\";\nimport { timing } from \"../client/timing\";\nimport {\n attachFrame,\n handleEnvelope,\n unsupportedVersion,\n useLink,\n} from \"../client/connection\";\nimport type {\n AttemptOutcome,\n Conn,\n StatewireEnvelope,\n} from \"../client/connection\";\nimport type { Statewire } from \"../types\";\nimport type { StatewireClient } from \"../statewire\";\nimport { useStatewireRemote } from \"./remote\";\nimport { CLIENT_QUERY_PARAM } from \"../host/adapter\";\nimport { WS_SUBPROTOCOL } from \"../wire/version\";\nimport type {\n RemoteLink,\n StatewireFrame,\n StatewireSubmitResult,\n StatewireRemoteProps,\n} from \"./sse\";\n\ntype WsChannel = {\n socket: WebSocket | null;\n};\n\nconst OPEN = 1;\n\nexport { WS_SUBPROTOCOL };\n\nconst socketUrl = async (\n options: StatewireClient.WebsocketOptions,\n clientId: string,\n) => {\n const { url, query } = options;\n const params = new URLSearchParams(\n typeof query === \"function\" ? await query() : query,\n );\n params.set(CLIENT_QUERY_PARAM, clientId);\n const resolved = new URL(url, globalThis.location?.href);\n resolved.protocol = resolved.protocol.replace(/^http/, \"ws\");\n resolved.pathname = `${resolved.pathname.replace(/\\/$/, \"\")}/ws`;\n resolved.search = params.toString();\n return resolved.href;\n};\n\nconst runSocket = (\n c: Conn,\n ws: WebSocket,\n frame: string,\n abort: AbortController,\n): Promise<AttemptOutcome> =>\n new Promise((resolve, reject) => {\n let timer: ReturnType<typeof setTimeout> | undefined;\n let timedOut = false;\n let outcome: AttemptOutcome | null = null;\n let closed = false;\n const arm = () => {\n clearTimeout(timer);\n timer = setTimeout(() => {\n timedOut = true;\n ws.close();\n }, timing.heartbeatTimeoutMs);\n };\n arm();\n abort.signal.addEventListener(\"abort\", () => ws.close());\n ws.onerror = () => {};\n ws.onopen = () => {\n const negotiated = ws.protocol.trim();\n if (negotiated !== WS_SUBPROTOCOL) {\n outcome = unsupportedVersion(c, negotiated);\n ws.close();\n return;\n }\n ws.send(frame);\n };\n const stale = () => Boolean(outcome) || closed || abort.signal.aborted;\n const fail = (error: unknown) => {\n clearTimeout(timer);\n reject(error instanceof Error ? error : new Error(\"protocol\"));\n ws.close();\n };\n ws.onmessage = (event) => {\n if (stale()) return;\n arm();\n let env: StatewireEnvelope;\n try {\n env = JSON.parse(String(event.data)) as StatewireEnvelope;\n } catch {\n fail(new SyntaxError(\"invalid JSON packet\"));\n return;\n }\n // {} heartbeats live below the packet layer: invisible to handlers.\n if (\n typeof env === \"object\" &&\n env !== null &&\n Object.keys(env).length === 0\n ) {\n return;\n }\n try {\n const done = flushTapSync(() => handleEnvelope(c, env));\n if (done) {\n outcome = done;\n ws.close();\n }\n } catch (error) {\n fail(error);\n }\n };\n ws.onclose = () => {\n closed = true;\n clearTimeout(timer);\n if (outcome) resolve(outcome);\n else reject(new Error(timedOut ? \"timeout\" : \"network\"));\n };\n });\n\nconst attemptSocket = async (\n c: Conn,\n w: WsChannel,\n options: StatewireClient.WebsocketOptions,\n abort: AbortController,\n): Promise<AttemptOutcome> => {\n let ws: WebSocket | null = null;\n try {\n const url = await socketUrl(options, c.ctx.clientId);\n const { body } = await attachFrame(c, options.headers);\n abort.signal.throwIfAborted();\n const { protocols, webSocket } = options;\n const WS = webSocket ?? globalThis.WebSocket;\n if (!WS) throw new Error(\"no WebSocket implementation\");\n const extra =\n protocols === undefined\n ? []\n : typeof protocols === \"string\"\n ? [protocols]\n : protocols;\n ws = new WS(url, [WS_SUBPROTOCOL, ...extra]);\n w.socket = ws;\n return await runSocket(c, ws, body, abort);\n } finally {\n if (w.socket === ws) w.socket = null;\n }\n};\n\nconst sendFrame = (\n w: WsChannel,\n frame: StatewireFrame,\n): Promise<StatewireSubmitResult> => {\n const ws = w.socket;\n if (!ws || ws.readyState !== OPEN) {\n return Promise.reject(new Error(\"network\"));\n }\n try {\n ws.send(JSON.stringify(frame));\n } catch {\n return Promise.reject(new Error(\"network\"));\n }\n return Promise.resolve({ type: \"ok\" });\n};\n\nconst useWsLink = (\n options: StatewireClient.WebsocketOptions,\n ctx: StatewireRemoteProps,\n): RemoteLink => {\n const [w] = useState((): WsChannel => ({ socket: null }));\n return useLink({\n ...options,\n ctx,\n attemptOnce: (c, abort) => attemptSocket(c, w, options, abort),\n send: (_, frame) => sendFrame(w, frame),\n });\n};\n\nconst useStatewireWebsocket = (\n options: StatewireClient.WebsocketOptions,\n): Statewire<any, any> => useStatewireRemote(options, useWsLink);\n\nexport const StatewireWebsocket = resource(useStatewireWebsocket) as <\n TState = any,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.WebsocketOptions,\n) => ResourceElement<Statewire<TState | undefined, TCommands>>;\n"],"mappings":";;;;;;;;;AA+BA,MAAMwB,OAAO;AAIb,MAAMC,YAAY,OAChBC,SACAE,aACG;CACH,MAAM,EAAEC,KAAKC,UAAUJ;CACvB,MAAMK,SAA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BZ,mBAAmBY,SAAS8E,SAAS;AAE/D,MAAaU,qBAAqBhH,SAAS+G,qBAAqB;AAjB9C,SAAAN,QAAA;CAAA,OAIuB,EAAArF,QAAU,KAAK;AAAC"}
|
|
1
|
+
{"version":3,"file":"ws.js","names":["useState","flushTapSync","resource","ResourceElement","timing","attachFrame","handleEnvelope","unsupportedVersion","useLink","AttemptOutcome","Conn","StatewireEnvelope","Statewire","StatewireClient","useStatewireRemote","CLIENT_QUERY_PARAM","WS_SUBPROTOCOL","RemoteLink","StatewireFrame","StatewireSubmitResult","StatewireRemoteProps","WsChannel","socket","WebSocket","OPEN","socketUrl","options","WebsocketOptions","clientId","url","query","params","URLSearchParams","set","resolved","URL","globalThis","location","href","protocol","replace","pathname","search","toString","runSocket","c","ws","frame","abort","AbortController","Promise","resolve","reject","timer","ReturnType","setTimeout","ping","setInterval","timedOut","outcome","closed","arm","clearTimeout","close","heartbeatTimeoutMs","signal","addEventListener","onerror","onopen","negotiated","trim","send","pingIntervalMs","stale","Boolean","aborted","fail","error","clearInterval","Error","onmessage","event","env","JSON","parse","String","data","SyntaxError","Object","keys","length","done","onclose","attemptSocket","w","ctx","body","headers","throwIfAborted","protocols","webSocket","WS","extra","undefined","sendFrame","readyState","stringify","type","useWsLink","$","_c","_temp","t0","t1","_","t2","attemptOnce","useStatewireWebsocket","StatewireWebsocket","Commands","TState","TCommands"],"sources":["../../src/transports/ws.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { flushTapSync, resource } from \"@assistant-ui/tap\";\nimport type { ResourceElement } from \"@assistant-ui/tap\";\nimport { timing } from \"../client/timing\";\nimport {\n attachFrame,\n handleEnvelope,\n unsupportedVersion,\n useLink,\n} from \"../client/connection\";\nimport type {\n AttemptOutcome,\n Conn,\n StatewireEnvelope,\n} from \"../client/connection\";\nimport type { Statewire } from \"../types\";\nimport type { StatewireClient } from \"../statewire\";\nimport { useStatewireRemote } from \"./remote\";\nimport { CLIENT_QUERY_PARAM } from \"../host/adapter\";\nimport { WS_SUBPROTOCOL } from \"../wire/version\";\nimport type {\n RemoteLink,\n StatewireFrame,\n StatewireSubmitResult,\n StatewireRemoteProps,\n} from \"./sse\";\n\ntype WsChannel = {\n socket: WebSocket | null;\n};\n\nconst OPEN = 1;\n\nexport { WS_SUBPROTOCOL };\n\nconst socketUrl = async (\n options: StatewireClient.WebsocketOptions,\n clientId: string,\n) => {\n const { url, query } = options;\n const params = new URLSearchParams(\n typeof query === \"function\" ? await query() : query,\n );\n params.set(CLIENT_QUERY_PARAM, clientId);\n const resolved = new URL(url, globalThis.location?.href);\n resolved.protocol = resolved.protocol.replace(/^http/, \"ws\");\n resolved.pathname = `${resolved.pathname.replace(/\\/$/, \"\")}/ws`;\n resolved.search = params.toString();\n return resolved.href;\n};\n\nconst runSocket = (\n c: Conn,\n ws: WebSocket,\n frame: string,\n abort: AbortController,\n): Promise<AttemptOutcome> =>\n new Promise((resolve, reject) => {\n let timer: ReturnType<typeof setTimeout> | undefined;\n let ping: ReturnType<typeof setInterval> | undefined;\n let timedOut = false;\n let outcome: AttemptOutcome | null = null;\n let closed = false;\n const arm = () => {\n clearTimeout(timer);\n timer = setTimeout(() => {\n timedOut = true;\n ws.close();\n }, timing.heartbeatTimeoutMs);\n };\n arm();\n abort.signal.addEventListener(\"abort\", () => ws.close());\n ws.onerror = () => {};\n ws.onopen = () => {\n const negotiated = ws.protocol.trim();\n if (negotiated !== WS_SUBPROTOCOL) {\n outcome = unsupportedVersion(c, negotiated);\n ws.close();\n return;\n }\n ws.send(frame);\n // A ping keeps a host that cannot run timers (a hibernating durable\n // object answers it without waking) talking before the silence timeout.\n ping = setInterval(() => ws.send(\"{}\"), timing.pingIntervalMs);\n };\n const stale = () => Boolean(outcome) || closed || abort.signal.aborted;\n const fail = (error: unknown) => {\n clearTimeout(timer);\n clearInterval(ping);\n reject(error instanceof Error ? error : new Error(\"protocol\"));\n ws.close();\n };\n ws.onmessage = (event) => {\n if (stale()) return;\n arm();\n let env: StatewireEnvelope;\n try {\n env = JSON.parse(String(event.data)) as StatewireEnvelope;\n } catch {\n fail(new SyntaxError(\"invalid JSON packet\"));\n return;\n }\n // {} heartbeats live below the packet layer: invisible to handlers.\n if (\n typeof env === \"object\" &&\n env !== null &&\n Object.keys(env).length === 0\n ) {\n return;\n }\n try {\n const done = flushTapSync(() => handleEnvelope(c, env));\n if (done) {\n outcome = done;\n ws.close();\n }\n } catch (error) {\n fail(error);\n }\n };\n ws.onclose = () => {\n closed = true;\n clearTimeout(timer);\n clearInterval(ping);\n if (outcome) resolve(outcome);\n else reject(new Error(timedOut ? \"timeout\" : \"network\"));\n };\n });\n\nconst attemptSocket = async (\n c: Conn,\n w: WsChannel,\n options: StatewireClient.WebsocketOptions,\n abort: AbortController,\n): Promise<AttemptOutcome> => {\n let ws: WebSocket | null = null;\n try {\n const url = await socketUrl(options, c.ctx.clientId);\n const { body } = await attachFrame(c, options.headers);\n abort.signal.throwIfAborted();\n const { protocols, webSocket } = options;\n const WS = webSocket ?? globalThis.WebSocket;\n if (!WS) throw new Error(\"no WebSocket implementation\");\n const extra =\n protocols === undefined\n ? []\n : typeof protocols === \"string\"\n ? [protocols]\n : protocols;\n ws = new WS(url, [WS_SUBPROTOCOL, ...extra]);\n w.socket = ws;\n return await runSocket(c, ws, body, abort);\n } finally {\n if (w.socket === ws) w.socket = null;\n }\n};\n\nconst sendFrame = (\n w: WsChannel,\n frame: StatewireFrame,\n): Promise<StatewireSubmitResult> => {\n const ws = w.socket;\n if (!ws || ws.readyState !== OPEN) {\n return Promise.reject(new Error(\"network\"));\n }\n try {\n ws.send(JSON.stringify(frame));\n } catch {\n return Promise.reject(new Error(\"network\"));\n }\n return Promise.resolve({ type: \"ok\" });\n};\n\nconst useWsLink = (\n options: StatewireClient.WebsocketOptions,\n ctx: StatewireRemoteProps,\n): RemoteLink => {\n const [w] = useState((): WsChannel => ({ socket: null }));\n return useLink({\n ...options,\n ctx,\n attemptOnce: (c, abort) => attemptSocket(c, w, options, abort),\n send: (_, frame) => sendFrame(w, frame),\n });\n};\n\nconst useStatewireWebsocket = (\n options: StatewireClient.WebsocketOptions,\n): Statewire<any, any> => useStatewireRemote(options, useWsLink);\n\nexport const StatewireWebsocket = resource(useStatewireWebsocket) as <\n TState = any,\n TCommands extends Statewire.Commands = Statewire.Commands,\n>(\n options: StatewireClient.WebsocketOptions,\n) => ResourceElement<Statewire<TState | undefined, TCommands>>;\n"],"mappings":";;;;;;;;;AA+BA,MAAMwB,OAAO;AAIb,MAAMC,YAAY,OAChBC,SACAE,aACG;CACH,MAAM,EAAEC,KAAKC,UAAUJ;CACvB,MAAMK,SAAS,IAAIC,gBACjB,OAAOF,UAAU,aAAa,MAAMA,MAAM,IAAIA,KAChD;CACAC,OAAOE,IAAIlB,oBAAoBa,QAAQ;CACvC,MAAMM,WAAW,IAAIC,IAAIN,KAAKO,WAAWC,UAAUC,IAAI;CACvDJ,SAASK,WAAWL,SAASK,SAASC,QAAQ,SAAS,IAAI;CAC3DN,SAASO,WAAW,GAAGP,SAASO,SAASD,QAAQ,OAAO,EAAE,EAAC;CAC3DN,SAASQ,SAASX,OAAOY,SAAS;CAClC,OAAOT,SAASI;AAClB;AAEA,MAAMM,aACJC,GACAC,IACAC,OACAC,UAEA,IAAIE,SAASC,SAASC,WAAW;CAC/B,IAAIC;CACJ,IAAIG;CACJ,IAAIE,WAAW;CACf,IAAIC,UAAiC;CACrC,IAAIC,SAAS;CACb,MAAMC,YAAY;EAChBC,aAAaT,KAAK;EAClBA,QAAQE,iBAAiB;GACvBG,WAAW;GACXZ,GAAGiB,MAAM;EACX,GAAG3D,OAAO4D,kBAAkB;CAC9B;CACAH,IAAI;CACJb,MAAMiB,OAAOC,iBAAiB,eAAepB,GAAGiB,MAAM,CAAC;CACvDjB,GAAGqB,gBAAgB,CAAC;CACpBrB,GAAGsB,eAAe;EAChB,MAAMC,aAAavB,GAAGP,SAAS+B,KAAK;EACpC,IAAID,eAAAA,gBAA+B;GACjCV,UAAUpD,mBAAmBsC,G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AA,OAAAf,GAAA;EACxDmB,MAAAC,GAAAnE,UAAcyD,UAAUV,GAAG/C,KAAK;EAAC8D,EAAA,KAAAf;EAAAe,EAAA,KAAAI;CAAA,OAAAA,KAAAJ,EAAA;CAAA,IAAAM;CAAA,IAAAN,EAAA,OAAAd,OAAAc,EAAA,OAAAnF,WAAAmF,EAAA,OAAAG,MAAAH,EAAA,OAAAI,IAAA;EAJ1BE,KAAA;GAAA,GACVzF;GAAOqE;GAAAqB,aAEGJ;GAAiDzC,MACxD0C;EACR;EAACJ,EAAA,KAAAd;EAAAc,EAAA,KAAAnF;EAAAmF,EAAA,KAAAG;EAAAH,EAAA,KAAAI;EAAAJ,EAAA,KAAAM;CAAA,OAAAA,KAAAN,EAAA;CAAA,OALMrG,QAAQ2G,EAKd;AAAC;AAGJ,MAAME,yBACJ3F,YACwBZ,mBAAmBY,SAASkF,SAAS;AAE/D,MAAaU,qBAAqBpH,SAASmH,qBAAqB;AAjB9C,SAAAN,QAAA;CAAA,OAIuB,EAAAzF,QAAU,KAAK;AAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -58,6 +58,8 @@ interface Statewire<TState = any, TCommands extends Statewire.Commands = Statewi
|
|
|
58
58
|
work?: {
|
|
59
59
|
readonly isIdle: boolean;
|
|
60
60
|
};
|
|
61
|
+
/** Called by the serving host once an attach is registered, before its hello: documents mounted here ride the hello. */
|
|
62
|
+
attaching?: (attach: StatewireHost.Attach) => void;
|
|
61
63
|
/** Called by the serving host when an attach ends; its ephemeral documents and includes are gone. */
|
|
62
64
|
detached?: (attach: StatewireHost.Attach) => void;
|
|
63
65
|
/** The retained failed-send lane; present on transports with a durable session. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;kBAMiB;OACH,WAAW,mBAAmB;OAE9B,QAAQ,kBAAkB,WAAW,eAC9C,WAAW;IAAc,QAAQ;IAAG,Q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GJ;IACN;IAAe,MAAM;IAAe;;IACpC;IAAW,MAAM;IAAe;;IAChC;IAAc,MAAM;;OAEd;IACV,YAAY;IACZ,eAAe;;;UAIF,UACf,cACA,kBAAkB,UAAU,WAAW,UAAU;EAEjD,OAAO;EACP,YAAY,mBAAmB;EAC/B,YAAY,kBAAkB;EAC9B,UACE,kBACA,gBACA,+BACG;EACL,UAAU,UAAU,cAAc;EAClC,aAAa,gBAAgB;EAC7B,SAAS,UAAU;;EAEnB;EACA;aAAkB;;;EAElB,aAAa,QAAQ,cAAc;;EAEnC,YAAY,QAAQ,cAAc;;EAElC;IACE,qBAAqB,gBAAgB;IACrC,QAAQ,OAAO,gBAAgB,kBAAkB;IACjD,UAAU,OAAO,gBAAgB;;;cAuBxB,2BAA2B;WAC7B,SAAS,gBAAgB;EAEtB,YAAA,SAAS,gBAAgB"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":["StatewireDocuments","StatewireHost","StatewireProtocol","StatewireClient","StatewirePath","Statewire","Commands","Record","args","Command","TCommands","method","K","params","Parameters","CommandsProxy","Promise","Awaited","ReturnType","CommandUpdate","protocol","seq","status","result","failure","SendFailure","StateSource","snapshot","takeChanged","setOnChange","listener","Op","op","path","value","Changes","isChangedAt","getChangedKeys","state","TState","documents","Source","selection","Selection","invoke","commands","connection","Connection","source","isBusy","work","isIdle","
|
|
1
|
+
{"version":3,"file":"types.js","names":["StatewireDocuments","StatewireHost","StatewireProtocol","StatewireClient","StatewirePath","Statewire","Commands","Record","args","Command","TCommands","method","K","params","Parameters","CommandsProxy","Promise","Awaited","ReturnType","CommandUpdate","protocol","seq","status","result","failure","SendFailure","StateSource","snapshot","takeChanged","setOnChange","listener","Op","op","path","value","Changes","isChangedAt","getChangedKeys","state","TState","documents","Source","selection","Selection","invoke","commands","connection","Connection","source","isBusy","work","isIdle","attaching","attach","Attach","detached","failures","list","FailedCommand","retry","entry","discard","describe","fate","code","message","cause","StatewireSendError","Error","constructor","name"],"sources":["../src/types.ts"],"sourcesContent":["import type { StatewireDocuments } from \"./wire/documents\";\nimport type { StatewireHost } from \"./host\";\nimport type { StatewireProtocol } from \"./wire/protocol\";\nimport type { StatewireClient } from \"./statewire\";\nimport type { StatewirePath } from \"./wire/ops\";\n\nexport namespace Statewire {\n export type Commands = Record<string, (...args: never[]) => unknown>;\n\n export type Command<TCommands extends Commands = Commands> = {\n [K in keyof TCommands]: { method: K; params: Parameters<TCommands[K]> };\n }[keyof TCommands];\n\n export type CommandsProxy<TCommands extends Commands> = {\n [K in keyof TCommands]: (\n ...args: Parameters<TCommands[K]>\n ) => Promise<Awaited<ReturnType<TCommands[K]>>>;\n };\n\n export type CommandUpdate<TCommands extends Commands = Commands> = {\n [K in keyof TCommands]: {\n readonly protocol?: string;\n method: K;\n params: Parameters<TCommands[K]>;\n seq: number;\n status: \"queued\" | \"posted\" | \"applied\" | \"settled\";\n result?: unknown;\n failure?: StatewireClient.SendFailure;\n };\n }[keyof TCommands];\n\n /** Push-based canonical state feed a statewire hands to the client core. */\n export type StateSource = {\n snapshot: () => unknown;\n /** Drain the paths changed since the last take (`[]` entry = whole state). */\n takeChanged: () => StatewirePath[];\n /** Synchronous, per change; the consumer owns coalescing. */\n setOnChange: (listener: (() => void) | null) => void;\n };\n\n export type Op =\n | { op: \"replace\"; path: StatewirePath; value: unknown }\n | { op: \"add\"; path: StatewirePath; value: unknown }\n | { op: \"remove\"; path: StatewirePath };\n\n export type Changes = {\n isChangedAt(path: readonly (string | number)[]): boolean;\n getChangedKeys(path: readonly (string | number)[]): string[];\n };\n}\n\nexport interface Statewire<\n TState = any,\n TCommands extends Statewire.Commands = Statewire.Commands,\n> {\n state: TState;\n documents?: StatewireDocuments.Source;\n selection?: StatewireProtocol.Selection | undefined;\n invoke?: (\n protocol: string,\n method: string,\n params: readonly unknown[],\n ) => Promise<unknown>;\n commands: Statewire.CommandsProxy<TCommands>;\n connection?: StatewireClient.Connection;\n source?: Statewire.StateSource;\n /** Negation of the server's broadcast idle level; absent means always busy. */\n isBusy?: boolean;\n work?: { readonly isIdle: boolean };\n /** Called by the serving host once an attach is registered, before its hello: documents mounted here ride the hello. */\n attaching?: (attach: StatewireHost.Attach) => void;\n /** Called by the serving host when an attach ends; its ephemeral documents and includes are gone. */\n detached?: (attach: StatewireHost.Attach) => void;\n /** The retained failed-send lane; present on transports with a durable session. */\n failures?: {\n list: () => readonly StatewireClient.FailedCommand[];\n retry: (entry: StatewireClient.FailedCommand) => Promise<unknown>;\n discard: (entry: StatewireClient.FailedCommand) => void;\n };\n}\n\nconst describe = (failure: StatewireClient.SendFailure): string => {\n switch (failure.fate) {\n case \"rejected\":\n return `statewire: command rejected${failure.code ? ` (${failure.code})` : \"\"}${failure.message ? `: ${failure.message}` : \"\"}`;\n case \"aborted\":\n return `statewire: command aborted (${failure.cause})`;\n case \"lost\":\n return `statewire: command lost (${failure.cause})`;\n case \"failed\":\n return failure.message\n ? `statewire: command failed: ${failure.message}`\n : \"statewire: command failed\";\n case \"crashed\":\n return \"statewire: command crashed\";\n case \"expired\":\n return `statewire: command result expired (${failure.cause})`;\n }\n};\n\nexport class StatewireSendError extends Error {\n readonly failure: StatewireClient.SendFailure;\n\n constructor(failure: StatewireClient.SendFailure) {\n super(describe(failure));\n this.name = \"StatewireSendError\";\n this.failure = failure;\n }\n}\n"],"mappings":";AAiFA,MAAM8D,YAAYtC,YAAiD;CACjE,QAAQA,QAAQuC,MAAhB;EACE,KAAK,YACH,OAAO,8BAA8BvC,QAAQwC,OAAO,KAAKxC,QAAQwC,KAAI,KAAM,KAAKxC,QAAQyC,UAAU,KAAKzC,QAAQyC,YAAY;EAC7H,KAAK,WACH,OAAO,+BAA+BzC,QAAQ0C,MAAK;EACrD,KAAK,QACH,OAAO,4BAA4B1C,QAAQ0C,MAAK;EAClD,KAAK,UACH,OAAO1C,QAAQyC,UACX,8BAA8BzC,QAAQyC,YACtC;EACN,KAAK,WACH,OAAO;EACT,KAAK,WACH,OAAO,sCAAsCzC,QAAQ0C,MAAK;CAC9D;AACF;AAEA,IAAaC,qBAAb,cAAwCC,MAAM;CAC5C;CAEAC,YAAY7C,SAAsC;EAChD,MAAMsC,SAAStC,OAAO,CAAC;EACvB,KAAK8C,OAAO;EACZ,KAAK9C,UAAUA;CACjB;AACF"}
|
package/package.json
CHANGED
package/src/client/connection.ts
CHANGED
|
@@ -341,6 +341,11 @@ const helloSynOf = (env: StatewireEnvelope): StatewireSyn => {
|
|
|
341
341
|
return env.syn as StatewireSyn;
|
|
342
342
|
};
|
|
343
343
|
|
|
344
|
+
// A host that woke on a retained socket re-sends its hello; the snapshot
|
|
345
|
+
// replaces the session the way a fresh attach would.
|
|
346
|
+
const isWakeHello = (env: StatewireEnvelope) =>
|
|
347
|
+
isObject(env.syn) && env.syn["version"] !== undefined;
|
|
348
|
+
|
|
344
349
|
const handleHello = (c: Conn, env: StatewireEnvelope) => {
|
|
345
350
|
const syn = helloSynOf(env);
|
|
346
351
|
if (c.negotiation !== undefined) validateSelection(syn, c.negotiation);
|
|
@@ -470,7 +475,7 @@ export const handleEnvelope = (
|
|
|
470
475
|
value: unknown,
|
|
471
476
|
): AttemptOutcome | null => {
|
|
472
477
|
const env = validateEnvelope(value);
|
|
473
|
-
if (c.awaitingHello) {
|
|
478
|
+
if (c.awaitingHello || isWakeHello(env)) {
|
|
474
479
|
if (env.fin !== undefined) return settleFin(c, env.fin);
|
|
475
480
|
handleHello(c, env);
|
|
476
481
|
return null;
|
package/src/client/core.ts
CHANGED
|
@@ -48,6 +48,7 @@ type Core = {
|
|
|
48
48
|
stale: boolean;
|
|
49
49
|
everAttached: boolean;
|
|
50
50
|
overlaysDirty: boolean;
|
|
51
|
+
materializeKey: unknown;
|
|
51
52
|
flushing: boolean;
|
|
52
53
|
scheduled: boolean;
|
|
53
54
|
listeners: Set<() => void>;
|
|
@@ -56,6 +57,7 @@ type Core = {
|
|
|
56
57
|
getState: () => unknown;
|
|
57
58
|
getBusy: () => boolean;
|
|
58
59
|
getFailed: () => readonly StatewireClient.FailedCommand[];
|
|
60
|
+
rematerialize: () => void;
|
|
59
61
|
commands: Statewire.CommandsProxy<any>;
|
|
60
62
|
subscribe: (listener: () => void) => () => void;
|
|
61
63
|
};
|
|
@@ -105,14 +107,17 @@ const dropCovered = (c: Core) => {
|
|
|
105
107
|
};
|
|
106
108
|
|
|
107
109
|
const recompute = (c: Core) => {
|
|
108
|
-
if (!c.
|
|
110
|
+
if (!c.value) return;
|
|
111
|
+
const { optimistic, materialize, materializeKey } = c.getOptions();
|
|
112
|
+
const rekeyed = !Object.is(materializeKey, c.materializeKey);
|
|
113
|
+
if (!c.stale && !rekeyed) return;
|
|
109
114
|
c.stale = false;
|
|
110
115
|
const prevCanonical = c.canonical;
|
|
111
116
|
drainCanonical(c);
|
|
112
117
|
if (c.canonical !== prevCanonical) dropCovered(c);
|
|
113
|
-
if (c.canonical === prevCanonical && !c.overlaysDirty) return;
|
|
118
|
+
if (c.canonical === prevCanonical && !c.overlaysDirty && !rekeyed) return;
|
|
114
119
|
c.overlaysDirty = false;
|
|
115
|
-
|
|
120
|
+
c.materializeKey = materializeKey;
|
|
116
121
|
let next = c.canonical;
|
|
117
122
|
let paths: readonly StatewirePath[] = [];
|
|
118
123
|
if (optimistic && c.canonical !== undefined) {
|
|
@@ -314,6 +319,7 @@ export const useStatewireCore = (
|
|
|
314
319
|
stale: false,
|
|
315
320
|
everAttached: false,
|
|
316
321
|
overlaysDirty: false,
|
|
322
|
+
materializeKey: undefined,
|
|
317
323
|
flushing: false,
|
|
318
324
|
scheduled: false,
|
|
319
325
|
listeners: new Set(),
|
|
@@ -354,6 +360,7 @@ export const useStatewireCore = (
|
|
|
354
360
|
},
|
|
355
361
|
getBusy: () => c.value?.isBusy ?? true,
|
|
356
362
|
getFailed: () => c.notifiedFailed,
|
|
363
|
+
rematerialize: () => flush(c),
|
|
357
364
|
commands: createMethodProxy<Statewire.CommandsProxy<any>>(
|
|
358
365
|
(method, params) => {
|
|
359
366
|
if (!c.value) {
|