@xemahq/ui-kernel 0.11.1 → 0.11.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.
Files changed (234) hide show
  1. package/package.json +1 -2
  2. package/src/index.ts +0 -17
  3. package/src/lib/biome-host/agent-validation.ts +0 -239
  4. package/src/lib/biome-host/biome-builders.ts +0 -112
  5. package/src/lib/biome-host/biome-mode.ts +0 -6
  6. package/src/lib/biome-host/biome-navigation.ts +0 -37
  7. package/src/lib/biome-host/biome-registry.ts +0 -299
  8. package/src/lib/biome-host/biome-scope-hooks.ts +0 -41
  9. package/src/lib/biome-host/biome-scope.tsx +0 -52
  10. package/src/lib/biome-host/biome-scoped-query.ts +0 -134
  11. package/src/lib/biome-host/create-biome-orval-config.ts +0 -82
  12. package/src/lib/biome-host/define-web-biome.spec.ts +0 -221
  13. package/src/lib/biome-host/define-web-biome.ts +0 -222
  14. package/src/lib/biome-host/errors.ts +0 -220
  15. package/src/lib/biome-host/frontend-biome.ts +0 -256
  16. package/src/lib/biome-host/host-bridge.ts +0 -281
  17. package/src/lib/biome-host/host-sources.ts +0 -54
  18. package/src/lib/biome-host/i18n-hooks.ts +0 -51
  19. package/src/lib/biome-host/i18n-port.test.ts +0 -50
  20. package/src/lib/biome-host/i18n-port.ts +0 -96
  21. package/src/lib/biome-host/index.ts +0 -35
  22. package/src/lib/biome-host/realtime-hooks.ts +0 -75
  23. package/src/lib/biome-host/realtime-port.ts +0 -115
  24. package/src/lib/biome-host/session-contributions.ts +0 -296
  25. package/src/lib/biome-host/use-mutation-with-error-toast.ts +0 -90
  26. package/src/lib/biome-host/use-page-state.ts +0 -248
  27. package/src/lib/capabilities/capability-context.ts +0 -25
  28. package/src/lib/capabilities/capability-provider.tsx +0 -73
  29. package/src/lib/capabilities/index.ts +0 -17
  30. package/src/lib/capabilities/types.ts +0 -69
  31. package/src/lib/capabilities/use-capability.ts +0 -79
  32. package/src/lib/contribution-registry/contribution-registry.spec.ts +0 -141
  33. package/src/lib/contribution-registry/contribution-registry.ts +0 -204
  34. package/src/lib/contribution-registry/index.ts +0 -9
  35. package/src/lib/contribution-registry/renderable-registry.ts +0 -142
  36. package/src/lib/system-bus/capability-invoke.ts +0 -92
  37. package/src/lib/system-bus/deeplink.ts +0 -210
  38. package/src/lib/system-bus/enums.ts +0 -86
  39. package/src/lib/system-bus/host-ports.ts +0 -93
  40. package/src/lib/system-bus/index.ts +0 -16
  41. package/src/lib/system-bus/intent-registry.ts +0 -109
  42. package/src/lib/system-bus/intents.ts +0 -113
  43. package/src/lib/system-bus/palette.ts +0 -78
  44. package/src/lib/system-bus/system-bus-builder.ts +0 -159
  45. package/src/lib/system-bus/system-bus.ts +0 -37
  46. package/src/lib/system-bus/windows.ts +0 -51
  47. package/src/offline/id.ts +0 -16
  48. package/src/offline/index.ts +0 -54
  49. package/src/offline/mutation.ts +0 -75
  50. package/src/offline/offline-status-badge.tsx +0 -51
  51. package/src/offline/online-controller.ts +0 -105
  52. package/src/offline/outbox.test.ts +0 -172
  53. package/src/offline/outbox.ts +0 -348
  54. package/src/offline/react-hooks.ts +0 -40
  55. package/src/offline/react.tsx +0 -32
  56. package/src/offline/storage.ts +0 -112
  57. package/src/offline/types.ts +0 -77
  58. package/src/org-db/components/DbResultTable.tsx +0 -176
  59. package/src/org-db/index.ts +0 -1
  60. package/src/pdf/index.ts +0 -11
  61. package/src/registry/index.ts +0 -9
  62. package/src/registry/lib/agent-validation-host.ts +0 -36
  63. package/src/registry/lib/biome-slot.tsx +0 -50
  64. package/src/registry/lib/biomes-enabled-context.ts +0 -20
  65. package/src/registry/lib/extension-points.ts +0 -134
  66. package/src/registry/lib/primitives/SessionEventCard.tsx +0 -164
  67. package/src/registry/lib/primitives/SessionEventTimeline.tsx +0 -95
  68. package/src/registry/lib/primitives/SessionMutationBar.tsx +0 -76
  69. package/src/registry/lib/primitives/SessionTabbedDrawer.tsx +0 -165
  70. package/src/registry/lib/primitives/index.ts +0 -18
  71. package/src/registry/lib/session-context-builder.ts +0 -84
  72. package/src/registry/lib/session-context-provider.tsx +0 -33
  73. package/src/registry/lib/session-context.ts +0 -21
  74. package/src/registry/lib/session-selectors.ts +0 -272
  75. package/src/session/comments/CommentRail.tsx +0 -164
  76. package/src/session/index.ts +0 -3
  77. package/src/session/lib/cn.ts +0 -11
  78. package/src/session/shell/SessionWorkspaceShell.tsx +0 -147
  79. package/src/session-kit/approvals/ApprovalButton.tsx +0 -89
  80. package/src/session-kit/approvals/ApprovalCard.tsx +0 -341
  81. package/src/session-kit/approvals/ApprovalsCenter.tsx +0 -336
  82. package/src/session-kit/approvals/CapabilityApprovalStickyBar.tsx +0 -118
  83. package/src/session-kit/approvals/Obligation.tsx +0 -111
  84. package/src/session-kit/approvals/ScopedBody.tsx +0 -392
  85. package/src/session-kit/approvals/approval-icons.ts +0 -31
  86. package/src/session-kit/approvals/approval-model.ts +0 -204
  87. package/src/session-kit/approvals/index.ts +0 -22
  88. package/src/session-kit/approvals/obligation-display.ts +0 -100
  89. package/src/session-kit/approvals/risk-accent.ts +0 -47
  90. package/src/session-kit/approvals/scope-icons.ts +0 -19
  91. package/src/session-kit/combobox/Combobox.tsx +0 -358
  92. package/src/session-kit/combobox/use-click-outside.ts +0 -21
  93. package/src/session-kit/display/ContextHeader.tsx +0 -148
  94. package/src/session-kit/display/FileDiffCard.tsx +0 -140
  95. package/src/session-kit/display/MD.tsx +0 -162
  96. package/src/session-kit/display/MessageTurn.tsx +0 -157
  97. package/src/session-kit/display/ThinkingPanel.tsx +0 -81
  98. package/src/session-kit/display/TodoChecklist.tsx +0 -120
  99. package/src/session-kit/display/TokenMeter.tsx +0 -89
  100. package/src/session-kit/display/ToolStrip.tsx +0 -278
  101. package/src/session-kit/display/TypingDots.tsx +0 -24
  102. package/src/session-kit/index.ts +0 -44
  103. package/src/session-kit/lib/enums.ts +0 -66
  104. package/src/session-kit/lib/portal-accent.ts +0 -30
  105. package/src/session-kit/lib/status-dot.ts +0 -68
  106. package/src/session-kit/primitives/Avatar.tsx +0 -44
  107. package/src/session-kit/primitives/PortalGlyph.tsx +0 -51
  108. package/src/session-kit/primitives/RiskPill.tsx +0 -95
  109. package/src/session-kit/primitives/ScopeDot.tsx +0 -47
  110. package/src/session-kit/primitives/Segmented.tsx +0 -71
  111. package/src/ui/branding/app-branding-context.ts +0 -20
  112. package/src/ui/branding/app-branding-provider.tsx +0 -123
  113. package/src/ui/branding/sanitize.ts +0 -89
  114. package/src/ui/breakpoints.ts +0 -14
  115. package/src/ui/capture/document-preview.tsx +0 -166
  116. package/src/ui/capture/field-spec-to-zod.test.ts +0 -81
  117. package/src/ui/capture/field-spec-to-zod.ts +0 -174
  118. package/src/ui/capture/field-spec-validate.ts +0 -236
  119. package/src/ui/capture/field-spec.test.ts +0 -49
  120. package/src/ui/capture/field-spec.ts +0 -274
  121. package/src/ui/capture/index.ts +0 -51
  122. package/src/ui/capture/schema-field-input.tsx +0 -620
  123. package/src/ui/capture/schema-form.tsx +0 -149
  124. package/src/ui/capture/submit-values.test.ts +0 -70
  125. package/src/ui/capture/submit-values.ts +0 -63
  126. package/src/ui/capture/use-prefill.ts +0 -186
  127. package/src/ui/capture/use-schema-validation.ts +0 -145
  128. package/src/ui/capture/use-wizard.ts +0 -180
  129. package/src/ui/capture/value-format.ts +0 -32
  130. package/src/ui/capture/wizard.tsx +0 -105
  131. package/src/ui/charts/bar-chart.tsx +0 -139
  132. package/src/ui/charts/chart-frame.tsx +0 -125
  133. package/src/ui/charts/chart-types.ts +0 -52
  134. package/src/ui/charts/chart-utils.ts +0 -190
  135. package/src/ui/charts/charts.test.tsx +0 -204
  136. package/src/ui/charts/donut-chart.tsx +0 -129
  137. package/src/ui/charts/index.ts +0 -27
  138. package/src/ui/charts/line-chart.tsx +0 -202
  139. package/src/ui/charts/sparkline.tsx +0 -109
  140. package/src/ui/chrome/AsyncBoundary.tsx +0 -74
  141. package/src/ui/chrome/EmptyState.tsx +0 -257
  142. package/src/ui/chrome/ErrorCard.tsx +0 -63
  143. package/src/ui/chrome/LoadingState.tsx +0 -61
  144. package/src/ui/chrome/PageHeader.tsx +0 -137
  145. package/src/ui/chrome/RecordCardList.tsx +0 -98
  146. package/src/ui/chrome/StateCard.tsx +0 -150
  147. package/src/ui/chrome/confirm-delete-dialog.tsx +0 -59
  148. package/src/ui/chrome/page-shell.spec.tsx +0 -117
  149. package/src/ui/chrome/page-shell.tsx +0 -221
  150. package/src/ui/chrome/scope-badge.tsx +0 -40
  151. package/src/ui/chrome/status-badge.tsx +0 -79
  152. package/src/ui/cn.ts +0 -32
  153. package/src/ui/design-tokens.ts +0 -372
  154. package/src/ui/hooks/use-debounced-value.ts +0 -16
  155. package/src/ui/hooks/use-is-mobile.ts +0 -28
  156. package/src/ui/index.ts +0 -110
  157. package/src/ui/pdf/pdf-preview.tsx +0 -122
  158. package/src/ui/primitives/alert-dialog.tsx +0 -104
  159. package/src/ui/primitives/async-combobox.tsx +0 -182
  160. package/src/ui/primitives/badge-variants.ts +0 -21
  161. package/src/ui/primitives/badge.tsx +0 -25
  162. package/src/ui/primitives/button-variants.ts +0 -31
  163. package/src/ui/primitives/button.tsx +0 -29
  164. package/src/ui/primitives/card.tsx +0 -43
  165. package/src/ui/primitives/checkbox.tsx +0 -26
  166. package/src/ui/primitives/collapsible.tsx +0 -9
  167. package/src/ui/primitives/dialog.tsx +0 -103
  168. package/src/ui/primitives/dropdown-menu.tsx +0 -179
  169. package/src/ui/primitives/form-stepper.tsx +0 -94
  170. package/src/ui/primitives/form-stepper.types.ts +0 -15
  171. package/src/ui/primitives/input.tsx +0 -22
  172. package/src/ui/primitives/label.tsx +0 -17
  173. package/src/ui/primitives/overflow-tabs.tsx +0 -331
  174. package/src/ui/primitives/popover.tsx +0 -33
  175. package/src/ui/primitives/preview-device-frame.tsx +0 -213
  176. package/src/ui/primitives/radio-group.tsx +0 -36
  177. package/src/ui/primitives/resizable.tsx +0 -67
  178. package/src/ui/primitives/scroll-area.tsx +0 -38
  179. package/src/ui/primitives/select.tsx +0 -143
  180. package/src/ui/primitives/separator.tsx +0 -20
  181. package/src/ui/primitives/sheet.tsx +0 -107
  182. package/src/ui/primitives/skeleton.tsx +0 -99
  183. package/src/ui/primitives/switch.tsx +0 -27
  184. package/src/ui/primitives/table.tsx +0 -72
  185. package/src/ui/primitives/tabs.tsx +0 -53
  186. package/src/ui/primitives/tag-multi-select.tsx +0 -261
  187. package/src/ui/primitives/textarea.tsx +0 -21
  188. package/src/ui/primitives/tooltip.tsx +0 -30
  189. package/src/ui/primitives/url-preview-chrome.tsx +0 -103
  190. package/src/vista/GenericCatalogueSurface.tsx +0 -179
  191. package/src/vista/SurfaceFallbackCard.tsx +0 -109
  192. package/src/vista/VistaPanel.tsx +0 -163
  193. package/src/vista/form-payload.spec.ts +0 -139
  194. package/src/vista/html/CustomUiFrame.spec.tsx +0 -127
  195. package/src/vista/html/CustomUiFrame.tsx +0 -206
  196. package/src/vista/html/custom-ui-frame-bridge.spec.ts +0 -191
  197. package/src/vista/html/custom-ui-frame-bridge.ts +0 -98
  198. package/src/vista/html/custom-ui-frame-contract.ts +0 -88
  199. package/src/vista/html/custom-ui-intents.spec.ts +0 -135
  200. package/src/vista/html/custom-ui-intents.ts +0 -155
  201. package/src/vista/html/custom-ui-srcdoc.spec.ts +0 -151
  202. package/src/vista/html/custom-ui-srcdoc.ts +0 -193
  203. package/src/vista/html/index.ts +0 -13
  204. package/src/vista/index.ts +0 -52
  205. package/src/vista/surface-catalogue.spec.ts +0 -140
  206. package/src/vista/surface-catalogue.ts +0 -90
  207. package/src/vista/surface-icons.ts +0 -57
  208. package/src/vista/surface-registry.ts +0 -56
  209. package/src/vista/surfaces/ChartSurface.tsx +0 -34
  210. package/src/vista/surfaces/FilesSurface.tsx +0 -202
  211. package/src/vista/surfaces/FormSurface.tsx +0 -380
  212. package/src/vista/surfaces/HtmlSurface.tsx +0 -43
  213. package/src/vista/surfaces/TableSurface.tsx +0 -100
  214. package/src/vista/surfaces/UrlSurface.tsx +0 -40
  215. package/src/vista/surfaces/blob-surfaces.tsx +0 -139
  216. package/src/vista/surfaces/elicitation-transport.spec.ts +0 -369
  217. package/src/vista/surfaces/elicitation-transport.ts +0 -367
  218. package/src/vista/surfaces/file-surface-utils.ts +0 -69
  219. package/src/vista/surfaces/index.ts +0 -24
  220. package/src/vista/surfaces/register-surfaces.tsx +0 -139
  221. package/src/vista/surfaces/text-surfaces.tsx +0 -132
  222. package/src/vista/use-vista-tabs.ts +0 -196
  223. package/src/vista/vista-code-renderer-context.ts +0 -16
  224. package/src/vista/vista-code-renderer.tsx +0 -35
  225. package/src/vista/vista-file-context.ts +0 -14
  226. package/src/vista/vista-file-source.tsx +0 -141
  227. package/src/vista/vista-form-context.ts +0 -20
  228. package/src/vista/vista-form-host.tsx +0 -48
  229. package/src/vista/vista-model.ts +0 -137
  230. package/src/vista/vista-open-context.tsx +0 -27
  231. package/src/vista/vista-open-state.ts +0 -11
  232. package/src/vista/vista-payloads.ts +0 -635
  233. package/src/vista/vista-tabs-manifest.ts +0 -138
  234. package/src/vista/vista-tools.ts +0 -232
