@cloudlet/toast 1.3.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.
- package/CHANGELOG.md +112 -0
- package/README.md +5 -1
- package/dist/agent.d.ts +334 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +494 -0
- package/dist/agent.js.map +1 -0
- package/dist/contracts/events.d.ts +14 -0
- package/dist/contracts/events.d.ts.map +1 -1
- package/dist/contracts/internal.d.ts +16 -2
- package/dist/contracts/internal.d.ts.map +1 -1
- package/dist/contracts/notifications.d.ts +16 -0
- package/dist/contracts/notifications.d.ts.map +1 -1
- package/dist/contracts/snapshots.d.ts +8 -0
- package/dist/contracts/snapshots.d.ts.map +1 -1
- package/dist/core/actions.d.ts +69 -0
- package/dist/core/actions.d.ts.map +1 -0
- package/dist/core/actions.js +160 -0
- package/dist/core/actions.js.map +1 -0
- package/dist/core/agent-port.d.ts +116 -0
- package/dist/core/agent-port.d.ts.map +1 -0
- package/dist/core/agent-port.js +138 -0
- package/dist/core/agent-port.js.map +1 -0
- package/dist/core/bridge.d.ts.map +1 -1
- package/dist/core/bridge.js +120 -231
- package/dist/core/bridge.js.map +1 -1
- package/dist/core/controller.d.ts +44 -9
- package/dist/core/controller.d.ts.map +1 -1
- package/dist/core/controller.js +367 -292
- package/dist/core/controller.js.map +1 -1
- package/dist/core/validation.d.ts.map +1 -1
- package/dist/core/validation.js +103 -94
- package/dist/core/validation.js.map +1 -1
- package/dist/react/card.d.ts.map +1 -1
- package/dist/react/card.js +3 -3
- package/dist/react/card.js.map +1 -1
- package/dist/react/host.d.ts.map +1 -1
- package/dist/react/host.js +257 -246
- package/dist/react/host.js.map +1 -1
- package/dist/react.d.ts +7 -0
- package/dist/react.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/agent.ts +879 -0
- package/src/contracts/events.ts +15 -0
- package/src/contracts/internal.ts +14 -1
- package/src/contracts/notifications.ts +16 -0
- package/src/contracts/snapshots.ts +8 -0
- package/src/core/actions.ts +149 -0
- package/src/core/agent-port.ts +237 -0
- package/src/core/bridge.ts +19 -105
- package/src/core/controller.ts +183 -56
- package/src/core/validation.ts +6 -3
- package/src/react/card.tsx +4 -3
- package/src/react/host.tsx +26 -3
- package/src/react.ts +7 -0
package/src/core/controller.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ClearResult, CloudletController, ControllerOptions, FocusTarget } from '../contracts/controller.js';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ChangeSource, Diagnostic, SemanticEvent } from '../contracts/events.js';
|
|
3
3
|
import type { ActionAttemptToken, ControllerIdentity, Generation, HandleRejection, NotificationHandle, OperationAttemptToken, Unsubscribe } from '../contracts/identity.js';
|
|
4
4
|
import type {
|
|
5
5
|
FocusRequest, FocusRequestToken, ControllerDependencies, FormErrors, HostLease, OperationAnnouncementPhase, PresentationToken,
|
|
@@ -12,12 +12,14 @@ import { copyContent, patchContent, validateContent, validateOptions, validatePa
|
|
|
12
12
|
|
|
13
13
|
export interface OperationBinding {
|
|
14
14
|
readonly getSnapshot: () => OperationSnapshot;
|
|
15
|
-
|
|
16
|
-
readonly
|
|
15
|
+
/** `source` is who asked: the renderer for a person, the agent port for an agent. */
|
|
16
|
+
readonly retry: (source: ChangeSource) => AttemptStartResult<unknown>;
|
|
17
|
+
readonly cancel: (reason: unknown, source: ChangeSource) => CancelResult;
|
|
17
18
|
readonly detach: () => Delivery;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
// The controller stamps identity, order and time; whoever queues an event names its source.
|
|
22
|
+
export type EventPayload = SemanticEvent extends infer E ? E extends SemanticEvent ? Omit<E, 'controller' | 'sequence' | 'at'> : never : never;
|
|
21
23
|
export type Delivery = EventPayload | { readonly callback: () => void; readonly source: string; readonly handle?: NotificationHandle };
|
|
22
24
|
|
|
23
25
|
export interface RecordState<Slot> {
|
|
@@ -45,16 +47,23 @@ export interface RecordState<Slot> {
|
|
|
45
47
|
form: FormState | null;
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
/** Who wrote a field's value last: its default, the person or an agent. */
|
|
51
|
+
export type FieldAuthor = 'app' | 'user' | 'agent';
|
|
52
|
+
|
|
48
53
|
export interface FormState {
|
|
49
54
|
readonly action: FormAction;
|
|
50
55
|
values: Readonly<Record<string, string>>;
|
|
51
56
|
errors: FormErrors;
|
|
57
|
+
/** Own keys only; a field nobody wrote and without a default has none. */
|
|
58
|
+
authors: Readonly<Record<string, FieldAuthor>>;
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
/** Open Island state; `null` is the collapsed stack. */
|
|
55
62
|
export interface IslandState<Slot> {
|
|
56
63
|
readonly reason: IslandReason;
|
|
57
64
|
current: RecordState<Slot>;
|
|
65
|
+
/** Who opened the Island or changed its slide last. */
|
|
66
|
+
source: ChangeSource;
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
export interface AttentionCandidate<Slot> {
|
|
@@ -64,6 +73,15 @@ export interface AttentionCandidate<Slot> {
|
|
|
64
73
|
waiting: boolean;
|
|
65
74
|
}
|
|
66
75
|
|
|
76
|
+
/** One committed change as it was when it was made. */
|
|
77
|
+
export interface Announcement<Slot> {
|
|
78
|
+
/** What the commit queued, events and callbacks alike. */
|
|
79
|
+
readonly deliveries: readonly Delivery[];
|
|
80
|
+
readonly snapshot: RendererSnapshot<Slot>;
|
|
81
|
+
/** Who closed the Island last, as of this commit; null before the first close. */
|
|
82
|
+
readonly closedBy: ChangeSource | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
67
85
|
export const ATTENTION_DELAY = 600;
|
|
68
86
|
|
|
69
87
|
const freeze = Object.freeze;
|
|
@@ -73,7 +91,7 @@ const handles = new WeakSet<object>();
|
|
|
73
91
|
const kernels = new WeakMap<object, ControllerKernel<unknown>>();
|
|
74
92
|
let nextController = 0;
|
|
75
93
|
const EMPTY: readonly never[] = freeze([]);
|
|
76
|
-
const COLLAPSED: IslandSnapshot = freeze({ open: false, reason: null, current: null });
|
|
94
|
+
const COLLAPSED: IslandSnapshot = freeze({ open: false, reason: null, current: null, source: null });
|
|
77
95
|
const SERVER: ControllerSnapshot<never> = freeze({
|
|
78
96
|
controller: null, revision: 0, disposed: false, notifications: EMPTY, stack: EMPTY,
|
|
79
97
|
island: COLLAPSED, overflow: freeze({ count: 0, pending: false }),
|
|
@@ -91,8 +109,9 @@ export const reject = <Reason extends string>(reason: Reason) => ({ status: 'rej
|
|
|
91
109
|
type Rejected<Reason extends string> = { status: 'rejected'; reason: Reason };
|
|
92
110
|
|
|
93
111
|
/**
|
|
94
|
-
* Controller state and transactions the renderer bridge
|
|
95
|
-
*
|
|
112
|
+
* Controller state and transactions the renderer bridge (core/bridge.ts) and the shared action
|
|
113
|
+
* functions (core/actions.ts) need. Both travel outside the start bundle, which carries only this
|
|
114
|
+
* accessor object.
|
|
96
115
|
*/
|
|
97
116
|
export interface ControllerKernel<Slot> {
|
|
98
117
|
readonly options: ReturnType<typeof validateOptions>;
|
|
@@ -110,7 +129,26 @@ export interface ControllerKernel<Slot> {
|
|
|
110
129
|
hostDelay: number | undefined;
|
|
111
130
|
hostAutoDismiss: boolean | undefined;
|
|
112
131
|
island: IslandState<Slot> | null;
|
|
132
|
+
/** Closes the Island and remembers who closed it. */
|
|
133
|
+
readonly collapse: (source: ChangeSource) => void;
|
|
134
|
+
/**
|
|
135
|
+
* The commit being announced to `subscribe` listeners; null outside such a call. A listener may
|
|
136
|
+
* have committed again since, so the current snapshot can be ahead of this one. The events
|
|
137
|
+
* themselves are delivered after the listeners.
|
|
138
|
+
*/
|
|
139
|
+
readonly announced: Announcement<Slot> | null;
|
|
113
140
|
attention: AttentionCandidate<Slot> | null;
|
|
141
|
+
/** A person is on the Toaster: hover, focus, a gesture, or a draft in the open Island. */
|
|
142
|
+
readonly interacting: () => boolean;
|
|
143
|
+
/** The record's form holds values other than its defaults. */
|
|
144
|
+
readonly dirty: (record: RecordState<Slot>) => boolean;
|
|
145
|
+
/** Opens the Island on the record or moves the open one to it, as navigation does. */
|
|
146
|
+
readonly reach: (record: RecordState<Slot>, source: ChangeSource) => void;
|
|
147
|
+
/**
|
|
148
|
+
* Brings the record in front of the person without moving focus: at once, or after the attention
|
|
149
|
+
* delay once they stop interacting. Callers check that a host is attached.
|
|
150
|
+
*/
|
|
151
|
+
readonly summon: (record: RecordState<Slot>) => 'shown' | 'waiting' | 'unchanged';
|
|
114
152
|
focusRequest: FocusRequest | null;
|
|
115
153
|
focused: NotificationHandle | null;
|
|
116
154
|
overflowAnnounced: number;
|
|
@@ -128,9 +166,11 @@ export interface ControllerKernel<Slot> {
|
|
|
128
166
|
=> (handle: NotificationHandle, ...args: Args) => Result | Rejected<HandleRejection | 'disposed'>;
|
|
129
167
|
readonly autoDismissing: (host: boolean | undefined) => boolean;
|
|
130
168
|
readonly setHostAutoDismiss: (value: boolean | undefined) => void;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
readonly
|
|
169
|
+
// Every entry names who acts. There is no "current source" to inherit: a listener that calls
|
|
170
|
+
// back in passes its own.
|
|
171
|
+
readonly dismiss: (handle: NotificationHandle, reason: CloseReason, source: ChangeSource) => typeof APPLIED | Rejected<HandleRejection | 'disposed'>;
|
|
172
|
+
readonly acknowledgeOverflow: (source: ChangeSource) => typeof APPLIED | typeof UNCHANGED | Rejected<'disposed'>;
|
|
173
|
+
readonly clear: (reason: 'clear' | 'clear-button', source: ChangeSource) => ClearResult;
|
|
134
174
|
readonly reportRendererLoadError: (error: unknown) => void;
|
|
135
175
|
}
|
|
136
176
|
|
|
@@ -168,6 +208,8 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
168
208
|
let revision = 0;
|
|
169
209
|
let eventSequence = 0;
|
|
170
210
|
let island: IslandState<Slot> | null = null;
|
|
211
|
+
let islandClosedBy: ChangeSource | null = null;
|
|
212
|
+
let announced: Announcement<Slot> | null = null;
|
|
171
213
|
// The presented record: Island current when open, otherwise the newest record.
|
|
172
214
|
let presented: RecordState<Slot> | null = null;
|
|
173
215
|
let hostDelay: number | undefined;
|
|
@@ -176,6 +218,11 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
176
218
|
let attention: AttentionCandidate<Slot> | null = null;
|
|
177
219
|
let cancelAttention: Unsubscribe | null = null;
|
|
178
220
|
let attentionEpoch = 0;
|
|
221
|
+
// A record an agent asked the person to look at while they were busy. It is kept apart from the
|
|
222
|
+
// auto-open candidate, so neither replaces the other, and it goes first.
|
|
223
|
+
let summoned: AttentionCandidate<Slot> | null = null;
|
|
224
|
+
let cancelSummon: Unsubscribe | null = null;
|
|
225
|
+
let summonEpoch = 0;
|
|
179
226
|
let overflow = 0;
|
|
180
227
|
let overflowAnnounced = 0;
|
|
181
228
|
let disposed = false;
|
|
@@ -205,14 +252,17 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
205
252
|
const action = record.content.action;
|
|
206
253
|
if (action?.kind !== 'form') return record.form = null;
|
|
207
254
|
if (record.form?.action !== action) {
|
|
255
|
+
// `Object.fromEntries` defines own keys, so a field named `__proto__` stays a field.
|
|
208
256
|
record.form = { action, errors: NO_FORM_ERRORS,
|
|
209
|
-
values: freeze(Object.fromEntries(action.fields.map((field) => [field.name, field.defaultValue ?? ''])))
|
|
257
|
+
values: freeze(Object.fromEntries(action.fields.map((field) => [field.name, field.defaultValue ?? '']))),
|
|
258
|
+
authors: freeze(Object.fromEntries(action.fields.flatMap((field): [string, FieldAuthor][] => field.defaultValue ? [[field.name, 'app']] : []))) };
|
|
210
259
|
}
|
|
211
260
|
return record.form;
|
|
212
261
|
};
|
|
213
262
|
const dirty = (record: RecordState<Slot>) => {
|
|
214
263
|
const form = formOf(record);
|
|
215
|
-
return !!form && form.action.fields.some((field) =>
|
|
264
|
+
return !!form && form.action.fields.some((field) =>
|
|
265
|
+
(Object.hasOwn(form.values, field.name) ? form.values[field.name] : '') !== (field.defaultValue ?? ''));
|
|
216
266
|
};
|
|
217
267
|
// A draft in the open Island holds it like a hand on it: attention never switches it away.
|
|
218
268
|
const interacting = () => globalPauses.has('hover') || globalPauses.has('focus')
|
|
@@ -248,7 +298,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
248
298
|
|
|
249
299
|
function diagnostic(diagnosticValue: Diagnostic): void {
|
|
250
300
|
const value = freeze({ ...diagnosticValue });
|
|
251
|
-
enqueue([{ type: 'diagnostic', diagnostic: value }]);
|
|
301
|
+
enqueue([{ type: 'diagnostic', source: 'system', diagnostic: value }]);
|
|
252
302
|
}
|
|
253
303
|
function failure(code: Diagnostic['code'], source: string, error: unknown, handle?: NotificationHandle): void {
|
|
254
304
|
diagnostic({ code, source, message: `${source} failed`, error, ...(handle ? { handle } : {}) });
|
|
@@ -262,8 +312,14 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
262
312
|
}
|
|
263
313
|
}
|
|
264
314
|
function enqueue(deliveries: readonly Delivery[], notify = false, at = clock.now(), final = false, release = false): void {
|
|
315
|
+
// Taken now: by the time a queued batch runs, a listener may have committed again.
|
|
316
|
+
const announcement = notify ? { deliveries, snapshot: rendererSnapshot, closedBy: islandClosedBy } : null;
|
|
265
317
|
batches.push(() => {
|
|
266
|
-
if (
|
|
318
|
+
if (announcement) {
|
|
319
|
+
announced = announcement;
|
|
320
|
+
notifyListeners(listeners, 'subscribe');
|
|
321
|
+
announced = null;
|
|
322
|
+
}
|
|
267
323
|
for (const delivery of deliveries) {
|
|
268
324
|
if ('callback' in delivery) {
|
|
269
325
|
try { delivery.callback(); } catch (error) {
|
|
@@ -284,7 +340,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
284
340
|
}
|
|
285
341
|
}
|
|
286
342
|
// Close-callback diagnostics are queued before disposal releases observers.
|
|
287
|
-
if (final) enqueue([{ type: 'disposed' }], false, at, false, true);
|
|
343
|
+
if (final) enqueue([{ type: 'disposed', source: 'system' }], false, at, false, true);
|
|
288
344
|
if (release) {
|
|
289
345
|
listeners.clear(); eventListeners.clear();
|
|
290
346
|
for (const set of operationSubscriptions.keys()) set.clear();
|
|
@@ -328,7 +384,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
328
384
|
const at = settle();
|
|
329
385
|
const due = [...records.values()].filter((record) => record.activeSince !== null && record.remaining === 0);
|
|
330
386
|
const deliveries: Delivery[] = [];
|
|
331
|
-
for (const record of due) remove(record, 'timeout', deliveries);
|
|
387
|
+
for (const record of due) remove(record, 'timeout', deliveries, 'system');
|
|
332
388
|
// An early delivery only rearms; a late delivery closes exactly the due generations.
|
|
333
389
|
if (due.length) commit(deliveries, at);
|
|
334
390
|
else schedule(at);
|
|
@@ -344,7 +400,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
344
400
|
})));
|
|
345
401
|
snapshot = freeze({ controller: identity, revision, disposed, notifications,
|
|
346
402
|
stack: freeze(stackOrder().map((record) => record.handle)),
|
|
347
|
-
island: island ? freeze({ open: true, reason: island.reason, current: island.current.handle }) : COLLAPSED,
|
|
403
|
+
island: island ? freeze({ open: true, reason: island.reason, current: island.current.handle, source: island.source }) : COLLAPSED,
|
|
348
404
|
overflow: freeze({ count: overflow, pending: overflow > 0 }),
|
|
349
405
|
});
|
|
350
406
|
rendererSnapshot = freeze({ ...snapshot,
|
|
@@ -352,7 +408,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
352
408
|
focusRequest,
|
|
353
409
|
forms: freeze(current.flatMap((record) => {
|
|
354
410
|
const form = formOf(record);
|
|
355
|
-
return form ? [freeze({ handle: record.handle, values: form.values, errors: form.errors })] : [];
|
|
411
|
+
return form ? [freeze({ handle: record.handle, values: form.values, errors: form.errors, authors: form.authors })] : [];
|
|
356
412
|
})),
|
|
357
413
|
announcements: freeze({ overflowCount: overflowAnnounced,
|
|
358
414
|
notifications: freeze(current.map((record) => freeze({
|
|
@@ -367,6 +423,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
367
423
|
function commit(deliveries: readonly Delivery[] = [], now = clock.now(), final = false): void {
|
|
368
424
|
present();
|
|
369
425
|
schedule(now);
|
|
426
|
+
armSummon(now);
|
|
370
427
|
armAttention(now);
|
|
371
428
|
++revision;
|
|
372
429
|
publish();
|
|
@@ -426,17 +483,74 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
426
483
|
const at = clock.now();
|
|
427
484
|
if (at < candidate.due) { armAttention(at); return; }
|
|
428
485
|
cancelAttention = null;
|
|
429
|
-
// Expiry during hover/focus/gesture waits; the next release restarts the delay.
|
|
430
|
-
|
|
486
|
+
// Expiry during hover/focus/gesture waits; the next release restarts the delay. An agent's
|
|
487
|
+
// pending request goes first: reaching its record restarts the delay as well.
|
|
488
|
+
if (interacting() || summoned) { candidate.waiting = true; return; }
|
|
431
489
|
attention = null;
|
|
432
490
|
const record = candidate.record;
|
|
433
491
|
if (!wantsAttention(record) || island?.current === record) return;
|
|
434
492
|
mutate(() => {
|
|
435
|
-
if (island) island.current = record;
|
|
436
|
-
else island = { reason: 'attention', current: record };
|
|
493
|
+
if (island) { island.current = record; island.source = 'system'; }
|
|
494
|
+
else island = { reason: 'attention', current: record, source: 'system' };
|
|
437
495
|
});
|
|
438
496
|
});
|
|
439
497
|
}
|
|
498
|
+
const delay = () => hostDelay ?? options.attentionDelay ?? ATTENTION_DELAY;
|
|
499
|
+
/** Navigation: reaching the pending auto-open's record fulfils it, any other restarts its delay. */
|
|
500
|
+
function reach(record: RecordState<Slot>, source: ChangeSource): void {
|
|
501
|
+
if (island) { island.current = record; island.source = source; }
|
|
502
|
+
else island = { reason: 'attention', current: record, source };
|
|
503
|
+
if (attention?.record === record) attention = null;
|
|
504
|
+
else if (attention) { attention.waiting = false; attention.due = clock.now() + delay(); }
|
|
505
|
+
}
|
|
506
|
+
function stopSummon(): void {
|
|
507
|
+
++summonEpoch;
|
|
508
|
+
cancelSummon?.();
|
|
509
|
+
cancelSummon = null;
|
|
510
|
+
}
|
|
511
|
+
function summon(record: RecordState<Slot>): 'shown' | 'waiting' | 'unchanged' {
|
|
512
|
+
// The newest request wins, as among auto-open candidates.
|
|
513
|
+
const replaced = summoned !== null;
|
|
514
|
+
summoned = null;
|
|
515
|
+
stopSummon();
|
|
516
|
+
if (island?.current === record) {
|
|
517
|
+
// An auto-open held back for the replaced request may go ahead.
|
|
518
|
+
if (replaced) armAttention(clock.now());
|
|
519
|
+
return 'unchanged';
|
|
520
|
+
}
|
|
521
|
+
// A busy person is never interrupted: the request waits for the end of the interaction.
|
|
522
|
+
if (interacting()) {
|
|
523
|
+
summoned = { record, due: 0, waiting: true };
|
|
524
|
+
return 'waiting';
|
|
525
|
+
}
|
|
526
|
+
// Only an agent asks for a person; without its step the Island would not have opened.
|
|
527
|
+
mutate(() => reach(record, 'agent'));
|
|
528
|
+
return 'shown';
|
|
529
|
+
}
|
|
530
|
+
/** The attention automaton's waiting rule, for an agent's request. */
|
|
531
|
+
function armSummon(now: number): void {
|
|
532
|
+
stopSummon();
|
|
533
|
+
const request = summoned;
|
|
534
|
+
if (!request) return;
|
|
535
|
+
// Without a host nobody can see the record any more: the request is dropped.
|
|
536
|
+
if (disposed || lease === null) { summoned = null; return; }
|
|
537
|
+
if (request.waiting) {
|
|
538
|
+
if (interacting()) return;
|
|
539
|
+
request.waiting = false;
|
|
540
|
+
request.due = now + delay();
|
|
541
|
+
}
|
|
542
|
+
const epoch = summonEpoch;
|
|
543
|
+
cancelSummon = clock.schedule(request.due, () => {
|
|
544
|
+
if (disposed || epoch !== summonEpoch || summoned !== request) return;
|
|
545
|
+
const at = clock.now();
|
|
546
|
+
if (at < request.due) { armSummon(at); return; }
|
|
547
|
+
cancelSummon = null;
|
|
548
|
+
if (interacting()) { request.waiting = true; return; }
|
|
549
|
+
summoned = null;
|
|
550
|
+
if (island?.current === request.record) armAttention(at);
|
|
551
|
+
else mutate(() => reach(request.record, 'agent'));
|
|
552
|
+
});
|
|
553
|
+
}
|
|
440
554
|
const autoDismissing = (host: boolean | undefined) => (host ?? options.autoDismiss) !== false;
|
|
441
555
|
/** Off stops every budget; on starts each one in full. */
|
|
442
556
|
function setHostAutoDismiss(value: boolean | undefined): void {
|
|
@@ -450,45 +564,51 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
450
564
|
if (record.content.duration !== undefined) return record.content.duration;
|
|
451
565
|
return (record.kind === 'info' || record.kind === 'success') && !record.content.action ? 6000 : null;
|
|
452
566
|
}
|
|
453
|
-
function remove(record: RecordState<Slot>, reason: CloseReason, deliveries: Delivery[]): void {
|
|
567
|
+
function remove(record: RecordState<Slot>, reason: CloseReason, deliveries: Delivery[], source: ChangeSource): void {
|
|
454
568
|
if (island?.current === record) {
|
|
455
569
|
// The next older slide, else the next newer one, else the Island closes.
|
|
456
570
|
const order = stackOrder();
|
|
457
571
|
const index = order.indexOf(record);
|
|
458
572
|
const next = order[index + 1] ?? order[index - 1];
|
|
459
|
-
|
|
573
|
+
// The Island moved on because this record closed: whoever closed it moved it.
|
|
574
|
+
if (next) { island.current = next; island.source = source; } else collapse(source);
|
|
460
575
|
}
|
|
461
576
|
if (attention?.record === record) attention = null;
|
|
577
|
+
if (summoned?.record === record) summoned = null;
|
|
462
578
|
records.delete(record.handle.id);
|
|
463
579
|
if (record.operation) deliveries.push(record.operation.detach());
|
|
464
|
-
deliveries.push({ type: 'closed', handle: record.handle, reason });
|
|
580
|
+
deliveries.push({ type: 'closed', source, handle: record.handle, reason });
|
|
465
581
|
if (record.onClose) {
|
|
466
582
|
const onClose = record.onClose;
|
|
467
|
-
const notice = freeze({ handle: record.handle, reason });
|
|
583
|
+
const notice = freeze({ handle: record.handle, reason, source });
|
|
468
584
|
deliveries.push({ callback: () => onClose(notice), source: 'onClose', handle: record.handle });
|
|
469
585
|
}
|
|
470
586
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
587
|
+
function collapse(source: ChangeSource): void {
|
|
588
|
+
island = null;
|
|
589
|
+
islandClosedBy = source;
|
|
590
|
+
}
|
|
591
|
+
const dismiss = withRecord((record, reason: CloseReason, source: ChangeSource) =>
|
|
592
|
+
mutate((deliveries) => remove(record, reason, deliveries, source)));
|
|
593
|
+
function forgetOverflow(deliveries: Delivery[], source: ChangeSource) {
|
|
594
|
+
deliveries.push({ type: 'overflow-acknowledged', source, count: overflow });
|
|
475
595
|
overflow = overflowAnnounced = 0;
|
|
476
596
|
if (focusRequest?.target.kind === 'overflow') focusRequest = null;
|
|
477
597
|
}
|
|
478
|
-
function acknowledgeOverflow() {
|
|
598
|
+
function acknowledgeOverflow(source: ChangeSource) {
|
|
479
599
|
if (disposed) return reject('disposed');
|
|
480
600
|
if (!overflow) return UNCHANGED;
|
|
481
|
-
return mutate(forgetOverflow);
|
|
601
|
+
return mutate((deliveries) => forgetOverflow(deliveries, source));
|
|
482
602
|
}
|
|
483
603
|
/** `clear()`, or the Island's clear button, which also acknowledges the missed count: nothing is left. */
|
|
484
|
-
function clear(reason: 'clear' | 'clear-button'): ClearResult {
|
|
604
|
+
function clear(reason: 'clear' | 'clear-button', source: ChangeSource): ClearResult {
|
|
485
605
|
if (disposed) return reject('disposed');
|
|
486
606
|
const count = records.size;
|
|
487
607
|
const acknowledge = reason === 'clear-button' && overflow > 0;
|
|
488
608
|
if (count || acknowledge) {
|
|
489
609
|
mutate((deliveries) => {
|
|
490
|
-
for (const record of records.values()) remove(record, reason, deliveries);
|
|
491
|
-
if (acknowledge) forgetOverflow(deliveries);
|
|
610
|
+
for (const record of records.values()) remove(record, reason, deliveries, source);
|
|
611
|
+
if (acknowledge) forgetOverflow(deliveries, source);
|
|
492
612
|
});
|
|
493
613
|
}
|
|
494
614
|
return { status: 'cleared', count };
|
|
@@ -515,7 +635,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
515
635
|
let occurrences = existing.occurrences;
|
|
516
636
|
mutate((deliveries) => {
|
|
517
637
|
occurrences = existing.occurrences = Math.min(Number.MAX_SAFE_INTEGER, occurrences + 1);
|
|
518
|
-
deliveries.push({ type: 'deduplicated', handle: existing.handle, occurrences });
|
|
638
|
+
deliveries.push({ type: 'deduplicated', source: 'app', handle: existing.handle, occurrences });
|
|
519
639
|
});
|
|
520
640
|
return { status: 'deduplicated', handle: existing.handle, occurrences };
|
|
521
641
|
}
|
|
@@ -523,7 +643,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
523
643
|
mutate((deliveries) => {
|
|
524
644
|
overflow = Math.min(Number.MAX_SAFE_INTEGER, overflow + 1);
|
|
525
645
|
const overflowValue = freeze({ count: overflow, pending: true });
|
|
526
|
-
deliveries.push({ type: 'overflow', count: overflow });
|
|
646
|
+
deliveries.push({ type: 'overflow', source: 'app', count: overflow });
|
|
527
647
|
if (options.onOverflow) deliveries.push({ callback: () => options.onOverflow!(overflowValue), source: 'onOverflow' });
|
|
528
648
|
});
|
|
529
649
|
return reject('capacity');
|
|
@@ -559,10 +679,10 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
559
679
|
records.set(id, record);
|
|
560
680
|
initialize?.(record);
|
|
561
681
|
nominate(record, now);
|
|
562
|
-
const deliveries: Delivery[] = [{ type: 'admitted', handle }];
|
|
682
|
+
const deliveries: Delivery[] = [{ type: 'admitted', source: 'app', handle }];
|
|
563
683
|
if (record.operation) {
|
|
564
684
|
const operation = record.operation.getSnapshot();
|
|
565
|
-
deliveries.push({ type: 'operation-started', handle, token: operation.token, attempt: operation.attempt });
|
|
685
|
+
deliveries.push({ type: 'operation-started', source: 'app', handle, token: operation.token, attempt: operation.attempt });
|
|
566
686
|
}
|
|
567
687
|
commit(deliveries, now);
|
|
568
688
|
return { status: 'created', handle };
|
|
@@ -703,15 +823,16 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
703
823
|
const uiApplied = !!record && !!content;
|
|
704
824
|
const deliveries: Delivery[] = [
|
|
705
825
|
...(current === attempt ? [notify] : []),
|
|
706
|
-
|
|
826
|
+
// The outcome belongs to nobody, whoever started or retried the attempt.
|
|
827
|
+
{ type: 'operation-settled', source: 'system', handle: notification, token: attempt.context.token, status: attempt.status, uiApplied },
|
|
707
828
|
];
|
|
708
829
|
deliver(deliveries, record && content ? () => replaceContent(record, content,
|
|
709
830
|
attempt.status === 'fulfilled' ? 'success' : attempt.status === 'cancelled' ? 'warning' : 'error') : undefined);
|
|
710
831
|
}
|
|
711
|
-
function requestCancellation(attempt: Attempt, reason: unknown): void {
|
|
832
|
+
function requestCancellation(attempt: Attempt, reason: unknown, by: ChangeSource): void {
|
|
712
833
|
attempt.cancelRequested = true;
|
|
713
834
|
const record = live(attempt);
|
|
714
|
-
const deliveries: Delivery[] = [notify, { type: 'cancellation-requested', handle: notification, token: attempt.context.token }];
|
|
835
|
+
const deliveries: Delivery[] = [notify, { type: 'cancellation-requested', source: by, handle: notification, token: attempt.context.token }];
|
|
715
836
|
deliver(deliveries, record ? () => {
|
|
716
837
|
record.content = patchContent(record.content, { title: options.messages?.cancellationRequested ?? 'Cancellation requested' });
|
|
717
838
|
} : undefined);
|
|
@@ -727,15 +848,15 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
727
848
|
error.name = 'AbortError';
|
|
728
849
|
return error;
|
|
729
850
|
}
|
|
730
|
-
function cancel(reason: unknown = cancellationReason()): CancelResult {
|
|
851
|
+
function cancel(reason: unknown = cancellationReason(), by: ChangeSource): CancelResult {
|
|
731
852
|
if (disposed) return reject('disposed');
|
|
732
853
|
if (current.status !== 'pending') return { status: 'unchanged', reason: 'settled' };
|
|
733
854
|
if (!factory && !cancelCallback) return reject('not-cancellable');
|
|
734
855
|
if (current.cancelRequested) return { status: 'unchanged', reason: 'already-requested' };
|
|
735
|
-
requestCancellation(current, reason);
|
|
856
|
+
requestCancellation(current, reason, by);
|
|
736
857
|
return { status: 'requested' };
|
|
737
858
|
}
|
|
738
|
-
function start(restart: boolean): AttemptStartResult<T> {
|
|
859
|
+
function start(restart: boolean, by: ChangeSource): AttemptStartResult<T> {
|
|
739
860
|
if (disposed) return reject('disposed');
|
|
740
861
|
if (!factory) return reject('not-retryable');
|
|
741
862
|
if (current.status === 'pending' && !restart) return reject('already-pending');
|
|
@@ -743,17 +864,18 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
743
864
|
const next = createAttempt();
|
|
744
865
|
current = next; // Invalidate old ownership before any observer or abort callback.
|
|
745
866
|
const record = live(next);
|
|
746
|
-
const deliveries: Delivery[] = [notify, { type: 'operation-started', handle: notification, token: next.context.token, attempt: next.context.attempt }];
|
|
867
|
+
const deliveries: Delivery[] = [notify, { type: 'operation-started', source: by, handle: notification, token: next.context.token, attempt: next.context.attempt }];
|
|
747
868
|
deliver(deliveries, record && config ? () => {
|
|
748
869
|
record.operationAnnounced.clear();
|
|
749
870
|
replaceContent(record, config.pending, 'loading');
|
|
750
871
|
} : undefined);
|
|
751
|
-
|
|
872
|
+
// Only `restart()` finds the previous attempt pending, and only the app holds the handle.
|
|
873
|
+
if (previous.status === 'pending' && !previous.cancelRequested) requestCancellation(previous, cancellationReason(), by);
|
|
752
874
|
run(next);
|
|
753
875
|
return { status: 'started', attempt: next.public };
|
|
754
876
|
}
|
|
755
877
|
const binding: OperationBinding = {
|
|
756
|
-
getSnapshot, retry: () => start(false), cancel,
|
|
878
|
+
getSnapshot, retry: (by) => start(false, by), cancel,
|
|
757
879
|
detach: () => { attached = false; return notify; },
|
|
758
880
|
};
|
|
759
881
|
let admission: OperationAdmissionResult;
|
|
@@ -771,7 +893,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
771
893
|
if (admitted.status === 'deduplicated') throw new Error('Tracked operation cannot be deduplicated');
|
|
772
894
|
admission = admitted;
|
|
773
895
|
}
|
|
774
|
-
if (admission.status === 'rejected') emit([{ type: 'operation-started', handle: null, token: first.context.token, attempt: 1 }]);
|
|
896
|
+
if (admission.status === 'rejected') emit([{ type: 'operation-started', source: 'app', handle: null, token: first.context.token, attempt: 1 }]);
|
|
775
897
|
run(first);
|
|
776
898
|
return freeze({ ...first.public, admission: freeze(admission), getSnapshot,
|
|
777
899
|
subscribe(listener: () => void) {
|
|
@@ -779,7 +901,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
779
901
|
if (subscribers.size) operationSubscriptions.set(subscribers, notify);
|
|
780
902
|
return () => { unsubscribe(); if (!subscribers.size) operationSubscriptions.delete(subscribers); };
|
|
781
903
|
},
|
|
782
|
-
cancel, retry: () => start(false), restart: () => start(true),
|
|
904
|
+
cancel: (reason?: unknown) => cancel(reason, 'app'), retry: () => start(false, 'app'), restart: () => start(true, 'app'),
|
|
783
905
|
});
|
|
784
906
|
}
|
|
785
907
|
|
|
@@ -812,12 +934,12 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
812
934
|
if (durationChanged || previousBudget !== nextBudget) record.remaining = nextBudget;
|
|
813
935
|
if (focusRequest?.target.kind === 'notification' && focusRequest.target.handle === handle && focusRequest.target.control === 'action' && !content.action) focusRequest = null;
|
|
814
936
|
if (attention?.record === record && !wantsAttention(record)) attention = null;
|
|
815
|
-
deliveries.push({ type: 'updated', handle });
|
|
937
|
+
deliveries.push({ type: 'updated', source: 'app', handle });
|
|
816
938
|
});
|
|
817
939
|
}),
|
|
818
940
|
getHandle: (id) => !disposed && typeof id === 'string' ? records.get(id)?.handle : undefined,
|
|
819
|
-
dismiss: (handle) => dismiss(handle, 'programmatic'),
|
|
820
|
-
clear: () => clear('clear'),
|
|
941
|
+
dismiss: (handle) => dismiss(handle, 'programmatic', 'app'),
|
|
942
|
+
clear: () => clear('clear', 'app'),
|
|
821
943
|
requestFocus(target: FocusTarget = { kind: 'anchor' }) {
|
|
822
944
|
if (disposed) return reject('disposed');
|
|
823
945
|
if (target?.kind === 'notification') {
|
|
@@ -843,7 +965,7 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
843
965
|
});
|
|
844
966
|
return { status: 'requested' };
|
|
845
967
|
},
|
|
846
|
-
acknowledgeOverflow,
|
|
968
|
+
acknowledgeOverflow: () => acknowledgeOverflow('app'),
|
|
847
969
|
getSnapshot: () => snapshot,
|
|
848
970
|
getServerSnapshot: () => SERVER,
|
|
849
971
|
subscribe: (listener) => subscribe(listeners, listener),
|
|
@@ -852,13 +974,15 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
852
974
|
if (disposed) return;
|
|
853
975
|
mutate((deliveries) => {
|
|
854
976
|
disposed = true;
|
|
855
|
-
for (const record of records.values()) remove(record, 'dispose', deliveries);
|
|
977
|
+
for (const record of records.values()) remove(record, 'dispose', deliveries, 'system');
|
|
856
978
|
// Detached/admission-rejected handles also lose their capabilities.
|
|
857
979
|
for (const notify of operationSubscriptions.values()) if (!deliveries.includes(notify)) deliveries.push(notify);
|
|
858
980
|
lease = lastLease = null;
|
|
859
|
-
island
|
|
981
|
+
if (island) collapse('system');
|
|
860
982
|
attention = null;
|
|
861
983
|
stopAttention();
|
|
984
|
+
summoned = null;
|
|
985
|
+
stopSummon();
|
|
862
986
|
focusRequest = null;
|
|
863
987
|
globalPauses.clear();
|
|
864
988
|
overflow = overflowAnnounced = 0;
|
|
@@ -878,10 +1002,13 @@ export function createCloudletCore<Slot = never, Signal extends CancellationSign
|
|
|
878
1002
|
get hostDelay() { return hostDelay; }, set hostDelay(value) { hostDelay = value; },
|
|
879
1003
|
get hostAutoDismiss() { return hostAutoDismiss; }, set hostAutoDismiss(value) { hostAutoDismiss = value; },
|
|
880
1004
|
get island() { return island; }, set island(value) { island = value; },
|
|
1005
|
+
get announced() { return announced; },
|
|
1006
|
+
collapse,
|
|
881
1007
|
get attention() { return attention; }, set attention(value) { attention = value; },
|
|
882
1008
|
get focusRequest() { return focusRequest; }, set focusRequest(value) { focusRequest = value; },
|
|
883
1009
|
get focused() { return focused; }, set focused(value) { focused = value; },
|
|
884
1010
|
get overflowAnnounced() { return overflowAnnounced; }, set overflowAnnounced(value) { overflowAnnounced = value; },
|
|
1011
|
+
interacting, dirty, reach, summon,
|
|
885
1012
|
token, diagnostic, failure, mutate, commit, settle, assign, newest, formOf, handleError, withRecord,
|
|
886
1013
|
autoDismissing, setHostAutoDismiss, dismiss, acknowledgeOverflow, clear,
|
|
887
1014
|
reportRendererLoadError(error) {
|
package/src/core/validation.ts
CHANGED
|
@@ -17,6 +17,7 @@ const messageKeys = [
|
|
|
17
17
|
] as const;
|
|
18
18
|
const fieldKeys = ['name', 'kind', 'label', 'placeholder', 'defaultValue', 'required', 'maxLength', 'autoComplete', 'inputMode'];
|
|
19
19
|
const inputModes = ['text', 'email', 'numeric', 'decimal', 'tel', 'url', 'search'];
|
|
20
|
+
const agentPermissions = ['allow', 'confirm', 'deny'] as const;
|
|
20
21
|
const noIssues: readonly InputIssue[] = Object.freeze([]);
|
|
21
22
|
const owns = Object.hasOwn;
|
|
22
23
|
const at = (path: string, key: PropertyKey): string => path ? `${path}.${String(key)}` : String(key);
|
|
@@ -138,9 +139,11 @@ function formFields(value: unknown, issues: InputIssue[]): void {
|
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
function action(value: unknown, issues: InputIssue[]): void {
|
|
141
|
-
const data = record(value, 'action', ['kind', 'label', 'run', 'errorMessage', 'href', 'target', 'rel', 'fields', 'validate'], issues);
|
|
142
|
+
const data = record(value, 'action', ['kind', 'label', 'run', 'errorMessage', 'href', 'target', 'rel', 'fields', 'validate', 'agent'], issues);
|
|
142
143
|
if (!data) return;
|
|
143
144
|
fields(data, issues, 'action', [['label', 'nonempty string', true]]);
|
|
145
|
+
if (data.kind === 'link') forbidden(data, issues, 'action', ['agent'], 'a link action');
|
|
146
|
+
else if (data.kind === 'command' || data.kind === 'form') fields(data, issues, 'action', [['agent', agentPermissions]]);
|
|
144
147
|
if (data.kind === 'form') {
|
|
145
148
|
forbidden(data, issues, 'action', ['href', 'target', 'rel'], 'a form action');
|
|
146
149
|
fields(data, issues, 'action', [['run', 'function', true], ['errorMessage', 'function'], ['validate', 'function']]);
|
|
@@ -282,11 +285,11 @@ const copiedActions = new WeakSet<PrimaryAction>();
|
|
|
282
285
|
|
|
283
286
|
function copyAction(value: PrimaryAction): PrimaryAction {
|
|
284
287
|
if (copiedActions.has(value)) return value;
|
|
285
|
-
const copy = value.kind === 'command' ? copyFields(value, ['kind', 'label', 'run'], ['errorMessage'])
|
|
288
|
+
const copy = value.kind === 'command' ? copyFields(value, ['kind', 'label', 'run'], ['errorMessage', 'agent'])
|
|
286
289
|
: value.kind === 'link' ? copyFields(value, ['kind', 'label', 'href'], ['target', 'rel'])
|
|
287
290
|
: copyFields({ ...value, fields: Object.freeze(value.fields.map((field) => copyFields(field, ['name', 'kind', 'label'],
|
|
288
291
|
['placeholder', 'defaultValue', 'required', 'maxLength', 'autoComplete', 'inputMode']))) },
|
|
289
|
-
['kind', 'fields', 'label', 'run'], ['errorMessage', 'validate']);
|
|
292
|
+
['kind', 'fields', 'label', 'run'], ['errorMessage', 'validate', 'agent']);
|
|
290
293
|
copiedActions.add(copy);
|
|
291
294
|
return copy;
|
|
292
295
|
}
|
package/src/react/card.tsx
CHANGED
|
@@ -256,14 +256,15 @@ function Form({ action, draft, notification, bridge, lease, part, titleId, actio
|
|
|
256
256
|
if (pending) return;
|
|
257
257
|
const result = bridge.invokeAction(lease, notification.handle);
|
|
258
258
|
if (result.status !== 'invalid') return;
|
|
259
|
-
const first = action.fields.find(field => result.errors.fields
|
|
259
|
+
const first = action.fields.find(field => Object.hasOwn(result.errors.fields, field.name));
|
|
260
260
|
if (first) fields.current.get(first.name)?.focus();
|
|
261
261
|
};
|
|
262
262
|
const field = (field: FormField, index: number, last: boolean) => {
|
|
263
263
|
const fieldId = `${id}-${index}`;
|
|
264
|
-
|
|
264
|
+
// Own keys only: a field named `constructor` must not read `Object.prototype`.
|
|
265
|
+
const error = Object.hasOwn(draft.errors.fields, field.name) ? draft.errors.fields[field.name] : undefined;
|
|
265
266
|
const props = {
|
|
266
|
-
id: fieldId, name: field.name, className: part('field', 'cloudlet-field'), defaultValue: draft.values[field.name]
|
|
267
|
+
id: fieldId, name: field.name, className: part('field', 'cloudlet-field'), defaultValue: Object.hasOwn(draft.values, field.name) ? draft.values[field.name] : '',
|
|
267
268
|
placeholder: field.placeholder ?? field.label, required: field.required, maxLength: field.maxLength,
|
|
268
269
|
autoComplete: field.autoComplete, inputMode: field.inputMode, readOnly: pending,
|
|
269
270
|
'aria-invalid': error === undefined ? undefined : true, 'aria-describedby': error === undefined ? undefined : `${fieldId}-error`,
|