{"id":17535652,"url":"https://github.com/drafakiller/eventemitter-dart","last_synced_at":"2025-04-23T20:31:16.273Z","repository":{"id":39889099,"uuid":"493469352","full_name":"DrafaKiller/EventEmitter-dart","owner":"DrafaKiller","description":"A Event-based system, inspired by NodeJS's Event Emitter. Uses generic types to allow for multiple data types, while still being intuitive. Based on JavaScript and suitable for Dart and Flutter.","archived":false,"fork":false,"pushed_at":"2025-01-08T20:49:50.000Z","size":92,"stargazers_count":13,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-18T04:55:59.828Z","etag":null,"topics":["dart","events","package","type-safe"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DrafaKiller.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-05-18T01:38:18.000Z","updated_at":"2025-02-03T18:01:03.000Z","dependencies_parsed_at":"2025-04-17T16:00:57.707Z","dependency_job_id":null,"html_url":"https://github.com/DrafaKiller/EventEmitter-dart","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrafaKiller%2FEventEmitter-dart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrafaKiller%2FEventEmitter-dart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrafaKiller%2FEventEmitter-dart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrafaKiller%2FEventEmitter-dart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrafaKiller","download_url":"https://codeload.github.com/DrafaKiller/EventEmitter-dart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250509507,"owners_count":21442444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","events","package","type-safe"],"created_at":"2024-10-20T19:08:09.186Z","updated_at":"2025-04-23T20:31:16.027Z","avatar_url":"https://github.com/DrafaKiller.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Pub.dev package](https://img.shields.io/badge/pub.dev-events__emitter-blue)](https://pub.dev/packages/events_emitter)\n[![GitHub repository](https://img.shields.io/badge/GitHub-EventEmitter--dart-blue?logo=github)](https://github.com/DrafaKiller/EventEmitter-dart)\n\n# Event Emitter\n\nAn Event-based system, highly inspired by [NodeJS's Event Emitter](https://nodejs.org/api/events.html). This implementation uses generic types to allow for multiple data types while being very intuitive.\n\nBased on JavaScript and suitable for Dart and Flutter with type safety.\n\n## Features\n\n* Attach multiple listeners to an event emitter.\n* Listen to events with a specific **event type** and **data type**.\n* Emit an event of a specific type to be broadcasted to all listeners.\n* Type safety, only the right type will be passed in.\n* Use callbacks with `EventEmitter`.\n* Use streams with `StreamEventEmitter`.\n* Can be extended to create custom event emitters and events.\n* Custom events can hold custom data to be used in the listeners.\n\n## Getting started\n\nInstall it using pub:\n```\ndart pub add events_emitter\n```\n\nAnd import the package:\n```dart\nimport 'package:events_emitter/events_emitter.dart';\n```\n\n## Usage\n\n```dart\nfinal events = EventEmitter();\n\nevents.on('message', (String data) =\u003e print('String: $data'));\nevents.on('message', (int data) =\u003e print('Integer: $data'));\n\nevents.emit('message', 'Hello World');\nevents.emit('message', 42);\n\n// [Output]\n// String: Hello World\n// Integer: 42\n``` \n\nTo remove a specific listener, you can use the subscription to stop it.\n```dart\nfinal listener = events.on('message', ... );\nlistener.cancel();\n```\n\nRemove listeners, by targeting an **event type**, **data type** and **callback**.\n```dart\n// Remove all listeners\nevents.off();\n\n// Remove listeners of data type `String`\nevents.off\u003cString\u003e();\n\n// Remove listeners on event type `message`\nevents.off(type: 'message');\n\n// Remove listeners of data type `String` on event type `message`\nevents.off\u003cString\u003e(type: 'message');\n```\n\n## Listeners\n\nListeners are attached to an event emitter and are called when an event matches its signature, they can be added manually for flexibility.\n\n```dart\nfinal events = EventEmitter();\n  ...\nfinal listener = EventListener('message', (String data) =\u003e print('String: $data'));\nevents.addEventListener(listener);\n  ...\nlistener.cancel();\n```\n\nProperties can be set on the listener to change its behavior:\n- `once`: If set to `true`, the listener will be removed after the first call.\n- `protected`: If set to `true`, the listener will be immune to `events.off()`.\n\nAdd callbacks to the listener:\n- `onAdd`: Called when the listener is added to the event emitter.\n- `onRemove`: Called when the listener is removed from the event emitter.\n- `onCall`: Called when the listener is called.\n- `onCancel`: Called when the listener is canceled.\n\n```dart\nfinal events = EventEmitter();\n  ...\nfinal listener = EventListener(\n  'message',\n  (String data) =\u003e print(data),\n  \n  once: false,\n  protected: false,\n\n  onAdd: (emitter) =\u003e print('Added to emitter'),\n  onRemove: (emitter) =\u003e print('Removed from emitter'),\n  onCall: (data) =\u003e print('Called with data: $data'),\n  onCancel: () =\u003e print('Listener canceled'),\n);\n```\n\n## Why is this package different?\n\n`events_emitter` implements the Event-based system using **callbacks** and **streams**, making it very easy to use and very flexible, allowing you to choose the best way to use it. If you need to fit your needs, you can extend the `EventEmitter` and `Event` classes to create your own custom interface.\n\nAnd something very important, `events_emitter` allows you to use **type-safe** events, so you can use the same event type for different data types. Not having to worry about the wrong type being passed in.\n\n## Example\n\nThe `EventEmitter` class can be used by itself or can be extended to create a custom event emitter.\n\n```dart\nimport 'package:events_emitter/events_emitter.dart';\n\nclass Person extends EventEmitter {\n    String name;\n\n    Person(this.name);\n\n    void say(String message) =\u003e emit('say', message);\n    void eat(String food) =\u003e emit('eat', food);\n    void jump(double height) =\u003e emit('jump', height);\n}\n\nvoid main() {\n  final person = Person('John');\n\n  person.on('say', (String message) =\u003e print('${person.name} said: $message'));\n  person.on('eat', (String food) =\u003e print('${person.name} ate $food'));\n  person.on('jump', (double height) =\u003e print('${person.name} jumped $height meters'));\n\n  person.say('I\\'m a human!');\n  person.eat('apple');\n  person.jump(0.5);\n\n  // [Output]\n  // John said: I'm a human!\n  // John ate apple\n  // John jumped 0.5 meters\n}\n```\n\nMore examples:\n* [EventEmitter](https://github.com/DrafaKiller/EventEmitter-dart/blob/main/example/lib/main.dart)\n* [StreamEventEmitter](https://github.com/DrafaKiller/EventEmitter-dart/blob/main/example/lib/event_emitter_stream.dart)\n* [Extendable](https://github.com/DrafaKiller/EventEmitter-dart/blob/main/example/lib/extendable.dart)\n* [CustomEvent](https://github.com/DrafaKiller/EventEmitter-dart/blob/main/example/lib/custom_event.dart)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrafakiller%2Feventemitter-dart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrafakiller%2Feventemitter-dart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrafakiller%2Feventemitter-dart/lists"}