Titan Engine
QUICKSTART & API OVERVIEW

Getting Started with Titan Engine

Titan is a UI-agnostic spreadsheet calculation engine written in Rust and compiled to WebAssembly. It manages formula compilation, topological dependency sorting, string interning, and undo history while exposing zero-copy typed array views to JavaScript.

1. Installation

Install via NPM or import directly from an ES module CDN:

Bundler (Vite / Next.js / Webpack) NPM
# 1. Install the package
npm install titan-engine

// 2. Import in your application
import { initTitan } from 'titan-engine';
Browser ES Module CDN
import { initTitan } from 'https://unpkg.com/titan-engine/titan.js';

2. Initialization, Addressing & Subscriptions

Initialize the WebAssembly module once with await initTitan(). Every sheet method accepts either Excel A1 strings ("B2") or zero-indexed coordinates ({ row: 1, col: 1 }).

import { initTitan } from 'titan-engine';

const Titan = await initTitan();
const engine = new Titan();
const sheet = engine.addSheet("Dashboard");

// React to cascading dependency changes
const unsubscribe = engine.onCellsChanged((changes) => {
  for (const change of changes) {
    console.log(`${change.sheetName}!${change.a1} = ${change.cell.display}`);
  }
});

// Mutate cells using A1 or { row, col }
sheet.set("A1", 15);
sheet.set({ row: 1, col: 0 }, 25);
sheet.set("B1", "=SUM(A1:A2) * 2"); // Evaluates to 80

3. Bulk Updates & Atomic Undo/Redo

When loading CSVs or pasting multi-cell selections, use sheet.setRange() or engine.setCells(). Batch mode suspends topological sorting during insertion and evaluates the dependency graph once at the end.

// 2D range paste starting at A1
sheet.setRange("A1", [
  ["ID", "Rep",   "Sales"],
  ["1",  "Alice", 500],
  ["2",  "Bob",   "=C2 * 1.5"]
]);

// Multi-sheet batch update
engine.setCells([
  { sheet: "Dashboard", cell: "Z1", value: 100 },
  { sheet: 0, cell: { row: 5, col: 5 }, value: "=Dashboard!Z1 * 10" }
]);

// O(1) reverse-patch time travel
engine.undo();
engine.redo();

4. Zero-Copy Viewports

Instead of serializing strings across the WASM boundary on every scroll frame, create a Viewport to read 32×32 SoA chunks directly from WASM linear memory via Uint8Array and Float64Array / BigUint64Array views.

const viewport = sheet.createViewport();

// Inside your 60fps canvas/grid render loop:
const { type, value } = viewport.getCell(150, 20);
// type: 0 = Empty, 1 = Number, 2 = StringId, 3 = Bool, 4 = ErrorId