A production-ready mediator pattern that decouples your Lua code through elegant event handling. Whether you're building game mods, server scripts, or standalone applications, this mediator provides a clean way to manage complex interactions without tight coupling.
- ๐ฏ Named Parameters: Crystal-clear, self-documenting API
- ๐ Multiple Returns: Callbacks can return multiple values with smart merging
- ๐ก๏ธ Default Values: Graceful fallbacks when callbacks return nil
- โก Zero Dependencies: Only requires classic.lua for OOP
- ๐ฎ Game-Ready: Perfect for Eluna, OpenResty and more
- ๐ฆ Lightweight: ~200 lines
- ๐ง Extensible: Easy to adapt to any Lua environment
require "Mediator" |
-- Register a callback
RegisterMediatorEvent("Player_Login", function(player)
if player.level < 10 then
return false, "Level too low"
end
return true, "Welcome!"
end)
-- Trigger the event
local success, msg = Mediator.On("Player_Login", {
arguments = {player},
defaults = {false, "Unknown error"}
}) |
Mediator.On("EventName", {
arguments = {arg1, arg2}, -- Data to pass to callbacks
defaults = {false, 0, ""} -- Fallback values for nil returns
})-- Callback 1: Check level
RegisterMediatorEvent("Validate", function(player)
return player.level >= 10 and true or nil
end)
-- Callback 2: Check gear
RegisterMediatorEvent("Validate", function(player)
return player.gearScore >= 1000 and true or nil
end)
-- First non-nil return wins
local isValid = Mediator.On("Validate", {
arguments = {player},
defaults = {false}
})-- Callback A returns: 100, nil, nil
-- Callback B returns: nil, 50, nil
-- Callback C returns: nil, nil, 25
-- Defaults: 0, 0, 0
--
-- Result: 100, 50, 25| Platform | Status | Notes |
|---|---|---|
| ๐ฏ Eluna / ALE (WoW) | โ Tested | AzerothCore, TrinityCore compatible |
| ๐ Pure Lua | โ Compatible | 5.1, 5.2, 5.3, 5.4, LuaJIT |
| ๐ง OpenResty | โ Compatible | Server-side applications |
-- Base damage
RegisterMediatorEvent("Calculate_Damage", function(attacker, target, base)
return base -- Pass through
end)
-- Critical hit modifier
RegisterMediatorEvent("Calculate_Damage", function(attacker, target, base)
if math.random() < 0.1 then -- 10% crit chance
return base * 2 -- Double damage
end
end)
-- Armor reduction
RegisterMediatorEvent("Calculate_Damage", function(attacker, target, base)
local reduction = target.armor * 0.01
return base * (1 - reduction)
end)
-- Usage in combat
local finalDamage = Mediator.On("Calculate_Damage", {
arguments = {player, enemy, 100},
defaults = {100}
})-- Step 1: Check credentials
RegisterMediatorEvent("Auth", function(username, password)
if not validateCredentials(username, password) then
return false, "Invalid credentials"
end
return nil -- Pass to next check
end)
-- Step 2: Check account status
RegisterMediatorEvent("Auth", function(username, password)
if isAccountLocked(username) then
return false, "Account locked"
end
return nil
end)
-- Step 3: Grant access
RegisterMediatorEvent("Auth", function(username, password)
return true, "Access granted"
end)
-- Authenticate user
local success, message = Mediator.On("Auth", {
arguments = {username, password},
defaults = {false, "Authentication failed"}
})-- Dark theme
RegisterMediatorEvent("Get_Colors", function(element)
if element == "background" then
return "#1a1a1a"
end
end)
-- Accent colors
RegisterMediatorEvent("Get_Colors", function(element)
if element == "primary" then
return "#007acc"
end
end)
-- Get color with fallback
local color = Mediator.On("Get_Colors", {
arguments = {"background"},
defaults = {"#ffffff"}
})|
Components don't know about each other, only about events. |
Add new behaviors without modifying existing code. |
Test components in isolation without dependencies. |
Triggers an event and collects return values.
Parameters:
eventName(string): Event identifierparams.arguments(table): Arguments for callbacksparams.defaults(table): Default return values
Returns: Multiple values merged from callbacks
Registers a callback for an event.
Parameters:
eventName(string): Event identifiercallback(function): Function to execute
Clears callbacks for an event.
Parameters:
eventName(string|nil): Event to clear, or nil for all
Gets callback count for debugging.
Parameters:
eventName(string|nil): Event name, or nil for total
Returns: (number) Callback count
This mediator implements several design patterns:
- Mediator Pattern: Centralized event handling
- Observer Pattern: Subscribe/notify mechanism
- Chain of Responsibility: Sequential callback processing
- Strategy Pattern: Pluggable behavior modification
- Callback Lookup: O(1) hash table access
- Execution: O(n) where n = number of callbacks
- Memory: ~2KB base + ~100 bytes per callback
- Zero Allocations: During event triggering (after warmup)
1M event triggers with 3 callbacks: ~150ms
10K callbacks registered: ~50ms
Average overhead per event: ~0.15ยตs
We welcome contributions! Here's how you can help:
- ๐ Report Bugs: Open an issue with details
- ๐ก Feature Ideas: Share your suggestions
- ๐ Documentation: Improve examples or guides
- ๐ง Code: Submit pull requests
If this project helps you, consider supporting its development:
- ๐จ OOP Foundation: classic.lua by rxi
- ๐ง Primary Development: iThorgrim
- ๐ Contributors: Everyone who provided feedback and improvements