@grundyjs/algiviz 0.5.1 → 0.7.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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0 — 2026-10-15
4
+
5
+ - Breaking: remove the deprecated /core entry point and bundled insertion/bubble
6
+ algorithms. Application-owned examples use /array; playback adapters now live there.
7
+
8
+ - Preserve source step indices in generic timeline frames, matching generator playback.
9
+ - Preserve primary playback errors when iterator cleanup also fails; standalone
10
+ cleanup failures still propagate.
11
+
12
+ ## 0.6.0 — 2026-10-14
13
+
14
+ - Add an application-owned graph BFS example with custom vertices, edges and queue
15
+ entries, shortest distances, disconnected components and both playback modes.
16
+ - Add createStepEmitter to /playback for per-iterator step numbering and frozen
17
+ wrappers; snapshot copying and state/event immutability remain author-owned.
18
+ - Use the helper in graph and merge examples, and document custom graph integration.
19
+
3
20
  ## 0.5.1 — 2026-10-14
4
21
 
5
22
  - Update the README around the current public API and examples; remove migration
package/README.md CHANGED
@@ -5,7 +5,15 @@ algorithm and define your objects; AlgiViz provides array operations, playback,
5
5
  typed scene dispatch and a ready-made Canvas array renderer. No runtime dependencies.
6
6
  Playback works without DOM, React or Next.js. ESM only, with TypeScript declarations.
7
7
 
8
- This README describes version 0.5.1.
8
+ This README describes version 0.7.0.
9
+
10
+ ## Minimal custom object
11
+
12
+ Start with [the standalone example](examples/minimal.html) and its
13
+ [complete module](examples/minimal.mjs). It defines a custom token, emits immutable
14
+ steps and draws interpolated positions using only public package imports.
15
+ No shared demo controller is needed. See [the walkthrough](docs/minimal-example.md)
16
+ for running it in your own application.
9
17
 
10
18
  ## Install
11
19
 
@@ -16,7 +24,7 @@ npm install @grundyjs/algiviz
16
24
  There is no root entry point or CommonJS build. Import the modules you need:
17
25
  `/array`, `/playback`, `/scene` and `/canvas`.
18
26
 
19
- The deprecated `/core` sorting API remains available for compatibility.
27
+ Algorithms live in application code and examples.
20
28
  For new integrations, use the APIs below. Version 0.x may introduce breaking changes
21
29
  in minor releases; consult [CHANGELOG.md](CHANGELOG.md) when updating.
22
30
 
@@ -123,6 +131,42 @@ Custom array events or tree/graph operations can use this contract and a custom
123
131
  renderer. The scene API below dispatches individual objects; there is no built-in
124
132
  graph/tree algorithm or operation set.
125
133
 
134
+ ### Playback contracts
135
+
136
+ Playback retains application snapshots without cloning them; keep steps, state and
137
+ events immutable while they are in use. A timeline copies the input array only.
138
+ Timeline timing follows array position, and finite sample times are clamped to its
139
+ range. Generator playback requires an explicit terminal step; an early end throws.
140
+ `dispose()` closes the source at most once, keeps the last frame readable and
141
+ prevents further advancement. Restart uses a fresh source.
142
+
143
+ Version 0.7.0 makes timeline `stepIndex` preserve the source `step.index`,
144
+ matching the player. If source consumption, a terminal predicate or rendering
145
+ throws, cleanup is attempted without replacing that primary error. A standalone
146
+ cleanup error from disposal or normal completion still propagates.
147
+
148
+ ### Step emission helper (since 0.6.0)
149
+
150
+ Version 0.6.0 adds `createStepEmitter` from `/playback`. It removes manual step numbering and freezes the step wrapper:
151
+
152
+ ```js
153
+ import { createStepEmitter } from '@grundyjs/algiviz/playback';
154
+
155
+ function* counter() {
156
+ let count = 0;
157
+ const emit = createStepEmitter(() => Object.freeze({ count }));
158
+ yield emit('start');
159
+ count++;
160
+ yield emit('done');
161
+ }
162
+ ```
163
+
164
+ Create the emitter inside each iterator. Its callback runs once per emission;
165
+ indices start at zero and failed captures do not consume an index. The helper
166
+ does not clone or freeze state/event data, infer terminal events or store history.
167
+ The callback must produce isolated immutable snapshots; immutable shared structures
168
+ can be reused. Returning a mutable workspace directly does not make it a snapshot.
169
+
126
170
  ### User-defined objects and visualizations
