@cloudlet/toast 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +271 -0
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/THIRD_PARTY_NOTICES.md +149 -0
- package/dist/contracts/controller.d.ts +71 -0
- package/dist/contracts/controller.d.ts.map +1 -0
- package/dist/contracts/events.d.ts +72 -0
- package/dist/contracts/events.d.ts.map +1 -0
- package/dist/contracts/identity.d.ts +32 -0
- package/dist/contracts/identity.d.ts.map +1 -0
- package/dist/contracts/internal.d.ts +188 -0
- package/dist/contracts/internal.d.ts.map +1 -0
- package/dist/contracts/notifications.d.ts +101 -0
- package/dist/contracts/notifications.d.ts.map +1 -0
- package/dist/contracts/operations.d.ts +76 -0
- package/dist/contracts/operations.d.ts.map +1 -0
- package/dist/contracts/snapshots.d.ts +48 -0
- package/dist/contracts/snapshots.d.ts.map +1 -0
- package/dist/core/bridge.d.ts +5 -0
- package/dist/core/bridge.d.ts.map +1 -0
- package/dist/core/bridge.js +277 -0
- package/dist/core/bridge.js.map +1 -0
- package/dist/core/controller.d.ts +117 -0
- package/dist/core/controller.d.ts.map +1 -0
- package/dist/core/controller.js +787 -0
- package/dist/core/controller.js.map +1 -0
- package/dist/core/validation.d.ts +17 -0
- package/dist/core/validation.d.ts.map +1 -0
- package/dist/core/validation.js +298 -0
- package/dist/core/validation.js.map +1 -0
- package/dist/create-cloudlet.d.ts +5 -0
- package/dist/create-cloudlet.d.ts.map +1 -0
- package/dist/create-cloudlet.js +34 -0
- package/dist/create-cloudlet.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/react/announcer.d.ts +15 -0
- package/dist/react/announcer.d.ts.map +1 -0
- package/dist/react/announcer.js +140 -0
- package/dist/react/announcer.js.map +1 -0
- package/dist/react/bindings.d.ts +17 -0
- package/dist/react/bindings.d.ts.map +1 -0
- package/dist/react/bindings.js +47 -0
- package/dist/react/bindings.js.map +1 -0
- package/dist/react/card.d.ts +58 -0
- package/dist/react/card.d.ts.map +1 -0
- package/dist/react/card.js +300 -0
- package/dist/react/card.js.map +1 -0
- package/dist/react/generators.d.ts +46 -0
- package/dist/react/generators.d.ts.map +1 -0
- package/dist/react/generators.js +115 -0
- package/dist/react/generators.js.map +1 -0
- package/dist/react/gestures.d.ts +50 -0
- package/dist/react/gestures.d.ts.map +1 -0
- package/dist/react/gestures.js +239 -0
- package/dist/react/gestures.js.map +1 -0
- package/dist/react/host.d.ts +37 -0
- package/dist/react/host.d.ts.map +1 -0
- package/dist/react/host.js +816 -0
- package/dist/react/host.js.map +1 -0
- package/dist/react/motion.d.ts +206 -0
- package/dist/react/motion.d.ts.map +1 -0
- package/dist/react/motion.js +414 -0
- package/dist/react/motion.js.map +1 -0
- package/dist/react/plan.d.ts +45 -0
- package/dist/react/plan.d.ts.map +1 -0
- package/dist/react/plan.js +58 -0
- package/dist/react/plan.js.map +1 -0
- package/dist/react/renderer.d.ts +7 -0
- package/dist/react/renderer.d.ts.map +1 -0
- package/dist/react/renderer.js +62 -0
- package/dist/react/renderer.js.map +1 -0
- package/dist/react/shape.d.ts +7 -0
- package/dist/react/shape.d.ts.map +1 -0
- package/dist/react/shape.js +21 -0
- package/dist/react/shape.js.map +1 -0
- package/dist/react/viewport.d.ts +15 -0
- package/dist/react/viewport.d.ts.map +1 -0
- package/dist/react/viewport.js +16 -0
- package/dist/react/viewport.js.map +1 -0
- package/dist/react.d.ts +176 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +2 -0
- package/dist/styles.css +1 -0
- package/package.json +19 -4
- package/src/contracts/controller.ts +61 -0
- package/src/contracts/events.ts +48 -0
- package/src/contracts/identity.ts +27 -0
- package/src/contracts/internal.ts +150 -0
- package/src/contracts/notifications.ts +104 -0
- package/src/contracts/operations.ts +71 -0
- package/src/contracts/snapshots.ts +59 -0
- package/src/core/bridge.ts +280 -0
- package/src/core/controller.ts +853 -0
- package/src/core/validation.ts +290 -0
- package/src/create-cloudlet.ts +43 -0
- package/src/index.ts +34 -0
- package/src/react/announcer.ts +198 -0
- package/src/react/bindings.tsx +56 -0
- package/src/react/card.tsx +222 -0
- package/src/react/generators.ts +274 -0
- package/src/react/gestures.ts +296 -0
- package/src/react/host.tsx +953 -0
- package/src/react/motion.ts +484 -0
- package/src/react/plan.ts +101 -0
- package/src/react/renderer.tsx +77 -0
- package/src/react/shape.ts +33 -0
- package/src/react/viewport.ts +10 -0
- package/src/react.ts +190 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** Package-private contracts; never exported from the package entrypoint. */
|
|
2
|
+
import type { CloudletController, ControllerOptions, FocusTarget, MutationResult } from './controller.js';
|
|
3
|
+
import type { Diagnostic, SemanticEvent } from './events.js';
|
|
4
|
+
import type { ActionAttemptToken, HandleRejection, NotificationHandle, OperationAttemptToken, Unsubscribe } from './identity.js';
|
|
5
|
+
import type { CloseReason } from './notifications.js';
|
|
6
|
+
import type { CancellationSignal } from './operations.js';
|
|
7
|
+
import type { ControllerSnapshot, IslandReason } from './snapshots.js';
|
|
8
|
+
|
|
9
|
+
declare const leaseBrand: unique symbol;
|
|
10
|
+
declare const presentationBrand: unique symbol;
|
|
11
|
+
declare const focusBrand: unique symbol;
|
|
12
|
+
|
|
13
|
+
export type HostLease = string & { readonly [leaseBrand]: never };
|
|
14
|
+
export type PresentationToken = string & { readonly [presentationBrand]: never };
|
|
15
|
+
export type FocusRequestToken = string & { readonly [focusBrand]: never };
|
|
16
|
+
|
|
17
|
+
export interface MonotonicClock {
|
|
18
|
+
readonly now: () => number;
|
|
19
|
+
/** Absolute deadline; never invokes callback synchronously. Cancellation is idempotent. */
|
|
20
|
+
readonly schedule: (deadline: number, callback: () => void) => Unsubscribe;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CancellationSource<Signal extends CancellationSignal = CancellationSignal> {
|
|
24
|
+
readonly signal: Signal;
|
|
25
|
+
readonly abort: (reason?: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ControllerDependencies<Signal extends CancellationSignal = CancellationSignal> {
|
|
29
|
+
readonly clock: MonotonicClock;
|
|
30
|
+
readonly createCancellation: () => CancellationSource<Signal>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type CreateCloudletCore = <Slot = never, Signal extends CancellationSignal = CancellationSignal>(options: ControllerOptions | undefined, dependencies: ControllerDependencies<Signal>) => CloudletController<Slot, Signal>;
|
|
34
|
+
|
|
35
|
+
/** Stable package-private lookup for controllers minted by this package instance. */
|
|
36
|
+
export type GetRendererBridge = <Slot, Signal extends CancellationSignal>(controller: CloudletController<Slot, Signal>) => RendererBridge<Slot>;
|
|
37
|
+
|
|
38
|
+
/** Per-attachment settings; omitted fields fall back to ControllerOptions. */
|
|
39
|
+
export interface HostOptions {
|
|
40
|
+
/** Finite nonnegative milliseconds; overrides ControllerOptions.attentionDelay while attached. */
|
|
41
|
+
readonly attentionDelay?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The single presented record (`location: 'current'`) and its presentation token. */
|
|
45
|
+
export interface RendererPresentation {
|
|
46
|
+
readonly handle: NotificationHandle;
|
|
47
|
+
/** Null without an active host. */
|
|
48
|
+
readonly token: PresentationToken | null;
|
|
49
|
+
readonly readable: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface FocusRequest {
|
|
53
|
+
readonly token: FocusRequestToken;
|
|
54
|
+
readonly target: FocusTarget;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type OperationAnnouncementPhase = 'started' | 'fulfilled' | 'rejected' | 'cancelled' | 'cancel-requested';
|
|
58
|
+
|
|
59
|
+
export interface NotificationAnnouncementLedger {
|
|
60
|
+
readonly handle: NotificationHandle;
|
|
61
|
+
readonly queued: boolean;
|
|
62
|
+
readonly firstPresentation: boolean;
|
|
63
|
+
readonly occurrences: number;
|
|
64
|
+
readonly action: { readonly token: ActionAttemptToken; readonly status: 'pending' | 'failed' } | null;
|
|
65
|
+
readonly operation: {
|
|
66
|
+
readonly token: OperationAttemptToken;
|
|
67
|
+
readonly phases: readonly OperationAnnouncementPhase[];
|
|
68
|
+
} | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AnnouncementLedgerSnapshot {
|
|
72
|
+
readonly notifications: readonly NotificationAnnouncementLedger[];
|
|
73
|
+
readonly overflowCount: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type AnnouncementAcknowledgement =
|
|
77
|
+
| { readonly kind: 'queued' | 'first-presentation'; readonly handle: NotificationHandle }
|
|
78
|
+
| { readonly kind: 'occurrences'; readonly handle: NotificationHandle; readonly count: number }
|
|
79
|
+
| { readonly kind: 'action'; readonly handle: NotificationHandle; readonly token: ActionAttemptToken; readonly status: 'pending' | 'failed' }
|
|
80
|
+
| { readonly kind: 'operation'; readonly handle: NotificationHandle; readonly token: OperationAttemptToken; readonly phase: OperationAnnouncementPhase }
|
|
81
|
+
| { readonly kind: 'overflow'; readonly count: number };
|
|
82
|
+
|
|
83
|
+
export interface RendererSnapshot<Slot = never> extends ControllerSnapshot<Slot> {
|
|
84
|
+
readonly presentation: RendererPresentation | null;
|
|
85
|
+
readonly focusRequest: FocusRequest | null;
|
|
86
|
+
readonly announcements: AnnouncementLedgerSnapshot;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type AttachResult =
|
|
90
|
+
| { readonly status: 'attached'; readonly lease: HostLease }
|
|
91
|
+
| { readonly status: 'rejected'; readonly reason: 'disposed' | 'host-exists' | 'invalid-input' };
|
|
92
|
+
|
|
93
|
+
export type BridgeRejection = HandleRejection | 'disposed' | 'stale-lease' | 'not-current' | 'stale-presentation' | 'stale-operation-token' | 'stale-focus-request' | 'invalid-input' | 'empty' | 'island-closed';
|
|
94
|
+
export type BridgeResult =
|
|
95
|
+
| { readonly status: 'applied' }
|
|
96
|
+
| { readonly status: 'unchanged' }
|
|
97
|
+
| { readonly status: 'rejected'; readonly reason: BridgeRejection };
|
|
98
|
+
|
|
99
|
+
export type PauseChange =
|
|
100
|
+
| { readonly scope: 'host'; readonly reason: 'hover' | 'focus' | 'document-hidden' | 'window-blur'; readonly active: boolean }
|
|
101
|
+
/** Island navigation (swipe/drag) is reported as `gesture` on the current record. */
|
|
102
|
+
| { readonly scope: 'notification'; readonly handle: NotificationHandle; readonly reason: 'gesture' | 'content-transition'; readonly active: boolean };
|
|
103
|
+
|
|
104
|
+
export type RendererCloseReason = Extract<CloseReason, 'close-button' | 'swipe' | 'navigation'>;
|
|
105
|
+
|
|
106
|
+
export type ActionResult =
|
|
107
|
+
| { readonly status: 'started' }
|
|
108
|
+
| { readonly status: 'rejected'; readonly reason: BridgeRejection | 'no-command' | 'already-pending' };
|
|
109
|
+
|
|
110
|
+
export type RetryOperationResult =
|
|
111
|
+
| { readonly status: 'started'; readonly token: OperationAttemptToken }
|
|
112
|
+
| { readonly status: 'rejected'; readonly reason: BridgeRejection | 'not-retryable' | 'already-pending' | 'no-operation' };
|
|
113
|
+
|
|
114
|
+
export type CancelOperationResult =
|
|
115
|
+
| { readonly status: 'requested' }
|
|
116
|
+
| { readonly status: 'unchanged'; readonly reason: 'already-requested' | 'settled' }
|
|
117
|
+
| { readonly status: 'rejected'; readonly reason: BridgeRejection | 'not-cancellable' | 'no-operation' };
|
|
118
|
+
|
|
119
|
+
export interface RendererBridge<Slot = never> {
|
|
120
|
+
/** Immutable presentation fallback; user options remain controller-owned. */
|
|
121
|
+
readonly renderError: string;
|
|
122
|
+
/** A rejected portal has no lease. This reports without acquiring host ownership. */
|
|
123
|
+
readonly reportInvalidContainer: (message: string, error?: unknown) => void;
|
|
124
|
+
readonly attach: (options?: HostOptions) => AttachResult;
|
|
125
|
+
readonly detach: (lease: HostLease) => BridgeResult;
|
|
126
|
+
readonly getSnapshot: () => RendererSnapshot<Slot>;
|
|
127
|
+
readonly getServerSnapshot: () => RendererSnapshot<Slot>;
|
|
128
|
+
readonly subscribe: (listener: () => void) => Unsubscribe;
|
|
129
|
+
readonly onEvent: (listener: (event: SemanticEvent) => void) => Unsubscribe;
|
|
130
|
+
/** Without handle current becomes the top record. When already open only current changes. */
|
|
131
|
+
readonly openIsland: (lease: HostLease, reason: IslandReason, handle?: NotificationHandle) => BridgeResult;
|
|
132
|
+
readonly collapseIsland: (lease: HostLease) => BridgeResult;
|
|
133
|
+
/** Overrides ControllerOptions.autoDismiss while attached; undefined restores it. */
|
|
134
|
+
readonly setAutoDismiss: (lease: HostLease, value?: boolean) => BridgeResult;
|
|
135
|
+
readonly navigate: (lease: HostLease, handle: NotificationHandle) => BridgeResult;
|
|
136
|
+
readonly setPause: (lease: HostLease, change: PauseChange) => BridgeResult;
|
|
137
|
+
readonly setFocused: (lease: HostLease, handle: NotificationHandle | null) => BridgeResult;
|
|
138
|
+
readonly beginPresentation: (lease: HostLease, handle: NotificationHandle, previous: PresentationToken) =>
|
|
139
|
+
| { readonly status: 'started'; readonly token: PresentationToken }
|
|
140
|
+
| { readonly status: 'rejected'; readonly reason: BridgeRejection };
|
|
141
|
+
readonly acknowledgePresentation: (lease: HostLease, handle: NotificationHandle, token: PresentationToken) => BridgeResult;
|
|
142
|
+
readonly acknowledgeFocus: (lease: HostLease, token: FocusRequestToken, outcome: 'focused' | 'unavailable') => BridgeResult;
|
|
143
|
+
readonly acknowledgeOverflow: (lease: HostLease) => BridgeResult;
|
|
144
|
+
readonly acknowledgeAnnouncements: (lease: HostLease, acknowledgements: readonly AnnouncementAcknowledgement[]) => BridgeResult;
|
|
145
|
+
readonly dismiss: (lease: HostLease, handle: NotificationHandle, reason: RendererCloseReason) => MutationResult | { readonly status: 'rejected'; readonly reason: 'stale-lease' };
|
|
146
|
+
readonly invokeAction: (lease: HostLease, handle: NotificationHandle) => ActionResult;
|
|
147
|
+
readonly retryOperation: (lease: HostLease, handle: NotificationHandle, expected: OperationAttemptToken) => RetryOperationResult;
|
|
148
|
+
readonly cancelOperation: (lease: HostLease, handle: NotificationHandle, expected: OperationAttemptToken, reason?: unknown) => CancelOperationResult;
|
|
149
|
+
readonly reportDiagnostic: (lease: HostLease, diagnostic: Diagnostic) => BridgeResult;
|
|
150
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { ActionAttemptToken, InputIssue, NotificationHandle } from './identity.js';
|
|
2
|
+
|
|
3
|
+
export type NotificationKind = 'info' | 'success' | 'loading' | 'warning' | 'error';
|
|
4
|
+
export type Priority = 'normal' | 'high';
|
|
5
|
+
export type AnnouncementPriority = 'polite' | 'assertive' | 'off';
|
|
6
|
+
export type Duration = number | null;
|
|
7
|
+
/** Auto-open rule: `auto` opens for an action, a retryable operation or a description; `open` always; `stack` never. */
|
|
8
|
+
export type Attention = 'auto' | 'open' | 'stack';
|
|
9
|
+
|
|
10
|
+
/** Core treats Slot as opaque and never traverses or freezes its payload. */
|
|
11
|
+
export type Description<Slot = never> =
|
|
12
|
+
| { readonly kind: 'text'; readonly text: string }
|
|
13
|
+
| { readonly kind: 'rich'; readonly content: Slot; readonly announcement: string };
|
|
14
|
+
|
|
15
|
+
export type Progress =
|
|
16
|
+
| { readonly kind: 'indeterminate'; readonly label?: string }
|
|
17
|
+
| { readonly kind: 'determinate'; readonly value: number; readonly label?: string };
|
|
18
|
+
|
|
19
|
+
export interface ActionContext {
|
|
20
|
+
readonly handle: NotificationHandle;
|
|
21
|
+
readonly token: ActionAttemptToken;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CommandAction {
|
|
25
|
+
readonly kind: 'command';
|
|
26
|
+
readonly label: string;
|
|
27
|
+
readonly run: (context: ActionContext) => unknown;
|
|
28
|
+
readonly errorMessage?: (error: unknown) => string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface NavigationAction {
|
|
32
|
+
readonly kind: 'link';
|
|
33
|
+
readonly label: string;
|
|
34
|
+
readonly href: string;
|
|
35
|
+
readonly target?: '_self' | '_blank' | '_parent' | '_top';
|
|
36
|
+
readonly rel?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type PrimaryAction = CommandAction | NavigationAction;
|
|
40
|
+
|
|
41
|
+
export interface NotificationContent<Slot = never> {
|
|
42
|
+
readonly title: string;
|
|
43
|
+
readonly description?: Description<Slot>;
|
|
44
|
+
readonly icon?: Slot;
|
|
45
|
+
readonly progress?: Progress;
|
|
46
|
+
readonly action?: PrimaryAction;
|
|
47
|
+
readonly announcementPriority?: AnnouncementPriority;
|
|
48
|
+
/** Omitted = policy default; null = persistent; number = readable milliseconds. */
|
|
49
|
+
readonly duration?: Duration;
|
|
50
|
+
/** Omitted = `auto`. */
|
|
51
|
+
readonly attention?: Attention;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type CloseReason =
|
|
55
|
+
| 'timeout'
|
|
56
|
+
| 'programmatic'
|
|
57
|
+
| 'close-button'
|
|
58
|
+
| 'swipe'
|
|
59
|
+
| 'action-success'
|
|
60
|
+
| 'navigation'
|
|
61
|
+
| 'clear'
|
|
62
|
+
| 'dispose';
|
|
63
|
+
|
|
64
|
+
export interface CloseNotice {
|
|
65
|
+
readonly handle: NotificationHandle;
|
|
66
|
+
readonly reason: CloseReason;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface NotificationSpec<Slot = never> extends NotificationContent<Slot> {
|
|
70
|
+
readonly id?: string;
|
|
71
|
+
readonly kind?: NotificationKind;
|
|
72
|
+
readonly priority?: Priority;
|
|
73
|
+
readonly dedupeKey?: string;
|
|
74
|
+
readonly onClose?: (notice: CloseNotice) => void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Omitted preserves; null removes optional content. IDs/dedupe keys are immutable. */
|
|
78
|
+
export interface NotificationPatch<Slot = never> {
|
|
79
|
+
readonly title?: string;
|
|
80
|
+
readonly kind?: NotificationKind;
|
|
81
|
+
readonly description?: Description<Slot> | null;
|
|
82
|
+
readonly icon?: Slot | null;
|
|
83
|
+
readonly progress?: Progress | null;
|
|
84
|
+
readonly action?: PrimaryAction | null;
|
|
85
|
+
readonly priority?: Priority;
|
|
86
|
+
readonly announcementPriority?: AnnouncementPriority;
|
|
87
|
+
readonly duration?: Duration;
|
|
88
|
+
readonly attention?: Attention;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type AdmissionRejection = 'disposed' | 'invalid-input' | 'id-conflict' | 'capacity';
|
|
92
|
+
|
|
93
|
+
export type AdmissionResult =
|
|
94
|
+
| { readonly status: 'created'; readonly handle: NotificationHandle }
|
|
95
|
+
| { readonly status: 'deduplicated'; readonly handle: NotificationHandle; readonly occurrences: number }
|
|
96
|
+
| { readonly status: 'rejected'; readonly reason: AdmissionRejection; readonly issues?: readonly InputIssue[] };
|
|
97
|
+
|
|
98
|
+
/** Track never participates in deduplication. */
|
|
99
|
+
export type OperationAdmissionResult = Exclude<AdmissionResult, { readonly status: 'deduplicated' }>;
|
|
100
|
+
|
|
101
|
+
export interface OverflowSnapshot {
|
|
102
|
+
readonly count: number;
|
|
103
|
+
readonly pending: boolean;
|
|
104
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { NotificationHandle, OperationAttemptToken, Unsubscribe } from './identity.js';
|
|
2
|
+
import type { NotificationContent, OperationAdmissionResult, Priority, CloseNotice } from './notifications.js';
|
|
3
|
+
|
|
4
|
+
/** Minimal abort port: native AbortSignal can implement it; core requires no DOM lib. */
|
|
5
|
+
export interface CancellationSignal {
|
|
6
|
+
readonly aborted: boolean;
|
|
7
|
+
readonly reason: unknown;
|
|
8
|
+
readonly throwIfAborted: () => void;
|
|
9
|
+
readonly addEventListener: (type: 'abort', listener: () => void, options?: { readonly once?: boolean }) => void;
|
|
10
|
+
readonly removeEventListener: (type: 'abort', listener: () => void) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface OperationContext<Signal extends CancellationSignal = CancellationSignal> {
|
|
14
|
+
readonly signal: Signal;
|
|
15
|
+
readonly token: OperationAttemptToken;
|
|
16
|
+
readonly attempt: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type OperationFactory<T, Signal extends CancellationSignal = CancellationSignal> = (context: OperationContext<Signal>) => T | PromiseLike<T>;
|
|
20
|
+
export type OperationSource<T, Signal extends CancellationSignal = CancellationSignal> = PromiseLike<T> | OperationFactory<T, Signal>;
|
|
21
|
+
export type OperationStatus = 'pending' | 'fulfilled' | 'rejected' | 'cancelled';
|
|
22
|
+
|
|
23
|
+
export interface OperationSnapshot {
|
|
24
|
+
readonly token: OperationAttemptToken;
|
|
25
|
+
readonly attempt: number;
|
|
26
|
+
readonly status: OperationStatus;
|
|
27
|
+
readonly cancelRequested: boolean;
|
|
28
|
+
readonly canCancel: boolean;
|
|
29
|
+
readonly canRetry: boolean;
|
|
30
|
+
readonly notification: NotificationHandle | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TrackOptions<T, Slot = never, Signal extends CancellationSignal = CancellationSignal> {
|
|
34
|
+
readonly id?: string;
|
|
35
|
+
readonly priority?: Priority;
|
|
36
|
+
readonly pending: Omit<NotificationContent<Slot>, 'duration'> & { readonly duration?: never };
|
|
37
|
+
readonly success: NotificationContent<Slot> | ((value: T) => NotificationContent<Slot>);
|
|
38
|
+
readonly error: NotificationContent<Slot> | ((error: unknown) => NotificationContent<Slot>);
|
|
39
|
+
readonly cancelled?: NotificationContent<Slot> | ((reason: unknown) => NotificationContent<Slot>);
|
|
40
|
+
/** Optional cooperative cancellation for an already-created promise. */
|
|
41
|
+
readonly cancel?: (reason: unknown) => void;
|
|
42
|
+
readonly isCancellation?: (error: unknown, context: OperationContext<Signal>) => boolean;
|
|
43
|
+
readonly onClose?: (notice: CloseNotice) => void;
|
|
44
|
+
readonly dedupeKey?: never;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface OperationAttempt<T> {
|
|
48
|
+
readonly token: OperationAttemptToken;
|
|
49
|
+
readonly attempt: number;
|
|
50
|
+
/** Preserves the business value/rejection, including cancellation rejection. */
|
|
51
|
+
readonly result: Promise<T>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type AttemptStartResult<T> =
|
|
55
|
+
| { readonly status: 'started'; readonly attempt: OperationAttempt<T> }
|
|
56
|
+
| { readonly status: 'rejected'; readonly reason: 'disposed' | 'not-retryable' | 'already-pending' };
|
|
57
|
+
|
|
58
|
+
export type CancelResult =
|
|
59
|
+
| { readonly status: 'requested' }
|
|
60
|
+
| { readonly status: 'unchanged'; readonly reason: 'already-requested' | 'settled' }
|
|
61
|
+
| { readonly status: 'rejected'; readonly reason: 'disposed' | 'not-cancellable' };
|
|
62
|
+
|
|
63
|
+
export interface OperationHandle<T> extends OperationAttempt<T> {
|
|
64
|
+
/** Initial admission is immutable, independent of every attempt's result. */
|
|
65
|
+
readonly admission: OperationAdmissionResult;
|
|
66
|
+
readonly getSnapshot: () => OperationSnapshot;
|
|
67
|
+
readonly subscribe: (listener: () => void) => Unsubscribe;
|
|
68
|
+
readonly cancel: (reason?: unknown) => CancelResult;
|
|
69
|
+
readonly retry: () => AttemptStartResult<T>;
|
|
70
|
+
readonly restart: () => AttemptStartResult<T>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ActionAttemptToken, ControllerIdentity, NotificationHandle } from './identity.js';
|
|
2
|
+
import type { NotificationContent, NotificationKind, OverflowSnapshot, Priority } from './notifications.js';
|
|
3
|
+
import type { OperationSnapshot } from './operations.js';
|
|
4
|
+
|
|
5
|
+
export type PauseReason =
|
|
6
|
+
| 'inactive'
|
|
7
|
+
| 'no-host'
|
|
8
|
+
| 'presentation-pending'
|
|
9
|
+
| 'hover'
|
|
10
|
+
| 'focus'
|
|
11
|
+
| 'gesture'
|
|
12
|
+
| 'action-pending'
|
|
13
|
+
| 'content-transition'
|
|
14
|
+
| 'island'
|
|
15
|
+
| 'document-hidden'
|
|
16
|
+
| 'window-blur';
|
|
17
|
+
|
|
18
|
+
export type ActionSnapshot =
|
|
19
|
+
| { readonly status: 'idle' }
|
|
20
|
+
| { readonly status: 'pending'; readonly token: ActionAttemptToken }
|
|
21
|
+
| { readonly status: 'failed'; readonly token: ActionAttemptToken; readonly message: string };
|
|
22
|
+
|
|
23
|
+
/** `current` is the one presented record: the top pill when collapsed, `island.current` when open. */
|
|
24
|
+
export type NotificationLocation = 'stack' | 'current';
|
|
25
|
+
|
|
26
|
+
export interface NotificationSnapshot<Slot = never> {
|
|
27
|
+
readonly handle: NotificationHandle;
|
|
28
|
+
readonly kind: NotificationKind;
|
|
29
|
+
readonly content: Readonly<NotificationContent<Slot>>;
|
|
30
|
+
readonly priority: Priority;
|
|
31
|
+
readonly occurrences: number;
|
|
32
|
+
readonly location: NotificationLocation;
|
|
33
|
+
readonly remainingMs: number | null;
|
|
34
|
+
readonly pauseReasons: readonly PauseReason[];
|
|
35
|
+
readonly operation: OperationSnapshot | null;
|
|
36
|
+
readonly action: ActionSnapshot;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type IslandReason = 'user' | 'attention';
|
|
40
|
+
|
|
41
|
+
export interface IslandSnapshot {
|
|
42
|
+
readonly open: boolean;
|
|
43
|
+
/** Null exactly when the Island is closed. */
|
|
44
|
+
readonly reason: IslandReason | null;
|
|
45
|
+
/** Null exactly when the Island is closed. */
|
|
46
|
+
readonly current: NotificationHandle | null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ControllerSnapshot<Slot = never> {
|
|
50
|
+
readonly controller: ControllerIdentity | null;
|
|
51
|
+
readonly revision: number;
|
|
52
|
+
readonly disposed: boolean;
|
|
53
|
+
/** Admission order, oldest first. */
|
|
54
|
+
readonly notifications: readonly NotificationSnapshot<Slot>[];
|
|
55
|
+
/** The same records in presentation order: newest first, index 0 is the top pill. */
|
|
56
|
+
readonly stack: readonly NotificationHandle[];
|
|
57
|
+
readonly island: IslandSnapshot;
|
|
58
|
+
readonly overflow: OverflowSnapshot;
|
|
59
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import type { Diagnostic } from '../contracts/events.js';
|
|
2
|
+
import type { ActionAttemptToken, NotificationHandle, OperationAttemptToken } from '../contracts/identity.js';
|
|
3
|
+
import type {
|
|
4
|
+
AnnouncementAcknowledgement, BridgeRejection, BridgeResult, FocusRequestToken, HostLease, HostOptions, PauseChange,
|
|
5
|
+
PresentationToken, RendererBridge,
|
|
6
|
+
} from '../contracts/internal.js';
|
|
7
|
+
import type { CancellationSignal } from '../contracts/operations.js';
|
|
8
|
+
import type { CloudletController } from '../contracts/controller.js';
|
|
9
|
+
import type { IslandReason, PauseReason } from '../contracts/snapshots.js';
|
|
10
|
+
import {
|
|
11
|
+
APPLIED, ATTENTION_DELAY, RENDERER_SERVER, UNCHANGED, getControllerKernel, reject,
|
|
12
|
+
type ControllerKernel, type OperationBinding, type RecordState,
|
|
13
|
+
} from './controller.js';
|
|
14
|
+
import { validDelay } from './validation.js';
|
|
15
|
+
|
|
16
|
+
// The renderer's side of a controller. It is imported only by react/renderer.tsx, so it travels
|
|
17
|
+
// in the lazily loaded renderer chunk; the controller keeps a kernel of the state it touches.
|
|
18
|
+
const freeze = Object.freeze;
|
|
19
|
+
const bridges = new WeakMap<object, RendererBridge<unknown>>();
|
|
20
|
+
|
|
21
|
+
export function getRendererBridge<Slot, Signal extends CancellationSignal>(controller: CloudletController<Slot, Signal>): RendererBridge<Slot> {
|
|
22
|
+
let bridge = bridges.get(controller);
|
|
23
|
+
if (!bridge) {
|
|
24
|
+
bridge = createRendererBridge(getControllerKernel<Slot>(controller)) as RendererBridge<unknown>;
|
|
25
|
+
bridges.set(controller, bridge);
|
|
26
|
+
}
|
|
27
|
+
return bridge as RendererBridge<Slot>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function createRendererBridge<Slot>(k: ControllerKernel<Slot>): RendererBridge<Slot> {
|
|
31
|
+
const leaseError = (value: HostLease): 'disposed' | 'stale-lease' | undefined =>
|
|
32
|
+
k.disposed ? 'disposed' : k.lease === null || value !== k.lease ? 'stale-lease' : undefined;
|
|
33
|
+
const currentError = (handle: NotificationHandle): BridgeRejection | undefined =>
|
|
34
|
+
k.handleError(handle) ?? (k.records.get(handle.id) !== k.presented ? 'not-current' : undefined);
|
|
35
|
+
function withCurrent<Args extends unknown[], Result>(action: (record: RecordState<Slot>, ...args: Args) => Result) {
|
|
36
|
+
return k.withRecord((record, ...args: Args) => record !== k.presented ? reject('not-current') : action(record, ...args));
|
|
37
|
+
}
|
|
38
|
+
function withOperation<Args extends unknown[], Result>(action: (operation: OperationBinding, ...args: Args) => Result) {
|
|
39
|
+
return k.withRecord((record, expected: OperationAttemptToken, ...args: Args) => {
|
|
40
|
+
const operation = record.operation;
|
|
41
|
+
return !operation ? reject('no-operation') : operation.getSnapshot().token !== expected
|
|
42
|
+
? reject('stale-operation-token') : action(operation, ...args);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function validHostOptions(value: HostOptions | undefined): boolean {
|
|
46
|
+
if (value === undefined) return true;
|
|
47
|
+
return value !== null && typeof value === 'object' && Object.keys(value).every((key) => key === 'attentionDelay')
|
|
48
|
+
&& (value.attentionDelay === undefined || validDelay(value.attentionDelay));
|
|
49
|
+
}
|
|
50
|
+
// Preserve the bridge's lease-first validation order in one entry guard.
|
|
51
|
+
function withLease<Args extends unknown[], Result>(action: (...args: Args) => Result) {
|
|
52
|
+
return (value: HostLease, ...args: Args) => {
|
|
53
|
+
const error = leaseError(value);
|
|
54
|
+
return error ? reject(error) : action(...args);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const bridge = freeze<RendererBridge<Slot>>({
|
|
58
|
+
renderError: k.options.messages?.renderError ?? 'Content unavailable',
|
|
59
|
+
reportInvalidContainer(message, error) {
|
|
60
|
+
if (!k.disposed) k.diagnostic({ code: 'invalid-container', source: 'Toaster.container', message, ...(error === undefined ? {} : { error }) });
|
|
61
|
+
},
|
|
62
|
+
attach(hostOptions) {
|
|
63
|
+
if (k.disposed) return reject('disposed');
|
|
64
|
+
if (k.lease !== null) {
|
|
65
|
+
k.diagnostic({ code: 'second-host', source: 'attach', message: 'A renderer is already attached' });
|
|
66
|
+
return reject('host-exists');
|
|
67
|
+
}
|
|
68
|
+
if (!validHostOptions(hostOptions)) return reject('invalid-input');
|
|
69
|
+
const nextLease = k.token() as HostLease;
|
|
70
|
+
k.mutate(() => {
|
|
71
|
+
k.lease = k.lastLease = nextLease;
|
|
72
|
+
k.hostDelay = hostOptions?.attentionDelay;
|
|
73
|
+
if (k.presented) k.assign(k.presented);
|
|
74
|
+
});
|
|
75
|
+
return { status: 'attached', lease: nextLease };
|
|
76
|
+
},
|
|
77
|
+
detach(value) {
|
|
78
|
+
if (k.disposed) return reject('disposed');
|
|
79
|
+
if (k.lease === null && k.lastLease !== null && value === k.lastLease) return UNCHANGED;
|
|
80
|
+
const error = leaseError(value);
|
|
81
|
+
if (error) return reject(error);
|
|
82
|
+
return k.mutate(() => {
|
|
83
|
+
k.lease = null;
|
|
84
|
+
k.hostDelay = undefined;
|
|
85
|
+
k.setHostAutoDismiss(undefined);
|
|
86
|
+
k.focusRequest = null;
|
|
87
|
+
k.focused = null;
|
|
88
|
+
k.globalPauses.clear();
|
|
89
|
+
for (const record of k.records.values()) { record.pauses.clear(); record.readable = false; record.presentation = null; }
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
getSnapshot: () => k.rendererSnapshot,
|
|
93
|
+
getServerSnapshot: () => RENDERER_SERVER,
|
|
94
|
+
subscribe: k.subscribe,
|
|
95
|
+
onEvent: k.onEvent,
|
|
96
|
+
openIsland: withLease((reason: IslandReason, handle?: NotificationHandle) => {
|
|
97
|
+
if (reason !== 'user' && reason !== 'attention') return reject('invalid-input');
|
|
98
|
+
let record: RecordState<Slot> | null;
|
|
99
|
+
if (handle === undefined) record = k.newest();
|
|
100
|
+
else {
|
|
101
|
+
const error = k.handleError(handle);
|
|
102
|
+
if (error) return reject(error);
|
|
103
|
+
record = k.records.get(handle.id)!;
|
|
104
|
+
}
|
|
105
|
+
if (!record) return reject('empty');
|
|
106
|
+
const target = record;
|
|
107
|
+
if (k.island?.current === target) return UNCHANGED;
|
|
108
|
+
// An open Island only moves its slider; the opening reason is kept.
|
|
109
|
+
return k.mutate(() => {
|
|
110
|
+
if (k.island) k.island.current = target;
|
|
111
|
+
else k.island = { reason, current: target };
|
|
112
|
+
});
|
|
113
|
+
}),
|
|
114
|
+
collapseIsland: withLease(() => k.island ? k.mutate(() => { k.island = null; }) : UNCHANGED),
|
|
115
|
+
setAutoDismiss: withLease((value?: boolean) => {
|
|
116
|
+
if (value !== undefined && typeof value !== 'boolean') return reject('invalid-input');
|
|
117
|
+
if (k.autoDismissing(value) === k.autoDismissing(k.hostAutoDismiss)) { k.hostAutoDismiss = value; return UNCHANGED; }
|
|
118
|
+
return k.mutate(() => k.setHostAutoDismiss(value));
|
|
119
|
+
}),
|
|
120
|
+
navigate: withLease(k.withRecord((record) => {
|
|
121
|
+
if (!k.island) return reject('island-closed');
|
|
122
|
+
if (k.island.current === record) return UNCHANGED;
|
|
123
|
+
return k.mutate(() => {
|
|
124
|
+
k.island!.current = record;
|
|
125
|
+
// Navigation is interaction: a pending auto-open restarts its delay,
|
|
126
|
+
// and reaching the initiator by hand fulfils it.
|
|
127
|
+
if (k.attention?.record === record) k.attention = null;
|
|
128
|
+
else if (k.attention) { k.attention.waiting = false; k.attention.due = k.clock.now() + (k.hostDelay ?? k.options.attentionDelay ?? ATTENTION_DELAY); }
|
|
129
|
+
});
|
|
130
|
+
})),
|
|
131
|
+
setPause: withLease((change: PauseChange) => {
|
|
132
|
+
if (!change || typeof change.active !== 'boolean') return reject('invalid-input');
|
|
133
|
+
let set: Set<PauseReason>;
|
|
134
|
+
if (change.scope === 'host') {
|
|
135
|
+
if (!['hover', 'focus', 'document-hidden', 'window-blur'].includes(change.reason)) return reject('invalid-input');
|
|
136
|
+
set = k.globalPauses;
|
|
137
|
+
} else if (change.scope === 'notification') {
|
|
138
|
+
const error = currentError(change.handle);
|
|
139
|
+
if (error) return reject(error);
|
|
140
|
+
if (!['gesture', 'content-transition'].includes(change.reason)) return reject('invalid-input');
|
|
141
|
+
set = k.records.get(change.handle.id)!.pauses;
|
|
142
|
+
} else return reject('invalid-input');
|
|
143
|
+
if (set.has(change.reason) === change.active) return UNCHANGED;
|
|
144
|
+
return k.mutate(() => {
|
|
145
|
+
if (change.active) set.add(change.reason); else set.delete(change.reason);
|
|
146
|
+
});
|
|
147
|
+
}),
|
|
148
|
+
// Informational since the stack has no slots to protect; it has no observable effect.
|
|
149
|
+
setFocused: withLease((handle: NotificationHandle | null) => {
|
|
150
|
+
const error = (handle === null ? undefined : currentError(handle));
|
|
151
|
+
if (error) return reject(error);
|
|
152
|
+
if (k.focused === handle) return UNCHANGED;
|
|
153
|
+
k.focused = handle;
|
|
154
|
+
return APPLIED;
|
|
155
|
+
}),
|
|
156
|
+
beginPresentation: withLease(withCurrent((record, previous: PresentationToken) => {
|
|
157
|
+
if (record.presentation !== previous) return reject('stale-presentation');
|
|
158
|
+
const presentation = k.token() as PresentationToken;
|
|
159
|
+
k.mutate(() => { record.presentation = presentation; record.readable = false; });
|
|
160
|
+
return { status: 'started' as const, token: presentation };
|
|
161
|
+
})),
|
|
162
|
+
acknowledgePresentation: withLease(withCurrent((record, presentation: PresentationToken) => {
|
|
163
|
+
if (record.presentation !== presentation) return reject('stale-presentation');
|
|
164
|
+
if (record.readable) return UNCHANGED;
|
|
165
|
+
return k.mutate((deliveries) => {
|
|
166
|
+
deliveries.push({ type: 'presented', handle: record.handle, firstPresentation: !record.everPresented });
|
|
167
|
+
record.readable = record.everPresented = true;
|
|
168
|
+
});
|
|
169
|
+
})),
|
|
170
|
+
acknowledgeFocus: withLease((focusToken: FocusRequestToken, outcome: 'focused' | 'unavailable') => {
|
|
171
|
+
if (k.focusRequest?.token !== focusToken) return reject('stale-focus-request');
|
|
172
|
+
if (outcome !== 'focused' && outcome !== 'unavailable') return reject('invalid-input');
|
|
173
|
+
return k.mutate(() => { k.focusRequest = null; });
|
|
174
|
+
}),
|
|
175
|
+
acknowledgeOverflow: withLease(() => {
|
|
176
|
+
return k.acknowledgeOverflow();
|
|
177
|
+
}),
|
|
178
|
+
acknowledgeAnnouncements: withLease((acknowledgements: readonly AnnouncementAcknowledgement[]) => {
|
|
179
|
+
return acknowledgeAnnouncements(acknowledgements);
|
|
180
|
+
}),
|
|
181
|
+
dismiss: withLease((handle: NotificationHandle, reason: 'close-button' | 'swipe' | 'navigation') => {
|
|
182
|
+
const error = k.handleError(handle);
|
|
183
|
+
if (error) return reject(error);
|
|
184
|
+
// Renderer close reasons are a closed protocol, never an exit callback.
|
|
185
|
+
if (!['close-button', 'swipe', 'navigation'].includes(reason)) return reject('invalid-input');
|
|
186
|
+
return k.dismiss(handle, reason);
|
|
187
|
+
}),
|
|
188
|
+
invokeAction: withLease(k.withRecord((record) => {
|
|
189
|
+
const handle = record.handle;
|
|
190
|
+
const action = record.content.action;
|
|
191
|
+
if (!action || action.kind !== 'command') return reject('no-command');
|
|
192
|
+
if (record.action.status === 'pending') return reject('already-pending');
|
|
193
|
+
const actionToken = k.token() as ActionAttemptToken;
|
|
194
|
+
const live = () => !k.disposed && k.records.get(handle.id) === record &&
|
|
195
|
+
record.action.status === 'pending' && record.action.token === actionToken;
|
|
196
|
+
const now = k.settle();
|
|
197
|
+
record.action = freeze({ status: 'pending', token: actionToken });
|
|
198
|
+
k.commit([{ type: 'action-started', handle, token: actionToken }], now);
|
|
199
|
+
function failed(reason: unknown): void {
|
|
200
|
+
if (!live()) return;
|
|
201
|
+
let message = k.options.messages?.actionError ?? 'Action failed';
|
|
202
|
+
if (action?.kind === 'command' && action.errorMessage) {
|
|
203
|
+
try {
|
|
204
|
+
const mapped = action.errorMessage(reason);
|
|
205
|
+
if (typeof mapped !== 'string' || !mapped.trim()) throw new TypeError('Invalid action error message');
|
|
206
|
+
message = mapped;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (!k.disposed) k.failure('mapper-error', 'action.errorMessage', error, handle);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (!live()) return;
|
|
212
|
+
const now = k.settle();
|
|
213
|
+
record.action = freeze({ status: 'failed', token: actionToken, message });
|
|
214
|
+
k.commit([{ type: 'action-failed', handle, token: actionToken, message }], now);
|
|
215
|
+
}
|
|
216
|
+
let result: Promise<unknown>;
|
|
217
|
+
try { result = Promise.resolve(action.run(freeze({ handle, token: actionToken }))); }
|
|
218
|
+
catch (error) { result = Promise.reject(error); }
|
|
219
|
+
void result.then(() => {
|
|
220
|
+
if (live()) k.dismiss(handle, 'action-success');
|
|
221
|
+
}, failed).catch((error: unknown) => {
|
|
222
|
+
if (!k.disposed) k.failure('callback-error', 'action-settlement', error, handle);
|
|
223
|
+
});
|
|
224
|
+
return { status: 'started' as const };
|
|
225
|
+
})),
|
|
226
|
+
retryOperation: withLease(withOperation((operation) => {
|
|
227
|
+
const result = operation.retry();
|
|
228
|
+
return result.status === 'started' ? { status: 'started' as const, token: result.attempt.token } : result;
|
|
229
|
+
})),
|
|
230
|
+
cancelOperation: withLease(withOperation((operation, reason?: unknown) => operation.cancel(reason))),
|
|
231
|
+
reportDiagnostic: withLease((report: Diagnostic) => {
|
|
232
|
+
if (!report || typeof report.source !== 'string' || typeof report.message !== 'string'
|
|
233
|
+
|| !['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');
|
|
234
|
+
k.diagnostic(report);
|
|
235
|
+
return APPLIED;
|
|
236
|
+
}),
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
function acknowledgeAnnouncements(acks: readonly AnnouncementAcknowledgement[]): BridgeResult {
|
|
240
|
+
if (!Array.isArray(acks)) return reject('invalid-input');
|
|
241
|
+
// Stage only effective changes; no ledger mutation occurs until every
|
|
242
|
+
// acknowledgement has passed validation. Repeated counts remain monotonic.
|
|
243
|
+
const changes: (() => void)[] = [];
|
|
244
|
+
const validCount = (count: number, maximum: number) => Number.isSafeInteger(count) && count >= 0 && count <= maximum;
|
|
245
|
+
for (const ack of acks) {
|
|
246
|
+
if (!ack || typeof ack !== 'object') return reject('invalid-input');
|
|
247
|
+
if (ack.kind === 'overflow') {
|
|
248
|
+
if (!validCount(ack.count, k.overflow)) return reject('invalid-input');
|
|
249
|
+
if (ack.count > k.overflowAnnounced) changes.push(() => { k.overflowAnnounced = Math.max(k.overflowAnnounced, ack.count); });
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const error = k.handleError(ack.handle);
|
|
253
|
+
if (error) return reject(error);
|
|
254
|
+
const record = k.records.get(ack.handle.id)!;
|
|
255
|
+
if (ack.kind === 'operation') {
|
|
256
|
+
const operation = record.operation?.getSnapshot();
|
|
257
|
+
if (!operation || operation.token !== ack.token) return reject('stale-operation-token');
|
|
258
|
+
if (!['started', 'fulfilled', 'rejected', 'cancelled', 'cancel-requested'].includes(ack.phase)) return reject('invalid-input');
|
|
259
|
+
if (ack.phase === 'cancel-requested' ? !operation.cancelRequested
|
|
260
|
+
: ack.phase !== 'started' && ack.phase !== operation.status) return reject('invalid-input');
|
|
261
|
+
if (!record.operationAnnounced.has(ack.phase)) changes.push(() => { record.operationAnnounced.add(ack.phase); });
|
|
262
|
+
} else if (ack.kind === 'action') {
|
|
263
|
+
if (record.action.status === 'idle' || record.action.token !== ack.token || record.action.status !== ack.status) return reject('invalid-input');
|
|
264
|
+
if (record.actionAnnounced?.token !== ack.token || record.actionAnnounced?.status !== ack.status) {
|
|
265
|
+
changes.push(() => { record.actionAnnounced = freeze({ token: ack.token, status: ack.status }); });
|
|
266
|
+
}
|
|
267
|
+
} else if (ack.kind === 'occurrences') {
|
|
268
|
+
if (!validCount(ack.count, record.occurrences)) return reject('invalid-input');
|
|
269
|
+
if (ack.count > record.occurrencesAnnounced) changes.push(() => { record.occurrencesAnnounced = Math.max(record.occurrencesAnnounced, ack.count); });
|
|
270
|
+
} else if (ack.kind === 'queued' || ack.kind === 'first-presentation') {
|
|
271
|
+
const key = ack.kind === 'queued' ? 'queuedAnnounced' : 'firstAnnounced';
|
|
272
|
+
if (!record[key]) changes.push(() => { record[key] = true; });
|
|
273
|
+
} else return reject('invalid-input');
|
|
274
|
+
}
|
|
275
|
+
if (!changes.length) return UNCHANGED;
|
|
276
|
+
return k.mutate(() => { for (const apply of changes) apply(); });
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return bridge;
|
|
280
|
+
}
|