@cloudlet/toast 1.2.0 → 1.4.0-rc.1

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 (60) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/README.md +7 -3
  3. package/dist/agent.d.ts +334 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +494 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/contracts/events.d.ts +14 -0
  8. package/dist/contracts/events.d.ts.map +1 -1
  9. package/dist/contracts/internal.d.ts +16 -2
  10. package/dist/contracts/internal.d.ts.map +1 -1
  11. package/dist/contracts/notifications.d.ts +16 -0
  12. package/dist/contracts/notifications.d.ts.map +1 -1
  13. package/dist/contracts/snapshots.d.ts +8 -0
  14. package/dist/contracts/snapshots.d.ts.map +1 -1
  15. package/dist/core/actions.d.ts +69 -0
  16. package/dist/core/actions.d.ts.map +1 -0
  17. package/dist/core/actions.js +160 -0
  18. package/dist/core/actions.js.map +1 -0
  19. package/dist/core/agent-port.d.ts +116 -0
  20. package/dist/core/agent-port.d.ts.map +1 -0
  21. package/dist/core/agent-port.js +138 -0
  22. package/dist/core/agent-port.js.map +1 -0
  23. package/dist/core/bridge.d.ts.map +1 -1
  24. package/dist/core/bridge.js +120 -231
  25. package/dist/core/bridge.js.map +1 -1
  26. package/dist/core/controller.d.ts +44 -9
  27. package/dist/core/controller.d.ts.map +1 -1
  28. package/dist/core/controller.js +367 -292
  29. package/dist/core/controller.js.map +1 -1
  30. package/dist/core/validation.d.ts.map +1 -1
  31. package/dist/core/validation.js +103 -94
  32. package/dist/core/validation.js.map +1 -1
  33. package/dist/react/card.d.ts.map +1 -1
  34. package/dist/react/card.js +3 -3
  35. package/dist/react/card.js.map +1 -1
  36. package/dist/react/gestures.d.ts +6 -0
  37. package/dist/react/gestures.d.ts.map +1 -1
  38. package/dist/react/gestures.js +28 -15
  39. package/dist/react/gestures.js.map +1 -1
  40. package/dist/react/host.d.ts.map +1 -1
  41. package/dist/react/host.js +290 -270
  42. package/dist/react/host.js.map +1 -1
  43. package/dist/react.d.ts +9 -1
  44. package/dist/react.d.ts.map +1 -1
  45. package/dist/styles.css +1 -1
  46. package/package.json +3 -2
  47. package/src/agent.ts +879 -0
  48. package/src/contracts/events.ts +15 -0
  49. package/src/contracts/internal.ts +14 -1
  50. package/src/contracts/notifications.ts +16 -0
  51. package/src/contracts/snapshots.ts +8 -0
  52. package/src/core/actions.ts +149 -0
  53. package/src/core/agent-port.ts +237 -0
  54. package/src/core/bridge.ts +19 -105
  55. package/src/core/controller.ts +183 -56
  56. package/src/core/validation.ts +6 -3
  57. package/src/react/card.tsx +4 -3
  58. package/src/react/gestures.ts +45 -8
  59. package/src/react/host.tsx +48 -12
  60. package/src/react.ts +9 -1
@@ -1,16 +1,17 @@
1
1
  import type { Diagnostic } from '../contracts/events.js';
2
- import type { ActionAttemptToken, NotificationHandle, OperationAttemptToken } from '../contracts/identity.js';
2
+ import type { NotificationHandle, OperationAttemptToken } from '../contracts/identity.js';
3
3
  import type {
4
- AnnouncementAcknowledgement, BridgeRejection, BridgeResult, FocusRequestToken, FormErrors, HostLease, HostOptions, PauseChange,
4
+ AnnouncementAcknowledgement, BridgeRejection, BridgeResult, FocusRequestToken, HostLease, HostOptions, PauseChange,
5
5
  PresentationToken, RendererBridge,
6
6
  } from '../contracts/internal.js';
7
7
  import type { CancellationSignal } from '../contracts/operations.js';
8
8
  import type { CloudletController } from '../contracts/controller.js';
9
9
  import type { IslandReason, PauseReason } from '../contracts/snapshots.js';
10
10
  import {
11
- APPLIED, ATTENTION_DELAY, NO_FORM_ERRORS, RENDERER_SERVER, UNCHANGED, getControllerKernel, reject,
12
- type ControllerKernel, type FormState, type OperationBinding, type RecordState,
11
+ APPLIED, ATTENTION_DELAY, RENDERER_SERVER, UNCHANGED, getControllerKernel, reject,
12
+ type ControllerKernel, type OperationBinding, type RecordState,
13
13
  } from './controller.js';
14
+ import { invokeAction, setFormValue } from './actions.js';
14
15
  import { validDelay } from './validation.js';
15
16
 
16
17
  // The renderer's side of a controller. It is imported only by react/renderer.tsx, so it travels
