pax_global_header 0000666 0000000 0000000 00000000064 13211416106 0014505 g ustar 00root root 0000000 0000000 52 comment=e313fff29ca3c6304b533d71ace1c18ee154fd4f
emitter-1.2.1/ 0000775 0000000 0000000 00000000000 13211416106 0013157 5 ustar 00root root 0000000 0000000 emitter-1.2.1/.editorconfig 0000664 0000000 0000000 00000000441 13211416106 0015633 0 ustar 00root root 0000000 0000000 # http://editorconfig.org/
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[{**/{actual,fixtures,expected,templates}/**,*.md}]
trim_trailing_whitespace = false
insert_final_newline = false
emitter-1.2.1/.eslintrc.json 0000664 0000000 0000000 00000006775 13211416106 0015772 0 ustar 00root root 0000000 0000000 {
"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
},
"globals": {
"document": false,
"navigator": false,
"window": false
},
"rules": {
"accessor-pairs": 2,
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"comma-dangle": [2, "never"],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"constructor-super": 2,
"curly": [2, "multi-line"],
"dot-location": [2, "property"],
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [2, { "before": true, "after": true }],
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
"new-parens": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-const-assign": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-dupe-args": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": [2, "functions"],
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { "max": 1 }],
"no-native-reassign": 0,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-proto": 0,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 0,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"use-isnan": 2,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
"yoda": [2, "never"]
}
}
emitter-1.2.1/.gitattributes 0000664 0000000 0000000 00000000200 13211416106 0016042 0 ustar 00root root 0000000 0000000 # Enforce Unix newlines
* text eol=lf
# binaries
*.ai binary
*.psd binary
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
emitter-1.2.1/.github/ 0000775 0000000 0000000 00000000000 13211416106 0014517 5 ustar 00root root 0000000 0000000 emitter-1.2.1/.github/contributing.md 0000664 0000000 0000000 00000011217 13211416106 0017552 0 ustar 00root root 0000000 0000000 # Contributing to emitter
First and foremost, thank you! We appreciate that you want to contribute to emitter, your time is valuable, and your contributions mean a lot to us.
## Important!
By contributing to this project, you:
* Agree that you have authored 100% of the content
* Agree that you have the necessary rights to the content
* Agree that you have received the necessary permissions from your employer to make the contributions (if applicable)
* Agree that the content you contribute may be provided under the Project license(s)
## Getting started
**What does "contributing" mean?**
Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following:
- Updating or correcting documentation
- Feature requests
- Bug reports
If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information.
**Showing support for emitter**
Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use.
Don't have time to contribute? No worries, here are some other ways to show your support for emitter:
- star the [project](https://github.com/jonschlinkert/emitter)
- tweet your support for emitter
## Issues
### Before creating an issue
Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues.
Try to follow these guidelines
- **Avoid creating issues for implementation help**. It's much better for discoverability, SEO, and semantics - to keep the issue tracker focused on bugs and feature requests - to ask implementation-related questions on [stackoverflow.com][so]
- **Investigate the issue**:
- **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue.
- Create the issue in the appropriate repository.
### Creating an issue
Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue:
- **version**: please note the version of emitter are you using
- **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using
- **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/)
### Closing issues
The original poster or the maintainer's of emitter may close an issue at any time. Typically, but not exclusively, issues are closed when:
- The issue is resolved
- The project's maintainers have determined the issue is out of scope
- An issue is clearly a duplicate of another issue, in which case the duplicate issue will be linked.
- A discussion has clearly run its course
## Next steps
**Tips for creating idiomatic issues**
Spending just a little extra time to review best practices and brush up on your contributing skills will, at minimum, make your issue easier to read, easier to resolve, and more likely to be found by others who have the same or similar issue in the future. At best, it will open up doors and potential career opportunities by helping you be at your best.
The following resources were hand-picked to help you be the most effective contributor you can be:
- The [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) is a great place for newcomers to start, but there is also information for experienced contributors there.
- Take some time to learn basic markdown. We can't stress this enough. Don't start pasting code into GitHub issues before you've taken a moment to review this [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601)
- The GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/) is another great markdown resource.
- Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/).
At the very least, please try to:
- Use backticks to wrap code. This ensures that it retains its formatting and isn't modified when it's rendered by GitHub, and makes the code more readable to others
- When applicable, use syntax highlighting by adding the correct language name after the first "code fence"
[so]: http://stackoverflow.com/questions/tagged/emitter
emitter-1.2.1/.gitignore 0000664 0000000 0000000 00000000446 13211416106 0015153 0 ustar 00root root 0000000 0000000 # always ignore files
*.DS_Store
.idea
*.sublime-*
# test related, or directories generated by tests
test/actual
actual
coverage
.nyc*
# npm
node_modules
npm-debug.log
# yarn
yarn.lock
yarn-error.log
# misc
_gh_pages
_draft
_drafts
bower_components
vendor
temp
tmp
TODO.md
package-lock.json emitter-1.2.1/.travis.yml 0000664 0000000 0000000 00000000146 13211416106 0015271 0 ustar 00root root 0000000 0000000 sudo: false
os:
- linux
- osx
language: node_js
node_js:
- node
- '9'
- '8'
- '7'
- '6'
emitter-1.2.1/.verb.md 0000664 0000000 0000000 00000000470 13211416106 0014516 0 ustar 00root root 0000000 0000000 ## Attribution
This is based on [component-emitter][], with some changes and a couple of new methods. That project seems to no longer be maintained.
## Usage
Add to your node.js project with the following line of code:
```js
var Emitter = require('{%= pkg.name %}');
```
## API
{%= apidocs("index.js") %}
emitter-1.2.1/LICENSE 0000664 0000000 0000000 00000002073 13211416106 0014166 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2017, Jon Schlinkert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
emitter-1.2.1/README.md 0000664 0000000 0000000 00000014035 13211416106 0014441 0 ustar 00root root 0000000 0000000 # emitter [](https://www.npmjs.com/package/@sellside/emitter) [](https://npmjs.org/package/@sellside/emitter) [](https://npmjs.org/package/@sellside/emitter) [](https://travis-ci.org/sellside/emitter)
> Event emitter for node.js projects.
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save @sellside/emitter
```
## Attribution
This is based on [component-emitter](https://github.com/component/emitter), with some changes and a couple of new methods. That project seems to no longer be maintained.
## Usage
Add to your node.js project with the following line of code:
```js
var Emitter = require('@sellside/emitter');
```
## API
### [Emitter](index.js#L18)
Initialize a new `Emitter`.
**Example**
```js
const Emitter = require('emitter');
// as an `Emitter` instance
const emitter = new Emitter;
emitter.emit('something');
// or inherit
class MyEmitter extends Emitter {}
```
### [Emitter#mixin](index.js#L39)
Mixin methods from Emitter.
**Params**
* `obj` **{Object}**
* `returns` **{Object}**
**Example**
```js
const Emitter = require('emitter');
const obj = {};
Emitter.mixin(obj);
obj.on('status', console.log);
obj.emit('status', 'I emit!');
```
### [.listeners](index.js#L58)
Return the array of registered listeners for `event`.
**Params**
* `event` **{String}**
* `returns` **{Array}**
**Example**
```js
// all listeners for event "status"
console.log(emitter.listeners('status'));
// all listeners
console.log(emitter.listeners());
```
### [.on](index.js#L78)
Listen on the given `event` with `fn`.
**Params**
* `event` **{String}**
* `fn` **{Function}**
* `returns` **{Emitter}**
**Example**
```js
emitter.on('foo', () => 'do stuff');
```
### [.once](index.js#L100)
Adds an `event` listener that will be invoked a single time then automatically removed.
**Params**
* `event` **{String}**
* `fn` **{Function}**
* `returns` **{Emitter}**
**Example**
```js
emitter.only('once', () => 'do stuff');
```
### [.only](index.js#L126)
Ensures that listeners for `event` are only **_registered_** once and are disabled correctly when specified. This is different from `.once`, which only **emits** once.
**Params**
* `event` **{String}**
* `options` **{Object}**
* `fn` **{Function}**
* `returns` **{Emitter}**
**Example**
```js
emitter.only('foo', () => 'do stuff');
```
### [.off](index.js#L170)
Remove the given listener for `event`, or remove all registered listeners if `event` is undefined.
**Params**
* `event` **{String}**
* `fn` **{Function}**
* `returns` **{Emitter}**
**Example**
```js
emitter.off();
emitter.off('foo');
emitter.off('foo', fn);
```
### [.has](index.js#L227)
Returns true if the emitter has registered listeners for `event`.
**Params**
* `event` **{String}**
* `returns` **{Boolean}**
**Example**
```js
emitter.on('foo', 'do stuff');
console.log(emitter.has('foo')); // true
console.log(emitter.has('bar')); // false
```
## About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
Building docs
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
To generate the readme, run the following command:
```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
### Related projects
You might also be interested in these projects:
* [breakdance](https://www.npmjs.com/package/breakdance): Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy… [more](http://breakdance.io) | [homepage](http://breakdance.io "Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy to use. It's time for your markup to get down.")
* [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
* [snapdragon](https://www.npmjs.com/package/snapdragon): Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… [more](https://github.com/jonschlinkert/snapdragon) | [homepage](https://github.com/jonschlinkert/snapdragon "Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.")
### Author
**Jon Schlinkert**
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 04, 2017._ emitter-1.2.1/examples.js 0000664 0000000 0000000 00000000216 13211416106 0015332 0 ustar 00root root 0000000 0000000 var Emitter = require('./');
var obj = {};
Emitter.mixin(obj);
obj.on('status', console.log);
obj.emit('status', 'I emit!');
console.log(obj)
emitter-1.2.1/index.js 0000664 0000000 0000000 00000013344 13211416106 0014631 0 ustar 00root root 0000000 0000000 'use strict';
/**
* Initialize a new `Emitter`.
*
* ```js
* const Emitter = require('emitter');
* // as an `Emitter` instance
* const emitter = new Emitter;
* emitter.emit('something');
* // or inherit
* class MyEmitter extends Emitter {}
* ```
* @name Emitter
* @api public
*/
class Emitter {
constructor(obj) {
if (obj) return mixin(obj);
}
/**
* Mixin methods from Emitter.
*
* ```js
* const Emitter = require('emitter');
* const obj = {};
* Emitter.mixin(obj);
* obj.on('status', console.log);
* obj.emit('status', 'I emit!');
* ```
* @name Emitter#mixin
* @param {Object} `obj`
* @return {Object}
* @api public
*/
static mixin(obj) {
return new Emitter(obj);
}
/**
* Return the array of registered listeners for `event`.
*
* ```js
* // all listeners for event "status"
* console.log(emitter.listeners('status'));
* // all listeners
* console.log(emitter.listeners());
* ```
* @name .listeners
* @param {String} `event`
* @return {Array}
* @api public
*/
listeners(event) {
if (!this._listeners) define(this, '_listeners', {});
if (!this._only) define(this, '_only', {});
if (!event) return this._listeners;
return this._listeners['$' + event] || (this._listeners['$' + event] = []);
}
/**
* Listen on the given `event` with `fn`.
*
* ```js
* emitter.on('foo', () => 'do stuff');
* ```
* @name .on
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
on(event, fn) {
if (this._only && this._only[event]) {
return this.only(event, fn);
}
this.listeners(event).push(fn);
return this;
}
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* ```js
* emitter.only('once', () => 'do stuff');
* ```
* @name .once
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
once(event, fn) {
const on = function() {
this.off(event, on);
fn.apply(this, arguments);
};
on.fn = fn;
this.on(event, on);
return this;
}
/**
* Ensures that listeners for `event` are only **_registered_** once
* and are disabled correctly when specified. This is different from
* `.once`, which only **emits** once.
*
* ```js
* emitter.only('foo', () => 'do stuff');
* ```
* @name .only
* @param {String} `event`
* @param {Object} `options`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
only(event, options, fn) {
this.listeners();
if (typeof options === 'function') {
fn = options;
options = null;
}
if (options && options.first === true) {
define(this, '_first', true);
}
if (!fn || !event || !this._only[event]) {
this.off(event);
if (!fn) return this;
}
const existing = this._only[event];
if (existing) {
if (this._first === true) return this;
this.off(event, existing);
}
this._only[event] = fn;
this.listeners(event).push(fn);
return this;
}
/**
* Remove the given listener for `event`, or remove all
* registered listeners if `event` is undefined.
*
* ```js
* emitter.off();
* emitter.off('foo');
* emitter.off('foo', fn);
* ```
* @name .off
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
off(event, fn) {
this.listeners();
// remove all listeners
if (!event) {
this._listeners = {};
this._only = {};
return this;
}
// remove all listeners for "event"
if (!fn) {
this._listeners['$' + event] = [];
this._only['$' + event] = [];
return this;
}
// remove all instances of "fn" from "event"
removeListeners(fn, this.listeners(event));
return this;
}
/**
* Emit `event` with the given args.
*
* ```js
* emitter.emit('foo', 'bar');
* ```
* @name .emit
* @param {String} `event`
* @param {Mixed} ...
* @return {Emitter}
*/
emit(event) {
const listeners = this.listeners(event).slice();
const args = [].slice.call(arguments, 1);
for (const fn of listeners) {
fn.apply(this, args);
}
return this;
}
/**
* Returns true if the emitter has registered listeners for `event`.
*
* ```js
* emitter.on('foo', 'do stuff');
* console.log(emitter.has('foo')); // true
* console.log(emitter.has('bar')); // false
* ```
* @name .has
* @param {String} `event`
* @return {Boolean}
* @api public
*/
has(event) {
return this.listeners(event).length > 0;
}
}
/**
* Expose common aliases for `.has`, `.on` and `.off`
*/
Emitter.prototype.hasListeners =
Emitter.prototype.has;
Emitter.prototype.addListener =
Emitter.prototype.addEventListener =
Emitter.prototype.on;
Emitter.prototype.removeListener =
Emitter.prototype.removeListeners =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener =
Emitter.prototype.off;
/**
* Remove all instances of the given `fn` from listeners.
*/
function removeListeners(fn, listeners) {
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i];
if (listener === fn || listener.fn === fn) {
listeners.splice(i, 1);
return removeListeners(fn, listeners);
}
}
}
/**
* Mixin emitter properties.
*/
function mixin(obj) {
const ctor = obj.constructor;
Object.setPrototypeOf(obj, Emitter.prototype);
if (ctor) define(obj, 'constructor', ctor);
return obj;
}
function define(obj, key, val) {
Reflect.defineProperty(obj, key, {
configurable: true,
writable: true,
value: val
});
}
/**
* Expose `Emitter`
*/
module.exports = Emitter;
emitter-1.2.1/package.json 0000664 0000000 0000000 00000002002 13211416106 0015437 0 ustar 00root root 0000000 0000000 {
"name": "@sellside/emitter",
"description": "Event emitter for node.js projects.",
"version": "1.2.1",
"homepage": "https://github.com/sellside/emitter",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "sellside/emitter",
"bugs": {
"url": "https://github.com/sellside/emitter/issues"
},
"license": "MIT",
"files": [
"index.js"
],
"main": "index.js",
"engines": {
"node": ">=6"
},
"scripts": {
"test": "mocha"
},
"devDependencies": {
"gulp-format-md": "^1.0.0",
"mocha": "^4.0.1"
},
"keywords": [
"",
"emitter",
"event",
"event-emitter",
"events",
"fire",
"listener",
"listeners",
"sellside"
],
"verb": {
"toc": false,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {
"list": [
"breakdance",
"micromatch",
"snapdragon"
]
},
"lint": {
"reflinks": true
}
}
}
emitter-1.2.1/test/ 0000775 0000000 0000000 00000000000 13211416106 0014136 5 ustar 00root root 0000000 0000000 emitter-1.2.1/test/test.js 0000664 0000000 0000000 00000020001 13211416106 0015444 0 ustar 00root root 0000000 0000000 'use strict';
require('mocha');
const assert = require('assert');
const Emitter = require('..');
describe('Emitter', function() {
describe('constructor', function() {
it('should work work with new Emitter', function(cb) {
let emitter = new Emitter();
emitter.on('foo', cb);
emitter.emit('foo');
});
});
describe('mixin', function() {
it('should mixin properties', function(callback) {
let proto = {};
new Emitter(proto);
proto.on('something', callback);
proto.emit('something');
});
it('should mixin using Emitter.mixin()', function(callback) {
let proto = {};
Emitter.mixin(proto);
proto.on('something', callback);
proto.emit('something');
});
});
describe('.on(event, fn)', function() {
it('should add listeners', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('foo', function(val) {
calls.push('one', val);
});
emitter.on('foo', function(val) {
calls.push('two', val);
});
emitter.emit('foo', 1);
emitter.emit('bar', 1);
emitter.emit('foo', 2);
assert.deepEqual(calls, ['one', 1, 'two', 1, 'one', 2, 'two', 2]);
});
it('should add listeners with Object.prototype method names', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('constructor', function(val) {
calls.push('one', val);
});
emitter.on('__proto__', function(val) {
calls.push('two', val);
});
emitter.emit('constructor', 1);
emitter.emit('__proto__', 2);
assert.deepEqual(calls, ['one', 1, 'two', 2]);
});
});
describe('.once(event, fn)', function() {
it('should add a single-shot listener', function() {
let emitter = new Emitter();
let calls = [];
emitter.once('foo', function(val) {
calls.push('one', val);
});
emitter.emit('foo', 1);
emitter.emit('foo', 2);
emitter.emit('foo', 3);
emitter.emit('bar', 1);
assert.deepEqual(calls, ['one', 1]);
});
});
describe('.only()', function() {
it('should only call the last listener registered', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('foo', () => calls.push('1'));
emitter.only('foo', () => calls.push('2'));
emitter.on('foo', () => calls.push('3'));
emitter.on('foo', () => calls.push('4'));
emitter.only('foo', () => calls.push('5'));
emitter.emit('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, ['5', '5', '5']);
});
it('should work with .only {first: true}', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('foo', () => calls.push('1'));
emitter.only('foo', {first: true}, () => calls.push('2'));
emitter.on('foo', () => calls.push('3'));
emitter.on('foo', () => calls.push('4'));
emitter.on('foo', () => calls.push('5'));
emitter.only('foo', () => calls.push('6'));
emitter.emit('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, ['2', '2', '2']);
});
it('should remove a listener registered with .only', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('foo', () => calls.push('1'));
emitter.only('foo', () => calls.push('2'));
emitter.on('foo', () => calls.push('3'));
emitter.on('foo', () => calls.push('4'));
emitter.on('foo', () => calls.push('5'));
emitter.only('foo', () => calls.push('6'));
emitter.only('foo'); //<= disable "foo"
emitter.emit('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, []);
});
it('should remove all listeners registered with .only', function() {
let emitter = new Emitter();
let calls = [];
emitter.on('foo', () => calls.push('1'));
emitter.only('foo', () => calls.push('2'));
emitter.on('foo', () => calls.push('3'));
emitter.on('foo', () => calls.push('4'));
emitter.on('foo', () => calls.push('5'));
emitter.only('foo', () => calls.push('6'));
emitter.only(); //<= disable all "only" listeners
emitter.emit('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, []);
});
});
describe('.off(event, fn)', function() {
it('should remove a listener', function() {
let emitter = new Emitter();
let calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('foo', two);
emitter.off('foo', two);
emitter.emit('foo');
assert.deepEqual(calls, ['one']);
});
it('should work with .once()', function() {
let emitter = new Emitter();
let calls = [];
function one() {
calls.push('one');
}
emitter.once('foo', one);
emitter.once('fee', one);
emitter.off('foo', one);
emitter.emit('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, []);
});
it('should work when called from an event', function() {
let emitter = new Emitter();
let called;
function b() {
called = true;
}
emitter.on('foo', function() {
emitter.off('foo', b);
});
emitter.on('foo', b);
emitter.emit('foo');
assert(called);
called = false;
emitter.emit('foo');
assert(!called);
});
});
describe('.off(event)', function() {
it('should remove all listeners for an event', function() {
let emitter = new Emitter();
let calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('foo', two);
emitter.off('foo');
emitter.emit('foo');
emitter.emit('foo');
assert.deepEqual(calls, []);
});
});
describe('.off()', function() {
it('should remove all listeners', function() {
let emitter = new Emitter();
let calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('bar', two);
emitter.emit('foo');
emitter.emit('bar');
emitter.off();
emitter.emit('foo');
emitter.emit('bar');
assert.deepEqual(calls, ['one', 'two']);
});
});
describe('.listeners(event)', function() {
describe('when handlers are present', function() {
it('should return an array of callbacks', function() {
let emitter = new Emitter();
function foo() {}
emitter.on('foo', foo);
assert.deepEqual(emitter.listeners('foo'), [foo]);
});
});
describe('when no handlers are present', function() {
it('should return an empty array', function() {
let emitter = new Emitter();
assert.deepEqual(emitter.listeners('foo'), []);
});
});
});
describe('.has(event)', function() {
describe('when handlers are present', function() {
it('should return true', function() {
let emitter = new Emitter();
emitter.on('foo', function() {});
assert(emitter.has('foo'));
});
});
describe('when no handlers are present', function() {
it('should return false', function() {
let emitter = new Emitter();
assert(!emitter.has('foo'));
});
});
});
describe('.hasListeners(event)', function() {
describe('when handlers are present', function() {
it('should return true', function() {
let emitter = new Emitter();
emitter.on('foo', function() {});
assert(emitter.hasListeners('foo'));
});
});
describe('when no handlers are present', function() {
it('should return false', function() {
let emitter = new Emitter();
assert(!emitter.hasListeners('foo'));
});
});
});
});