Principle:AGenUI AGenUI Multi Instance Surface Management
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A multi-instance management pattern where a central engine creates and tracks independent surface manager instances, each owning its own virtual DOM, data model, and event callbacks, while sharing a single worker thread for internal processing.
Description
In generative UI frameworks, a single application may need multiple independent rendering surfaces — for example, a chat window and a side panel rendering different LLM streams concurrently. The Multiton pattern provides keyed access to multiple instances, each with isolated state.
This principle separates the engine (singleton, owns global config and the shared worker thread) from surface managers (multi-instance, each with its own VDOM, data binding, and protocol parsing). Each `SurfaceManager` receives a unique sequential `instanceId` via an atomic counter. The engine tracks all instances in a map guarded by a mutex.
A critical design decision is thread sharing: all surface managers post their internal work to one shared worker thread. This avoids thread proliferation on mobile devices while keeping each surface's data isolated. Public APIs are called on the main thread; the work is captured in a `shared_ptr` lambda and posted to the worker thread, ensuring the surface stays alive during async processing.
Usage
Use this principle when:
- An application needs multiple independent UI rendering contexts (chat, card, panel)
- Each context must have isolated VDOM, data model, and event callbacks
- A shared worker thread is preferred over per-instance threads for resource efficiency
- Surfaces must be created and destroyed dynamically at runtime
Theoretical Basis
The instance isolation invariant:
Engine (singleton)
├── SurfaceManager #1 (instanceId=0) → VDOM₁, DataModel₁, Callbacks₁
├── SurfaceManager #2 (instanceId=1) → VDOM₂, DataModel₂, Callbacks₂
└── shared worker thread (processes tasks from all instances)
Thread-safe creation via atomic ID assignment and shared_ptr lifetime:
ISurfaceManager* createSurfaceManager() {
int instanceId = _nextInstanceId.fetch_add(1); // lock-free ID
auto sm = std::make_shared<SurfaceManager>(instanceId);
{
std::lock_guard<std::mutex> lock(_mutex);
_surfaceManagers[instanceId] = sm; // tracked
}
messageThread->post([sm]() { sm->init(); }); // init on worker thread
return sm.get();
}
Practical Guide
- Call `createSurfaceManager()` for each independent UI surface.
- Each returned `ISurfaceManager*` is independent — different LLM streams can feed different instances.
- Destroy surfaces via `destroySurfaceManager()` when no longer needed.
- All surfaces share one worker thread; heavy processing on one surface may delay another.