observable 0.1.3 observable: ^0.1.3 copied to clipboard
Easy registration and notification of events
Synopsis #
Observable provides a convenient mechanism for registering to events and dispatching notifications to listening clients.
How to use #
Inheriting from Observable
#
The easiest way to use Observable is by inheriting from Observable
, as given
by:
import 'package:obvervable/obvervable.dart';
class Foo extends Observable
Clients register for notifications as illustrated in the following snippet:
Foo myFoo = new Foo();
myFoo.on.anEvent(() => print("AnEvent fired"));
Dispatching of events is done by invoking the dispatch
method:
class Foo extends Observable
{
.
:
.
void method()
{
// carry out operations
observer.dispatch("anEvent");
}
}
Composition #
Since Dart does not support multiple inheritance, you may at times not be able to use the method outlined above. The alternative, then, is to use composition. Example:
import 'package:observable/observer.dart';
import 'package:observable/observer_events.dart';
class Foo extends SomeOtherClass
{
Observer observer;
ObserverEvents on;
Foo()
: observer = new Observer()
{
on = observer.on;
}
}
Clients register for notifications and events are dispatched in the same manner as exemplified in the preceding section.