Launchpad (Templating Engine) — Internals
This document covers how Launchpad works under the hood: the class architecture, the template compilation pipeline, the reactive dependency tracking system, the assemble/launch execution flow, template caching, wrappers, threading, hot-reload behavior, and Server Element processing. It is intended for contributors, framework developers, and advanced users who need to understand or debug Launchpad's internal behavior.
Architecture Overview
Launchpad's internals are spread across several classes in the spaceport.launchpad package, plus a supporting class in spaceport.engineering:
| Class | File | Responsibility |
|---|---|---|
Launchpad |
Launchpad.groovy |
Orchestrates assembly, priming, rendering, reactive binding management, WebSocket lifecycle, and Server Element processing |
SpaceportTemplateEngine |
SpaceportTemplateEngine.groovy |
Preprocesses .ghtml source text, processes <g:*> generator tags, compiles templates into executable Groovy scripts |
SpaceportTemplate |
Inner class of SpaceportTemplateEngine |
Parses template text into a Groovy script string, holds the compiled Script, and produces Writable instances for rendering |
Catch |
Catch.groovy |
Proxy object that intercepts property access during reactive expression evaluation to discover which variables an expression depends on |
GHTMLScriptMetaClass |
LaunchpadMetaClasses.groovy |
Custom DelegatingMetaClass that provides _source and _error properties on script objects and redirects missing properties to $-prefixed binding variables |
Beacon |
engineering/Beacon.groovy |
File-system watcher (Java WatchService) used for hot-reload in debug mode |
How the Classes Relate
Launchpad (orchestrator)
|
|-- creates --> SpaceportTemplateEngine (per prime() call)
| |
| |-- creates --> SpaceportTemplate (compiled template)
| |
| |-- uses --> GHTMLScriptMetaClass (reactive property resolution)
|
|-- creates --> Catch (proxy for dependency tracking during ${{ }} evaluation)
|
|-- uses --> Beacon (file watcher for hot-reload, debug mode only)
These subsystems interact during two distinct phases:
- Render phase — When
launch()is called. Templates are preprocessed, compiled, executed, and the final HTML is written to the client. Reactive closures and server action closures are registered during this phase. - Interactive phase — After the page is delivered. The client sends events back over HTTP (server action invocations) and maintains a WebSocket connection (for receiving reactive updates and communicating with Server Elements).
Template Compilation Pipeline
When a .ghtml file is primed (rendered), it passes through a multi-stage pipeline. Understanding this pipeline is essential for debugging template errors, since error messages and stack traces reference the compiled Groovy script rather than the original template source.
Stage 1: File Reading
The prime() method reads the raw file content from disk:
text = new File(sourcePath + 'parts/' + filePath)?.text
The file path is relative to the parts/ subdirectory under the Launchpad source path. A folder traversal check prevents .. in paths (unless allowFolderTraversal is explicitly set to true). If the file does not exist, prime() returns an HTML comment: <!-- LaunchPad: Part not found: filename -->.
Stage 2: Preprocessing (SpaceportTemplateEngine.createTemplate)
The engine applies a series of regex-based transformations to the raw text, in this order:
1. Triple-slash comment removal. Lines matching ^\s*///.* are stripped entirely:
text = text.replaceAll(/(?m)^\s*\/\/\/.*/, '')
These comments never appear in the rendered output, unlike HTML comments which are sent to the client.
2. HUD tag normalization. <hud:*> tags are converted to <g:*> tags so they go through the same generator processing pipeline:
text = text.replaceAll(/<hud:/, '<g:')
text = text.replaceAll(/<\/hud:/, '</g:')
3. <g:javascript> expansion. Converted to standard script tags for IDE compatibility:
text = text.replaceAll(/<g:javascript>/, '<script type="text/javascript">')
text = text.replaceAll(/<\/g:javascript>/, '</script>')
4. Generator tag processing. All <g:*> tags are processed from innermost to outermost. The engine locates the last <g: occurrence in the text, processes it, then repeats until no more remain. This inside-out approach ensures nested generators are resolved before their parents.
For each tag, the engine:
- Parses the tag name, attributes (handling quoted values, unquoted values, and values containing
{ }brackets), and body content - Dispatches to either:
- A built-in generator (defined in
SpaceportTemplateEngine.generators) — the generator closure transforms the tag into Groovy template code - A registered Server Element — the tag is replaced with comment-based placeholder markers for later element processing
- If neither matches, an HTML comment is produced:
<!-- SpaceportTemplateEngine: Unknown tag: tagName -->
- A built-in generator (defined in
Built-in generators and their transformations:
| Generator | Input | Output |
|---|---|---|
<g:each in="source" var="x">body</g:each> |
Source collection + optional var name | <% for (def x in source) { %> body <% } %> |
<g:repeat count="n">body</g:repeat> |
Count attribute | Literal string repetition of body n times |
Iteration sources accept three syntaxes for their value attributes:
storeName#flagPath— expanded toCargo.fromStore("storeName").get("flagPath")${expression}— the${ }wrapper is stripped, leaving the raw expression$variable— the$prefix is stripped
When a <g:*> tag matches a registered Server Element instead of a built-in generator, it is replaced with placeholder markers:
<!-- id-styles --><id originalAttributes><!-- id-body -->body<!-- END id-body --></id><!-- id-handler -->
These markers are resolved later during the parseElements() phase of launch().
5. @Provided annotation stripping. Lines containing @Provided are wrapped in block comments:
text = text.replaceAll(/(@Provided.*)/, '/* $1 */')
@Provided is purely an IDE/documentation hint — it has no runtime effect.
6. Action delegate injection. Inside server action closures (${ _{ ... }}), closure calls like $closureName(args) have delegate assignment code injected. This ensures that closures called within server actions use the Catch proxy as their delegate, enabling recursive reactive dependency tracking:
// Before preprocessing
${ _{ $myHandler(data) }}
// After preprocessing (conceptual)
${ _{ ;try{$myHandler.delegate=actionDelegate;$myHandler.resolveStrategy=Closure.DELEGATE_FIRST;}catch(e){};$myHandler(data) }}
This transformation also applies to regular script-scoped closure assignments (= { ... }) that contain $closureName() calls.
7. Root-level def removal. The removeDefsOutsideBraces() method strips def keywords from variable declarations at the root scope (not inside { } blocks). It uses brace-depth tracking to determine whether each def is at the top level:
text = removeDefsOutsideBraces(text)
This converts root-level local variables into binding properties, enabling:
- Cross-part variable sharing — all parts in a single
launch()share the sameBinding - Reactive tracking — binding properties can be intercepted by the
Catchproxy
Variables inside closures retain their def and remain local to that closure scope.
8. Reactive expression sugar. The ${{ expr }} syntax is transformed into a two-parameter closure call:
text = text.replaceAll(/\$\{\{\s*/, '\\${ _{ _script, _registration -> ')
So ${{ dock.counter.get() }} becomes ${ _{ _script, _registration -> dock.counter.get() }}. The two parameters (_script, _registration) signal to the bind() method that this is a "launch reaction" rather than a "launch binding" (server action).
9. <g:prime> inline inclusion. Tags like <g:prime src="partial.ghtml"/> are replaced with the rendered content of the referenced file. This inclusion happens during preprocessing, before the template is compiled, so the primed content becomes part of the same compiled script.
10. Import conversion. JSP/GSP-style import directives are extracted, converted to Groovy import statements, and prepended to the template text:
<%@ page import="spaceport.Spaceport; documents.MyDoc" %>
Becomes:
<% import spaceport.Spaceport; import documents.MyDoc; %>
Multiple <%@ page import="..." %> directives are collected and combined into a single import block.
Stage 3: Template Parsing (SpaceportTemplate.parse)
After preprocessing, the text is passed to SpaceportTemplate.parse(), which converts the mixed HTML/Groovy text into a pure Groovy script string. The parser is a character-level state machine that processes the input one character at a time:
- Literal text is accumulated and wrapped in
out.print("""...""")statements <% ... %>scriptlet blocks are extracted as raw Groovy code. The parser closes the currentout.print(""")call, inserts the code, then opens a newout.print("""<%= expr %>expression blocks are converted to${expr}inside the print statement${ ... }GString expressions are passed through, with nested brace tracking to handle expressions like${ map.collect { it.key } }- Double quotes in literal text are escaped with backslashes (since the output is wrapped in triple-quoted strings)
- Line endings are normalized to
\n
The resulting script starts with out.print(""" and ends with """);\n/* Generated by SpaceportTemplateEngine */.
Stage 4: Script Compilation
The parsed script string is compiled into a Script class using a GroovyShell that shares the application's classloader (SourceStore.classLoader):
def n = name.replace('-','_').replace('.','_') + '_' + counter++ + ".ghtml.groovy"
template.script = this.groovyShell.parse(script, n)
The engine itself receives a Launchpad reference (new SpaceportTemplateEngine(launchpad).createTemplate(filePath, text)) so that compile-time <g:> resolution can use launchpad.resolveElement(name) — meaning each slice's templates see its own local elements plus the shared pool. The no-arg constructor still works for callers without a Launchpad context; those fall back to the shared pool only.
The script is given a generated name like index_ghtml_42.ghtml.groovy for stack trace readability. The monotonically increasing counter ensures unique class names even when the same template is recompiled. Compilation errors at this stage indicate syntax problems in the template's Groovy code (after all preprocessing transformations).
Stage 5: Caching
The compiled SpaceportTemplate is stored in a static ConcurrentHashMap keyed by template file path:
templateCache.put(name, [template: template, hash: originalText.hashCode()])
On subsequent requests, if the template name exists in the cache and the text hash matches, the cached template is returned without recompilation:
if (templateCache.containsKey(name) && templateCache.get(name).get('hash') == text.hashCode()) {
return templateCache.get(name).get('template') // Cache hit
}
This means:
- First request — Full preprocessing, parsing, and compilation. Result is cached.
- Subsequent requests with unchanged file — Cache hit. Only the file read occurs; all processing is skipped.
- Requests after file modification — Hash mismatch triggers full recompilation and cache update.
The cache is also cleared whenever new Elements are registered into a Launchpad, so templates are recompiled against the updated element registry.
Known limitation. templateCache is keyed on template name/hash, not on Launchpad. If the same template path gets compiled from two different Launchpads with diverging element-visibility views, the cache could return a placeholder-baked version meant for the other Launchpad. In practice each Launchpad has its own source path (so template file paths naturally differ), but it's worth knowing if include/share patterns cross Launchpad boundaries.
Stage 6: Execution (SpaceportTemplate.make)
When prime() calls template.make(scriptBinding), the template creates a Writable that performs the actual rendering:
- Creates a fresh
Scriptinstance from the compiled class viaInvokerHelper.createScript(). This ensures each rendering gets its own script object even though the compiled class is shared. - Attaches a
GHTMLScriptMetaClassto the script for reactive property resolution and error tracking. - Sets up the script's
PrintWriter(bound toout),_selfreference,_bbinding reference, and_source(for debugging). - Registers the script in the binding's
_scriptsmap by class name. - Executes
scriptObject.run(), which writes rendered HTML to the writer. - If execution throws, captures the error with line-number context, surrounding source lines, and a collapsible full stack trace.
The Binding System
All templates rendered in a single launch() call share a Binding object backed by a per-Launchpad map (Collections.synchronizedMap([:])). The other maps on Launchpad — bindingSatellites, elements, sharedPool, poolOwners, byName, and the per-binding _reactions / _bindings sub-maps — are ConcurrentHashMaps; they're pointer registries keyed by UUID or name with object values.
When launch() begins, it populates the binding with request-scoped variables:
binding.putAll([
r : r, // HttpResult
context : r.context, // HttpContext
client : r.context.client, // Client
data : r.context.data, // Request data Map
dock : r.context.dock, // Session Cargo
cookies : r.context.cookies, // Cookies
prime : this.&prime, // The prime() method reference
_ : this.&bind, // The bind() method reference (server actions/reactions)
__ : this.&parseElements, // Internal element parsing
'_reactions' : new ConcurrentHashMap(),
'_bindings' : new ConcurrentHashMap(),
'_launchID' : '',
'_scripts' : [:]
])
When templates execute, their root-level variables (with def stripped by preprocessing) become additional binding properties. After all templates render, launch() renames user-defined variables by prefixing them with $:
def modifiedMap = variables.collectEntries { key, value ->
if (key.startsWith('_') || ['out','client','data','prime','cookies','r','context','dock'].contains(key)) {
[(key): value] // Reserved names stay as-is
} else {
[('$' + key): value] // User variables get $ prefix
}
}
This separates user-defined variables from framework internals and reserved names. The $ prefix is transparent to templates thanks to GHTMLScriptMetaClass.
GHTMLScriptMetaClass
Each template script object receives a custom DelegatingMetaClass that:
- Provides the
_sourceproperty (aStringBuilderholding the compiled script source for error reporting) - Provides the
_errorproperty (aStringBuilderfor capturing rendering errors) - Falls back to
$-prefixed binding variables when a property is not found on the script object directly:
Object getProperty(Object object, String property) {
if (property == '_source') return source
if (property == '_error') return error
try {
return super.getProperty(object, property)
} catch (MissingPropertyException e) {
return object.getBinding().variables.get('$' + property)
}
}
When a template references myVar, the metaclass first tries a normal property lookup. If that fails with MissingPropertyException, it looks for $myVar in the binding. This is how the def-stripping and $-prefixing remain invisible to template authors while enabling reactive tracking.
Reactive Dependency Tracking (The Catch Proxy)
The Catch class is the core mechanism for reactive dependency tracking. It acts as a transparent proxy that records which properties are accessed during expression evaluation.
Catch Class Implementation
class Catch {
Object through // The real object being proxied
def caught = new HashSet() // Set of property/method names accessed
def getProperty(String name) {
if (name == 'caught') return caught
caught << name // Record the access
return through."$name" // Delegate to real object
}
void setProperty(String name, value) {
caught << name // Record the write
through."$name" = value // Delegate to real object
}
def invokeMethod(String name, args) {
caught << name // Record the call
// If the property is a Closure, clone it with this Catch as delegate
if (through."$name" instanceof Closure) {
def copy = through."$name".clone()
copy.resolveStrategy = Closure.DELEGATE_ONLY
copy.delegate = this
return copy(*args)
}
return through.invokeMethod(name, args)
}
}
Key behaviors:
- Property reads record the property name and return the real value from the proxied object.
- Property writes record the property name and set the real value.
- Method calls record the method name. Critically, if the method resolves to a Closure property on the underlying object, the Closure is cloned with
Catchset as its delegate. This means nested closures within a reactive expression also have their dependencies tracked recursively. - The
caughtproperty itself is exempted from tracking (returns the raw set).
Reactivity model — important properties
Two pieces of how reactivity works are worth knowing:
- Tracking is by variable name.
caughtis aHashSet<String>of property/method names accessed during evaluation. Reactions store the set of names they touched on first evaluation as_triggers. The dispatcher fires reactions whose triggers intersect the current action's caught names. No value-based identity tracking, no proxy objects — purely string-keyed. - Cargo manages its own reactivity. The reaction dispatcher explicitly skips reactions whose result is a
Cargoinstance; Cargo has its own parent/document propagation, so the standard reaction loop steps aside.
How Reactive Bindings (Launch Reactions) Work
When bind() receives a two-parameter closure (indicating a reactive binding from ${{ }}), the following sequence occurs:
- A
Catchproxy is created, wrapping the closure's owner (the template script object). - The closure's resolve strategy is set to
DELEGATE_ONLYand its delegate is set to theCatchproxy. - The closure is called with
(closure.owner, uuid)to get the initial rendered value. During execution, every property access goes through theCatchproxy, which records the names in itscaughtset. - The closure's delegate and resolve strategy are restored to their originals.
- A reaction record is stored in
binding._reactions:
closestScript._reactions.put(uuid, [
'created' : System.currentTimeMillis(),
'reaction' : closure, // The original closure (for re-evaluation)
'_source' : sourceText, // Source line for debugging
'_currentHash' : reactionDigest(returnString), // 64-bit digest for change detection
'_triggers': delegate.caught // Set of variable names this expression depends on
])
- The rendered output is wrapped in HTML comment markers:
<!-- START_CHUNK@abc123def456 -->rendered content<!-- END_CHUNK@abc123def456 -->
These markers allow the client-side code to locate and replace the specific DOM region when updates arrive.
How Server Actions (Launch Bindings) Work
When bind() receives a one-parameter closure (indicating a server action from _{ t -> ... }), it:
-
Stores the closure in
binding._bindingswith a UUID key:closestScript._bindings.put(uuid, [ 'created' : System.currentTimeMillis(), 'action' : closure, 'script' : closestScript, '_source' : sourceText ]) -
Returns a URL string:
/!/lp/bind?launch={launchID}&uuid={uuid}. HUD-Core on the client parseson-*attributes, extracts these URLs, and calls them when the corresponding DOM event fires.
How Server Actions Trigger Reactive Re-evaluation
When a server action is invoked via HTTP to /!/lp/bind, the serveBinding() handler:
- Retrieves the binding from
bindingSatellitesby launch ID and the action closure by UUID. If not found in the main bindings, it checks Server Element bindings. - Validates that the requesting client matches the binding's client (security check — returns 405 on mismatch).
- Enhances the request data map
r.context.datawith typed accessor methods (getNumber,getBool,getString,getList,getInteger). - Creates a new
Catchproxy wrapping the action's script, sets it as the action closure's delegate, and calls the action with the enhanced data map. - After action execution, iterates all registered reactions for this launch:
for (def reaction in bindingSatellites.get(launch)?._reactions) {
// Skip reactions whose triggers don't overlap with accessed variables
if ((reaction.value._triggers as List).intersect(delegate.caught).size() == 0) continue
// Skip Cargo-backed reactions (Cargo handles its own reactivity)
if (binding.script."$reaction.key" instanceof Cargo) continue
def payload = reaction.value.reaction(action.owner, reaction.key).toString()
payload = bindingSatellites.get(launch).__(payload) // Process Server Elements in the new content
def payloadHash = reactionDigest(payload)
if (reaction.value._currentHash != payloadHash) {
// Send updated content via WebSocket
bindingSatellites.get(launch)._socket.session?.remote?.sendStringByFuture(
new JsonBuilder([
'action' : 'applyReaction',
'uuid' : reaction.key,
'payload': payload
]).toString())
reaction.value._currentHash = payloadHash
}
}
- The action closure's return value is processed based on its type:
MaporList— serialized to JSONStringorGString— passed throughparseElements()for Server Element processing, then returned as-is- Other types — converted to String via
.toString()
The Assemble/Launch Flow
assemble()
assemble() creates a brand-new Launchpad instance with the same source path but a fresh launchID, binding, and script binding:
Launchpad assemble(List<String> payloadFiles) {
def newConfiguration = new Launchpad(sourcePath)
newConfiguration.parts = payloadFiles
return newConfiguration
}
This design is critical for thread safety: every request gets its own Launchpad instance with its own isolated binding state. The original static Launchpad instance (typically declared as static Launchpad launchpad = new Launchpad() in a source module) is never modified by request processing.
launch()
launch() is synchronized on the Launchpad instance. Since each request gets a new instance from assemble(), this synchronization does not create contention between concurrent requests. The full execution sequence:
1. Populate the binding with request-scoped variables (as shown in the Binding System section above).
2. Prime the vessel (wrapper template) first:
def vtext = vessel ? prime(vessel) : null
The vessel is primed before any parts so that variables it defines are available to the parts.
3. Prime each part in order, concatenating their output:
for (def part in parts) {
if (part.toString().endsWith('.ghtml')) {
html = html + prime(part.toString())
}
}
4. Insert parts into the vessel by replacing the <payload/> tag:
html = vtext.replace('<payload/>', html)
If the vessel is missing <payload/>, the response status is set to 501.
5. Rename binding variables with $ prefix for reactive property resolution (described in the Binding System section).
6. Process Server Elements by calling parseElements(html), which instantiates element classes, calls their prerender() and initialize() methods, and injects their styles, handlers, and content into the HTML.
7. Inject WebSocket script if reactive features were used. The framework detects this by checking binding._socket:
if (binding._socket) {
if (!(binding._socket instanceof SocketContext)) {
binding._socket = launchID
binding._launchId = launchID
bindingSatellites.put(launchID, binding)
}
// Inject <script> before </body>
}
The injected script opens a WebSocket, registers with the server, and handles incoming applyReaction, elementResponse, and ping messages.
8. Write the final HTML to the client and log timing information:
r.writeToClient(html)
prime()
The prime() method renders a single template file:
- Checks for folder traversal in the path
- Reads the file from
sourcePath + 'parts/' + filePath - Creates a
SpaceportTemplateEngineand callscreateTemplate(filePath, text)(which runs the full preprocessing pipeline or returns a cached template) - Saves the current
outwriter and_selfscript reference from the binding (since prime can be called recursively from within templates) - Calls
template.make(scriptBinding)to execute the template and capture output as a String - Checks for errors in
binding._self._error - If the template contained Server Elements, creates element instances and registers them in
binding._elements(or stashes them for later processing) - Restores the previous writer and script reference
- Returns the rendered HTML string
Recursive priming works because the writer and script context are saved and restored on each call. All primed templates share the same binding, so variables defined in one template are visible in all others within the same launch().
How Wrappers (Vessels) Work Internally
A wrapper (vessel) is simply a regular .ghtml template that contains a <payload/> tag. There is no special class or configuration — any template with <payload/> in it can serve as a vessel.
The internal process:
- The vessel is primed first (before any content parts), giving it the opportunity to define variables, set up closures, or establish shared state.
- All content parts are primed in order and their output is concatenated.
- The vessel's rendered output has its
<payload/>tag replaced with the concatenated parts HTML:html = vtext.replace('<payload/>', html) - If
<payload/>is missing from the vessel, the response status is set to 501 and a debug message is logged.
Because the vessel shares the same binding with parts:
- Variables defined in the vessel's
<% %>blocks are accessible to all parts. - Parts can modify variables that the vessel defined.
- However, since the vessel's HTML is already rendered by the time parts execute, modifications to variables in parts do not retroactively change the vessel's rendered output. They only affect reactive expressions (
${{ }}) that are re-evaluated later via WebSocket, and they affect any parts that execute after the variable is modified.
Binding Satellites and WebSocket Lifecycle
When a launch uses reactive features (any ${{ }} or _{ }), the binding is registered as a "binding satellite" in a global static map:
static final bindingSatellites = new ConcurrentHashMap() // launchID -> Binding
Registration Flow
-
During template rendering, when
bind()encounters a reactive closure or server action, it walks up the closure's owner chain to find the owningScriptorElementobject. If_socketis not already aSocketContext(i.e., this is the first binding in this launch), it sets_socket = trueto signal that a WebSocket connection will be needed. -
At the end of
launch(), ifbinding._socketis truthy, the binding is stored inbindingSatelliteskeyed bylaunchID, and a client-side WebSocket script is injected before</body>. -
When the page loads in the browser, the injected script opens a WebSocket to
/!/lp/socketand sends a registration message:{ "handler-id": "launchpad-socket", "action": "register", "uuid": "<launchID>" } -
The
@Alert('on socket launchpad-socket')handler receives the registration, retrieves the binding satellite by UUID, and stores theSocketContext:Binding binding = bindingSatellites.get(uuid) binding._socket = r.context
Keep-Alive and Cleanup
After registration, a Timer is scheduled with a TimerTask that runs every 10 seconds:
timer.schedule(task, 10000, 10000)
On each tick:
- If the WebSocket session is still open, a
pingmessage is sent. The client responds with apong. - If the session is closed (the user navigated away or the connection dropped), the binding satellite is removed from
bindingSatellitesand the timer is cancelled. This prevents memory leaks from accumulated stale bindings.
Unbinding
When client-side elements are removed from the DOM (e.g., navigating away from a section using HUD-Core's SPA-like behavior), HUD-Core sends unbind requests to /!/lp/bind/u/ with a list of binding UUIDs. The unbind() handler removes these specific closures from the binding satellite, freeing memory incrementally rather than waiting for the WebSocket to close.
WebSocket Message Types
The injected client-side script handles three types of incoming messages:
| Action | Description |
|---|---|
ping |
Server keep-alive check. Client responds with pong. |
applyReaction |
Reactive update. Contains uuid and payload. The client finds the START_CHUNK@uuid / END_CHUNK@uuid comment markers in the DOM, creates a Range between them, deletes the old content, and inserts the new HTML from payload. |
elementResponse |
Server Element callback response. Contains function, payload, and optional requestId. If requestId is present, the client invokes the stored callback function from window.spaceportCallbacks. |
Thread Safety and Concurrency
-
launch()synchronization —launch()issynchronizedon the Launchpad instance. Sinceassemble()creates a new instance per call, concurrent requests to the same route handler do not block each other. The synchronization exists to prevent accidental concurrent use of the same assembled instance. -
bindingSatellitesis aConcurrentHashMap, providing safe concurrent read/write from HTTP request threads and WebSocket handler threads. -
SpaceportTemplateEngine.templateCacheis aConcurrentHashMap. If two requests compile the same template simultaneously, both complete and the last write wins. Since both produce identical output from the same source text, this race is harmless. -
launchpad.elements(per-instance) andLaunchpad.sharedPool/Launchpad.poolOwners/Launchpad.byName(static) are allConcurrentHashMaps, safe for concurrent Server Element registration and lookup. -
binding(the per-Launchpad script-binding map) isCollections.synchronizedMap([:]), providing coarse-grained thread-safety for the websocket-event paths that read and write it during reactive updates. -
Per-request isolation — Each request gets its own
Bindingobject (created in theLaunchpadconstructor) with its own_reactionsand_bindingsmaps (bothConcurrentHashMap). There is no shared mutable state between concurrent requests during the render phase. -
Server action dispatch — When server actions execute via
serveBinding(), they access the shared binding satellite. The reaction iteration and WebSocket sending are not explicitly synchronized beyond the ConcurrentHashMap guarantees. In practice, HUD-Core serializes actions from a single browser tab, so concurrent actions on the same launch ID are rare.
Hot-Reload Behavior
Hot-reload is enabled only when Spaceport.inDebugMode is true. It is initialized during the on initialize Alert lifecycle event.
How It Works
- The
_init()method locates the launchpad directory (checking{root}/{name}/launchpadfirst, then{root}/launchpad). - A
Beaconis created for that directory.Beaconuses Java'sWatchServiceAPI and recursively registers watches on all subdirectories viaFiles.walkFileTree(). - A background thread continuously calls
watcher.take()to block until a file event occurs. - When any file event fires (ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE), the Beacon's callback is invoked.
- The callback uses a debounce pattern:
if (!reloading) {
reloading = true
Thread.start {
Thread.sleep(100)
SpaceportTemplateEngine.templateCache.clear()
clearAllElements() // per-instance maps + shared pool + pool owners
reloading = false
}
}
clearAllElements() iterates Launchpad.byName.values() and clears each Launchpad's elements map, then clears sharedPool and poolOwners. The 100ms sleep allows a batch of file changes (e.g., an IDE saving multiple files) to trigger only one clear rather than one per file.
What Gets Reloaded
| Changed File | Effect |
|---|---|
.ghtml templates |
Recompiled on next access (template cache was cleared) |
.groovy Server Elements |
Re-parsed and re-registered on next Launchpad construction. Every Launchpad's elements map is empty, so each one re-scans its own elements/ directory on its next instantiation. |
| Other files in the launchpad directory | Cache is still cleared (the Beacon watches all file types) |
In addition to the file-watch path, the template cache is cleared each time a Launchpad finishes installing its Elements, regardless of debug mode. This guarantees that adding a new Element file doesn't leave templates compiled against an older view of the registry.
Limitations
- Active connections are not updated. Clients with open WebSocket connections retain their old binding satellites and closures. Only new page loads pick up template changes.
- Source modules are separate. Changes to
.groovyfiles in themodules/directory are handled by Spaceport's separate source-reloading mechanism, not by Launchpad's Beacon. - Production mode has no watching. When debug mode is off, templates are compiled once and cached for the lifetime of the JVM. Template changes require a server restart.
- Beacon lifecycle. Beacons are deactivated during server de-initialization via the
on deinitializedAlert, which cancels all watch keys and closes the WatchService.
Server Element Processing Internals
Server Elements are custom <g:tag-name> components defined as Groovy classes in the elements/ directory. Their lifecycle is deeply integrated with the Launchpad pipeline.
Registration (per-Launchpad)
Each Launchpad has its own elements map (instance field), and contributes to the static cross-Launchpad sharedPool according to ownership rules. During Launchpad construction, unless the elements map has been inherited from a same-source-path predecessor (the assemble() case), all .groovy files in this.sourcePath + 'elements/' are loaded through a single shared GroovyClassLoader so sibling Element classes can reference each other by class name:
def elementLoader = new GroovyClassLoader(SourceStore.classLoader)
for (def element in new File(this.sourcePath + 'elements').listFiles()) {
if (element.name.endsWith('.groovy')) {
def text = element.text
// Apply ${{ }} syntactic sugar
text = text.replaceAll(/\$\{\{\s*/, '\\${ _{ _script, _registration -> ')
def clazz = elementLoader.parseClass(text)
def tagName = element.name.replace('.groovy', '').kebab()
this.elements.put(tagName, clazz) // per-instance local
contributeToSharedPool(tagName, clazz) // ownership-aware
}
}
SpaceportTemplateEngine.templateCache.clear()
The filename is converted to kebab-case for the tag name (e.g., StatCard.groovy becomes stat-card, TagInput.groovy becomes tag-input). Path normalization uses the trailing-slash-aware this.sourcePath, so constructor paths work with or without a trailing slash.
Multi-Launchpad resolution
Resolution from a rendering Launchpad walks: local map → shared pool → unknown. The shared pool is global-prefers-global, with slice-vs-slice collisions evicting the name entirely. A qualified <g:slice/element> tag bypasses both layers and reaches into a named Launchpad's local map directly. See the Launchpad API reference for the full rules and worked examples.
Tag Processing in SpaceportTemplateEngine
When a <g:tag-name> is encountered during preprocessing and the tag name matches a registered element (but not a built-in generator), the engine generates placeholder markers with a unique ID:
<!-- id-styles --><id originalAttributes><!-- id-body -->body content<!-- END id-body --></id><!-- id-handler -->
The element's ID and name are stored in the template's elements map for later resolution.
Element Instantiation in prime()
After a template is rendered, if it contained Server Element tags, prime() scans the rendered HTML for handler markers (<!-- id-handler -->) and creates element instances. Element class resolution honors the qualified <g:slice/element> syntax (when the engine saw one, it stashed a launchpadName field on the entry):
Class elementClass = resolveForEntry(e) // local → shared pool, or qualified slice's local map
if (elementClass == null) continue
Element newElement = elementClass.newInstance()
newElement.launchpad = this
newElement.client = binding.client
newElement.dock = binding.dock
newElement._id = 6.randomID()
Elements are added to binding._elements. Every template.elements entry — not just those missing from the primed output — is also copied into binding._stashed_elements as a recipe. parseElements() uses these recipes via the reconcileMarkerCount pass to spin up additional instances when handler markers in the payload outnumber the existing entries (the reactive-update-grew case as well as the conditional-finally-rendered case).
Element Rendering in parseElements()
The parseElements() method runs after all templates are primed and the vessel is assembled. Before the main loop, it calls reconcileMarkerCount(html) to spin up any Element instances needed to cover handler markers currently in the HTML — this handles both the "stashed-on-first-appearance" case (an Element inside a conditional that finally rendered) and the "reactive update grew the list" case (a ${{ items.combine { '<g:row>...' } }} whose items grew past its initial size). See the Server Elements internals for the full reconcile mechanism.
The main loop processes each registered element:
- Attribute extraction — Parses HTML attributes from the element's rendered tag using regex matching. The tag-end scan respects quoted attribute values, so a
>insidetitle="<i>x</i>"doesn't truncate the opening tag. - Server-side attribute resolution — Attributes whose values are server action URLs (starting with
/!/) but are noton-*event attributes are resolved immediately by calling the associated binding closure. The result replaces the URL. - Typed accessor injection — The attributes map is enhanced with
getNumber,getBool,getString,getList, andgetIntegermethods (same as the transmission object). Bare boolean attributes (e.g.<g:btn disabled>) capture as"true". - Prerender —
element.prerender(body, attributes)is called. The element transforms its body content based on attributes. - Nested element scan — The string returned by
prerender()is scanned byprocessNestedElements()for<g:>tags. Each tag found gets a fresh Element instance registered intobinding._elementsand is inlined as the same placeholder markup the template engine would have produced at compile time, so the outer loop picks them up on subsequent passes. - Initialize —
element.initialize()is called. The element builds its CSS, JavaScript event handlers, and any other runtime setup. - Style injection — The
<!-- id-styles -->comment is replaced with the element's CSS. Global_style(from@CSS) is emitted once per element type per response; per-instance_scopedStyle(from@ScopedCSS) is emitted for every instance. - Handler injection — The
<!-- id-handler -->comment is replaced with the element's_handler(JavaScript). - Tag replacement — The placeholder tag
<id>is replaced with the actual element tag<tag-name element-id="rid">. - Prepend/append — Global prepend content is inserted before
</head>(or at the start of the HTML). Global append content is inserted after</body>. Scoped prepend/append content is inserted adjacent to individual element instances.
The processing loop is capped at 16 passes to defend against an Element whose prerender() accidentally emits its own <g:> tag (a true cycle would otherwise loop forever). When the cap is hit, the loop terminates safely — the offending Element doesn't render, but the server doesn't hang.
Element WebSocket Communication
Server Elements can expose methods that are callable from client-side JavaScript via WebSocket. When the client sends an element-call message:
{
"handler-id": "launchpad-socket",
"action": "element-call",
"function": "methodName",
"element": "elementId",
"uuid": "launchId",
"args": [...],
"request-id": "callbackId"
}
The server looks up the element in the binding satellite, finds the method in the element's _handlerMap, calls it with the provided arguments, and sends the return value back to the client. If a request-id was included, it is echoed back so the client can match the response to a pending callback.
Error Handling
Template errors are handled at multiple levels, with increasing specificity:
Preprocessing / Compilation Errors
If SpaceportTemplateEngine.createTemplate() throws during preprocessing or script compilation, prime() catches the exception and returns an HTML error fragment:
<fatal class="error">Error creating template: filename<br><tt>error message</tt></fatal>
The GroovyRuntimeException thrown by the engine includes a descriptive message about the nature of the failure.
Runtime Errors
If the compiled template script throws during execution, the SpaceportTemplate.make() writable catches the exception and:
- Extracts the line number from the stack trace by matching entries with
.ghtml.groovyfilenames - Reads the compiled source from the
_sourceStringBuilder - Writes an HTML diagnostic block to the
_errorStringBuilder:
<strong>Oops!</strong> script_name has encountered errors. <br>
Found problem in line: [ 42 ] <br>
Error message<br>
<tt>
37 line of code...
38 line of code...
--> 42 the problematic line
43 line of code...
</tt>
<details><summary>Full stacktrace:</summary>
...full stack trace...
</details>
The prime() method then wraps this in a <fatal class="error"> element.
Binding / Security Errors
| Condition | Response |
|---|---|
| Binding UUID not found | HTTP 404 |
| Client mismatch (different user trying to invoke another user's server action) | HTTP 405 |
Unknown <g:*> tag |
HTML comment: <!-- SpaceportTemplateEngine: Unknown tag: name --> |
Missing <payload/> in vessel |
HTTP 501 status, debug log message |
| Folder traversal detected in template path | HTML comment: <!-- LaunchPad: Folder traversal detected --> |
| Template file not found | HTML comment: <!-- LaunchPad: Part not found: filename --> |
Reactive State Memory
Reactions retain _currentHash, a 64-bit FNV-1a digest of the last rendered string, rather than the full previous HTML in _current. Changed payloads are still sent as HTML; the digest reduces retained memory for large reactive blocks. Per-binding satellite cleanup removes abandoned socket state, and element processing can create additional instances when a reactive update grows its placeholder count.
Make the docs better.Edit this page on GitHub