Eventhub

Methods

dispatch

void dispatch(String event, Map<String, dynamic> data)

The dispatch method sends an event to all registered subscribers and passes the associated data.

  • event: A string representing the name of the event.
  • data: A key-value map containing the data associated with the event.

listen

String listen(String event, Function(Map<String, dynamic>) callback)

The listen method registers a new subscriber for a specific event.

  • event: A string representing the name of the event to subscribe to.
  • callback: A function to be called when the event is emitted. It receives a key-value map containing the data associated with the event.

Returns: A string representing the unique identifier of the subscription.

getEvent

Map<String, dynamic> getEvent(String event)

The getEvent method retrieves the data of the most recent event.

  • event: A string representing the name of the event.

Returns: A key-value map containing the data associated with the most recent event.

dispose

void dispose(String event, String id)

The dispose method removes a registered subscriber for a specific event.

  • event: A string representing the name of the event.
  • id: A string representing the unique identifier of the subscription to be removed.

disposeAll

void disposeAll(String event)

The disposeAll method removes all registered subscribers for a specific event.

  • event: A string representing the name of the event.

Example Usage

final eventhub = Eventhub();

// Listen to the "myEvent" event
final subscriptionId = eventhub.listen("myEvent", (data) {
  print("Event received: $data");
});

// Emit an event
eventhub.dispatch("myEvent", {"message": "Hello, world!"});

// Get the data of the most recent event
final eventData = eventhub.getEvent("myEvent");
print("Data of the most recent event: $eventData");

// Remove a specific subscriber
eventhub.dispose("myEvent", subscriptionId);

// Remove all subscribers for a specific event
eventhub.disposeAll("myEvent");