@wavelo/async 0.0.0 → 0.9.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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/action-button.d.ts +12 -0
- package/dist/action-button.js +53 -0
- package/dist/ai-stream.d.ts +24 -0
- package/dist/ai-stream.js +54 -0
- package/dist/angular.d.ts +26 -0
- package/dist/angular.js +35 -0
- package/dist/announcer.d.ts +33 -0
- package/dist/announcer.js +102 -0
- package/dist/boundary.d.ts +37 -0
- package/dist/boundary.js +147 -0
- package/dist/controller.d.ts +7 -0
- package/dist/controller.js +363 -0
- package/dist/dom.d.ts +35 -0
- package/dist/dom.js +243 -0
- package/dist/export-tool.d.ts +38 -0
- package/dist/export-tool.js +89 -0
- package/dist/group.d.ts +65 -0
- package/dist/group.js +235 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3 -0
- package/dist/innovation-progress.d.ts +101 -0
- package/dist/innovation-progress.js +276 -0
- package/dist/innovation-session.d.ts +70 -0
- package/dist/innovation-session.js +130 -0
- package/dist/innovation-ui.d.ts +55 -0
- package/dist/innovation-ui.js +122 -0
- package/dist/react.d.ts +17 -0
- package/dist/react.js +166 -0
- package/dist/registry-ext.d.ts +24 -0
- package/dist/registry-ext.js +41 -0
- package/dist/router.d.ts +25 -0
- package/dist/router.js +47 -0
- package/dist/runtime.d.ts +40 -0
- package/dist/runtime.js +140 -0
- package/dist/schema.d.ts +108 -0
- package/dist/schema.js +56 -0
- package/dist/skeleton.d.ts +53 -0
- package/dist/skeleton.js +152 -0
- package/dist/stages.d.ts +69 -0
- package/dist/stages.js +206 -0
- package/dist/svelte.d.ts +27 -0
- package/dist/svelte.js +44 -0
- package/dist/testing.d.ts +10 -0
- package/dist/testing.js +45 -0
- package/dist/themes.d.ts +29 -0
- package/dist/themes.js +66 -0
- package/dist/transfer.d.ts +57 -0
- package/dist/transfer.js +165 -0
- package/dist/types.d.ts +63 -0
- package/dist/types.js +1 -0
- package/dist/vue.d.ts +26 -0
- package/dist/vue.js +53 -0
- package/dist/web-components.d.ts +2 -0
- package/dist/web-components.js +22 -0
- package/package.json +147 -3
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wavelo contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is furnished
|
|
10
|
+
to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# `@wavelo/async`
|
|
2
|
+
|
|
3
|
+
Headless async loading controllers, anti-flicker timing, weighted task groups, and accessible DOM/framework bindings for Wavelo.
|
|
4
|
+
|
|
5
|
+
- **Dependencies:** The package declares `@wavelo/core` as an exact runtime dependency (it is not optional, and npm resolves it to the matching release). It is only loaded by the visual entries that render or style loaders — `@wavelo/async/dom`, `/boundary`, `/action-button`, `/runtime`, `/schema`, `/custom-registry`, and `/export`. The headless root entry (`@wavelo/async`) and the headless helpers (`/stages`, `/transfer`, `/ai`, `/skeleton`, `/testing`, `/innovation-*`) import no runtime dependencies.
|
|
6
|
+
- **Framework peers are optional:** react >=16.8.0 <20.0.0, vue >=3.0.0 <4.0.0, svelte >=5.0.0 <6.0.0 and @angular/core >=14.0.0 <23.0.0 are declared as optional peer dependencies; install only the framework whose entry you use (`@wavelo/async/react`, `/vue`, `/svelte`, `/angular`). `@wavelo/async/web-components` needs no framework.
|
|
7
|
+
- **No DOM globals or framework imports** in the headless controller (`createLoadingController`, `createTaskGroup`, `createAnnouncementPolicy`).
|
|
8
|
+
- **Anti-flicker timing (`showDelay`, `minVisible`, `slowThreshold`):** Separates `pending` from `visible` so fast work never flashes a spinner and slower work stays visible for a smooth minimum window. Visual timing is per-operation: a superseded or settling operation cannot shorten the next operation's `minVisible` window.
|
|
9
|
+
- **Latest-operation ownership:** Starting a new operation supersedes stale requests and distinguishes `cancelled` from `error`. `getCurrentOperation()` exposes the in-flight context without superseding it.
|
|
10
|
+
- **Updatable DOM handle (`@wavelo/async/dom`):** `createLoaderHandle` patches compatible visual/ARIA attributes in place, `bindBusyRegion` reference-counts `aria-busy`, and `mountStatusRegion` manages a single polite `role="status"` announcer.
|
|
11
|
+
- **Framework bindings:** `@wavelo/async/react`, `@wavelo/async/vue`, `@wavelo/async/svelte`, `@wavelo/async/angular`, `@wavelo/async/web-components`. The React hooks (`useLoadingController`, `useTaskGroup`) survive React 18/19 StrictMode remounts (verified in Chromium): the exposed controller/group proxy stays stable and rebinds a live controller after a simulated unmount/remount, while final unmount disposes the live instance and stops updates.
|
|
12
|
+
- **React Server Components:** `@wavelo/async/react` declares `'use client'` in the built artifact, so a hook-using component imports the entry directly with no wrapper of its own. The headless entries (`@wavelo/async`, `/stages`, `/transfer`, `/ai`, `/skeleton`, `/testing`, `/innovation-*`) declare no boundary and stay constructible in a Server Component; `scripts/verify-react-entries.mjs` fails the build if either half drifts.
|
|
13
|
+
- **Task group failures:** `fail(id, error)` keeps only a sanitized summary (`name`/`message` clipped to bounded lengths, never the original error object) and exposes it as `TaskGroupSnapshot.errors`. Retention is bounded twice over: `createTaskGroup({ maxRetainedFailures })` (default 20) keeps the most recently failed summaries and drops the least recently failed ones (reported in registration order), and `remove`, `reset`, restarting the task, or `dispose` drop a summary. Past the cap a task stays failed and still counts in `failedCount` — the cap limits what the snapshot carries, not what happened.
|
|
14
|
+
- **Disposal is terminal, per binding:** `dispose()` on a controller or group releases its timers, listeners and abortable operation, and the same instance never becomes live again. Each binding decides what happens afterwards. The React hooks hand out one stable proxy that builds a fresh instance when the hook's effect remounts (the StrictMode cycle) and stays inert after the final unmount; `useLoadingController` also returns `dispose` for an early release. The Vue composables bind disposal to the effect scope or component instance, so an explicit `dispose()` ends the object they returned — call the composable again for a new one. The Svelte stores and Angular wrappers are the controller itself, so the application constructs a new one.
|
|
15
|
+
|
|
16
|
+
## Entry points
|
|
17
|
+
|
|
18
|
+
The subpaths are separate import specifiers on purpose: nothing outside the entry you name
|
|
19
|
+
is pulled into the bundle, and only the visual entries reach `@wavelo/core`.
|
|
20
|
+
|
|
21
|
+
| Entry | What it holds |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| `@wavelo/async` | `createLoadingController`, `createTaskGroup`, `createAnnouncementPolicy` and their types |
|
|
24
|
+
| `@wavelo/async/dom` | `createLoaderHandle`, `bindBusyRegion`, `mountStatusRegion` |
|
|
25
|
+
| `@wavelo/async/react` | `useLoadingController`, `useTaskGroup` (declares `'use client'`) |
|
|
26
|
+
| `@wavelo/async/vue` | `useLoadingController`, `useTaskGroup` composables over shallow refs |
|
|
27
|
+
| `@wavelo/async/svelte` | `createLoadingStore`, `createTaskGroupStore` readable stores |
|
|
28
|
+
| `@wavelo/async/angular` | `createAngularLoadingController`, `createAngularTaskGroup` (Observable-compatible) |
|
|
29
|
+
| `@wavelo/async/web-components` | `bindControllerToElement` |
|
|
30
|
+
| `@wavelo/async/stages` | `createStageProgress`, `createWorkflowGraph` (F14, F25) |
|
|
31
|
+
| `@wavelo/async/transfer` | `observeResponseStream`, `observeXhrProgress`, `formatTransferRate`, `createBatchTransfer` (F15, F26) |
|
|
32
|
+
| `@wavelo/async/ai` | `createAiStreamPresenter` (F16) |
|
|
33
|
+
| `@wavelo/async/skeleton` | `renderSkeletonTree`, `reserveGeometry`, `skeletonFromSchema` (F11–F13, F28) |
|
|
34
|
+
| `@wavelo/async/boundary` | `createLoadingBoundary`, `createMediaReadiness` (F09, F18, F27) |
|
|
35
|
+
| `@wavelo/async/action-button` | `bindLoadingButton` (F10) |
|
|
36
|
+
| `@wavelo/async/runtime` | `createVisibilityCoordinator`, `createTargetStyleManager`, `createLoadingDiagnostics` (F19, F21, F24) |
|
|
37
|
+
| `@wavelo/async/themes` | `createThemePack`, `getLowMotionPose` (F20, F29) |
|
|
38
|
+
| `@wavelo/async/custom-registry` | `createCustomDesignRegistry` (F22) |
|
|
39
|
+
| `@wavelo/async/schema` | `generateConfigSchema`, `generateCapabilityManifest` (F23) |
|
|
40
|
+
| `@wavelo/async/export` | `exportLoaderBundle`, `exportStaticStrictCsp` (F30, F41) |
|
|
41
|
+
| `@wavelo/async/router` | `bindRouterNavigation`, `mapQueryState` (F17) |
|
|
42
|
+
| `@wavelo/async/testing` | `createFakeClock` (F31) |
|
|
43
|
+
| `@wavelo/async/innovation-ui` | `createPartialReveal`, `createAttentionCoordinator`, `applyRtlLayout` (F33, F34, F40) |
|
|
44
|
+
| `@wavelo/async/innovation-progress` | `createStallMonitor`, `createWorkerProgressBridge`, `createTruthfulSmoother` (F35, F36, F42) |
|
|
45
|
+
| `@wavelo/async/innovation-session` | `createHandoffSnapshot`, `restoreHandoffSnapshot`, `createSessionRecorder`, `replaySession`, `createTimingCalibrator` (F37–F39) |
|
|
46
|
+
|
|
47
|
+
Integration recipes, including the worker-bridge envelope and the strict-CSP export, are in
|
|
48
|
+
[../../docs/RECIPES.md](../../docs/RECIPES.md); the feature record is in
|
|
49
|
+
[../../docs/V0_9_FEATURES.md](../../docs/V0_9_FEATURES.md).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LoadingController } from './types.js';
|
|
2
|
+
export interface LoadingButtonOptions {
|
|
3
|
+
disableWhilePending?: boolean;
|
|
4
|
+
reserveWidth?: boolean;
|
|
5
|
+
loaderType?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Loading action/button binding (F10).
|
|
9
|
+
* Prevents duplicate submits, preserves native button semantics and focus, and
|
|
10
|
+
* reserves label width so the button does not jump when loading starts.
|
|
11
|
+
*/
|
|
12
|
+
export declare function bindLoadingButton(button: HTMLButtonElement, controller: LoadingController, action: (signal: AbortSignal) => Promise<unknown>, options?: LoadingButtonOptions): () => void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createLoaderHandle } from './dom.js';
|
|
2
|
+
/**
|
|
3
|
+
* Loading action/button binding (F10).
|
|
4
|
+
* Prevents duplicate submits, preserves native button semantics and focus, and
|
|
5
|
+
* reserves label width so the button does not jump when loading starts.
|
|
6
|
+
*/
|
|
7
|
+
export function bindLoadingButton(button, controller, action, options = {}) {
|
|
8
|
+
const disableWhilePending = options.disableWhilePending ?? true;
|
|
9
|
+
const slot = button.ownerDocument.createElement('span');
|
|
10
|
+
slot.setAttribute('data-wavelo-button-slot', '');
|
|
11
|
+
button.appendChild(slot);
|
|
12
|
+
const handle = createLoaderHandle(slot, {
|
|
13
|
+
type: options.loaderType ?? 'button-confirm',
|
|
14
|
+
inline: true,
|
|
15
|
+
loading: false,
|
|
16
|
+
decorative: true
|
|
17
|
+
});
|
|
18
|
+
if (options.reserveWidth && typeof button.getBoundingClientRect === 'function') {
|
|
19
|
+
const rect = button.getBoundingClientRect();
|
|
20
|
+
if (rect.width > 0)
|
|
21
|
+
button.style.minWidth = `${Math.ceil(rect.width)}px`;
|
|
22
|
+
}
|
|
23
|
+
const sync = () => {
|
|
24
|
+
const snap = controller.getSnapshot();
|
|
25
|
+
if (disableWhilePending)
|
|
26
|
+
button.disabled = snap.pending;
|
|
27
|
+
if (snap.pending)
|
|
28
|
+
button.setAttribute('aria-busy', 'true');
|
|
29
|
+
else
|
|
30
|
+
button.removeAttribute('aria-busy');
|
|
31
|
+
handle.update({
|
|
32
|
+
type: options.loaderType ?? 'button-confirm',
|
|
33
|
+
inline: true,
|
|
34
|
+
loading: snap.visible || snap.status === 'success' || snap.status === 'error',
|
|
35
|
+
'data-wavelo-state': snap.pending ? 'loading' : snap.status === 'success' ? 'success' : snap.status === 'error' ? 'error' : 'idle',
|
|
36
|
+
decorative: true
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const onClick = () => {
|
|
40
|
+
if (controller.getSnapshot().pending)
|
|
41
|
+
return;
|
|
42
|
+
void controller.run(({ signal }) => action(signal)).catch(() => { });
|
|
43
|
+
};
|
|
44
|
+
button.addEventListener('click', onClick);
|
|
45
|
+
const unsub = controller.subscribe(sync);
|
|
46
|
+
sync();
|
|
47
|
+
return () => {
|
|
48
|
+
button.removeEventListener('click', onClick);
|
|
49
|
+
unsub();
|
|
50
|
+
handle.destroy();
|
|
51
|
+
slot.remove();
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type AiStreamPhase = 'idle' | 'waiting' | 'tool_call' | 'streaming' | 'completed' | 'error' | 'stopped';
|
|
2
|
+
export interface AiStreamSnapshot {
|
|
3
|
+
phase: AiStreamPhase;
|
|
4
|
+
recommendedDesign: string;
|
|
5
|
+
elapsedMs: number;
|
|
6
|
+
toolName?: string;
|
|
7
|
+
error?: unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Provider-neutral AI/streaming phase presenter (F16).
|
|
11
|
+
* Maps explicit phase events onto existing Wavelo designs without fabricating token
|
|
12
|
+
* percentages or logging/announcing prompt/token contents.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createAiStreamPresenter(options?: {
|
|
15
|
+
now?: () => number;
|
|
16
|
+
}): {
|
|
17
|
+
getSnapshot: () => AiStreamSnapshot;
|
|
18
|
+
subscribe(listener: (snap: AiStreamSnapshot) => void): () => boolean;
|
|
19
|
+
transition(phase: AiStreamPhase, meta?: {
|
|
20
|
+
toolName?: string;
|
|
21
|
+
error?: unknown;
|
|
22
|
+
}): void;
|
|
23
|
+
reset(): void;
|
|
24
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const PHASE_DESIGN_MAP = {
|
|
2
|
+
idle: 'thinking-glow',
|
|
3
|
+
waiting: 'thinking-glow',
|
|
4
|
+
tool_call: 'ai-tool-handoff',
|
|
5
|
+
streaming: 'token-stream',
|
|
6
|
+
completed: 'success-ring',
|
|
7
|
+
error: 'error-shake',
|
|
8
|
+
stopped: 'stream-cursor'
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Provider-neutral AI/streaming phase presenter (F16).
|
|
12
|
+
* Maps explicit phase events onto existing Wavelo designs without fabricating token
|
|
13
|
+
* percentages or logging/announcing prompt/token contents.
|
|
14
|
+
*/
|
|
15
|
+
export function createAiStreamPresenter(options = {}) {
|
|
16
|
+
const now = options.now ?? (() => Date.now());
|
|
17
|
+
const listeners = new Set();
|
|
18
|
+
let startedAt = null;
|
|
19
|
+
let snapshot = {
|
|
20
|
+
phase: 'idle',
|
|
21
|
+
recommendedDesign: PHASE_DESIGN_MAP.idle,
|
|
22
|
+
elapsedMs: 0
|
|
23
|
+
};
|
|
24
|
+
const emit = (next) => {
|
|
25
|
+
snapshot = next;
|
|
26
|
+
for (const l of [...listeners])
|
|
27
|
+
l(snapshot);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
getSnapshot: () => snapshot,
|
|
31
|
+
subscribe(listener) {
|
|
32
|
+
listeners.add(listener);
|
|
33
|
+
return () => listeners.delete(listener);
|
|
34
|
+
},
|
|
35
|
+
transition(phase, meta = {}) {
|
|
36
|
+
if (phase === 'waiting' && startedAt === null)
|
|
37
|
+
startedAt = now();
|
|
38
|
+
const elapsedMs = startedAt !== null ? Math.max(0, now() - startedAt) : 0;
|
|
39
|
+
if (phase === 'idle')
|
|
40
|
+
startedAt = null;
|
|
41
|
+
emit({
|
|
42
|
+
phase,
|
|
43
|
+
recommendedDesign: PHASE_DESIGN_MAP[phase],
|
|
44
|
+
elapsedMs,
|
|
45
|
+
toolName: phase === 'tool_call' ? meta.toolName : undefined,
|
|
46
|
+
error: phase === 'error' ? meta.error : undefined
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
reset() {
|
|
50
|
+
startedAt = null;
|
|
51
|
+
emit({ phase: 'idle', recommendedDesign: PHASE_DESIGN_MAP.idle, elapsedMs: 0 });
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TaskGroup, TaskGroupOptions, TaskGroupSnapshot } from './group.js';
|
|
2
|
+
import type { LoadingController, LoadingControllerOptions, LoadingSnapshot } from './types.js';
|
|
3
|
+
export interface Subscribable<V> {
|
|
4
|
+
subscribe: (observer: ((value: V) => void) | {
|
|
5
|
+
next: (value: V) => void;
|
|
6
|
+
}) => {
|
|
7
|
+
unsubscribe: () => void;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export interface AngularLoadingController<T = unknown> extends Subscribable<LoadingSnapshot<T>> {
|
|
11
|
+
getSnapshot: LoadingController<T>['getSnapshot'];
|
|
12
|
+
getCurrentOperation: LoadingController<T>['getCurrentOperation'];
|
|
13
|
+
start: LoadingController<T>['start'];
|
|
14
|
+
run: LoadingController<T>['run'];
|
|
15
|
+
reset: LoadingController<T>['reset'];
|
|
16
|
+
destroy: LoadingController<T>['dispose'];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Angular-friendly Observable-compatible controller wrapper (Angular 14+, no Signals required).
|
|
20
|
+
*/
|
|
21
|
+
export declare function createAngularLoadingController<T = unknown>(options?: LoadingControllerOptions): AngularLoadingController<T>;
|
|
22
|
+
export interface AngularTaskGroup extends Subscribable<TaskGroupSnapshot> {
|
|
23
|
+
group: TaskGroup;
|
|
24
|
+
destroy: TaskGroup['dispose'];
|
|
25
|
+
}
|
|
26
|
+
export declare function createAngularTaskGroup(options?: TaskGroupOptions): AngularTaskGroup;
|
package/dist/angular.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createLoadingController } from './controller.js';
|
|
2
|
+
import { createTaskGroup } from './group.js';
|
|
3
|
+
/**
|
|
4
|
+
* Angular-friendly Observable-compatible controller wrapper (Angular 14+, no Signals required).
|
|
5
|
+
*/
|
|
6
|
+
export function createAngularLoadingController(options = {}) {
|
|
7
|
+
const controller = createLoadingController(options);
|
|
8
|
+
return {
|
|
9
|
+
subscribe(observer) {
|
|
10
|
+
const next = typeof observer === 'function' ? observer : (v) => observer.next(v);
|
|
11
|
+
next(controller.getSnapshot());
|
|
12
|
+
const unsub = controller.subscribe(next);
|
|
13
|
+
return { unsubscribe: unsub };
|
|
14
|
+
},
|
|
15
|
+
getSnapshot: controller.getSnapshot,
|
|
16
|
+
getCurrentOperation: controller.getCurrentOperation,
|
|
17
|
+
start: controller.start,
|
|
18
|
+
run: controller.run,
|
|
19
|
+
reset: controller.reset,
|
|
20
|
+
destroy: controller.dispose
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function createAngularTaskGroup(options = {}) {
|
|
24
|
+
const group = createTaskGroup(options);
|
|
25
|
+
return {
|
|
26
|
+
group,
|
|
27
|
+
subscribe(observer) {
|
|
28
|
+
const next = typeof observer === 'function' ? observer : (v) => observer.next(v);
|
|
29
|
+
next(group.getSnapshot());
|
|
30
|
+
const unsub = group.subscribe(next);
|
|
31
|
+
return { unsubscribe: unsub };
|
|
32
|
+
},
|
|
33
|
+
destroy: group.dispose
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { LoadingSnapshot } from './types.js';
|
|
2
|
+
export type AnnouncementKind = 'loading' | 'slow' | 'progress' | 'success' | 'error' | 'cancelled';
|
|
3
|
+
export interface AnnouncementEvent {
|
|
4
|
+
kind: AnnouncementKind;
|
|
5
|
+
operationId: number;
|
|
6
|
+
percent?: number;
|
|
7
|
+
message: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AnnouncementMessages {
|
|
10
|
+
loading?: string;
|
|
11
|
+
slow?: string;
|
|
12
|
+
success?: string;
|
|
13
|
+
error?: string;
|
|
14
|
+
cancelled?: string;
|
|
15
|
+
progress?: (percent: number) => string;
|
|
16
|
+
}
|
|
17
|
+
export interface AnnouncementPolicyOptions {
|
|
18
|
+
milestones?: number[];
|
|
19
|
+
throttleMs?: number;
|
|
20
|
+
messages?: AnnouncementMessages;
|
|
21
|
+
format?: (event: Omit<AnnouncementEvent, 'message'>) => string;
|
|
22
|
+
now?: () => number;
|
|
23
|
+
}
|
|
24
|
+
export interface AnnouncementPolicy {
|
|
25
|
+
evaluate: (snapshot: LoadingSnapshot) => AnnouncementEvent | null;
|
|
26
|
+
reset: () => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Restrained status announcement policy (F06).
|
|
30
|
+
* Coalesces repeated updates, announces configured progress milestones, and ensures
|
|
31
|
+
* terminal messages occur once per operation.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createAnnouncementPolicy(options?: AnnouncementPolicyOptions): AnnouncementPolicy;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const defaultMessages = {
|
|
2
|
+
loading: 'Loading',
|
|
3
|
+
slow: 'Still loading',
|
|
4
|
+
success: 'Completed',
|
|
5
|
+
error: 'Loading failed',
|
|
6
|
+
cancelled: 'Cancelled',
|
|
7
|
+
progress: (percent) => `${percent}%`
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Restrained status announcement policy (F06).
|
|
11
|
+
* Coalesces repeated updates, announces configured progress milestones, and ensures
|
|
12
|
+
* terminal messages occur once per operation.
|
|
13
|
+
*/
|
|
14
|
+
export function createAnnouncementPolicy(options = {}) {
|
|
15
|
+
const milestones = [...(options.milestones ?? [25, 50, 75, 100])].sort((a, b) => a - b);
|
|
16
|
+
const throttleMs = options.throttleMs ?? 1000;
|
|
17
|
+
const now = options.now ?? (() => Date.now());
|
|
18
|
+
const msgs = { ...defaultMessages, ...options.messages };
|
|
19
|
+
let lastOperationId = -1;
|
|
20
|
+
let announcedLoading = false;
|
|
21
|
+
let announcedSlow = false;
|
|
22
|
+
let announcedTerminal = false;
|
|
23
|
+
let lastMilestone = -1;
|
|
24
|
+
let lastAnnouncementAt = -Infinity;
|
|
25
|
+
const formatMessage = (kind, percent) => {
|
|
26
|
+
if (options.format) {
|
|
27
|
+
return options.format({ kind, operationId: lastOperationId, percent });
|
|
28
|
+
}
|
|
29
|
+
if (kind === 'progress')
|
|
30
|
+
return msgs.progress(percent ?? 0);
|
|
31
|
+
return msgs[kind];
|
|
32
|
+
};
|
|
33
|
+
const resetForOperation = (operationId) => {
|
|
34
|
+
lastOperationId = operationId;
|
|
35
|
+
announcedLoading = false;
|
|
36
|
+
announcedSlow = false;
|
|
37
|
+
announcedTerminal = false;
|
|
38
|
+
lastMilestone = -1;
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
evaluate(snapshot) {
|
|
42
|
+
if (snapshot.disposed)
|
|
43
|
+
return null;
|
|
44
|
+
if (snapshot.operationId !== lastOperationId) {
|
|
45
|
+
resetForOperation(snapshot.operationId);
|
|
46
|
+
}
|
|
47
|
+
if (snapshot.status === 'success' || snapshot.status === 'error' || snapshot.status === 'cancelled') {
|
|
48
|
+
if (announcedTerminal)
|
|
49
|
+
return null;
|
|
50
|
+
announcedTerminal = true;
|
|
51
|
+
lastAnnouncementAt = now();
|
|
52
|
+
return {
|
|
53
|
+
kind: snapshot.status,
|
|
54
|
+
operationId: snapshot.operationId,
|
|
55
|
+
message: formatMessage(snapshot.status)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (!snapshot.pending)
|
|
59
|
+
return null;
|
|
60
|
+
if (snapshot.isSlow && !announcedSlow) {
|
|
61
|
+
announcedSlow = true;
|
|
62
|
+
lastAnnouncementAt = now();
|
|
63
|
+
return {
|
|
64
|
+
kind: 'slow',
|
|
65
|
+
operationId: snapshot.operationId,
|
|
66
|
+
message: formatMessage('slow')
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (!snapshot.progress.indeterminate && snapshot.progress.percent !== undefined) {
|
|
70
|
+
const pct = snapshot.progress.percent;
|
|
71
|
+
const reached = milestones.filter((m) => pct >= m && m > lastMilestone).pop();
|
|
72
|
+
if (reached !== undefined) {
|
|
73
|
+
const t = now();
|
|
74
|
+
if (t - lastAnnouncementAt >= throttleMs || reached === 100) {
|
|
75
|
+
lastMilestone = reached;
|
|
76
|
+
lastAnnouncementAt = t;
|
|
77
|
+
return {
|
|
78
|
+
kind: 'progress',
|
|
79
|
+
operationId: snapshot.operationId,
|
|
80
|
+
percent: pct,
|
|
81
|
+
message: formatMessage('progress', pct)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (snapshot.visible && !announcedLoading) {
|
|
87
|
+
announcedLoading = true;
|
|
88
|
+
lastAnnouncementAt = now();
|
|
89
|
+
return {
|
|
90
|
+
kind: 'loading',
|
|
91
|
+
operationId: snapshot.operationId,
|
|
92
|
+
message: formatMessage('loading')
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
},
|
|
97
|
+
reset() {
|
|
98
|
+
resetForOperation(-1);
|
|
99
|
+
lastAnnouncementAt = -Infinity;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type BoundaryState = 'content' | 'pending' | 'refresh' | 'empty' | 'error';
|
|
2
|
+
export interface LoadingBoundaryOptions {
|
|
3
|
+
retainContent?: boolean;
|
|
4
|
+
renderPending?: () => string;
|
|
5
|
+
renderEmpty?: () => string;
|
|
6
|
+
renderError?: (error: unknown) => string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Loading boundary controller (F09 + F18).
|
|
10
|
+
* Distinguishes initial pending from background refresh (preserving content & focus).
|
|
11
|
+
*
|
|
12
|
+
* Security: the HTML returned by the render callbacks and `payload.html` is inserted with
|
|
13
|
+
* `innerHTML` without sanitization. Applications must pass trusted, application-owned markup.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createLoadingBoundary(container: HTMLElement, options?: LoadingBoundaryOptions): {
|
|
16
|
+
getState: () => BoundaryState;
|
|
17
|
+
setState(next: BoundaryState, payload?: {
|
|
18
|
+
html?: string;
|
|
19
|
+
error?: unknown;
|
|
20
|
+
}): void;
|
|
21
|
+
destroy(): void;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Media readiness adapter with source-generation identity (F27).
|
|
25
|
+
* Prevents obsolete load events from revealing stale media after `src` changes.
|
|
26
|
+
* Exactly one listener set is attached to the media element at any time: each
|
|
27
|
+
* `load()` detaches the previous source's unfired listeners before attaching
|
|
28
|
+
* its own, and `destroy()` removes whatever is still pending.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createMediaReadiness(media: HTMLImageElement | HTMLVideoElement, placeholderHost: HTMLElement, options?: {
|
|
31
|
+
aspectRatio?: string;
|
|
32
|
+
loaderType?: string;
|
|
33
|
+
}): {
|
|
34
|
+
getStatus: () => "error" | "loading" | "ready";
|
|
35
|
+
load: (src: string) => void;
|
|
36
|
+
destroy(): void;
|
|
37
|
+
};
|
package/dist/boundary.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { bindBusyRegion, createLoaderHandle } from './dom.js';
|
|
2
|
+
/**
|
|
3
|
+
* Loading boundary controller (F09 + F18).
|
|
4
|
+
* Distinguishes initial pending from background refresh (preserving content & focus).
|
|
5
|
+
*
|
|
6
|
+
* Security: the HTML returned by the render callbacks and `payload.html` is inserted with
|
|
7
|
+
* `innerHTML` without sanitization. Applications must pass trusted, application-owned markup.
|
|
8
|
+
*/
|
|
9
|
+
export function createLoadingBoundary(container, options = {}) {
|
|
10
|
+
const retainContent = options.retainContent ?? true;
|
|
11
|
+
const busy = bindBusyRegion(container);
|
|
12
|
+
let releaseBusy = null;
|
|
13
|
+
let contentHtml = container.innerHTML;
|
|
14
|
+
let state = 'content';
|
|
15
|
+
const overlaySlot = container.ownerDocument.createElement('div');
|
|
16
|
+
overlaySlot.setAttribute('data-wavelo-boundary-slot', '');
|
|
17
|
+
const setBusy = (active) => {
|
|
18
|
+
if (active && !releaseBusy)
|
|
19
|
+
releaseBusy = busy.acquire();
|
|
20
|
+
else if (!active && releaseBusy) {
|
|
21
|
+
releaseBusy();
|
|
22
|
+
releaseBusy = null;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
getState: () => state,
|
|
27
|
+
setState(next, payload = {}) {
|
|
28
|
+
state = next;
|
|
29
|
+
if (next === 'content') {
|
|
30
|
+
setBusy(false);
|
|
31
|
+
if (payload.html !== undefined) {
|
|
32
|
+
contentHtml = payload.html;
|
|
33
|
+
container.innerHTML = contentHtml;
|
|
34
|
+
}
|
|
35
|
+
else if (!retainContent && contentHtml) {
|
|
36
|
+
container.innerHTML = contentHtml;
|
|
37
|
+
}
|
|
38
|
+
overlaySlot.remove();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (next === 'pending') {
|
|
42
|
+
setBusy(true);
|
|
43
|
+
container.innerHTML = options.renderPending ? options.renderPending() : '';
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (next === 'refresh') {
|
|
47
|
+
setBusy(true);
|
|
48
|
+
if (!retainContent) {
|
|
49
|
+
container.innerHTML = options.renderPending ? options.renderPending() : '';
|
|
50
|
+
}
|
|
51
|
+
else if (!overlaySlot.isConnected) {
|
|
52
|
+
overlaySlot.innerHTML = options.renderPending ? options.renderPending() : '';
|
|
53
|
+
container.appendChild(overlaySlot);
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (next === 'empty') {
|
|
58
|
+
setBusy(false);
|
|
59
|
+
container.innerHTML = options.renderEmpty ? options.renderEmpty() : '';
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (next === 'error') {
|
|
63
|
+
setBusy(false);
|
|
64
|
+
if (retainContent && contentHtml) {
|
|
65
|
+
overlaySlot.innerHTML = options.renderError ? options.renderError(payload.error) : 'Refresh failed';
|
|
66
|
+
if (!overlaySlot.isConnected)
|
|
67
|
+
container.appendChild(overlaySlot);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
container.innerHTML = options.renderError ? options.renderError(payload.error) : 'Error';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
destroy() {
|
|
75
|
+
setBusy(false);
|
|
76
|
+
overlaySlot.remove();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Media readiness adapter with source-generation identity (F27).
|
|
82
|
+
* Prevents obsolete load events from revealing stale media after `src` changes.
|
|
83
|
+
* Exactly one listener set is attached to the media element at any time: each
|
|
84
|
+
* `load()` detaches the previous source's unfired listeners before attaching
|
|
85
|
+
* its own, and `destroy()` removes whatever is still pending.
|
|
86
|
+
*/
|
|
87
|
+
export function createMediaReadiness(media, placeholderHost, options = {}) {
|
|
88
|
+
if (options.aspectRatio) {
|
|
89
|
+
placeholderHost.style.aspectRatio = options.aspectRatio;
|
|
90
|
+
}
|
|
91
|
+
const handle = createLoaderHandle(placeholderHost, {
|
|
92
|
+
type: options.loaderType ?? 'image-placeholder',
|
|
93
|
+
label: 'Loading media'
|
|
94
|
+
});
|
|
95
|
+
let generation = 0;
|
|
96
|
+
let status = 'loading';
|
|
97
|
+
let attachedListeners = [];
|
|
98
|
+
const detachListeners = () => {
|
|
99
|
+
for (const { type, fn } of attachedListeners) {
|
|
100
|
+
media.removeEventListener(type, fn);
|
|
101
|
+
}
|
|
102
|
+
attachedListeners = [];
|
|
103
|
+
};
|
|
104
|
+
const load = (src) => {
|
|
105
|
+
// Drop the superseded source's listeners so rapid source changes cannot
|
|
106
|
+
// accumulate unfired once-listeners on the media element.
|
|
107
|
+
detachListeners();
|
|
108
|
+
const gen = ++generation;
|
|
109
|
+
status = 'loading';
|
|
110
|
+
handle.update({ type: options.loaderType ?? 'image-placeholder', loading: true, label: 'Loading media' });
|
|
111
|
+
media.setAttribute('data-wavelo-media-state', 'loading');
|
|
112
|
+
const settle = (next) => {
|
|
113
|
+
if (gen !== generation)
|
|
114
|
+
return;
|
|
115
|
+
detachListeners();
|
|
116
|
+
status = next;
|
|
117
|
+
if (next === 'ready') {
|
|
118
|
+
handle.update({ type: options.loaderType ?? 'image-placeholder', loading: false });
|
|
119
|
+
media.setAttribute('data-wavelo-media-state', 'ready');
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
handle.update({ type: 'error-shake', loading: true, 'data-wavelo-state': 'error', label: 'Media failed' });
|
|
123
|
+
media.setAttribute('data-wavelo-media-state', 'error');
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const onReady = () => settle('ready');
|
|
127
|
+
const onError = () => settle('error');
|
|
128
|
+
attachedListeners = [
|
|
129
|
+
{ type: 'load', fn: onReady },
|
|
130
|
+
{ type: 'canplay', fn: onReady },
|
|
131
|
+
{ type: 'error', fn: onError }
|
|
132
|
+
];
|
|
133
|
+
for (const { type, fn } of attachedListeners) {
|
|
134
|
+
media.addEventListener(type, fn, { once: true });
|
|
135
|
+
}
|
|
136
|
+
media.src = src;
|
|
137
|
+
};
|
|
138
|
+
return {
|
|
139
|
+
getStatus: () => status,
|
|
140
|
+
load,
|
|
141
|
+
destroy() {
|
|
142
|
+
generation++;
|
|
143
|
+
detachListeners();
|
|
144
|
+
handle.destroy();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LoadingController, LoadingControllerOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Headless async operation controller (F01 + F02).
|
|
4
|
+
* Owns latest-operation identity, cancellation vs failure, and anti-flicker visibility timing.
|
|
5
|
+
* Framework-independent and DOM-free; schedules no timers until an operation starts.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createLoadingController<T = unknown>(options?: LoadingControllerOptions): LoadingController<T>;
|