events_emitter 0.0.5 copy "events_emitter: ^0.0.5" to clipboard
events_emitter: ^0.0.5 copied to clipboard

outdated

A Event-based system, inspired by NodeJS's Event Emitter. Intuitive generic types to allow for multiple data types. Based on JavaScript and suitable for Dart and Flutter.

Event Emitter #

A Event-based system, highly inspired by NodeJS's EventEmitter. This implementation uses generic types to allow for multiple data types, while still being intuitive.

Based on JavaScript and suitable for Dart and Flutter.

Features #

  • Attach multiple listeners to an event.
  • Listen to a topic and data type.
  • Emit a message on a specific topic to be broadcasted to all listeners.
  • Use callbacks with EventEmitter.
  • Use streams with EventStreamEmitter.
  • Can be extented to create custom event emitter objects.

Getting started #

Install it using pub:

flutter pub add events_emitter

And import the package:

import 'package:events_emitter/events_emitter.dart';

Usage #

EventEmitter events = EventEmitter();

events.on('message', (String data) => print('String: $data'));
events.on('message', (int data) => print('Integer: $data'));

events.emit('message', 'Hello World');
events.emit('message', 42);

// [Output]
// String: Hello World
// Integer: 42

To remove a specific listener, you can use the subscription to stop it.

final subscription = events.on('message', ... ));
subscription.cancel();

Remove listeners, by targeting a type, topic and callback.

// Remove all listeners
events.off();

// Remove listeners of type `String`
events.off<String>();

// Remove listeners on topic `message`
events.off(topic: 'message');

// Remove listeners of type `String` on topic `message`
events.off<String>(topic: 'message');

GitHub #

The package code is available on Github: Dart - EventEmitter

Example #

The EventEmitter class can be used by itself or can be extend to create a custom event emitter.

import 'package:events_emitter/events_emitter.dart';

class Person extends EventEmitter {
    String name;

    Person(this.name);

    void say(String message) => emit('say', message);
    void eat(String food) => emit('eat', food);
    void jump(double height) => emit('jump', height);
}

void main() {
  final person = Person('John');

  person.on('say', (String message) => print('${person.name} said: $message'));
  person.on('eat', (String food) => print('${person.name} ate $food'));
  person.on('jump', (double height) => print('${person.name} jumped $height meters'));

  person.say('I\'m a human!');
  person.eat('apple');
  person.jump(0.5);

  // [Output]
  // John said: I'm a human!
  // John ate apple
  // John jumped 0.5 meters
}

More examples:

17
likes
0
points
30.6k
downloads

Publisher

verified publisherdrafakiller.com

Weekly Downloads

A Event-based system, inspired by NodeJS's Event Emitter. Intuitive generic types to allow for multiple data types. Based on JavaScript and suitable for Dart and Flutter.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on events_emitter