EventEmitter - Event-Driven Architecture
BanglaCode supports event-driven programming through the EventEmitter API. This allows you to build reactive applications where components communicate through events, perfect for building message buses, pub/sub systems, and event-driven architectures.
Key Concepts
ghotona_srishti()(ঘটনা সৃষ্টি) - Create an EventEmitter instanceghotona_shuno()(ঘটনা শুনো) - Listen to eventsghotona_ekbar()(ঘটনা একবার) - Listen to event onceghotona_prokash()(ঘটনা প্রকাশ) - Emit/trigger eventsghotona_bondho()(ঘটনা বন্ধ) - Remove listener
Creating an EventEmitter
Create a new EventEmitter instance using ghotona_srishti():
| 1 | // Create an event emitter |
| 2 | dhoro emitter = ghotona_srishti(); |
| 3 | dekho(emitter); // EventEmitter(events=0, listeners=0) |
Adding Event Listeners
ghotona_shuno() - On
Use ghotona_shuno() to add an event listener. The listener will be called every time the event is emitted:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | // Add a listener |
| 4 | ghotona_shuno(emitter, "message", kaj(data) { |
| 5 | dekho("Received:", data); |
| 6 | }); |
| 7 | |
| 8 | // Emit the event multiple times |
| 9 | ghotona_prokash(emitter, "message", "Hello"); // Received: Hello |
| 10 | ghotona_prokash(emitter, "message", "World"); // Received: World |
ghotona_ekbar() - Once
Use ghotona_ekbar() to add a listener that only runs once:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | // Add a one-time listener |
| 4 | ghotona_ekbar(emitter, "startup", kaj() { |
| 5 | dekho("Application started!"); |
| 6 | }); |
| 7 | |
| 8 | // Emit multiple times - listener only runs once |
| 9 | ghotona_prokash(emitter, "startup"); // Application started! |
| 10 | ghotona_prokash(emitter, "startup"); // (no output - listener already removed) |
Emitting Events
ghotona_prokash() - Emit
Use ghotona_prokash() to emit an event with optional data:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | ghotona_shuno(emitter, "user:login", kaj(username, time) { |
| 4 | dekho(username + " logged in at " + time); |
| 5 | }); |
| 6 | |
| 7 | // Emit with multiple data arguments |
| 8 | ghotona_prokash(emitter, "user:login", "Ankan", "09:30 AM"); |
| 9 | // Output: Ankan logged in at 09:30 AM |
Multiple Listeners
You can add multiple listeners to the same event. All listeners will be called in the order they were added:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | // First listener |
| 4 | ghotona_shuno(emitter, "data", kaj(value) { |
| 5 | dekho("Listener 1:", value); |
| 6 | }); |
| 7 | |
| 8 | // Second listener |
| 9 | ghotona_shuno(emitter, "data", kaj(value) { |
| 10 | dekho("Listener 2:", value * 2); |
| 11 | }); |
| 12 | |
| 13 | ghotona_prokash(emitter, "data", 10); |
| 14 | // Output: |
| 15 | // Listener 1: 10 |
| 16 | // Listener 2: 20 |
Removing Listeners
ghotona_bondho() - Off
Remove a specific listener using ghotona_bondho():
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | // Store listener reference |
| 4 | dhoro listener = kaj(msg) { |
| 5 | dekho("Received:", msg); |
| 6 | }; |
| 7 | |
| 8 | ghotona_shuno(emitter, "update", listener); |
| 9 | ghotona_prokash(emitter, "update", "First"); // Received: First |
| 10 | |
| 11 | // Remove the listener |
| 12 | ghotona_bondho(emitter, "update", listener); |
| 13 | ghotona_prokash(emitter, "update", "Second"); // (no output) |
ghotona_sob_bondho() - Remove All
Remove all listeners for a specific event or all events:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | ghotona_shuno(emitter, "event1", kaj() { dekho("Event 1"); }); |
| 4 | ghotona_shuno(emitter, "event2", kaj() { dekho("Event 2"); }); |
| 5 | |
| 6 | // Remove all listeners for "event1" |
| 7 | ghotona_sob_bondho(emitter, "event1"); |
| 8 | |
| 9 | // Remove all listeners for all events |
| 10 | ghotona_sob_bondho(emitter); |
Inspecting EventEmitter
ghotona_shrotara() - Get Listeners
Get all listeners for a specific event:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | ghotona_shuno(emitter, "test", kaj() { }); |
| 4 | ghotona_shuno(emitter, "test", kaj() { }); |
| 5 | |
| 6 | dhoro listeners = ghotona_shrotara(emitter, "test"); |
| 7 | dekho("Number of listeners:", dorghyo(listeners)); // 2 |
ghotona_naam_sob() - Get Event Names
Get all event names that have listeners:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | ghotona_shuno(emitter, "event1", kaj() { }); |
| 4 | ghotona_shuno(emitter, "event2", kaj() { }); |
| 5 | ghotona_shuno(emitter, "event3", kaj() { }); |
| 6 | |
| 7 | dhoro events = ghotona_naam_sob(emitter); |
| 8 | dekho("Events:", events); // ["event1", "event2", "event3"] |
Method Chaining
EventEmitter methods return the emitter, allowing method chaining:
| 1 | dhoro emitter = ghotona_srishti(); |
| 2 | |
| 3 | ghotona_shuno(emitter, "start", kaj() { dekho("Starting..."); }) |
| 4 | ghotona_shuno(emitter, "process", kaj() { dekho("Processing..."); }) |
| 5 | ghotona_shuno(emitter, "end", kaj() { dekho("Done!"); }); |
| 6 | |
| 7 | // Chain emit calls |
| 8 | ghotona_prokash(emitter, "start"); |
| 9 | ghotona_prokash(emitter, "process"); |
| 10 | ghotona_prokash(emitter, "end"); |
Real-World Examples
Example 1: Message Bus
| 1 | // Create a message bus for pub/sub |
| 2 | dhoro messageBus = ghotona_srishti(); |
| 3 | |
| 4 | // Subscribe to messages |
| 5 | ghotona_shuno(messageBus, "notification", kaj(msg) { |
| 6 | dekho("[NOTIFICATION]", msg); |
| 7 | }); |
| 8 | |
| 9 | ghotona_shuno(messageBus, "error", kaj(err) { |
| 10 | dekho("[ERROR]", err); |
| 11 | }); |
| 12 | |
| 13 | // Publish messages |
| 14 | ghotona_prokash(messageBus, "notification", "User logged in"); |
| 15 | ghotona_prokash(messageBus, "error", "Database connection failed"); |
Example 2: Custom Event Handling
| 1 | // Simple task queue with events |
| 2 | dhoro taskQueue = ghotona_srishti(); |
| 3 | dhoro tasks = []; |
| 4 | |
| 5 | // Listen for task:add event |
| 6 | ghotona_shuno(taskQueue, "task:add", kaj(task) { |
| 7 | dhokao(tasks, task); |
| 8 | dekho("Task added:", task["name"]); |
| 9 | ghotona_prokash(taskQueue, "task:count", dorghyo(tasks)); |
| 10 | }); |
| 11 | |
| 12 | // Listen for task count updates |
| 13 | ghotona_shuno(taskQueue, "task:count", kaj(count) { |
| 14 | dekho("Total tasks:", count); |
| 15 | }); |
| 16 | |
| 17 | // Add tasks |
| 18 | ghotona_prokash(taskQueue, "task:add", {"name": "Send email"}); |
| 19 | ghotona_prokash(taskQueue, "task:add", {"name": "Process payment"}); |
| 20 | ghotona_prokash(taskQueue, "task:add", {"name": "Update database"}); |
Example 3: State Management
| 1 | // Simple state manager with events |
| 2 | dhoro stateManager = ghotona_srishti(); |
| 3 | dhoro state = {"count": 0}; |
| 4 | |
| 5 | // Listen for state changes |
| 6 | ghotona_shuno(stateManager, "state:change", kaj(newState) { |
| 7 | state = newState; |
| 8 | dekho("State updated:", state); |
| 9 | }); |
| 10 | |
| 11 | // Listen for specific actions |
| 12 | ghotona_shuno(stateManager, "increment", kaj() { |
| 13 | state["count"] = state["count"] + 1; |
| 14 | ghotona_prokash(stateManager, "state:change", state); |
| 15 | }); |
| 16 | |
| 17 | ghotona_shuno(stateManager, "decrement", kaj() { |
| 18 | state["count"] = state["count"] - 1; |
| 19 | ghotona_prokash(stateManager, "state:change", state); |
| 20 | }); |
| 21 | |
| 22 | // Trigger actions |
| 23 | ghotona_prokash(stateManager, "increment"); // count: 1 |
| 24 | ghotona_prokash(stateManager, "increment"); // count: 2 |
| 25 | ghotona_prokash(stateManager, "decrement"); // count: 1 |
API Reference
ghotona_srishti()
Creates a new EventEmitter instance.
Returns: EventEmitter
ghotona_shuno(emitter, event_name, callback)
Adds an event listener that runs every time the event is emitted.
Parameters:
emitter- EventEmitter instanceevent_name- String event namecallback- Function to call when event is emitted
Returns: EventEmitter (for chaining)
ghotona_ekbar(emitter, event_name, callback)
Adds an event listener that runs only once, then is automatically removed.
Returns: EventEmitter (for chaining)
ghotona_prokash(emitter, event_name, ...data)
Emits an event with optional data arguments. All listeners for this event will be called.
Returns: Boolean (sotti/true)
ghotona_bondho(emitter, event_name, callback)
Removes a specific listener from an event.
Returns: EventEmitter (for chaining)
ghotona_sob_bondho(emitter, [event_name])
Removes all listeners for a specific event, or all listeners for all events if event_name is not provided.
Returns: EventEmitter (for chaining)
ghotona_shrotara(emitter, event_name)
Returns an array of all listeners for a specific event.
Returns: Array of functions
ghotona_naam_sob(emitter)
Returns an array of all event names that have listeners.
Returns: Array of strings
Best Practices
✅ Do
- Use descriptive event names (e.g., "user:login", "data:received")
- Store listener references if you plan to remove them later
- Use
ghotona_ekbar()for one-time events - Document what events your components emit
- Clean up listeners when they're no longer needed
❌ Don't
- Don't create too many EventEmitters - reuse when possible
- Don't forget to remove listeners to prevent memory leaks
- Don't rely on listener execution order for critical logic
- Don't throw errors in listeners without handling them