VOOZH about

URL: https://deepwiki.com/hypervel/prompts/9.5-event-system

⇱ Event System | hypervel/prompts | DeepWiki


Loading...
Last indexed: 7 February 2026 (2e2181)
Menu

Event System

Purpose and Scope

The Events trait (src/Concerns/Events.php9-43) provides a lightweight observer pattern implementation that allows prompts to emit events and register callbacks during their lifecycle. This enables decoupled event handling where application code can react to prompt lifecycle events (such as key presses, state changes, or value updates) without modifying prompt internals.

The event system is used throughout the prompt base class to expose interaction points for custom behavior. For related documentation, see:

Sources: src/Concerns/Events.php9-43 src/Prompt.php20


Trait Architecture

Overview

The Events trait maintains an internal registry of event listeners organized by event name. When an event is emitted, all registered callbacks for that event are invoked in registration order with the provided data.

Title: Event System Flow


Sources: src/Concerns/Events.php9-43 src/Prompt.php20-333

Internal Data Structure

The trait stores event listeners in a protected property:


The structure maps event names to arrays of closures: ['eventName' => [Closure, Closure, ...], ...]

This allows multiple listeners to be registered for the same event, executed in the order they were registered.

Sources: src/Concerns/Events.php16


Public API Methods

on() - Register Event Listener


Registers a callback to be invoked when the specified event is emitted. Multiple callbacks can be registered for the same event.

ParameterTypeDescription
$eventstringThe event name to listen for
$callbackClosureThe callback to invoke when event is emitted

Implementation: Appends the callback to the listeners array for the specified event name (src/Concerns/Events.php21-24).

Sources: src/Concerns/Events.php21-24

emit() - Trigger Event


Triggers an event, invoking all registered callbacks for that event in registration order. Each callback receives the provided data arguments.

ParameterTypeDescription
$eventstringThe event name to emit
...$datamixedVariable arguments passed to each callback

Implementation: Iterates through listeners[$event] (if it exists) and invokes each callback with the provided data using spread syntax (src/Concerns/Events.php29-33).

Sources: src/Concerns/Events.php29-33

clearListeners() - Remove All Listeners


Removes all registered event listeners by resetting the listeners array to empty. This is called automatically in the prompt lifecycle cleanup.

Implementation: Sets $this->listeners = [] (src/Concerns/Events.php39-42).

Sources: src/Concerns/Events.php39-42


Event Lifecycle and Usage in Prompt

Event Emission in Prompt::prompt()

The Prompt base class uses the Events trait and emits the 'key' event during the input processing loop. This allows application code to react to every key press.

Title: Key Event Emission Flow


Sources: src/Prompt.php104-366

Key Event Implementation

The 'key' event is emitted in the handleKeyPress() method (src/Prompt.php333):


This occurs for every key press during the input loop, before prompt-specific key handling logic executes.

Sources: src/Prompt.php327-366

Automatic Listener Cleanup

Event listeners are automatically cleared in the finally block of Prompt::prompt() (src/Prompt.php159):


This ensures listeners don't persist beyond a single prompt invocation, preventing memory leaks and unintended behavior in subsequent prompts.

Sources: src/Prompt.php104-161


Practical Usage Examples

Example 1: Logging Key Presses


The callback receives the raw key string from Terminal::read(), including special keys like Key::UP, Key::ENTER, Key::CTRL_C, etc.

Example 2: Custom Validation Triggers


Example 3: Debugging State Changes


This allows inspection of prompt state transitions during interaction.

Sources: src/Prompt.php20-333


Event Name Conventions

While the Events trait is event-name agnostic, the codebase currently uses:

Event NameEmitted ByData PassedPurpose
'key'Prompt::handleKeyPress()string $keyRaw key press from terminal

Individual prompt implementations may extend this by emitting custom events for:

  • State changes ('stateChanged', 'validated', 'error')
  • Value updates ('valueChanged', 'selectionChanged')
  • Lifecycle events ('rendered', 'submitted')

The event system is designed to support any event name without modification to the trait.

Sources: src/Concerns/Events.php9-43 src/Prompt.php333


Implementation Details

Listener Storage and Execution

The emit() method uses a null coalescing operator to handle events with no registered listeners:


If no listeners are registered for the event, the null coalescing operator (??) returns an empty array, and the foreach loop executes zero times. This makes emitting events with no listeners a no-op with no performance overhead.

Sources: src/Concerns/Events.php29-33

Multiple Listeners Per Event

The implementation allows unlimited listeners per event. Each on() call appends to the array:


Listeners execute in registration order, and each receives the same data arguments passed to emit().

Sources: src/Concerns/Events.php21-24

Memory Management

Event listeners hold references to closures, which may capture variables from their defining scope. The clearListeners() method releases these references:


This is essential for preventing memory leaks, especially when prompts capture large objects or when running multiple prompts in succession.

Sources: src/Concerns/Events.php39-42


Code Entity Reference

This diagram maps conceptual event system components to their concrete code implementations:

Title: Event System Code Entity Mapping


Sources: src/Concerns/Events.php9-43 src/Prompt.php20-333


Usage Patterns

Registering Event Listeners

When a prompt component uses the Events trait, application code can register listeners:


The event names and data signatures depend on what events the specific prompt implementation emits.

Configuring Non-Interactive Mode

Before creating prompts in a non-interactive environment:


Handling Non-Interactive Validation Failures


Sources: src/Concerns/Events.php21-33 src/Concerns/Interactivity.php19-38 src/Exceptions/NonInteractiveValidationException.php9-11


Trait Composition

Both traits are designed to be mixed into prompt components. A typical prompt class structure:


Sources: src/Concerns/Events.php9-43 src/Concerns/Interactivity.php9-39

Individual prompt implementations decide which events to emit and how to handle non-interactive mode based on their specific requirements.