127
171
 
128
172
  Define a type map with your own data, then
@@ -311,6 +355,8 @@ included in the npm archive.
311
355
  | [Sorting lab](examples/index.html) | Insertion and bubble sort using the array operations and renderer |
312
356
  | [Merge sort](examples/merge.html) | Custom state and a two-row scene for the main array and buffer |
313
357
  | [Quick sort partitions](examples/quick.html) | Lomuto and three-way partitioning with custom state and scene |
358
+ | [Heap sort](examples/heap.html) | Max-heap construction and extraction with synchronized tree and array identities |
359
+ | [Graph BFS](examples/graph.html) | Cycles, disconnected components, shortest distances and an animated queue |
314
360
  | [Tree traversal](examples/tree.html) | Custom nodes, edges and pointer with Canvas and retained SVG backends |
315
361
 
316
362
  All examples reuse [demo-player.mjs](examples/demo-player.mjs) and
@@ -344,6 +390,18 @@ n(n−1)/2 checks with this Lomuto variant. Neither variant guarantees stability
344
390
  Worst-case algorithm time is quadratic; recursion can use O(n) stack space.
345
391
  Full snapshot history has a separate O(n³) space upper bound.
346
392
 
393
+ ### Heap sort
394
+
395
+ The [algorithm](examples/heap-algorithm.mjs) owns immutable states and events.
396
+ The [scene](examples/heap-scene.mjs) assigns separate tree/array visual IDs to each
397
+ element and uses the public scene projector with viewport-independent endpoints.
398
+ Tree positions stay fixed as the heap shrinks. At most 15 array cells wrap on narrow
399
+ screens; larger arrays use a dense row and hide unreadable labels.
400
+ Bottom-up construction is O(n), worst-case sorting O(n log n), and the iterative
401
+ in-place algorithm uses O(1) auxiliary space. This demo copies input and retains
402
+ O(n) adjacent snapshot data in generator mode; full history can use O(n² log n).
403
+ It is not stable: [2a, 2b, 1] becomes [1, 2b, 2a].
404
+
347
405
  ### Tree traversal
348
406
 
349
407
  The example defines its own `node`, `edge` and `pointer` types. The pointer follows
@@ -353,6 +411,18 @@ switches between Canvas and SVG without restarting playback.
353
411
  and removes stale elements even after seeking skips exit transitions.
354
412
  No tree-specific algorithm or renderer is added to the package.
355
413
 
414
+ ### Graph breadth-first search
415
+
416
+ Open [examples/graph.html](examples/graph.html) and choose a start vertex.
417
+ The example explores only its connected component, marks vertices when enqueued
418
+ and shows shortest unweighted distances. Its custom vertex, connection and queue
419
+ entry types are projected from immutable algorithm state. Both playback modes use
420
+ the same generator; changing the start creates a new traversal.
421
+
422
+ See [Building a custom graph example](docs/custom-graph-example.md) for the
423
+ algorithm, projection and renderer boundaries. This example uses the existing
424
+ public APIs and adds no graph-specific package exports.
425
+
356
426
  ## Development
357
427
 
358
428
  Requires Node.js 22 or later and npm.
@@ -366,7 +436,7 @@ npm run test:package
366
436
  `npm test` builds the library, checks TypeScript fixtures and runs the tests.
367
437
  `npm run test:package` builds a tarball, installs it offline in an isolated temporary
368
438
  consumer, compiles against the installed declarations and exercises public APIs,
369
- including compatibility exports. The temporary consumer is removed afterward.
439
+ including rejection of the removed `/core` export. The temporary consumer is removed afterward.
370
440
 
371
441
  GitHub Actions runs both checks on pushes to `master` and pull requests, using
372
442
  Node.js 22 and 24 on Linux and Windows. CI checks do not publish packages.
@@ -392,7 +462,7 @@ candidate commit.
392
462
 
393
463
  The [publish workflow](.github/workflows/npm-publish.yml) runs when a stable GitHub
394
464
  Release is published. Drafts, prereleases and tag pushes alone do not publish.
395
- Use `v` followed by the exact package version, such as `v0.5.1`, at the tested commit.
465
+ Use `v` followed by the exact package version, such as `v0.7.0`, at the tested commit.
396
466
 
397
467
  The workflow uses Node.js 24 and npm 11, checks the version and packed consumer,
398
468
  then publishes with provenance through npm trusted publishing (OIDC).
@@ -1,7 +1,7 @@
1
1
  export { createArrayTrace, defineArrayAlgorithm } from "./trace.js";
2
2
  export type { ArrayTrace, ArrayAlgorithm, SortedRegions } from "./trace.js";
3
3
  export type { Item, SortEvent as ArrayEvent, SortSnapshot as ArraySnapshot, SortStep as ArrayStep } from "./types.js";
4
- export { createSortPlayer as createArrayPlayer } from "../core/player.js";
5
- export { createSortTimeline as createArrayTimeline } from "../core/timeline.js";
6
- export type { SortPlayer as ArrayPlayer } from "../core/player.js";
7
- export type { SortFrame as ArrayFrame, SortTimeline as ArrayTimeline } from "../core/timeline.js";
4
+ export { createArrayPlayer } from "./player.js";
5
+ export { createArrayTimeline } from "./timeline.js";
6
+ export type { ArrayPlayer } from "./player.js";
7
+ export type { ArrayFrame, ArrayTimeline } from "./timeline.js";
@@ -1,3 +1,3 @@
1
1
  export { createArrayTrace, defineArrayAlgorithm } from "./trace.js";
2
- export { createSortPlayer as createArrayPlayer } from "../core/player.js";
3
- export { createSortTimeline as createArrayTimeline } from "../core/timeline.js";
2
+ export { createArrayPlayer } from "./player.js";
3
+ export { createArrayTimeline } from "./timeline.js";
@@ -0,0 +1,10 @@
1
+ import { type Player } from "../playback/index.js";
2
+ import type { SortStep, SortSnapshot, SortEvent } from "./types.js";
3
+ import type { ArrayFrame } from "./timeline.js";
4
+ export type ArrayPlayer = Player<SortSnapshot, SortEvent>;
5
+ /** Array playback adapter for array steps ending in done. */
6
+ export declare function createArrayPlayer(steps: Iterable<SortStep>, options: {
7
+ stepDurationMs: number;
8
+ finalHoldMs: number;
9
+ render?: (frame: ArrayFrame) => void;
10
+ }): ArrayPlayer;
@@ -1,5 +1,5 @@
1
1
  import { createPlayer } from "../playback/index.js";
