@steve31415/baselib 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/app-update/index.d.ts +2 -0
- package/dist/app-update/index.js +2 -0
- package/dist/app-update/retained-assets.d.ts +21 -0
- package/dist/app-update/retained-assets.js +51 -0
- package/dist/app-update/shell.d.ts +16 -0
- package/dist/app-update/shell.js +35 -0
- package/dist/app-update-browser/build-update.d.ts +52 -0
- package/dist/app-update-browser/build-update.js +161 -0
- package/dist/app-update-browser/index.d.ts +2 -0
- package/dist/app-update-browser/index.js +2 -0
- package/dist/app-update-browser/reload-safety.d.ts +8 -0
- package/dist/app-update-browser/reload-safety.js +23 -0
- package/dist/log-browser.js +3 -1
- package/dist/rum.d.ts +2 -0
- package/dist/rum.js +2 -0
- package/dist/sync/broadcaster.d.ts +9 -0
- package/dist/sync/broadcaster.js +47 -0
- package/dist/sync/engine.d.ts +55 -0
- package/dist/sync/engine.js +197 -0
- package/dist/sync/index.d.ts +5 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/schema.d.ts +13 -0
- package/dist/sync/schema.js +24 -0
- package/dist/sync/session.d.ts +29 -0
- package/dist/sync/session.js +189 -0
- package/dist/sync/types.d.ts +79 -0
- package/dist/sync/types.js +8 -0
- package/dist/sync-browser/client.d.ts +187 -0
- package/dist/sync-browser/client.js +832 -0
- package/dist/sync-browser/index.d.ts +2 -0
- package/dist/sync-browser/index.js +2 -0
- package/dist/sync-browser/outbox.d.ts +47 -0
- package/dist/sync-browser/outbox.js +317 -0
- package/package.json +20 -3
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { Logger as BrowserLogger } from '../log-browser.js';
|
|
2
|
+
import type { ClientEvent, EventBody, SyncEvent } from '../sync/index.js';
|
|
3
|
+
import type { IndexedDbOutbox } from './outbox.js';
|
|
4
|
+
export interface PendingEntry<C extends EventBody, Summary = unknown> {
|
|
5
|
+
event: ClientEvent<C>;
|
|
6
|
+
summary: Summary;
|
|
7
|
+
createdAt: number;
|
|
8
|
+
/** Durable submission order; assigned by the outbox. */
|
|
9
|
+
ordinal?: number;
|
|
10
|
+
/** Runtime-only fields; persistence excludes these fields. */
|
|
11
|
+
status: 'pending' | 'unconfirmed' | 'rejected';
|
|
12
|
+
error?: string;
|
|
13
|
+
recovered?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export type PersistedPendingEntry<C extends EventBody, Summary = unknown> = Pick<PendingEntry<C, Summary>, 'event' | 'summary' | 'createdAt' | 'ordinal'>;
|
|
16
|
+
export interface SyncStoreAdapter<B, C extends EventBody, S extends EventBody> {
|
|
17
|
+
installBootstrap(data: B): void;
|
|
18
|
+
currentSeq(): number;
|
|
19
|
+
setCurrentSeq(seq: number): void;
|
|
20
|
+
applyOptimistic(event: ClientEvent<C>): void;
|
|
21
|
+
restorePending(event: ClientEvent<C>): void;
|
|
22
|
+
applyServerEvent(event: SyncEvent<S>): void;
|
|
23
|
+
rejectPending(eventGuid: string, error: {
|
|
24
|
+
code: string;
|
|
25
|
+
message: string;
|
|
26
|
+
}): void;
|
|
27
|
+
}
|
|
28
|
+
export interface CatchupResult<S extends EventBody> {
|
|
29
|
+
reload?: boolean;
|
|
30
|
+
events: SyncEvent<S>[];
|
|
31
|
+
buildId?: string;
|
|
32
|
+
}
|
|
33
|
+
export interface SyncBrowserRuntime {
|
|
34
|
+
now(): number;
|
|
35
|
+
randomGuid(): string;
|
|
36
|
+
createSocket(url: string): WebSocket;
|
|
37
|
+
sendBeacon(url: string, body: Blob): boolean;
|
|
38
|
+
visibility(): DocumentVisibilityState;
|
|
39
|
+
onVisibilityChange(listener: () => void): () => void;
|
|
40
|
+
onOnline(listener: () => void): () => void;
|
|
41
|
+
onPageHide(listener: () => void): () => void;
|
|
42
|
+
setTimeout: typeof globalThis.setTimeout;
|
|
43
|
+
clearTimeout: typeof globalThis.clearTimeout;
|
|
44
|
+
setInterval: typeof globalThis.setInterval;
|
|
45
|
+
clearInterval: typeof globalThis.clearInterval;
|
|
46
|
+
}
|
|
47
|
+
export type SyncClientState = {
|
|
48
|
+
kind: 'starting';
|
|
49
|
+
pending: number;
|
|
50
|
+
} | {
|
|
51
|
+
kind: 'connecting';
|
|
52
|
+
pending: number;
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'connected';
|
|
55
|
+
pending: 0;
|
|
56
|
+
} | {
|
|
57
|
+
kind: 'syncing';
|
|
58
|
+
pending: number;
|
|
59
|
+
} | {
|
|
60
|
+
kind: 'reconnecting';
|
|
61
|
+
pending: number;
|
|
62
|
+
inSeconds: number;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'disconnected';
|
|
65
|
+
pending: number;
|
|
66
|
+
} | {
|
|
67
|
+
kind: 'error';
|
|
68
|
+
pending: number;
|
|
69
|
+
message: string;
|
|
70
|
+
} | {
|
|
71
|
+
kind: 'expired';
|
|
72
|
+
pending: number;
|
|
73
|
+
};
|
|
74
|
+
export interface SyncClientOptions<B, C extends EventBody, S extends EventBody, Summary = unknown> {
|
|
75
|
+
clientGuid: string;
|
|
76
|
+
outbox: Pick<IndexedDbOutbox<PersistedPendingEntry<C, Summary>>, 'init' | 'add' | 'remove'>;
|
|
77
|
+
store: SyncStoreAdapter<B, C, S>;
|
|
78
|
+
fetchBootstrap(signal: AbortSignal): Promise<B>;
|
|
79
|
+
fetchCatchup(afterSeq: number, signal: AbortSignal): Promise<CatchupResult<S>>;
|
|
80
|
+
beaconUrl: string;
|
|
81
|
+
socketUrl(): string;
|
|
82
|
+
runtime: SyncBrowserRuntime;
|
|
83
|
+
authState: {
|
|
84
|
+
isBlocked(): boolean;
|
|
85
|
+
markBlocked(): void;
|
|
86
|
+
onBlocked(listener: () => void): () => void;
|
|
87
|
+
};
|
|
88
|
+
logger: Pick<BrowserLogger, 'info' | 'warn' | 'error'>;
|
|
89
|
+
onState(state: SyncClientState): void;
|
|
90
|
+
onEventFailed(entry: PendingEntry<C, Summary>, error: string, rejected: boolean): void;
|
|
91
|
+
onEventRecovered?(eventGuid: string): void;
|
|
92
|
+
onSafetyChange?(): void;
|
|
93
|
+
onReloadRequired(reason: 'server' | 'sequence-gap'): void;
|
|
94
|
+
onBuildObserved?(buildId: string): void;
|
|
95
|
+
onBuildChanged?(): void;
|
|
96
|
+
markPerformance?(name: 'outbox-start' | 'outbox-ready' | 'bootstrap-start' | 'bootstrap-response' | 'store-ready'): void;
|
|
97
|
+
reconnectBeforeMs?: number;
|
|
98
|
+
}
|
|
99
|
+
export declare class SyncClient<B, C extends EventBody, S extends EventBody, Summary = unknown> {
|
|
100
|
+
private readonly options;
|
|
101
|
+
private socket;
|
|
102
|
+
private readonly pending;
|
|
103
|
+
private readonly unresolvedFailures;
|
|
104
|
+
private readonly rejected;
|
|
105
|
+
private readonly confirmedPending;
|
|
106
|
+
private readonly pendingRejections;
|
|
107
|
+
private readonly settlementInFlight;
|
|
108
|
+
private readonly settledPending;
|
|
109
|
+
private readonly failureTimers;
|
|
110
|
+
private readonly subscribers;
|
|
111
|
+
private readonly abortControllers;
|
|
112
|
+
private readonly removeListeners;
|
|
113
|
+
private currentState;
|
|
114
|
+
private reconnectAttempts;
|
|
115
|
+
private reconnectTimer;
|
|
116
|
+
private countdownTimer;
|
|
117
|
+
private pingTimer;
|
|
118
|
+
private watchdogTimer;
|
|
119
|
+
private rolloverTimer;
|
|
120
|
+
private escalateTimer;
|
|
121
|
+
private escalateArmedAt;
|
|
122
|
+
private outageStartedAt;
|
|
123
|
+
private outageEscalated;
|
|
124
|
+
private halted;
|
|
125
|
+
private refetchInFlight;
|
|
126
|
+
private probeInFlight;
|
|
127
|
+
private resyncTimes;
|
|
128
|
+
private reconciliationTail;
|
|
129
|
+
private initialization;
|
|
130
|
+
private disposed;
|
|
131
|
+
constructor(options: SyncClientOptions<B, C, S, Summary>);
|
|
132
|
+
init(): Promise<void>;
|
|
133
|
+
private initialize;
|
|
134
|
+
dispose(): void;
|
|
135
|
+
submit(event: ClientEvent<C>, summary: Summary): Promise<void>;
|
|
136
|
+
/** Current-tab entries only; recovered work is retried silently. */
|
|
137
|
+
pendingEntries(): PendingEntry<C, Summary>[];
|
|
138
|
+
failedEntries(): Array<{
|
|
139
|
+
eventGuid: string;
|
|
140
|
+
entry: PendingEntry<C, Summary>;
|
|
141
|
+
error: string;
|
|
142
|
+
}>;
|
|
143
|
+
hasPendingWork(): boolean;
|
|
144
|
+
hasUnsafeWork(): boolean;
|
|
145
|
+
hasUnresolvedFailures(): boolean;
|
|
146
|
+
dismissFailure(eventGuid: string): void;
|
|
147
|
+
refreshConnectionForBuild(): void;
|
|
148
|
+
state(): SyncClientState;
|
|
149
|
+
subscribe(listener: (state: SyncClientState) => void): () => void;
|
|
150
|
+
private installRuntimeListeners;
|
|
151
|
+
private wakeConnection;
|
|
152
|
+
private connect;
|
|
153
|
+
private connectNow;
|
|
154
|
+
private scheduleReconnect;
|
|
155
|
+
private probeSession;
|
|
156
|
+
private startOutage;
|
|
157
|
+
private handleMessage;
|
|
158
|
+
private handleSocketCatchup;
|
|
159
|
+
/** Returns false when a forward gap prevented application. */
|
|
160
|
+
private handleServerEvent;
|
|
161
|
+
private handleRejection;
|
|
162
|
+
private confirmPending;
|
|
163
|
+
private clearFailureTimer;
|
|
164
|
+
private retrySettlement;
|
|
165
|
+
private finishConfirmation;
|
|
166
|
+
private finishRejection;
|
|
167
|
+
private reconcileStore;
|
|
168
|
+
private failUnconfirmed;
|
|
169
|
+
private onSyncEstablished;
|
|
170
|
+
private requestReload;
|
|
171
|
+
private resyncFromServer;
|
|
172
|
+
private sendConnect;
|
|
173
|
+
private sendRaw;
|
|
174
|
+
private trySend;
|
|
175
|
+
private flushViaBeacon;
|
|
176
|
+
private updateConnectedState;
|
|
177
|
+
private updatePendingState;
|
|
178
|
+
private publish;
|
|
179
|
+
private pendingCount;
|
|
180
|
+
private stopBeforeStoreMutation;
|
|
181
|
+
private halt;
|
|
182
|
+
private clearConnectionTimers;
|
|
183
|
+
private clearAllTimers;
|
|
184
|
+
private detachSocket;
|
|
185
|
+
private abortable;
|
|
186
|
+
private abortFetches;
|
|
187
|
+
}
|