@cloudlet/toast 1.0.0-rc.3 → 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 +42 -7
- package/README.md +5 -4
- package/dist/contracts/events.d.ts +1 -1
- package/dist/contracts/events.d.ts.map +1 -1
- 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 +113 -3
- package/dist/core/controller.d.ts.map +1 -1
- package/dist/core/controller.js +416 -583
- package/dist/core/controller.js.map +1 -1
- package/dist/react/announcer.d.ts.map +1 -1
- package/dist/react/announcer.js +29 -23
- package/dist/react/announcer.js.map +1 -1
- package/dist/react/bindings.d.ts +9 -0
- package/dist/react/bindings.d.ts.map +1 -1
- package/dist/react/bindings.js +32 -69
- package/dist/react/bindings.js.map +1 -1
- package/dist/react/card.d.ts +1 -1
- package/dist/react/card.js.map +1 -1
- package/dist/react/generators.d.ts +2 -2
- package/dist/react/generators.js.map +1 -1
- package/dist/react/gestures.js.map +1 -1
- package/dist/react/host.d.ts.map +1 -1
- package/dist/react/host.js +1 -1
- package/dist/react/host.js.map +1 -1
- package/dist/react/motion.d.ts +3 -3
- package/dist/react/motion.js.map +1 -1
- package/dist/react/plan.d.ts +1 -1
- package/dist/react/plan.d.ts.map +1 -1
- package/dist/react/plan.js.map +1 -1
- 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.d.ts +4 -4
- package/dist/react.d.ts.map +1 -1
- package/package.json +4 -2
- package/src/contracts/events.ts +2 -1
- package/src/core/bridge.ts +280 -0
- package/src/core/controller.ts +83 -268
- package/src/react/announcer.ts +9 -0
- package/src/react/bindings.tsx +28 -72
- package/src/react/card.tsx +3 -3
- package/src/react/generators.ts +2 -2
- package/src/react/gestures.ts +1 -1
- package/src/react/host.tsx +10 -4
- package/src/react/motion.ts +4 -4
- package/src/react/plan.ts +2 -2
- package/src/react/renderer.tsx +77 -0
- package/src/react.ts +4 -4
|
@@ -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
|
+
}
|