Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AGenUI AGenUI Observer Pattern Surface Events

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, UI_Framework
Last Updated 2026-10-24 17:21 GMT

Overview

An observer pattern where platform renderers implement a listener interface to receive surface lifecycle events, component diffs, action notifications, and errors from the core engine, with thread-safe listener registration and event fan-out.

Description

The Observer pattern defines a one-to-many dependency between a subject and its observers, so that when the subject changes state, all observers are notified automatically. In a generative UI SDK, the C++ core engine is the subject — it produces component diffs, surface lifecycle events, and action notifications. The platform rendering layer (iOS, Android, HarmonyOS) is the observer — it consumes these events to update native UI.

The interface `IAGenUIMessageListener` defines callback methods: `onCreateSurface`, `onDeleteSurface`, `onComponentsUpdate`, `onComponentsAdd`, `onComponentsRemove`, `onActionEventRouted`, `onError`. The platform implements these and registers its listener via `addSurfaceEventListener()`.

The `EventDispatcher` manages the listener list under a mutex and fans out events. A cached-listener mechanism handles the race where a listener is added before the dispatcher is fully initialized: the listener is stored in a cache and flushed to the dispatcher when `init()` completes, all under the same lock.

Usage

Use this principle when:

  • A C++ core needs to notify a platform rendering layer about UI changes
  • Multiple listeners may observe the same surface (e.g., renderer + analytics)
  • Listeners can be added/removed at any time, even during initialization
  • Events must be dispatched safely from a worker thread to listener callbacks

Theoretical Basis

The observer registration and fan-out structure:

Subject: EventDispatcher
  └── listeners: [IAGenUIMessageListener*]

Event flow:
  Core produces event → EventDispatcher.dispatch*(msg)
    → for each listener: listener.on*(msg)
    → Platform renderer updates native UI

The cached-listener race protection:

void addSurfaceEventListener(IAGenUIMessageListener* listener) {
    std::lock_guard<std::mutex> lock(_cachedListenersMutex);
    if (_dispatcher) {
        _dispatcher->addEventListener(listener);    // normal path
    } else {
        _cachedListeners.push_back(listener);       // pre-init: cache
    }
}
// On init(): flush cache to dispatcher under same lock

Practical Guide

  • Implement `IAGenUIMessageListener` on the platform side with the callbacks you need.
  • Register via `surfaceManager->addSurfaceEventListener(&myListener)`.
  • Override `onComponentsUpdate` for incremental rendering of component diffs.
  • Override `onError` to surface SDK errors to the platform crash reporter.
  • Listener pointers are non-owning; keep them alive until removed or surface destroyed.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment