Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kseo/event_kit
Simple library for implementing and consuming evented APIs
https://github.com/kseo/event_kit
Last synced: 27 days ago
JSON representation
Simple library for implementing and consuming evented APIs
- Host: GitHub
- URL: https://github.com/kseo/event_kit
- Owner: kseo
- License: bsd-3-clause
- Created: 2015-11-18T11:39:36.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-03T10:00:35.000Z (about 8 years ago)
- Last Synced: 2023-08-20T21:54:10.972Z (about 1 year ago)
- Language: Dart
- Homepage: https://pub.dartlang.org/packages/event_kit
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# event_kit
This is a simple library for implementing event subscription APIs. This project is inspired by [event-kit][event-kit].
[![Build Status](https://travis-ci.org/kseo/event_kit.svg)](https://travis-ci.org/kseo/event_kit)
[![Coverage Status](https://coveralls.io/repos/kseo/event_kit/badge.svg?branch=master&service=github)](https://coveralls.io/github/kseo/event_kit?branch=master)[event-kit]: https://www.npmjs.com/package/event-kit
## Implementing Event Subscription APIs
```dart
import 'package:event_kit/event_kit.dart';class User {
final Emitter _emitter = new Emitter();String _name = '';
Disposable onDidChangeName(void handler(String name)) =>
_emitter.on('did-change-name', handler);void set name(String name) {
if (_name != name) {
_name = name;
_emitter.emit('did-change-name', [name]);
}
}
}
```In the example above, we implement `onDidChangeName` on the user object, which
will register callbacks to be invoked whenever the user's name changes. To do
so, we make use of an internal `Emitter` instance. We use `on` to subscribe
the given callback in `onDidChangeName`, and `emit` in `setName` to notify
subscribers. Finally, when the `User` instance is destroyed we call `dispose`
on the emitter to unsubscribe all subscribers.## Consuming Event Subscription APIs
`Emitter.on` returns a `Disposable` instance, which has a `dispose` method.
To unsubscribe, simply call dispose on the returned object.```dart
import 'package:event_kit/event_kit.dart';var subscription = user.onDidChangeName((name) => print('My name is ${name}'));
// Later, to unsubscribe...
subscription.dispose();
```You can also use `CompositeDisposable` to combine disposable instances together.
```dart
import 'package:event_kit/event_kit.dart';var subscriptions = new CompositeDisposable();
subscriptions.add(user1.onDidChangeName((name) => print('User 1: ${name}')));
subscriptions.add(user2.onDidChangeName((name) => print('User 2: ${name}')));// Later, to unsubscribe from *both*...
subscriptions.dispose();
```## Features and bugs
Please file feature requests and bugs at the [issue tracker][tracker].
[tracker]: https://github.com/kseo/event_kit/issues