waves-sineThe Event Continuum

In Happen, we've distilled event processing to its purest essence: a continuous flow of functions that process events and determine what happens next.

The Pure Functional Flow Model

At its core, the Event Continuum views event handling as a single entry point that can branch into a chain of functions through return values:

// Register a single entry point
orderNode.on("create-order", validateOrder);

// The flow is controlled by function returns
function validateOrder(event, context) {
  if (!isValidOrder(event.payload)) {
    return { success: false, reason: "Invalid order" };
  }
  
  // Return the next function to execute
  return processOrder;
}

function processOrder(event, context) {
  // Process the order
  const orderId = createOrderInDatabase(event.payload);
  
  // Store in context
  context.orderId = orderId;
  
  // Return the next function
  return notifyCustomer;
}

function notifyCustomer(event, context) {
  // Send notification using context data
  sendOrderConfirmation(context.orderId, event.payload.customer);
  
  // Return final result
  return { success: true, orderId: context.orderId };
}

This creates an elegant flow system with just one fundamental mechanism: functions that return either the next function or a final value.

How the Continuum Works

When an event arrives at a node, the system:

  1. Calls the registered handler function

  2. Examines the return value:

    • If it's a function: Execute that function next

    • If it's any other value: Complete the flow with that value as the result

This simple mechanism creates a remarkably powerful and flexible flow system.

Flow Control Through Return Values

The Event Continuum achieves its power through a pure functional approach:

Continuing to the Next Step

When a handler returns another function, the flow continues with that function:

Completing the Flow with a Result

When a handler returns any non-function value, the flow completes with that value as the result:

Dynamic Flow Selection

Functions can return different next steps based on any condition:

Shared Context

A shared context object flows through the function chain, allowing communication between steps:

This provides a clean way for functions to build up state as the flow progresses.

Complex Flow Patterns

The pure functional model supports sophisticated flow patterns:

Conditional Branching

Loop Patterns

You can create loops by returning a function that's already been executed:

Error Handling Patterns

Error handling becomes part of the natural flow:

Building Complete Workflows

The Event Continuum naturally supports building complex workflows:

The Power of Pure Functional Flows

This pure functional approach offers several benefits:

  1. Ultimate Simplicity: One consistent pattern for all event handling

  2. Maximum Flexibility: Functions can compose and branch in unlimited ways

  3. Transparent Logic: Flow control is explicit in function returns

  4. Perfect Testability: Each function can be tested independently

  5. Minimal API Surface: Just .on() with a single handler

This approach embodies Happen's philosophy in its purest form - a single registration method and function returns create a system of unlimited expressiveness.

Examples of the Event Continuum in Action

Simple Event Handling

Validation Pattern

Request-Response with Direct Return

Ending Flow with Bare Return

A bare return statement will end the flow without returning any value:

This is equivalent to returning undefined but is more explicit about the intention to end the flow without a value.

Building Reusable Flow Patterns

The functional nature of the Event Continuum encourages building reusable patterns:

Parallel Processing

For more advanced scenarios, you can implement parallel processing:

By treating event handling as a pure functional flow where each function determines what happens next, Happen enables a system of unlimited expressiveness that can handle everything from simple events to complex workflows with the same consistent pattern.

Ready to explore more? Continue to the Communication Patterns section to see how the Event Continuum integrates with other aspects of the Happen framework.

Last updated