Skip to content

wigz/Eventful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Eventful

A simple pub/sub superclass for ES2015. Allows you to use events to communicate between instances.

Use

class SomeOtherEventfulClass extends Eventful {

  /*
  ------------------------------------------
  | constructor:void
  |
  | init:object - init params
  |
  | Construct.
  ------------------------------------------ */
  constructor(init) {
    super();

    // trigger
    this.trigger('another_event', {'data': 'another data'});
  }
}

class MyClass extends Eventful {

  /*
  ------------------------------------------
  | constructor:void
  |
  | init:object - init params
  |
  | Construct.
  ------------------------------------------ */
  constructor(init) {
    super();

    let my_instance = new SomeOtherEventfulClass();

    // use internal
    this.on('some_event', this.onSomeEvent);

    // use external
    my_instance.on('another_event', (e) => this.onAnotherEvent(e));

    // trigger
    this.trigger('some_event', {'data': 'some data'});

    // trigger here
    my_instance.trigger('another_event', {'data': 'another data'});

    // remove specific
    this.off('some_event', this.onSomeEvent);

    // remove all
    my_instance.off('another_event');
  }

  /*
  ------------------------------------------
  | onSomeEvent:void
  |
  | e:object - event object
  |
  | Handle some_event.
  ------------------------------------------ */
  onSomeEvent(e) {
    console.log(e);
  }

  /*
  ------------------------------------------
  | onAnotherEvent:void
  |
  | e:object - event object
  |
  | Handle another_event.
  ------------------------------------------ */
  onAnotherEvent(e) {
    console.log(e);
  }
}

About

A simple pub/sub superclass for ES2015.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published