Building the iPhone app in SwiftUI
The iPhone app of Tessera is written in SwiftUI, with no web view, and its engine is the TypeScript core that also runs the web app, executed inside JavaScriptCore. This post follows one edit from the key press to the screen and describes each part that it passes through.
I built the first version of this app around an engine written in Rust, which the app reached through UniFFI and which kept the library in a SQLite database; Swift drew the views that the engine described, and it sent what the person did back as typed commands. That version taught me which boundary to keep, a small Swift protocol named LibraryService through which every view talks to the engine, so that whatever stands behind it can be replaced while the views stay as they are. The version I am building now replaces the Rust engine with the TypeScript core that also runs the web app, so that a rule, a plugin or a bug fix is written once and applies to both, and everything below describes this version, which is under construction, and not the one in the App Store today.
The views are made from the components that Apple ships with iOS. In the sidebar, Today, Upcoming, Deadlines, Anytime and the Logbook are rows of a plain List, and choosing one pushes its page onto a NavigationStack; tags filter through a standard Menu, and dates are chosen with the system date picker; photos come from the system photo picker, an invitation from another device is scanned with VisionKit, and the events of the calendars on the phone, which iOS provides through EventKit, appear among the dated tasks; the Home Screen widget is built with WidgetKit. I use these components because they carry behaviour that I would otherwise have to rebuild and then maintain through every release of the system: a List scrolls and bounces as every other list on the phone does, a swipe from the left edge goes back, a long press opens the system's menu, text follows the size chosen in Settings, and animations stop when Reduce Motion is on.
The only UIKit component in the app is the text of a row, a text view wrapped for SwiftUI, because editing text is what a person does most in an outliner, and the system's text view already provides selection, dictation, spell checking and reading aloud, which a text field built from scratch would have to reproduce.
The engine runs in JavaScriptCore, the JavaScript engine that is part of iOS. The app creates one virtual machine with one context, held by an actor named TesseraCore and pinned to a dedicated thread, so that no other thread touches the engine and a long computation does not block the screen; every call into the engine is a Swift async function that returns when the engine has answered. Inside an app JavaScriptCore runs without its JIT compiler, which is why its speed has to be measured on a phone and not estimated from a Mac, and the plugins run in the same engine, each in an isolated compartment, so that a plugin which lists ios among its platforms behaves on the phone as it does in the browser.
SwiftUI views
Draw what the stores hold, from the standard components: List, NavigationStack, Menu, sheets, the system date picker and a UIKit text view for the text of a row.
Stores
One @Observable model for the sidebar, one for each open outline and one for each visible row, on the main actor. A batch is applied to all of them in one turn.
TesseraKit
A Swift package with the actor TesseraCore. It sends intents to the engine, reads the one stream of batches, and carries the generated Swift types.
The core, in JavaScriptCore
The same TypeScript core as the web app: the rules, the plugins, undo, search and sync. One engine on one dedicated thread.
Ports
SQLite, the Keychain, URLSession, WebRTC, notifications and files. They carry bytes and decide nothing.
Beside the app
The Home Screen widget, a separate extension. It reads small summaries that the app writes for it, and it never opens the library.
Every type that crosses the boundary between Swift and the engine, such as an intent, an outcome, an event or a row as it is shown, is declared once in the core as a schema, and I do not keep the two sides in agreement by hand. The TypeScript types and the runtime checks are produced from that schema, a generator produces the Swift types, and a check fails when the generated files are out of date; where a type is one of several shapes, the generator writes a Swift enum with associated values, so that a switch in a view is written as it would be by hand and the compiler reports a case that is missing.
What comes back from the engine is a single ordered stream with one batch for each change, whoever made the change: the person typing, another device, undo or a plugin. One Swift task reads the stream and applies each batch to the stores in one turn of the main actor, which is why the sidebar, the page and the settings pane show the same revision after every batch. There is one store for the sidebar, one for each open outline and one for each visible row, so a changed title redraws that row and nothing else; a view subscribes with task(id:), which starts the subscription when the view appears, restarts it when the id changes and cancels it when the view disappears, and the cancellation continues into the engine, which stops producing items for that view. The text being typed lives in the model of its row and is excluded from observation, so that a key press updates the text view without redrawing the list around it.
Typing needs an answer before the next key is pressed, which an asynchronous engine cannot promise; the live preview of Markdown in a note and the items of the / menu are therefore computed on every keystroke by a few functions that run in a second context, on the main thread, with no state, while every change to the library still goes through the actor and the engine like any other change.
One edit, from the key to the screen
- The text view of a row reports the change to the store of that row.
- The store sends an intent to TesseraCore, which hands it to the engine on the engine's dedicated thread.
- The engine runs it through the steps that every change goes through, where a plugin can refuse or amend it, commits it, and puts one batch on the stream.
- The task that reads the stream applies the batch to the stores in one turn of the main actor.
- Only the views of the rows that changed are drawn again.
The engine reaches the outside of the app only through ports, which carry bytes and decide nothing: storage is the SQLite that ships with iOS; secrets are kept in the Keychain, sealed with AES-256-GCM under a wrapping key that never leaves the phone; HTTP goes through URLSession, notifications through the notification centre, and the data channels that the sync between devices uses go through a WebRTC package. Building a request, signing it, reading the answer and retrying all happen in the engine, so that Swift only fetches bytes and hands them over, and the callbacks that WebRTC delivers on threads that it chooses cross into the engine through an AsyncStream, with no lock held while the engine processes them.
The widget is a separate extension that never opens the library; the app is the only process that does, and each time Today changes it writes a small summary, replaced in one step, which the widget reads.