Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Introduce an EventBus for Decoupled CLI Event Handling #23

Open
supitsdu opened this issue Jan 12, 2025 · 0 comments
Open

Proposal: Introduce an EventBus for Decoupled CLI Event Handling #23

supitsdu opened this issue Jan 12, 2025 · 0 comments
Assignees
Labels
architecture discussion enhancement New feature or request help wanted Extra attention is needed

Comments

@supitsdu
Copy link
Owner

I’d like to propose adding an EventBus feature to climonad.js to handle and dispatch various events—such as errors, usage tracking, and command lifecycle events—without relying on Node.js-specific modules. This would allow me to decouple cross-cutting concerns, like logging and error reporting, in a more modular and testable way.

Motivation

  • Decouple Error Handling: Instead of scattering error handling logic throughout the code, an EventBus can emit “error” events that listeners handle in their own modules.
  • Publish/Subscribe Mechanism: Dispatching “commandStart” or “commandFinish” events can help track CLI usage without tightly coupling modules.
  • Simplify Testing: An in-house event system can be easily mocked or replaced in tests to verify CLI behavior in isolation.

Possible Implementation

Below is a sample EventBus class in TypeScript. It implements a lightweight publish/subscribe pattern and doesn’t use Node.js-specific APIs:

export class EventBus {
  private listeners: Record<string, Array<(...args: unknown[]) => void>> = {}

  /**
   * Register an event listener for a specific event.
   */
  public on(event: string, listener: (...args: unknown[]) => void): void {
    if (!this.listeners[event]) {
      this.listeners[event] = []
    }
    this.listeners[event].push(listener)
  }

  /**
   * Unregister an event listener.
   */
  public off(event: string, listener: (...args: unknown[]) => void): void {
    if (this.listeners[event]) {
      this.listeners[event] = this.listeners[event].filter(l => l !== listener)
    }
  }

  /**
   * Emit an event to all registered listeners.
   */
  public emit(event: string, ...args: unknown[]): void {
    if (this.listeners[event]) {
      for (const listener of this.listeners[event]) {
        listener(...args)
      }
    }
  }
}

Here’s an example of how this might integrate into the CLI logic. Let’s assume I have a global instance of the EventBus:

import { EventBus } from "./EventBus"

const bus = new EventBus()

// Register a listener for error events.
bus.on("error", (error: string) => {
  console.error("[climonad.js] Error:", error)
})

// In some CLI processing logic:
try {
  // Some CLI actions...
} catch (e) {
  // Emit the error event so it's handled in a centralized way.
  bus.emit("error", e instanceof Error ? e.message : String(e))
}

Benefits

  1. Modular Error Handling: Consolidate logging, analytics, or error reporting without cluttering the main command logic.
  2. Flexible Scopes: Expand the CLI to listen or emit additional events (like a “usage” event) without altering existing modules.
  3. Testing Friendliness: Mock the EventBus in unit tests, ensuring each part of the CLI remains independently verifiable.

I believe this addition could make climonad.js more extensible and easier to maintain. I’d love to hear any feedback, suggestions, or concerns.

@supitsdu supitsdu added enhancement New feature or request help wanted Extra attention is needed discussion architecture labels Jan 12, 2025
@supitsdu supitsdu self-assigned this Jan 12, 2025
@supitsdu supitsdu moved this to Brainstorm Ideas in Climonad.js - Roadmap Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
architecture discussion enhancement New feature or request help wanted Extra attention is needed
Projects
Status: Brainstorm Ideas
Development

No branches or pull requests

1 participant