@@ -54,35 +55,6 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
54
55
  return error ? reject(error) : action(...args);
55
56
  };
56
57
  }
57
- /** `required` and `maxLength` first, then the form's own `validate`; null lets the submit run. */
58
- function checkForm(form: FormState, handle: NotificationHandle): FormErrors | null {
59
- const messages = k.options.messages;
60
- const fields: Record<string, string> = {};
61
- for (const field of form.action.fields) {
62
- const value = form.values[field.name] ?? '';
63
- if (field.required && !value.trim()) fields[field.name] = messages?.fieldRequired ?? 'This field is required';
64
- else if (field.maxLength !== undefined && value.length > field.maxLength) fields[field.name] = messages?.fieldTooLong ?? 'This text is too long';
65
- }
66
- if (Object.keys(fields).length) return freeze({ form: null, fields: freeze(fields) });
67
- if (!form.action.validate) return null;
68
- let result: unknown;
69
- try {
70
- result = form.action.validate(form.values);
71
- } catch (error) {
72
- k.failure('callback-error', 'action.validate', error, handle);
73
- return freeze({ form: messages?.actionError ?? 'Action failed', fields: freeze({}) });
74
- }
75
- if (result === true) return null;
76
- if (typeof result === 'string' && result.trim()) return freeze({ form: result, fields: freeze({}) });
77
- const names = form.action.fields.map((field) => field.name);
78
- if (result !== null && typeof result === 'object' && Object.getPrototypeOf(result) === Object.prototype &&
79
- Object.entries(result).every(([name, message]) => names.includes(name) && typeof message === 'string' && message.trim())) {
80
- return Object.keys(result).length ? freeze({ form: null, fields: freeze({ ...result as Record<string, string> }) }) : null;
81
- }
82
- k.diagnostic({ code: 'callback-error', source: 'action.validate', handle,
83
- message: 'validate must return true, a message, or messages keyed by field name' });
84
- return freeze({ form: messages?.actionError ?? 'Action failed', fields: freeze({}) });
85
- }
86
58
  const bridge = freeze<RendererBridge<Slot>>({
87
59
  renderError: k.options.messages?.renderError ?? 'Content unavailable',
88
60
  reportInvalidContainer(message, error) {
@@ -136,11 +108,11 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
136
108
  if (k.island?.current === target) return UNCHANGED;
137
109
  // An open Island only moves its slider; the opening reason is kept.
138
110
  return k.mutate(() => {
139
- if (k.island) k.island.current = target;
140
- else k.island = { reason, current: target };
111
+ if (k.island) { k.island.current = target; k.island.source = 'user'; }
112
+ else k.island = { reason, current: target, source: 'user' };
141
113
  });
142
114
  }),
143
- collapseIsland: withLease(() => k.island ? k.mutate(() => { k.island = null; }) : UNCHANGED),
115
+ collapseIsland: withLease(() => k.island ? k.mutate(() => { k.collapse('user'); }) : UNCHANGED),
144
116
  setAutoDismiss: withLease((value?: boolean) => {
145
117
  if (value !== undefined && typeof value !== 'boolean') return reject('invalid-input');
146
118
  if (k.autoDismissing(value) === k.autoDismissing(k.hostAutoDismiss)) { k.hostAutoDismiss = value; return UNCHANGED; }
@@ -151,6 +123,7 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
151
123
  if (k.island.current === record) return UNCHANGED;
152
124
  return k.mutate(() => {
153
125
  k.island!.current = record;
126
+ k.island!.source = 'user';
154
127
  // Navigation is interaction: a pending auto-open restarts its delay,
155
128
  // and reaching the initiator by hand fulfils it.
156
129
  if (k.attention?.record === record) k.attention = null;
@@ -192,7 +165,7 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
192
165
  if (record.presentation !== presentation) return reject('stale-presentation');
193
166
  if (record.readable) return UNCHANGED;
194
167
  return k.mutate((deliveries) => {
195
- deliveries.push({ type: 'presented', handle: record.handle, firstPresentation: !record.everPresented });
168
+ deliveries.push({ type: 'presented', source: 'system', handle: record.handle, firstPresentation: !record.everPresented });
196
169
  record.readable = record.everPresented = true;
197
170
  });
198
171
  })),
@@ -202,7 +175,7 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
202
175
  return k.mutate(() => { k.focusRequest = null; });
203
176
  }),
204
177
  acknowledgeOverflow: withLease(() => {
205
- return k.acknowledgeOverflow();
178
+ return k.acknowledgeOverflow('user');
206
179
  }),
207
180
  acknowledgeAnnouncements: withLease((acknowledgements: readonly AnnouncementAcknowledgement[]) => {
208
181
  return acknowledgeAnnouncements(acknowledgements);
@@ -212,80 +185,21 @@ function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<S
212
185
  if (error) return reject(error);
213
186
  // Renderer close reasons are a closed protocol, never an exit callback.
214
187
  if (!['close-button', 'swipe', 'navigation'].includes(reason)) return reject('invalid-input');
215
- return k.dismiss(handle, reason);
188
+ return k.dismiss(handle, reason, 'user');
216
189
  }),
