Giter Club home page Giter Club logo

eventable's Introduction

#Eventable Build Status

Eventable provides 2 mixins to make your classes eventable, EventEmitter and EventDetector. Simply use these mixins to make your types either emit, detect or do both for asynchronous events. An Event can carry any data object, to listen for an Event carrying a particular data type just listen for that data type. Usage is best described with a simple example:

##Example

class Dog extends Object with EventEmitter{

  void bark(int volume){
    emitEvent(new Bark(volume));
  }
  
}

class Cat extends Object with EventDetector{

  void dogBarkHandler(Event<Bark> event){
    var bark = event.data;
    if(bark.volume > 10){
      runaway();
    }else{
      print('cat not disturbed');
    }
  }

  void runaway(){
    print('cat running away');
  }

}

class Bark{
  final int volume;
  Bark(this.volume);
}

void main(){

  var dog = new Dog();
  var cat = new Cat();

  cat.listen(dog, Bark, cat.dogBarkHandler);

  dog.bark(9);  // cat not disturbed
  dog.bark(11); // cat runs away

}

##Omni

There is a special event data type Omni which allows detectors to listen to every event emitted by an EventEmitter with a single EventAction.

##EventAction

A typedef is specified in the eventable library which serves as the function signature accepted as the last argument to an EventDetectors listen method. The signature is:

typedef void EventAction(Event event);

##DuplicateEventSettingError

A given EventDetector can only listen to a specific event-data-type from a specific EventEmitter once, therefore it is an error to try to attach more EventActions to the same EventEmitter / event-data-type combination. If a second attempt is made by an EventDetector to listen to the same EventEmitter / event-data-type combination a DuplicateEventSettingError will be thrown.

##EmitTimeQueueChangeError

It is not permitted to add or remove EventActions whilst the event is being emitted, meaning you may not attach an EventAction to an event which adds or removes EventActions from that same event-data-type queue, if such an attempt is made a EmitTimeQueueChangeError will be thrown. to remove EventActions for an emitting Event it is best to do so in the finished Future property on the Event object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.