Targets · TypeScript · Entry point

Entry point

Starting a program and attaching it to a root element in the page.

Contents

How a Program becomes TypeScript, in parts. Start with the introduction, then follow each part into the rendered output and the runtime it links against.

One call, typed

A program starts with a single typed call. It takes the root component — a callback that runs the program — and the array of drivers the embed uses, and returns a cancel function that tears the program down.

const cancel: () => void = Program.Application(
  (): void => Main(),              // the root component — the program itself
  [ Ui.Dom.Driver('app') ],        // render the interface into <div id="app">
);
window.addEventListener('beforeunload', cancel);

The signature is Program.Application(content: () => void, drivers: Driver[]): () => void. Types are carried through the whole call: the root component's reactive values are ReactiveValue<T>, and each driver is checked against the Driver interface, so a mis-wired embed fails to compile rather than at runtime.

Attaching to the page

The interface is attached to one root element. The DOM interface driver takes that element — by id or as an HTMLElement — owns everything inside it, and patches it in place each cycle.

<div id="app"></div>
<script type="module">
  import { Program, Ui } from './app.js';
  Program.Application(() => Main(), [ Ui.Dom.Driver('app') ]);
</script>

Many isolated programs can share one page, each mounted into its own node.

Stopping

The returned cancel stops the cycle, tears down the drivers, and releases the root element — wired to beforeunload for a whole-page app, or called by hand to remove an embed that comes and goes.