eventnotifier 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

EventNotifier #

Pub Package

NOTE: Consider using the Event package instead.

This package is an implementation of a central 'Event Bus' or 'Event Broker' which is suited to some specific use-cases.

In general, using a lightweight custom Event is a preferable way to have something that is independent of dependencies, inform others that something of interest has occurred.

Broadcasts named events to interested subscribers. When an event occurs, a method (callback) associated with the subscriber is executed.

EventNotifier is an implementation of the Observer Pattern, providing the ability to publish events, and subscribe to them elsewhere. Such implementations are sometimes called an 'Event Broker' or 'Event Bus'.

This is primarily intended to be mixed-in with a problem domain model, to enable it to provide change notifications.

Background #

It can be ideal to model problems or systems independent of user interfaces (UI) or system interfaces (SI). Doing so, one can model the problem or system without regard to how the model may (or may not be) consumed.

However, given an independent model, how can something else outside it know if something in the domain model changed? One solution is to use EventNotifier.

An Illustration #

Consider that one wanted to model the operation of a single 'Elevator'. One might produce an independent domain model in object-oriented Dart code that modeled it.

Later, one might want to provide a user interface (UI) that let one interact with the model, or provide a system interface (SI) that interacted with (for example) the Programmable Logic Controller (PLC) of a real-life Elevator.

In order to let external modules or systems know that the Elevator changed floors, the Elevator model can notify them through EventNotifier, so they can react as appropriate.

See also #

Event - Create lightweight custom Dart Events. An alternative to this centralized Event Bus/Event Broker approach.

Dependencies #

None. This Dart package has no non-development dependencies on any other packages.

Usage #

A simple example (see the static EventNotifier test method):

import 'package:eventnotifier/eventnotifier.dart';

main() {
    var e = EventNotifier();

    e.subscribe('myChange', (_) => print('boom'));
    e.subscribe('myChange', (args) => print(args['pi']));

    // number of event names
    assert(e.count() == 1);
    // number of handlers subscribed to the event name
    assert(e.count('myChange') == 2);

    // indicate that event 'myChange' has occured
    // rguments are only required if a subscriber
    // expects one or more
    e.notify('myChange', {'pi': 3.14159});

    // displays ...
    // boom
    // 3.14159
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

Version 1.1.1 (2020-01-23) #

  • Added recommendation to generally use the Event package to the README.
  • Added licence header to source files.

Version 1.1.0 (2020-01-23) #

  • Removed references to EventSubscriber, as it now supports Event rather than EventNotifier

Version 1.0.5 (2020-01-14) #

  • Add support again for optional event arguments (was previously removed). Arguments should now be supplied as a Map, e.g. notify('myEvent', {'age': 32});
  • A more detailed error is shown if a subscriber expects an argument, but it isn't supplied when notified.
  • One can now access the EventNotifier using the getEventNotifier() method. Getting the instance can be useful from within a mixin.

Version 1.0.4 (2020-01-13) #

  • Fixed LICENSE reference to package

Version 1.0.3 (2020-01-13) #

  • Package example updated to show the static test() method.
  • Add a link to EventSubscriber in the README.

Version 1.0.2 (2020-01-12) #

  • Removed support for optional event arguments. Query the model instead.
  • Exceptions are now also output to the console.
  • README describes how to use Flutter with EventNotifier

Version 1.0.1 (2020-01-11) #

  • Improved README and minor reformatting changes

Version 1.0.0 (2020-01-11) #

  • Initial release

example/eventnotifier_example.dart

import 'package:eventnotifier/eventnotifier.dart';

// NOTE: EventNotifier is typically 'mixed-in' with a domain model

void main() {
  var e = EventNotifier();

  e.subscribe('myChange', (_) => print('boom'));
  e.subscribe('myChange', (args) => print(args['pi']));

  // number of event names
  assert(e.count() == 1);
  // number of handlers subscribed to the event name
  assert(e.count('myChange') == 2);

  // indicate that event 'myChange' has occured
  // rguments are only required if a subscriber
  // expects one or more
  e.notify('myChange', {'pi': 3.14159});

  // displays ...
  // boom
  // 3.14159
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  eventnotifier: ^1.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:eventnotifier/eventnotifier.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
11
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Mar 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/eventnotifier.dart. (-1 points)

Analysis of lib/src/eventnotifier.dart reported 2 hints:

line 59 col 7: DO use curly braces for all flow control structures.

line 71 col 9: DO use curly braces for all flow control structures.

Maintenance suggestions

The description is too long. (-10 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0