AaronAcerboni / PubSub.as

A port of Morgan Roderick's PubSubJS. This is an ActionScript 3 library for using stand alone pub/sub or inherited event emitters.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's this?

PubSub.as allows ActionScript 3 developers to practice the publish / subscribe pattern to a similar nature found in JavaScript implementations.

PubSub.as is a port of Morgan Roderick's PubSubJS.

Do not take this port to be fully representative of what can be found in the Morgan's original source code.

Usage

Pub/Sub can be practiced as a stand alone broadcaster.

var $:PubSub = new PubSub();

$.sub('myEvent', function(evt, data){
    trace(data);
    // > outputs 'hello'
});

$.pub('myEvent', 'hello');

Or with class inheritance in similar nature to JavaScript's EventEmitter

public class Waiter extends PubSub {

  public function Waiter(){
    bringFood();
  }

  private function bringFood():void{
    this.emit('serve', 'cake');
  }

}

// ...

var waiter:Waiter = new Waiter();

waiter.on('serve', function(evt, food){
  man.eat(food);
})

Subscriptions can be removed by passing the token they return.

var token:Number = $.sub('myEvent', function(eventName, data){
    // ...
});

$.remove(token);

This PubSub library provides alias methods that some programmers may prefer to use.

PubSub.pub == PubSub.emit

PubSub.sub == PubSub.on

About

A port of Morgan Roderick's PubSubJS. This is an ActionScript 3 library for using stand alone pub/sub or inherited event emitters.


Languages

Language:ActionScript 100.0%