Tropical

Server-rendered islands for Datastar. An island is a function returning hiccup, defined with
defisland. Each browser tab has a session on the server, which renders the tab's islands again when something they
read changes, and sends them over one SSE stream for Datastar to morph into the page.
Three guarantees come with the rendering:
- Resources live exactly as long as the view that uses them. A subscription, a poll loop or a query belongs to the
islands that ask for it, and is released when the last of them stops asking. Readers of the same call site and
arguments share one, across sessions, and it lingers briefly after its last reader leaves.
- Access control is scope-based. A subtree the user may not see is never rendered: its markup is never produced,
its subscriptions are never opened, and its actions never exist.
- A failure stays in its island. A render that throws, an
assert included, renders an error view in the island's
place. Its parent never sees the exception, and the rest of the page goes on. The default view shows only a
reference, which the exception is logged under, so nothing the exception says reaches the browser.
co.multiply/tropical {:mvn/version "0.9.0"}
Requires Clojure 1.12+ and JDK 25+. The wire follows Datastar's SDK specification, tested against the Datastar 1.0.4
client.
An example
(ns my.app
(:require
[co.multiply.tropical.action :refer [use-action]]
[co.multiply.tropical.island :refer [defisland use-watch]]
[co.multiply.tropical.ring :as ring]
[dev.onionpancakes.chassis.core :as h]))
(defonce !clicks (atom 0))
(defisland counter
[]
(let [n (use-watch !clicks)
click (use-action :click (fn [_] (swap! !clicks inc) nil))]
[:p "Clicked " n " times " [:button {:data-on:click click} "+1"]]))
(defisland app
[uid]
[:main [:h1 "Hello, " uid] (counter)])
(defn page
"The page, rendered from the session's first frame, which the body holds
(`ring/mount`). Only its islands are patched from then on."
[_]
(h/html
[h/doctype-html5
[:html
[:head [:script {:type "module"
:src "https://cdn.jsdelivr.net/gh/starfederation/[email protected]/bundles/datastar.js"}]]
(ring/mount :body)]]))
(def tropical
{:root (fn [{:keys [uid]}] (app uid))
:page page
;; Your authentication: the user a request is made on behalf of, or nil.
:uid current-user})
(def routes
;; The page, the stream it opens and its actions, all at the page's URL.
[["/" {:handler (ring/handler tropical)}]])
Every tab that shows the counter sees each click: counter read !clicks, so each session renders counter again,
without rendering app, and sends that one island.
The model
- Everything renders on the server. There is no client component model and no client-side Clojure: the browser
holds HTML, Datastar's signals, and the scripts the app chose to load. What changes without a round trip is a
Datastar expression in an attribute.
- Islands are functions of their arguments. Calling one places it. The runtime renders it when it is new, when its
arguments changed, or when something it read through a hook changed, and otherwise reuses its last output. Hooks
are keyed by name, not by call order. (Islands)
- Reads from outside the page are shared resources.
observe opens a subscription, a poll loop or a query once for
every reader of its call site and arguments, across sessions, and closes it after the last leaves. (Reading from
outside the page)
- Writes are actions. An island renders an action into the page as a token bound to the user, revoked when the
island stops rendering it. The handler changes state, and the islands that read that state render again and go out
over the stream. (Actions)
- One session per tab, not per connection. A tab's session renders its islands on a thread of its own, and
outlives its stream for a grace period, so a reconnect resyncs from what it has. Changes coalesce, so a slow client
gets fewer frames, never a backlog. (How it works)
Mounting it