Urgent.News

What's breaking now, across thousands of outlets.

Tech

Bonsai: Janestreet's UI Library

Article URL: https://github.com/janestreet/bonsai Comments URL: https://news.ycombinator.com/item?id=49152842 Points: 188 # Comments: 70

Bonsai is a UI library designed for creating efficient, reactive web applications in OCaml. Inspired by Elm, Bonsai allows developers to build almost all web applications within Jane Street, from corporate directories to trading system monitoring tools.

Components in Bonsai are implemented as purely functional state machines, making them easy to compose. The framework's incrementalization ensures that values are only recomputed when necessary, unlike other web frameworks that combine state, incrementality, and rendering into a single abstraction.

Bonsai distinguishes itself by allowing developers to compose state and incrementality primitives, preventing the entire page from re-rendering during user interaction. This same capability can also be used to incrementally compute expensive business logic on live-updating datasets.

For state management, Bonsai provides an extensive API, including lifecycle and scoping management. This is particularly useful when embedding stateful UI components within another component, such as in a tabbed interface.

Bonsai's use of OCaml enables the application of the same language and types on both the backend and frontend. This alignment enhances legibility and simplifies managing large web app codebases, especially when leveraging OCaml's type system to minimize errors.

Jane Street utilizes Bonsai to port existing types and business logic from terminal UIs to the web. Additionally, Bonsai comes with a powerful templating language, support for component-specific stylesheets, and automated whole-app testing. One of its most powerful features is the ability to write realistic tests, allowing programmers to programmatically manipulate UI elements and observe the DOM's evolution.

The library also supports mock server calls and can test state changes driving the UI. With these tests, developers can write entire components without opening their browsers. The Bonsai Quick Start and Thinking in Bonsai offer hands-on introductions to the library, with Bonsai_web and Bonsai_term offering specialized applications for web and terminal-based UIs, respectively.

Written by urgent.news from Hacker News Best's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at github.com →

More in Tech

More from Monday 3 August →