abstract/emitter module

requires

  • Emitter() class

    Object oriented event API. Good for a base class or mixin.

    methods

    • .off(listenerKey:object)

      Removes a listener

      listenerKey
      Returned from .on or .once
    • .off(eventName:string)

      Removes all listeners of an event name

    • .off()

      Removes all listeners

    • .on(eventName:string, handler:function) returns listenerKey:object

      Adds a listener Returns Can be passed to .off to remove listener

    • .once(eventName:string, handler:function) returns listenerKey:object

      Adds a listener to be fired once Returns Can be passed to .off to remove listener

    • .trigger(eventName:string, nArgs:any*)

      Executes all handler functions listening for an event

      nArgs
      Additional arguments are passed to handler functions
    • .trigger(options:object, nArgs:any*)

      Executes all handler functions listening for an event

      nArgs
      Additional arguments are passed to handler functions

code

/* istanbul ignore if */
if (typeof define !== "function") {
	var define = require("amdefine")(module);
}

define(function (require) {
	"use strict";

	require("../array/from");

	var Emitter,
		meta = require("../meta"),
		createClass = require("../class"),
		event = require("../event");
	
	Emitter = createClass(
		meta({
			"name": "Emitter",
			"type": "class",
			"description": "Object oriented event API. Good for a base class or mixin."
		})
	);

	Emitter.method(
		meta({
			"name": "on",
			"description": "Adds a listener",
			"arguments": [{
				"name": "eventName",
				"type": "string"
			}, {
				"name": "handler",
				"type": "function"
			}],
			"returns": {
				"name": "listenerKey",
				"type": "object",
				"description": "Can be passed to .off to remove listener"
			}
		}),
		on
	);

	Emitter.method(
		meta({
			"name": "once",
			"description": "Adds a listener to be fired once",
			"arguments": [{
				"name": "eventName",
				"type": "string"
			}, {
				"name": "handler",
				"type": "function"
			}],
			"returns": {
				"name": "listenerKey",
				"type": "object",
				"description": "Can be passed to .off to remove listener"
			}
		}),
		once
	);

	Emitter.method(
		meta({
			"name": "off",
			"description": "Removes a listener",
			"arguments": [{
				"name": "listenerKey",
				"type": "object",
				"description": "Returned from .on or .once"
			}]
		}),
		offByListener
	);

	Emitter.method(
		meta({
			"name": "off",
			"description": "Removes all listeners of an event name",
			"arguments": [{
				"name": "eventName",
				"type": "string"
			}]
		}),
		offByEventName
	);

	Emitter.method(
		meta({
			"name": "off",
			"description": "Removes all listeners",
			"arguments": []
		}),
		allOff
	);

	Emitter.method(
		meta({
			"name": "trigger",
			"description": "Executes all handler functions listening for an event",
			"arguments": [{
				"name": "eventName",
				"type": "string"
			}, {
				"name": "nArgs",
				"type": "any",
				"required": false,
				"repeating": true,
				"description": "Additional arguments are passed to handler functions"
			}]
		}),
		trigger
	);

	Emitter.method(
		meta({
			"name": "trigger",
			"description": "Executes all handler functions listening for an event",
			"arguments": [{
				"name": "options",
				"type": "object",
				"properties": {
					"name": "string",
					"when": "string",
					"params": "array"
				}
			}, {
				"name": "nArgs",
				"type": "any",
				"required": false,
				"repeating": true,
				"description": "Additional arguments are passed to handler functions"
			}]
		}),
		trigger
	);

	function on (eventName, handler) {
		return event.addListener(this, eventName, handler);
	}

	function once (eventName, handler) {
		return event.addListenerOnce(this, eventName, handler);
	}

	function offByListener (listenerKey) {
		event.removeListener(listenerKey);
	}

	function offByEventName (eventName) {
		event.removeListeners(this, eventName);
	}

	function allOff () {
		event.removeAllListeners(this);
	}

	function trigger () {
		var args = Array.from(arguments);
		
		args.unshift(this);
		event.trigger.apply(event, args);
	}

	return Emitter;
});
Page built with api-meta