CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-wolfy87-eventemitter

Event based JavaScript for the browser with comprehensive event management API

91

1.26x
Overview
Eval results
Files

task.mdevals/scenario-4/

Notification Center

Build a notification center system that manages multiple event handlers for different notification types. The system should support efficient bulk registration and removal of event handlers.

Requirements

Create a notification center module that:

  1. Allows registering multiple notification handlers at once for a specific notification type
  2. Supports registering handlers for multiple notification types in a single operation
  3. Can remove multiple handlers from a notification type efficiently
  4. Emits notifications to all registered handlers
  5. Provides a way to retrieve all handlers for a given notification type

The notification center should handle the following notification types:

  • user.login - fired when a user logs in
  • user.logout - fired when a user logs out
  • system.alert - fired for system alerts
  • message.received - fired when a message is received

Implementation

@generates

API

/**
 * NotificationCenter manages event handlers for different notification types.
 */
class NotificationCenter {
  constructor();

  /**
   * Register multiple handlers for a specific notification type.
   * @param {string} notificationType - The type of notification
   * @param {Function[]} handlers - Array of handler functions
   * @returns {NotificationCenter} The instance for chaining
   */
  registerHandlers(notificationType, handlers);

  /**
   * Register handlers for multiple notification types at once.
   * @param {Object} handlersMap - Object mapping notification types to handler functions or arrays
   * @returns {NotificationCenter} The instance for chaining
   */
  registerBulkHandlers(handlersMap);

  /**
   * Remove multiple handlers from a notification type.
   * @param {string} notificationType - The type of notification
   * @param {Function[]} handlers - Array of handler functions to remove
   * @returns {NotificationCenter} The instance for chaining
   */
  unregisterHandlers(notificationType, handlers);

  /**
   * Send a notification to all registered handlers.
   * @param {string} notificationType - The type of notification
   * @param {*} data - Data to pass to handlers
   * @returns {NotificationCenter} The instance for chaining
   */
  notify(notificationType, data);

  /**
   * Get all handlers registered for a notification type.
   * @param {string} notificationType - The type of notification
   * @returns {Function[]} Array of handler functions
   */
  getHandlers(notificationType);
}

module.exports = { NotificationCenter };

Test Cases

  • Registering multiple handlers for a single notification type adds all handlers, and notifying that type calls all handlers @test
  • Registering handlers for multiple notification types using an object adds handlers to each type correctly @test
  • Removing multiple handlers from a notification type removes only those handlers @test
  • Getting handlers for a notification type returns an array of all registered handler functions @test

Dependencies { .dependencies }

wolfy87-eventemitter { .dependency }

Provides event management capabilities for handling notifications.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-wolfy87-eventemitter

tile.json