217
190
  // The button lives only in the open Island.
218
- clearAll: withLease(() => k.island ? k.clear('clear-button') : reject('island-closed')),
191
+ clearAll: withLease(() => k.island ? k.clear('clear-button', 'user') : reject('island-closed')),
219
192
  invokeAction: withLease(k.withRecord((record) => {
220
- const handle = record.handle;
221
- const action = record.content.action;
222
- if (!action || action.kind === 'link') return reject('no-command');
223
- if (record.action.status === 'pending') return reject('already-pending');
224
- const form = k.formOf(record);
225
- if (form) {
226
- const errors = checkForm(form, handle);
227
- if (errors) {
228
- k.mutate(() => { form.errors = errors; });
229
- return { status: 'invalid' as const, errors };
230
- }
231
- }
232
- const actionToken = k.token() as ActionAttemptToken;
233
- const live = () => !k.disposed && k.records.get(handle.id) === record &&
234
- record.action.status === 'pending' && record.action.token === actionToken;
235
- const now = k.settle();
236
- record.action = freeze({ status: 'pending', token: actionToken });
237
- if (form) form.errors = NO_FORM_ERRORS;
238
- k.commit([{ type: 'action-started', handle, token: actionToken }], now);
239
- function failed(reason: unknown): void {
240
- if (!live()) return;
241
- let message = k.options.messages?.actionError ?? 'Action failed';
242
- if (action && action.kind !== 'link' && action.errorMessage) {
243
- try {
244
- const mapped = action.errorMessage(reason);
245
- if (typeof mapped !== 'string' || !mapped.trim()) throw new TypeError('Invalid action error message');
246
- message = mapped;
247
- } catch (error) {
248
- if (!k.disposed) k.failure('mapper-error', 'action.errorMessage', error, handle);
249
- }
250
- }
251
- if (!live()) return;
252
- const now = k.settle();
253
- record.action = freeze({ status: 'failed', token: actionToken, message });
254
- k.commit([{ type: 'action-failed', handle, token: actionToken, message }], now);
255
- }
256
- let result: Promise<unknown>;
257
- try {
258
- result = Promise.resolve(form ? form.action.run(freeze({ handle, token: actionToken, values: form.values }))
259
- : action.kind === 'command' ? action.run(freeze({ handle, token: actionToken })) : undefined);
260
- }
261
- catch (error) { result = Promise.reject(error); }
262
- void result.then(() => {
263
- if (live()) k.dismiss(handle, 'action-success');
264
- }, failed).catch((error: unknown) => {
265
- if (!k.disposed) k.failure('callback-error', 'action-settlement', error, handle);
266
- });
267
- return { status: 'started' as const };
268
- })),
269
- setFormValue: withLease(k.withRecord((record, name: string, value: string) => {
270
- const form = k.formOf(record);
271
- if (!form) return reject('no-form');
272
- if (typeof value !== 'string' || !form.action.fields.some((field) => field.name === name)) return reject('invalid-input');
273
- if (record.action.status === 'pending') return reject('already-pending');
274
- if (form.values[name] === value) return UNCHANGED;
275
- return k.mutate(() => {
276
- form.values = freeze({ ...form.values, [name]: value });
277
- // Editing a field clears its own message; the rest wait for the next submit.
278
- if (form.errors.fields[name] !== undefined) {
279
- const { [name]: _, ...fields } = form.errors.fields;
280
- form.errors = freeze({ form: form.errors.form, fields: freeze(fields) });
281
- }
282
- });
193
+ const result = invokeAction(k, record, 'user');
194
+ // The attempt's token is for callers without a snapshot; the renderer reads it there.
195
+ return result.status === 'started' ? { status: 'started' as const } : result;
283
196
  })),
197
+ setFormValue: withLease(k.withRecord((record, name: string, value: string) => setFormValue(k, record, name, value))),
284
198
  retryOperation: withLease(withOperation((operation) => {
285
- const result = operation.retry();
199
+ const result = operation.retry('user');
286
200
  return result.status === 'started' ? { status: 'started' as const, token: result.attempt.token } : result;
287
201
  })),
288
- cancelOperation: withLease(withOperation((operation, reason?: unknown) => operation.cancel(reason))),
202
+ cancelOperation: withLease(withOperation((operation, reason?: unknown) => operation.cancel(reason, 'user'))),
289
203
  reportDiagnostic: withLease((report: Diagnostic) => {
290
204
  if (!report || typeof report.source !== 'string' || typeof report.message !== 'string'
291
205
  || !['invalid-input', 'second-host', 'stale-host', 'mapper-error', 'callback-error', 'listener-error', 'cancellation-error', 'slot-error', 'formatter-error', 'invalid-container'].includes(report.code)) return reject('invalid-input');