2
- /** Compatibility adapter for array steps ending in done. */
3
- export function createSortPlayer(steps, options) {
2
+ /** Array playback adapter for array steps ending in done. */
3
+ export function createArrayPlayer(steps, options) {
4
4
  return createPlayer(steps, { ...options, isTerminal: step => step.event.type === "done" });
5
5
  }
@@ -0,0 +1,8 @@
1
+ import { type Frame, type Timeline } from "../playback/index.js";
2
+ import type { SortEvent, SortSnapshot, SortStep } from "./types.js";
3
+ export type ArrayFrame = Frame<SortSnapshot, SortEvent>;
4
+ export type ArrayTimeline = Timeline<SortSnapshot, SortEvent>;
5
+ export declare function createArrayTimeline(steps: readonly SortStep[], options: {
6
+ stepDurationMs: number;
7
+ finalHoldMs: number;
8
+ }): ArrayTimeline;
@@ -1,4 +1,4 @@
1
1
  import { createTimeline } from "../playback/index.js";
2
- export function createSortTimeline(steps, options) {
2
+ export function createArrayTimeline(steps, options) {
3
3
  return createTimeline(steps, options);
4
4
  }
@@ -1,5 +1,5 @@
1
- import type { Item, SortSnapshot } from "../core/types.js";
2
- import type { SortFrame } from "../core/timeline.js";
1
+ import type { Item, SortSnapshot } from "../array/types.js";
2
+ import type { ArrayFrame as SortFrame } from "../array/timeline.js";
3
3
  import { type Scene } from "../scene/index.js";
4
4
  export type ArrayObjects = {
5
5
  bar: {
@@ -2,3 +2,4 @@ export { createPlayer } from "./player.js";
2
2
  export type { Player } from "./player.js";
3
3
  export { createTimeline } from "./timeline.js";
4
4
  export type { Step, Frame, Timeline } from "./types.js";
5
+ export { createStepEmitter } from "./step-emitter.js";
@@ -1,2 +1,3 @@
1
1
  export { createPlayer } from "./player.js";
2
2
  export { createTimeline } from "./timeline.js";
3
+ export { createStepEmitter } from "./step-emitter.js";
@@ -13,6 +13,13 @@ export function createPlayer(steps, options) {
13
13
  iterator.return?.();
14
14
  }
15
15
  };
16
+ // A cleanup failure must not replace the source, predicate or render error.
17
+ const closeAfterFailure = () => {
18
+ try {
19
+ close();
20
+ }
21
+ catch { /* Preserve the error already being handled. */ }
22
+ };
16
23
  let current;
17
24
  try {
18
25
  const first = iterator.next();
@@ -21,7 +28,7 @@ export function createPlayer(steps, options) {
21
28
  current = first.value;
22
29
  }
23
30
  catch (error) {
24
- close();
31
+ closeAfterFailure();
25
32
  throw error;
26
33
  }
27
34
  let previous = current;
@@ -46,7 +53,7 @@ export function createPlayer(steps, options) {
46
53
  options.render?.(frame());
47
54
  }
48
55
  catch (error) {
49
- close();
56
+ closeAfterFailure();
50
57
  throw error;
51
58
  }
52
59
  return Object.freeze({
@@ -85,7 +92,7 @@ export function createPlayer(steps, options) {
85
92
  }
86
93
  catch (error) {
87
94
  disposed = true;
88
- close();
95
+ closeAfterFailure();
89
96
  throw error;
90
97
  }
91
98
  },
@@ -0,0 +1,6 @@
1
+ import type { Step } from "./types.js";
2
+ /**
3
+ * Capture state in a frozen, consecutively indexed step. Use one emitter per iterator.
4
+ * The callback owns snapshot isolation; state and event are not cloned or frozen.
5
+ */
6
+ export declare function createStepEmitter<State>(snapshot: () => State): <Event>(event: Event) => Step<State, Event>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Capture state in a frozen, consecutively indexed step. Use one emitter per iterator.
3
+ * The callback owns snapshot isolation; state and event are not cloned or frozen.
4
+ */
5
+ export function createStepEmitter(snapshot) {
6
+ let index = 0;
7
+ return (event) => {
8
+ const state = snapshot();
9
+ const step = Object.freeze({ index, state, event });
10
+ index++;
11
+ return step;
12
+ };
13
+ }
@@ -1,5 +1,5 @@
1
1
  import type { Step, Timeline } from "./types.js";
2
- /** Step i is complete at i * stepDurationMs; the final state is held afterwards. */
2
+ /** The step at array position i is complete at i * stepDurationMs; the final state is held afterwards. */
3
3
  export declare function createTimeline<S, E>(steps: readonly Step<S, E>[], options: {
4
4
  stepDurationMs: number;
5
5
  finalHoldMs: number;
@@ -1,4 +1,4 @@
1
- /** Step i is complete at i * stepDurationMs; the final state is held afterwards. */
1
+ /** The step at array position i is complete at i * stepDurationMs; the final state is held afterwards. */
2
2
  export function createTimeline(steps, options) {
3
3
  if (!steps.length)
4
4
  throw new RangeError("Timeline requires at least one step");
@@ -24,7 +24,7 @@ export function createTimeline(steps, options) {
24
24
  const progress = previous === step ? 1 :
25
25
  Math.min(1, (time - (index - 1) * stepDurationMs) / stepDurationMs);
26
26
  return Object.freeze({ previous: previous.state, current: step.state,
27
- progress, stepIndex: index, event: step.event });
27
+ progress, stepIndex: step.index, event: step.event });
28
28
  }
29
29
  });
30
30
  }
@@ -8,6 +8,7 @@ export interface Frame<S, E> {
8
8
  readonly previous: S;
9
9
  readonly current: S;
10
10
  readonly progress: number;
11
+ /** Source step.index; timing is based on sequence position, not this value. */
11
12
  readonly stepIndex: number;
12
13
  readonly event: E;
13
14
  }
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@grundyjs/algiviz",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "Deterministic algorithm steps for educational visualizations",
5
5
  "type": "module",
6
6
  "files": [
7
- "dist",
7
+ "dist/array",
8
+ "dist/playback",
9
+ "dist/scene",
10
+ "dist/canvas",
8
11
  "README.md",
9
12
  "LICENSE",
10
13
  "CHANGELOG.md"
@@ -25,11 +28,6 @@
25
28
  "import": "./dist/playback/index.js",
26
29
  "default": "./dist/playback/index.js"
27
30
  },
28
- "./core": {
29
- "types": "./dist/core/index.d.ts",
30
- "import": "./dist/core/index.js",
31
- "default": "./dist/core/index.js"
32
- },
33
31
  "./canvas": {
34
32
  "types": "./dist/canvas/index.d.ts",
35
33
  "import": "./dist/canvas/index.js",
@@ -1,2 +0,0 @@
1
- export declare const insertion: import("../array/trace.js").ArrayAlgorithm;
2
- export declare const bubble: import("../array/trace.js").ArrayAlgorithm;
@@ -1,36 +0,0 @@
1
- import { defineArrayAlgorithm } from "../array/trace.js";
2
- // Legacy core exports only. New consumers provide algorithms like examples/algorithms.mjs.
3
- export const insertion = defineArrayAlgorithm(function* (array) {
4
- yield array.start({ sortedPrefixLength: Math.min(1, array.length) });
5
- for (let i = 1; i < array.length; i++) {
6
- const key = array.at(i);
7
- yield array.select(i, { sortedPrefixLength: i });
8
- let hole = i;
9
- while (hole > 0) {
10
- yield array.compare(hole - 1, 'held');
11
- if (array.at(hole - 1).value <= key.value)
12
- break;
13
- yield array.shift(hole - 1, hole, { sortedPrefixLength: hole - 1 });
14
- hole--;
15
- }
16
- yield array.insert(hole, { sortedPrefixLength: i + 1 });
17
- }
18
- yield array.done({ sortedPrefixLength: array.length });
19
- });
20
- export const bubble = defineArrayAlgorithm(function* (array) {
21
- yield array.start({ sortedSuffixLength: array.length < 2 ? array.length : 0 });
22
- for (let end = array.length - 1; end > 0; end--) {
23
- let swapped = false;
24
- for (let left = 0; left < end; left++) {
25
- yield array.compare(left, left + 1);
26
- if (array.at(left).value <= array.at(left + 1).value)
27
- continue;
28
- yield array.swap(left, left + 1);
29
- swapped = true;
30
- }
31
- yield array.pass(end, { sortedSuffixLength: swapped ? array.length - end : array.length });
32
- if (!swapped)
33
- break;
34
- }
35
- yield array.done({ sortedSuffixLength: array.length });
36
- });
@@ -1,4 +0,0 @@
1
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
2
- export declare const bubbleSortSteps: (values: readonly number[]) => readonly import("./types.js").SortStep[];
3
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
4
- export declare const iterateBubbleSortSteps: (values: readonly number[]) => Generator<import("./types.js").SortStep, void, unknown>;
@@ -1,5 +0,0 @@
1
- import { bubble } from "../compat/algorithms.js";
2
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
3
- export const bubbleSortSteps = bubble.steps;
4
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
5
- export const iterateBubbleSortSteps = bubble.iterate;
@@ -1,7 +0,0 @@
1
- export { insertionSortSteps, iterateInsertionSortSteps } from "./insertion-sort.js";
2
- export { bubbleSortSteps, iterateBubbleSortSteps } from "./bubble-sort.js";
3
- export type { Item, SortEvent, SortSnapshot, SortStep } from "./types.js";
4
- export { createSortTimeline } from "./timeline.js";
5
- export type { SortFrame, SortTimeline } from "./timeline.js";
6
- export { createSortPlayer } from "./player.js";
7
- export type { SortPlayer } from "./player.js";
@@ -1,4 +0,0 @@
1
- export { insertionSortSteps, iterateInsertionSortSteps } from "./insertion-sort.js";
2
- export { bubbleSortSteps, iterateBubbleSortSteps } from "./bubble-sort.js";
3
- export { createSortTimeline } from "./timeline.js";
4
- export { createSortPlayer } from "./player.js";
@@ -1,4 +0,0 @@
1
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
2
- export declare const insertionSortSteps: (values: readonly number[]) => readonly import("./types.js").SortStep[];
3
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
4
- export declare const iterateInsertionSortSteps: (values: readonly number[]) => Generator<import("./types.js").SortStep, void, unknown>;
@@ -1,5 +0,0 @@
1
- import { insertion } from "../compat/algorithms.js";
2
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
3
- export const insertionSortSteps = insertion.steps;
4
- /** @deprecated Define your algorithm with @grundyjs/algiviz/array. */
5
- export const iterateInsertionSortSteps = insertion.iterate;
@@ -1,10 +0,0 @@
1
- import { type Player } from "../playback/index.js";
2
- import type { SortStep, SortSnapshot, SortEvent } from "./types.js";
3
- import type { SortFrame } from "./timeline.js";
4
- export type SortPlayer = Player<SortSnapshot, SortEvent>;
5
- /** Compatibility adapter for array steps ending in done. */
6
- export declare function createSortPlayer(steps: Iterable<SortStep>, options: {
7
- stepDurationMs: number;
8
- finalHoldMs: number;
9
- render?: (frame: SortFrame) => void;
10
- }): SortPlayer;
@@ -1,8 +0,0 @@
1
- import { type Frame, type Timeline } from "../playback/index.js";
2
- import type { SortEvent, SortSnapshot, SortStep } from "./types.js";
3
- export type SortFrame = Frame<SortSnapshot, SortEvent>;
4
- export type SortTimeline = Timeline<SortSnapshot, SortEvent>;
5
- export declare function createSortTimeline(steps: readonly SortStep[], options: {
6
- stepDurationMs: number;
7
- finalHoldMs: number;
8
- }): SortTimeline;
@@ -1 +0,0 @@
1
- export type { Item, SortEvent, SortSnapshot, SortStep } from "../array/types.js";
@@ -1 +0,0 @@
1
- export {};