1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00
psychojs/docs/util_EventEmitter.js.html
2019-04-19 09:50:14 +02:00

198 lines
7.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: util/EventEmitter.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: util/EventEmitter.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Event Emitter.
*
* @author Alain Pitiot
* @version 3.0.8
* @copyright (c) 2019 Ilixa Ltd. ({@link http://ilixa.com})
* @license Distributed under the terms of the MIT License
*/
import * as util from './Util';
/**
* &lt;p>EventEmitter implements the classic observer/observable pattern.&lt;/p>
*
* &lt;p>Note: this is heavily inspired by http://www.datchley.name/es6-eventemitter/&lt;/p>
*
* @name module:util.EventEmitter
* @class
*
* @example
* let observable = new EventEmitter();
* let uuid1 = observable.on('change', data => { console.log(data); });
* observable.emit("change", { a: 1 });
* observable.off("change", uuid1);
* observable.emit("change", { a: 1 });
*/
export class EventEmitter
{
constructor()
{
this._listeners = new Map();
this._onceUuids = new Map();
}
/**
* Listener called when this instance emits an event for which it is registered.
*
* @callback module:util.EventEmitter~Listener
* @param {object} data - the data passed to the listener
*/
/**
* Register a new listener for events with the given name emitted by this instance.
*
* @name module:util.EventEmitter#on
* @function
* @public
* @param {String} name - the name of the event
* @param {module:util.EventEmitter~Listener} listener - a listener called upon emission of the event
* @return string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)
*/
on(name, listener)
{
// check that the listener is a function:
if (typeof listener !== 'function')
throw new TypeError('listener must be a function');
// generate a new uuid:
let uuid = util.makeUuid();
// add the listener to the event map:
if (!this._listeners.has(name))
this._listeners.set(name, []);
this._listeners.get(name).push({uuid, listener});
return uuid;
}
/**
* Register a new listener for the given event name, and remove it as soon as the event has been emitted.
*
* @name module:util.EventEmitter#once
* @function
* @public
* @param {String} name - the name of the event
* @param {module:util.EventEmitter~Listener} listener - a listener called upon emission of the event
* @return string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)
*/
once(name, listener)
{
let uuid = this.on(name, listener);
if (!this._onceUuids.has(name))
this._onceUuids.set(name, []);
this._onceUuids.get(name).push(uuid);
return uuid;
}
/**
* Remove the listener with the given uuid associated to the given event name.
*
* @name module:util.EventEmitter#off
* @function
* @public
* @param {String} name - the name of the event
* @param {module:util.EventEmitter~Listener} listener - a listener called upon emission of the event
*/
off(name, uuid)
{
let relevantUuidListeners = this._listeners.get(name);
if (relevantUuidListeners &amp;&amp; relevantUuidListeners.length) {
this._listeners.set(name, relevantUuidListeners.filter( uuidlistener => (uuidlistener.uuid != uuid) ) );
return true;
}
return false;
}
/**
* Emit an event with a given name and associated data.
*
* @name module:util.EventEmitter#emit
* @function
* @public
* @param {String} name - the name of the event
* @param {object} data - the data of the event
* @return {boolean} true if at least one listener has been registered for that event, and false otherwise
*/
emit(name, data)
{
let relevantUuidListeners = this._listeners.get(name);
if (relevantUuidListeners &amp;&amp; relevantUuidListeners.length)
{
let onceUuids = this._onceUuids.get(name);
let self = this;
relevantUuidListeners.forEach( ({uuid, listener}) => {
listener(data);
if (typeof onceUuids !== 'undefined' &amp;&amp; onceUuids.includes(uuid))
self.off(name, uuid);
});
return true;
}
return false;
}
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="module-core.BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="module-core.EventManager.html">EventManager</a></li><li><a href="module-core.GUI.html">GUI</a></li><li><a href="module-core.MinimalStim.html">MinimalStim</a></li><li><a href="module-core.Mouse.html">Mouse</a></li><li><a href="module-core.PsychoJS.html">PsychoJS</a></li><li><a href="module-core.ServerManager.html">ServerManager</a></li><li><a href="module-core.Window.html">Window</a></li><li><a href="module-data.ExperimentHandler.html">ExperimentHandler</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.Sound.html">Sound</a></li><li><a href="module-sound.TonePlayer.html">TonePlayer</a></li><li><a href="module-sound.TrackPlayer.html">TrackPlayer</a></li><li><a href="module-util.Clock.html">Clock</a></li><li><a href="module-util.Color.html">Color</a></li><li><a href="module-util.CountdownTimer.html">CountdownTimer</a></li><li><a href="module-util.EventEmitter.html">EventEmitter</a></li><li><a href="module-util.Logger.html">Logger</a></li><li><a href="module-util.MixinBuilder.html">MixinBuilder</a></li><li><a href="module-util.MonotonicClock.html">MonotonicClock</a></li><li><a href="module-util.PsychObject.html">PsychObject</a></li><li><a href="module-util.Scheduler.html">Scheduler</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.MovieStim.html">MovieStim</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.ShapeStim.html">ShapeStim</a></li><li><a href="module-visual.Slider.html">Slider</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li><li><a href="module-visual.VisualStim.html">VisualStim</a></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a></li><li><a href="module-util.ColorMixin.html">ColorMixin</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#offerDataForDownload">offerDataForDownload</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Apr 19 2019 09:44:48 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>