@@ -1,172 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { OnlineController } from './online-controller';
4
- import { Outbox, type OutboxOptions } from './outbox';
5
- import { InMemoryOutboxStorage } from './storage';
6
- import { OutboxEntryStatus, type OutboxExecuteContext } from './types';
7
-
8
- const KIND = 'test:mutation';
9
-
10
- function makeOutbox(overrides: Partial<OutboxOptions> = {}): {
11
- outbox: Outbox;
12
- online: OnlineController;
13
- } {
14
- const online = new OnlineController(); // node: navigator undefined ⇒ online
15
- const outbox = new Outbox({
16
- storage: new InMemoryOutboxStorage(),
17
- online,
18
- // Immediate scheduler: retries are driven synchronously within `drain()`.
19
- scheduler: (fn) => fn(),
20
- ...overrides,
21
- });
22
- return { outbox, online };
23
- }
24
-
25
- describe('Outbox', () => {
26
- it('queues while offline and delivers on reconnect', async () => {
27
- const { outbox, online } = makeOutbox();
28
- const send = vi.fn(() => Promise.resolve());
29
- outbox.registerExecutor(KIND, send);
30
-
31
- online.reportOffline();
32
- await outbox.enqueue(KIND, { a: 1 });
33
- expect(send).not.toHaveBeenCalled();
34
- expect(outbox.getSnapshot().pendingCount).toBe(1);
35
-
36
- online.reportOnline();
37
- await outbox.drain();
38
- expect(send).toHaveBeenCalledTimes(1);
39
- expect(outbox.getSnapshot().entries).toHaveLength(0);
40
- });
41
-
42
- it('delivers immediately when online and removes the entry; idempotency key is stable', async () => {
43
- const { outbox } = makeOutbox();
44
- const keys: string[] = [];
45
- outbox.registerExecutor(KIND, (_p, ctx: OutboxExecuteContext) => {
46
- keys.push(ctx.idempotencyKey);
47
- return Promise.resolve();
48
- });
49
- await outbox.enqueue(KIND, { a: 1 });
50
- await outbox.drain();
51
- expect(keys).toHaveLength(1);
52
- expect(keys[0]).toMatch(/[0-9a-f-]{36}/);
53
- expect(outbox.getSnapshot().entries).toHaveLength(0);
54
- });
55
-
56
- it('retries a 5xx and eventually succeeds', async () => {
57
- const { outbox } = makeOutbox();
58
- let calls = 0;
59
- outbox.registerExecutor(KIND, () => {
60
- calls += 1;
61
- if (calls < 3) {
62
- return Promise.reject({ status: 503, message: 'unavailable' });
63
- }
64
- return Promise.resolve();
65
- });
66
- await outbox.enqueue(KIND, { a: 1 });
67
- await outbox.drain();
68
- expect(calls).toBe(3);
69
- expect(outbox.getSnapshot().entries).toHaveLength(0);
70
- });
71
-
72
- it('dead-letters a 4xx immediately (no pointless retries)', async () => {
73
- const { outbox } = makeOutbox();
74
- const send = vi.fn(() =>
75
- Promise.reject({ status: 400, message: 'bad request' }),
76
- );
77
- outbox.registerExecutor(KIND, send);
78
- await outbox.enqueue(KIND, { a: 1 });
79
- await outbox.drain();
80
- expect(send).toHaveBeenCalledTimes(1);
81
- const snap = outbox.getSnapshot();
82
- expect(snap.failedCount).toBe(1);
83
- expect(snap.entries[0]?.status).toBe(OutboxEntryStatus.Failed);
84
- });
85
-
86
- it('dead-letters after exhausting retries on persistent 5xx', async () => {
87
- const { outbox } = makeOutbox({ maxAttempts: 3 });
88
- let calls = 0;
89
- outbox.registerExecutor(KIND, () => {
90
- calls += 1;
91
- return Promise.reject({ status: 503 });
92
- });
93
- await outbox.enqueue(KIND, { a: 1 });
94
- await outbox.drain();
95
- expect(calls).toBe(3);
96
- expect(outbox.getSnapshot().entries[0]?.status).toBe(
97
- OutboxEntryStatus.Failed,
98
- );
99
- });
100
-
101
- it('a network failure never advances the attempt counter or dead-letters', async () => {
102
- // Captured scheduler so the offline-retry loop does not run forever.
103
- const timers: Array<() => void> = [];
104
- const { outbox } = makeOutbox({ scheduler: (fn) => void timers.push(fn) });
105
- let succeed = false;
106
- outbox.registerExecutor(KIND, () => {
107
- if (!succeed) {
108
- return Promise.reject({
109
- name: 'TypeError',
110
- message: 'Failed to fetch',
111
- });
112
- }
113
- return Promise.resolve();
114
- });
115
-
116
- await outbox.enqueue(KIND, { a: 1 });
117
- await outbox.drain();
118
- const parked = outbox.getSnapshot().entries[0];
119
- if (!parked) {
120
- throw new Error(
121
- 'the offline entry was dropped from the outbox instead of parked',
122
- );
123
- }
124
- expect(parked.status).toBe(OutboxEntryStatus.Pending);
125
- expect(parked.attempts).toBe(0); // offline is not the payload's fault
126
- expect(outbox.getSnapshot().failedCount).toBe(0);
127
-
128
- // Reconnect + retry drains it.
129
- succeed = true;
130
- await outbox.drain();
131
- expect(outbox.getSnapshot().entries).toHaveLength(0);
132
- });
133
-
134
- it('delivers queued entries in FIFO order', async () => {
135
- const { outbox, online } = makeOutbox();
136
- const order: number[] = [];
137
- outbox.registerExecutor(KIND, (p: { n: number }) => {
138
- order.push(p.n);
139
- return Promise.resolve();
140
- });
141
- online.reportOffline();
142
- await outbox.enqueue(KIND, { n: 1 });
143
- await outbox.enqueue(KIND, { n: 2 });
144
- await outbox.enqueue(KIND, { n: 3 });
145
- online.reportOnline();
146
- await outbox.drain();
147
- expect(order).toEqual([1, 2, 3]);
148
- });
149
-
150
- it('recovers crash-orphaned inflight entries to pending on start', async () => {
151
- const storage = new InMemoryOutboxStorage();
152
- const ts = 1;
153
- await storage.put({
154
- id: 'orphan',
155
- kind: KIND,
156
- payload: { a: 1 },
157
- idempotencyKey: 'k',
158
- status: OutboxEntryStatus.Inflight, // died mid-send
159
- attempts: 1,
160
- createdAt: ts,
161
- updatedAt: ts,
162
- });
163
- const online = new OnlineController();
164
- online.reportOffline(); // keep it queued so we can observe the recovery
165
- const outbox = new Outbox({ storage, online, scheduler: (fn) => fn() });
166
- await outbox.start();
167
- expect(outbox.getSnapshot().entries[0]?.status).toBe(
168
- OutboxEntryStatus.Pending,
169
- );
170
- outbox.stop();
171
- });
172
- });
@@ -1,348 +0,0 @@
1
- import { newId } from './id';
2
- import { OnlineController, isNetworkUnavailableError } from './online-controller';
3
- import { createDefaultOutboxStorage } from './storage';
4
- import {
5
- OutboxEntryStatus,
6
- type OutboxEntry,
7
- type OutboxExecutor,
8
- type OutboxSnapshot,
9
- type OutboxStorage,
10
- } from './types';
11
-
12
- /** Read an HTTP status off an error (Orval/fetch shapes), or undefined. */
13
- function statusOf(err: unknown): number | undefined {
14
- if (!err || typeof err !== 'object') {
15
- return undefined;
16
- }
17
- const e = err as { status?: number; response?: { status?: number } };
18
- if (typeof e.status === 'number') {
19
- return e.status;
20
- }
21
- if (e.response && typeof e.response.status === 'number') {
22
- return e.response.status;
23
- }
24
- return undefined;
25
- }
26
-
27
- /** Default retry policy: retry transport failures, timeouts, 429 and 5xx; a
28
- * 4xx client error is the caller's bug — dead-letter it, do not hammer. */
29
- function defaultIsRetriable(err: unknown): boolean {
30
- if (isNetworkUnavailableError(err)) {
31
- return true;
32
- }
33
- const status = statusOf(err);
34
- if (status === undefined) {
35
- return true; // unknown/opaque failure — give it a few tries
36
- }
37
- return status >= 500 || status === 429 || status === 408;
38
- }
39
-
40
- export interface OutboxOptions {
41
- storage?: OutboxStorage;
42
- online?: OnlineController;
43
- /** Attempts before an entry is dead-lettered (Failed). Default 5. */
44
- maxAttempts?: number;
45
- /** Base backoff (ms) — doubled each attempt, capped at maxBackoffMs. */
46
- baseBackoffMs?: number;
47
- maxBackoffMs?: number;
48
- isRetriable?: (err: unknown) => boolean;
49
- /** Injectable clock (tests). Default Date.now. */
50
- now?: () => number;
51
- /** Injectable timer (tests). Default setTimeout. */
52
- scheduler?: (fn: () => void, ms: number) => void;
53
- }
54
-
55
- export type OutboxListener = (snapshot: OutboxSnapshot) => void;
56
-
57
- export interface EnqueueOptions {
58
- idempotencyKey?: string;
59
- label?: string;
60
- }
61
-
62
- /**
63
- * A durable, provider-agnostic mutation outbox. Register an executor per `kind`
64
- * once; then `enqueue` mutations (they persist immediately). When online the
65
- * queue drains in FIFO order with retry/backoff and idempotency; when the send
66
- * fails as network-unavailable it stops and resumes on reconnect. A mutation
67
- * that exhausts retries is dead-lettered for manual retry/discard — never
68
- * silently dropped.
69
- */
70
- export class Outbox {
71
- private readonly storage: OutboxStorage;
72
- private readonly online: OnlineController;
73
- private readonly maxAttempts: number;
74
- private readonly baseBackoffMs: number;
75
- private readonly maxBackoffMs: number;
76
- private readonly isRetriable: (err: unknown) => boolean;
77
- private readonly now: () => number;
78
- private readonly scheduler: (fn: () => void, ms: number) => void;
79
-
80
- private readonly executors = new Map<string, OutboxExecutor>();
81
- private readonly listeners = new Set<OutboxListener>();
82
- private cache: OutboxEntry[] = [];
83
- private snapshot: OutboxSnapshot = {
84
- entries: [],
85
- pendingCount: 0,
86
- failedCount: 0,
87
- draining: false,
88
- online: true,
89
- };
90
- private draining = false;
91
- private drainRequested = false;
92
- private retryScheduled = false;
93
- private drainPromise: Promise<void> | null = null;
94
- private unsubscribeOnline: (() => void) | null = null;
95
-
96
- constructor(opts: OutboxOptions = {}) {
97
- this.storage = opts.storage ?? createDefaultOutboxStorage();
98
- this.online = opts.online ?? new OnlineController();
99
- this.maxAttempts = opts.maxAttempts ?? 5;
100
- this.baseBackoffMs = opts.baseBackoffMs ?? 1000;
101
- this.maxBackoffMs = opts.maxBackoffMs ?? 60_000;
102
- this.isRetriable = opts.isRetriable ?? defaultIsRetriable;
103
- this.now = opts.now ?? (() => Date.now());
104
- this.scheduler =
105
- opts.scheduler ?? ((fn, ms) => void setTimeout(fn, ms));
106
- }
107
-
108
- /** Register the sender for a mutation `kind`. Registering re-triggers a drain
109
- * (entries that were parked waiting for their executor can now go). */
110
- registerExecutor<TPayload>(kind: string, executor: OutboxExecutor<TPayload>): void {
111
- this.executors.set(kind, executor as OutboxExecutor);
112
- void this.drain();
113
- }
114
-
115
- hasExecutor(kind: string): boolean {
116
- return this.executors.has(kind);
117
- }
118
-
119
- /** Begin: start connectivity listeners, hydrate from storage, drain. */
120
- async start(): Promise<void> {
121
- this.online.start();
122
- this.unsubscribeOnline?.();
123
- this.unsubscribeOnline = this.online.subscribe((online) => {
124
- if (online) {
125
- void this.drain();
126
- } else {
127
- this.notify();
128
- }
129
- });
130
- await this.hydrate();
131
- void this.drain();
132
- }
133
-
134
- stop(): void {
135
- this.online.stop();
136
- this.unsubscribeOnline?.();
137
- this.unsubscribeOnline = null;
138
- }
139
-
140
- isOnline(): boolean {
141
- return this.online.isOnline();
142
- }
143
-
144
- /** Persist a mutation for delivery. Resolves once it is durably queued. */
145
- async enqueue<TPayload>(
146
- kind: string,
147
- payload: TPayload,
148
- opts: EnqueueOptions = {},
149
- ): Promise<OutboxEntry<TPayload>> {
150
- const ts = this.now();
151
- const entry: OutboxEntry<TPayload> = {
152
- id: newId(),
153
- kind,
154
- payload,
155
- idempotencyKey: opts.idempotencyKey ?? newId(),
156
- status: OutboxEntryStatus.Pending,
157
- attempts: 0,
158
- createdAt: ts,
159
- updatedAt: ts,
160
- ...(opts.label !== undefined ? { label: opts.label } : {}),
161
- };
162
- await this.storage.put(entry);
163
- await this.refresh();
164
- void this.drain();
165
- return entry;
166
- }
167
-
168
- /** Reset a dead-lettered entry to pending and drain. */
169
- async retry(id: string): Promise<void> {
170
- const entry = this.cache.find((e) => e.id === id);
171
- if (!entry) {
172
- return;
173
- }
174
- await this.storage.put({
175
- ...entry,
176
- status: OutboxEntryStatus.Pending,
177
- attempts: 0,
178
- updatedAt: this.now(),
179
- });
180
- await this.refresh();
181
- void this.drain();
182
- }
183
-
184
- /** Permanently discard a queued/dead-lettered entry. */
185
- async discard(id: string): Promise<void> {
186
- await this.storage.delete(id);
187
- await this.refresh();
188
- }
189
-
190
- async clear(): Promise<void> {
191
- await this.storage.clear();
192
- await this.refresh();
193
- }
194
-
195
- /** Returns a STABLE reference between changes (safe for useSyncExternalStore). */
196
- getSnapshot(): OutboxSnapshot {
197
- return this.snapshot;
198
- }
199
-
200
- subscribe(listener: OutboxListener): () => void {
201
- this.listeners.add(listener);
202
- listener(this.snapshot);
203
- return () => this.listeners.delete(listener);
204
- }
205
-
206
- /** Attempt to deliver every deliverable entry once, in FIFO order. Safe to
207
- * call concurrently — a single flight runs and re-runs if requested. */
208
- async drain(): Promise<void> {
209
- if (!this.online.isOnline()) {
210
- return;
211
- }
212
- // Single-flight: a concurrent drain() awaits the in-flight one (and requests
213
- // an extra pass) rather than returning early — so `await drain()` always
214
- // resolves after the queue is fully processed.
215
- if (this.drainPromise) {
216
- this.drainRequested = true;
217
- return this.drainPromise;
218
- }
219
- this.drainPromise = this.runDrain();
220
- try {
221
- await this.drainPromise;
222
- } finally {
223
- this.drainPromise = null;
224
- }
225
- }
226
-
227
- private async runDrain(): Promise<void> {
228
- this.draining = true;
229
- this.notify();
230
- try {
231
- do {
232
- this.drainRequested = false;
233
- await this.drainOnce();
234
- } while (this.shouldContinueDrain());
235
- } finally {
236
- this.draining = false;
237
- this.notify();
238
- }
239
- }
240
-
241
- private shouldContinueDrain(): boolean {
242
- return this.drainRequested && this.online.isOnline();
243
- }
244
-
245
- // ── internals ──────────────────────────────────────────────────────────────
246
-
247
- private async hydrate(): Promise<void> {
248
- const stored = await this.storage.getAll();
249
- // Recover entries that were mid-flight when the tab/process died: they may
250
- // or may not have applied server-side, but the idempotency key makes a
251
- // redelivery safe, so re-queue them rather than drop them.
252
- for (const entry of stored) {
253
- if (entry.status === OutboxEntryStatus.Inflight) {
254
- entry.status = OutboxEntryStatus.Pending;
255
- await this.storage.put(entry);
256
- }
257
- }
258
- await this.refresh();
259
- }
260
-
261
- private async drainOnce(): Promise<void> {
262
- const entries = await this.storage.getAll();
263
- for (const entry of entries) {
264
- if (!this.online.isOnline()) {
265
- break;
266
- }
267
- if (entry.status === OutboxEntryStatus.Failed) {
268
- continue; // dead-letter — needs manual retry
269
- }
270
- const executor = this.executors.get(entry.kind);
271
- if (!executor) {
272
- continue; // no sender registered yet — leave pending
273
- }
274
-
275
- const attempt = entry.attempts + 1;
276
- await this.write({ ...entry, status: OutboxEntryStatus.Inflight, attempts: attempt });
277
-
278
- try {
279
- await executor(entry.payload, {
280
- idempotencyKey: entry.idempotencyKey,
281
- attempt,
282
- entryId: entry.id,
283
- });
284
- await this.storage.delete(entry.id);
285
- await this.refresh();
286
- } catch (err) {
287
- const message = err instanceof Error ? err.message : String(err);
288
- if (isNetworkUnavailableError(err)) {
289
- // Network is down. This is NOT the payload's fault, so do NOT advance
290
- // the attempt counter (being offline for a long time must never
291
- // dead-letter a capture). Park the entry, stop the burst, and resume
292
- // on reconnect or after a backoff.
293
- await this.write({ ...entry, status: OutboxEntryStatus.Pending, attempts: entry.attempts, lastError: message });
294
- this.scheduleRetry(entry.attempts + 1);
295
- break;
296
- }
297
- if (this.isRetriable(err) && attempt < this.maxAttempts) {
298
- await this.write({ ...entry, status: OutboxEntryStatus.Retrying, attempts: attempt, lastError: message });
299
- this.scheduleRetry(attempt);
300
- } else {
301
- // 4xx client error, or retries exhausted → dead-letter for manual
302
- // retry/discard. Never silently dropped.
303
- await this.write({ ...entry, status: OutboxEntryStatus.Failed, attempts: attempt, lastError: message });
304
- }
305
- }
306
- }
307
- }
308
-
309
- private scheduleRetry(attempt: number): void {
310
- if (this.retryScheduled) {
311
- return;
312
- }
313
- this.retryScheduled = true;
314
- const delay = Math.min(
315
- this.maxBackoffMs,
316
- this.baseBackoffMs * 2 ** Math.max(0, attempt - 1),
317
- );
318
- this.scheduler(() => {
319
- this.retryScheduled = false;
320
- void this.drain();
321
- }, delay);
322
- }
323
-
324
- private async write(entry: OutboxEntry): Promise<void> {
325
- await this.storage.put({ ...entry, updatedAt: this.now() });
326
- await this.refresh();
327
- }
328
-
329
- private async refresh(): Promise<void> {
330
- this.cache = await this.storage.getAll();
331
- this.notify();
332
- }
333
-
334
- private notify(): void {
335
- // Rebuild the cached snapshot ONCE per change so getSnapshot() returns a
336
- // stable reference (required by React's useSyncExternalStore).
337
- this.snapshot = {
338
- entries: this.cache,
339
- pendingCount: this.cache.filter((e) => e.status !== OutboxEntryStatus.Failed).length,
340
- failedCount: this.cache.filter((e) => e.status === OutboxEntryStatus.Failed).length,
341
- draining: this.draining,
342
- online: this.online.isOnline(),
343
- };
344
- for (const l of this.listeners) {
345
- l(this.snapshot);
346
- }
347
- }
348
- }
@@ -1,40 +0,0 @@
1
- import { createContext, useContext, useSyncExternalStore } from 'react';
2
-
3
- import { type Outbox } from './outbox';
4
-
5
- import type { OutboxSnapshot } from './types';
6
-
7
- export const OutboxContext = createContext<Outbox | null>(null);
8
-
9
- export function useOutbox(): Outbox {
10
- const ctx = useContext(OutboxContext);
11
- if (!ctx) {
12
- throw new Error('useOutbox must be used within an <OutboxProvider>.');
13
- }
14
- return ctx;
15
- }
16
-
17
- function resolveOutbox(
18
- explicit: Outbox | undefined,
19
- ctx: Outbox | null,
20
- ): Outbox {
21
- const instance = explicit ?? ctx;
22
- if (!instance) {
23
- throw new Error('An <OutboxProvider> or an explicit outbox is required.');
24
- }
25
- return instance;
26
- }
27
-
28
- export function useOutboxSnapshot(outbox?: Outbox): OutboxSnapshot {
29
- const ctx = useContext(OutboxContext);
30
- const instance = resolveOutbox(outbox, ctx);
31
- return useSyncExternalStore(
32
- (cb) => instance.subscribe(cb),
33
- () => instance.getSnapshot(),
34
- () => instance.getSnapshot(),
35
- );
36
- }
37
-
38
- export function useOnlineStatus(outbox?: Outbox): boolean {
39
- return useOutboxSnapshot(outbox).online;
40
- }
@@ -1,32 +0,0 @@
1
- import {
2
- useEffect,
3
- useMemo,
4
- useRef,
5
- type ReactNode,
6
- } from 'react';
7
-
8
- import { Outbox, type OutboxOptions } from './outbox';
9
- import { OutboxContext } from './react-hooks';
10
-
11
- export interface OutboxProviderProps {
12
- /** Bring your own Outbox, or let the provider create one from `options`. */
13
- outbox?: Outbox;
14
- options?: OutboxOptions;
15
- children: ReactNode;
16
- }
17
-
18
- /** Provides a shared {@link Outbox} and manages its lifecycle (starts
19
- * connectivity listeners + hydrates on mount, stops on unmount). */
20
- export function OutboxProvider({ outbox, options, children }: OutboxProviderProps): JSX.Element {
21
- // Options are captured once; changing them after mount does not recreate the
22
- // outbox (a durable queue must not be swapped underneath in-flight work).
23
- const optionsRef = useRef(options);
24
- const instance = useMemo(() => outbox ?? new Outbox(optionsRef.current), [outbox]);
25
-
26
- useEffect(() => {
27
- void instance.start();
28
- return () => instance.stop();
29
- }, [instance]);
30
-
31
- return <OutboxContext.Provider value={instance}>{children}</OutboxContext.Provider>;
32
- }
@@ -1,112 +0,0 @@
1
- import type { OutboxEntry, OutboxStorage } from './types';
2
-
3
- /** In-memory outbox storage — the SSR / test fallback (no persistence). */
4
- export class InMemoryOutboxStorage implements OutboxStorage {
5
- private readonly map = new Map<string, OutboxEntry>();
6
-
7
- getAll(): Promise<OutboxEntry[]> {
8
- return Promise.resolve(
9
- [...this.map.values()].sort((a, b) => a.createdAt - b.createdAt),
10
- );
11
- }
12
-
13
- put(entry: OutboxEntry): Promise<void> {
14
- this.map.set(entry.id, { ...entry });
15
- return Promise.resolve();
16
- }
17
-
18
- delete(id: string): Promise<void> {
19
- this.map.delete(id);
20
- return Promise.resolve();
21
- }
22
-
23
- clear(): Promise<void> {
24
- this.map.clear();
25
- return Promise.resolve();
26
- }
27
- }
28
-
29
- const DB_VERSION = 1;
30
-
31
- /** IndexedDB-backed outbox storage — durable across reloads and app restarts,
32
- * so a mutation captured offline survives the tab being closed. */
33
- export class IndexedDbOutboxStorage implements OutboxStorage {
34
- private dbPromise: Promise<IDBDatabase> | null = null;
35
-
36
- constructor(
37
- private readonly dbName = 'xema-offline-outbox',
38
- private readonly storeName = 'entries',
39
- ) {}
40
-
41
- private open(): Promise<IDBDatabase> {
42
- if (this.dbPromise) {
43
- return this.dbPromise;
44
- }
45
- this.dbPromise = new Promise<IDBDatabase>((resolve, reject) => {
46
- const req = indexedDB.open(this.dbName, DB_VERSION);
47
- req.onupgradeneeded = () => {
48
- const db = req.result;
49
- if (!db.objectStoreNames.contains(this.storeName)) {
50
- db.createObjectStore(this.storeName, { keyPath: 'id' });
51
- }
52
- };
53
- req.onsuccess = () => resolve(req.result);
54
- req.onerror = () => reject(req.error ?? new Error('indexedDB open failed'));
55
- });
56
- return this.dbPromise;
57
- }
58
-
59
- private async tx<T>(
60
- mode: IDBTransactionMode,
61
- run: (store: IDBObjectStore) => IDBRequest<T> | void,
62
- ): Promise<T | undefined> {
63
- const db = await this.open();
64
- return new Promise<T | undefined>((resolve, reject) => {
65
- const transaction = db.transaction(this.storeName, mode);
66
- const store = transaction.objectStore(this.storeName);
67
- let request: IDBRequest<T> | void;
68
- try {
69
- request = run(store);
70
- } catch (err) {
71
- reject(err);
72
- return;
73
- }
74
- transaction.onabort = () =>
75
- reject(transaction.error ?? new Error('indexedDB tx aborted'));
76
- transaction.oncomplete = () =>
77
- resolve(request ? request.result : undefined);
78
- transaction.onerror = () =>
79
- reject(transaction.error ?? new Error('indexedDB tx error'));
80
- });
81
- }
82
-
83
- async getAll(): Promise<OutboxEntry[]> {
84
- const all = (await this.tx<OutboxEntry[]>('readonly', (s) => s.getAll())) ?? [];
85
- return all.sort((a, b) => a.createdAt - b.createdAt);
86
- }
87
-
88
- async put(entry: OutboxEntry): Promise<void> {
89
- await this.tx('readwrite', (s) => s.put(entry));
90
- }
91
-
92
- async delete(id: string): Promise<void> {
93
- await this.tx('readwrite', (s) => s.delete(id));
94
- }
95
-
96
- async clear(): Promise<void> {
97
- await this.tx('readwrite', (s) => s.clear());
98
- }
99
- }
100
-
101
- /** Pick the best available storage: IndexedDB in a browser, else in-memory
102
- * (SSR / tests / private-mode fallbacks). */
103
- export function createDefaultOutboxStorage(): OutboxStorage {
104
- if (typeof indexedDB !== 'undefined') {
105
- try {
106
- return new IndexedDbOutboxStorage();
107
- } catch {
108
- // Some privacy modes throw on open — fall back to in-memory.
109
- }
110
- }
111
- return new InMemoryOutboxStorage();
112
- }