@interncom/diplomatic 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/web/index.mjs +1 -1
- package/dist/web/openClient.d.ts +11 -0
- package/dist/web/react/useClient.d.ts +6 -1
- package/dist/web/stores/idb/store.d.ts +4 -1
- package/dist/web/types.d.ts +11 -4
- package/dist/web/worker/client.d.ts +20 -1
- package/dist/web/worker/runtime.d.ts +6 -0
- package/dist/web/worker.mjs +1 -1
- package/package.json +1 -1
package/dist/web/openClient.d.ts
CHANGED
|
@@ -13,6 +13,16 @@ import type { IClient, IStore } from "./types";
|
|
|
13
13
|
* (built as `worker.mjs` in the published package). It must run as a
|
|
14
14
|
* **module** worker (`type: "module"`).
|
|
15
15
|
*
|
|
16
|
+
* ## Handshake (race-safe)
|
|
17
|
+
*
|
|
18
|
+
* The worker posts an unsolicited `{ kind: "ready" }` after init. Apps often
|
|
19
|
+
* construct the Worker at module load (or before `openDiplomaticClient` finishes
|
|
20
|
+
* opening IndexedDB), so that event can fire before the library sets
|
|
21
|
+
* `onmessage`. **That is fine:** connect also probes with a request/response
|
|
22
|
+
* `ping`. The worker holds commands until init completes, so the probe succeeds
|
|
23
|
+
* even if `ready` was dropped. Do **not** reimplement message buffering in the
|
|
24
|
+
* app unless you need it for other reasons.
|
|
25
|
+
*
|
|
16
26
|
* ## Vite (recommended for SPA templates)
|
|
17
27
|
*
|
|
18
28
|
* ```ts
|
|
@@ -23,6 +33,7 @@ import type { IClient, IStore } from "./types";
|
|
|
23
33
|
*
|
|
24
34
|
* `?worker` makes Vite emit a real worker asset and a constructor. Create the
|
|
25
35
|
* instance once (module scope or `useMemo`/`useRef`) — not on every render.
|
|
36
|
+
* Early construction is supported; the handshake recovers if `ready` is missed.
|
|
26
37
|
*
|
|
27
38
|
* ## webpack / Rollup / esbuild / Parcel (`new URL` + import.meta.url)
|
|
28
39
|
*
|
|
@@ -19,11 +19,16 @@ type UseClientBase = {
|
|
|
19
19
|
* import DiplomaticWorker from "@interncom/diplomatic/worker?worker";
|
|
20
20
|
* const syncWorker = new DiplomaticWorker();
|
|
21
21
|
* useClient({ worker: syncWorker, seed, host });
|
|
22
|
+
*
|
|
23
|
+
* Handshake is race-safe (ready event and/or probe ping). See
|
|
24
|
+
* `openDiplomaticClient` for bundler recipes and handshake notes.
|
|
22
25
|
*/
|
|
23
26
|
export type UseClientWorkerOptions = UseClientBase & {
|
|
24
27
|
/**
|
|
25
28
|
* App-constructed sync Worker. Create once (module scope or useMemo/useRef),
|
|
26
|
-
* not each render.
|
|
29
|
+
* not each render. Early construction before `useClient` opens IDB is OK —
|
|
30
|
+
* the library does not require catching the unsolicited `ready` event.
|
|
31
|
+
* See `openDiplomaticClient` for instantiation recipes.
|
|
27
32
|
*/
|
|
28
33
|
worker: Worker;
|
|
29
34
|
store?: never;
|
|
@@ -10,7 +10,10 @@ export declare const HOSTS_TABLE = "hosts";
|
|
|
10
10
|
export declare const UPLOAD_QUEUE_TABLE = "uploadQueue";
|
|
11
11
|
export declare const DOWNLOAD_QUEUE_TABLE = "downloadQueue";
|
|
12
12
|
export declare const MESSAGES_TABLE = "messages";
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Index on messages.apld. Values are "f" (pending) / "t" (applied) —
|
|
15
|
+
* booleans are not valid IndexedDB keys; single-char strings keep keys compact.
|
|
16
|
+
*/
|
|
14
17
|
export declare const MESSAGES_APLD_INDEX = "apld";
|
|
15
18
|
/** Schema version: v3 adds messages.apld index for the apply queue. */
|
|
16
19
|
export declare const DIPLOMATIC_STORE_DB_VERSION = 3;
|
package/dist/web/types.d.ts
CHANGED
|
@@ -77,15 +77,17 @@ export interface IStoredMessageFields {
|
|
|
77
77
|
body?: EncodedMessage;
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
|
-
* What may come back from
|
|
80
|
+
* What may come back from storage (pre-apld rows can omit the field).
|
|
81
|
+
* IDB stores "t"|"f" (booleans are not valid IndexedDB index keys).
|
|
81
82
|
* Prefer {@link normalizeStoredMessageData} before use.
|
|
82
83
|
*/
|
|
83
84
|
export interface IStoredMessageData extends IStoredMessageFields {
|
|
84
|
-
apld?: boolean;
|
|
85
|
+
apld?: boolean | "t" | "f";
|
|
85
86
|
}
|
|
86
87
|
/**
|
|
87
|
-
* Required shape for every put into the message archive.
|
|
88
|
+
* Required shape for every put into the message archive (app/API layer).
|
|
88
89
|
* Callers must set `apld` (false until applied, then true).
|
|
90
|
+
* The IDB adapter persists this as "t"|"f" for indexing.
|
|
89
91
|
*/
|
|
90
92
|
export type IStoredMessageWrite = IStoredMessageFields & {
|
|
91
93
|
apld: boolean;
|
|
@@ -100,7 +102,12 @@ export interface IStoredMessage {
|
|
|
100
102
|
body?: EncodedMessage;
|
|
101
103
|
applied: boolean;
|
|
102
104
|
}
|
|
103
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Coerce stored `apld` to boolean.
|
|
107
|
+
* Applied: true | "t". Pending: false | "f" | missing.
|
|
108
|
+
*/
|
|
109
|
+
export declare function apldFromStored(v: unknown): boolean;
|
|
110
|
+
/** Coerce storage rows to the write shape with boolean apld. */
|
|
104
111
|
export declare function normalizeStoredMessageData(data: IStoredMessageData): IStoredMessageWrite;
|
|
105
112
|
/** Pending apply when not yet marked applied. */
|
|
106
113
|
export declare function isPendingApply(data: IStoredMessageData): boolean;
|
|
@@ -8,11 +8,19 @@ import type { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHost
|
|
|
8
8
|
*
|
|
9
9
|
* Provide a live `Worker` — the library never constructs one. See
|
|
10
10
|
* `openDiplomaticClient` for bundler/CDN instantiation recipes.
|
|
11
|
+
*
|
|
12
|
+
* Handshake is race-safe: the worker posts unsolicited `{ kind: "ready" }`, but
|
|
13
|
+
* connect also probes with `ping`. Early construction (module scope) is fine even
|
|
14
|
+
* if `ready` fired before `onmessage` was set — the ping still succeeds once the
|
|
15
|
+
* worker has finished init (cmds are held until then on the worker side).
|
|
11
16
|
*/
|
|
12
17
|
export type WorkerClientOptions = {
|
|
13
18
|
/** Already-constructed module Worker running `@interncom/diplomatic/worker`. */
|
|
14
19
|
worker: Worker;
|
|
15
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Max wait for handshake (`ready` event or probe ping; default 15s).
|
|
22
|
+
* Failures throw; no fallback.
|
|
23
|
+
*/
|
|
16
24
|
readyTimeoutMs?: number;
|
|
17
25
|
clock?: IClock;
|
|
18
26
|
/**
|
|
@@ -48,8 +56,19 @@ export declare class WorkerClient implements IClient<URL> {
|
|
|
48
56
|
* Attach to an app-provided Worker. `store` is the shared protocol IDB (main
|
|
49
57
|
* connection) used for local msg writes; worker opens its own connection.
|
|
50
58
|
* Throws if the worker never becomes ready — does not fall back to main thread.
|
|
59
|
+
*
|
|
60
|
+
* Handshake: wait for unsolicited `ready` **or** a successful probe `ping`.
|
|
61
|
+
* The probe covers the common case where the app started the Worker early and
|
|
62
|
+
* `ready` was dropped before this thread set `onmessage`.
|
|
51
63
|
*/
|
|
52
64
|
static connect(state: IStateManager, store: IStore<URL>, opts: WorkerClientOptions): Promise<WorkerClient>;
|
|
65
|
+
/** Resolve the ready barrier (idempotent). */
|
|
66
|
+
private markReady;
|
|
67
|
+
/**
|
|
68
|
+
* Active handshake: post ping without waiting for the ready event.
|
|
69
|
+
* On pong, mark ready so connect can proceed even if `ready` was missed.
|
|
70
|
+
*/
|
|
71
|
+
private probeReady;
|
|
53
72
|
/** Lightweight RPC check after connect. */
|
|
54
73
|
ping(): Promise<void>;
|
|
55
74
|
/** Terminate the worker (drops protocol DB connection in that thread). */
|
|
@@ -4,8 +4,14 @@ export type PostFn = (msg: WorkerEvent, transfer?: Transferable[]) => void;
|
|
|
4
4
|
export declare class WorkerRuntime {
|
|
5
5
|
private client;
|
|
6
6
|
private post;
|
|
7
|
+
/** Resolves when init finishes (success or failure). Cmds wait on this. */
|
|
8
|
+
private whenReady;
|
|
9
|
+
private resolveReady;
|
|
10
|
+
private rejectReady;
|
|
7
11
|
constructor(post: PostFn);
|
|
8
12
|
init(): Promise<void>;
|
|
13
|
+
private markReady;
|
|
14
|
+
private failReady;
|
|
9
15
|
private requireClient;
|
|
10
16
|
private emitClientState;
|
|
11
17
|
private emitXferState;
|