├── .npmrc ├── .gitattributes ├── .babelrc ├── .prettierrc ├── .travis.yml ├── .npmignore ├── CHANGELOG.md ├── index.js ├── .editorconfig ├── .gitignore ├── .bumpedrc ├── LICENSE ├── index.test.js ├── package.json └── README.md /.npmrc: -------------------------------------------------------------------------------- 1 | unsafe-perm=true 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env"] 3 | } 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": false, 3 | "singleQuote": true, 4 | "trailingComma": "es5" 5 | } 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'node' 4 | - 'lts/*' 5 | before_script: 6 | - npm run clean 7 | script: 8 | - npm run build 9 | after_success: npm run coveralls 10 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | .project 4 | *.sublime-* 5 | .DS_Store 6 | *.seed 7 | *.log 8 | *.csv 9 | *.dat 10 | *.out 11 | *.pid 12 | *.swp 13 | *.swo 14 | node_modules 15 | coverage 16 | *.tgz 17 | *.xml 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | # 1.0.0 (2017-07-24) 3 | 4 | * Add unit tests with Jest 🃏 ([eafe1ba](https://github.com/kutyel/es6-emitter/commit/eafe1ba)) 5 | * Initial commit ([1ae2f3a](https://github.com/kutyel/es6-emitter/commit/1ae2f3a)) 6 | * Turn into a node module (not CLI) ([153a692](https://github.com/kutyel/es6-emitter/commit/153a692)) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default class Emitter { 2 | constructor (events = []) { 3 | this.events = new Map(events) 4 | } 5 | 6 | subscribe (name, cb) { 7 | this.events.set(name, [...(this.events.get(name) || []), cb]) 8 | return () => this.events.set(name, this.events.get(name).filter(fn => fn !== cb)) 9 | } 10 | 11 | emit (name, ...args) { 12 | return this.events.has(name) && this.events.get(name).map(fn => fn(...args)) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 2 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | max_line_length = 100 13 | indent_brace_style = 1TBS 14 | spaces_around_operators = true 15 | quote_type = auto 16 | 17 | [package.json] 18 | indent_style = space 19 | indent_size = 2 20 | 21 | [*.md] 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################ 2 | # npm 3 | ############################ 4 | node_modules 5 | npm-debug.log 6 | package-lock.json 7 | 8 | ############################ 9 | # tmp, editor & OS files 10 | ############################ 11 | .tmp 12 | *.swo 13 | *.swp 14 | *.swn 15 | *.swm 16 | .DS_Store 17 | *# 18 | *~ 19 | .idea 20 | .vscode 21 | *sublime* 22 | nbproject 23 | 24 | ############################ 25 | # Tests 26 | ############################ 27 | testApp 28 | coverage 29 | .nyc_output 30 | 31 | ############################ 32 | # Other 33 | ############################ 34 | .node_history 35 | dist 36 | -------------------------------------------------------------------------------- /.bumpedrc: -------------------------------------------------------------------------------- 1 | files: 2 | - package.json 3 | plugins: 4 | prerelease: 5 | Linting config files: 6 | plugin: bumped-finepack 7 | postrelease: 8 | Generating CHANGELOG file: 9 | plugin: bumped-changelog 10 | Committing new version: 11 | plugin: bumped-terminal 12 | command: 'git add CHANGELOG.md package.json && git commit -m "Release $newVersion"' 13 | Detecting problems before publish: 14 | plugin: bumped-terminal 15 | command: 'git-dirty && npm test' 16 | Publishing tag to GitHub: 17 | plugin: bumped-terminal 18 | command: 'git tag $newVersion && git push && git push --tags' 19 | Publishing to NPM: 20 | plugin: bumped-terminal 21 | command: npm publish 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2017 Flavio Corpa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.test.js: -------------------------------------------------------------------------------- 1 | import Emitter from '.' 2 | 3 | describe('Emitter', () => { 4 | let sub 5 | const callback1 = jest.fn() 6 | const callback2 = jest.fn() 7 | const emitter = new Emitter() 8 | 9 | test('should trigger nothing', () => { 10 | emitter.emit('event', 'foo') 11 | expect(callback1).not.toHaveBeenCalled() 12 | }) 13 | 14 | test('should trigger 1 callback', () => { 15 | sub = emitter.subscribe('event', callback1) 16 | emitter.emit('event', 'foo') 17 | expect(callback1).toHaveBeenCalledTimes(1) 18 | expect(callback1).toHaveBeenCalledWith('foo') 19 | }) 20 | 21 | test('should trigger 2 callbacks', () => { 22 | emitter.subscribe('event', callback2) 23 | emitter.emit('event', 'bar', 'baz') 24 | expect(callback1).toHaveBeenCalledTimes(2) 25 | expect(callback1).toHaveBeenCalledWith('bar', 'baz') 26 | expect(callback2).toHaveBeenCalledTimes(1) 27 | expect(callback2).toHaveBeenCalledWith('bar', 'baz') 28 | }) 29 | 30 | test('should release first callback, and call the second', () => { 31 | sub() 32 | emitter.emit('event', 'meow') 33 | expect(callback1).toHaveBeenCalledTimes(2) // same number as before 34 | expect(callback2).toHaveBeenCalledTimes(2) 35 | expect(callback2).toHaveBeenCalledWith('meow') 36 | }) 37 | 38 | test('should return the return values of all the events in the callstack', () => { 39 | emitter.subscribe('myEvent', () => 1) 40 | emitter.subscribe('myEvent', () => 2) 41 | emitter.subscribe('myEvent', () => true) 42 | const result = emitter.emit('myEvent') 43 | expect(result).toEqual([1, 2, true]) 44 | }) 45 | }) 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es6-emitter", 3 | "description": "Smallest event emitter for JavaScript with all the power of ES6 Maps!", 4 | "homepage": "https://documentup.com/kutyel/es6-emitter", 5 | "version": "1.0.1", 6 | "main": "dist/es6-emitter.js", 7 | "umd:main": "dist/es6-emitter.umd.js", 8 | "module": "dist/es6-emitter.mjs", 9 | "source": "index.js", 10 | "author": { 11 | "email": "flaviocorpa@gmail.com", 12 | "name": "Flavio Corpa", 13 | "url": "http://flaviocorpa.com" 14 | }, 15 | "files": [ 16 | "index.js", 17 | "README.md" 18 | ], 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/kutyel/es6-emitter.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/kutyel/es6-emitter/issues" 25 | }, 26 | "keywords": [ 27 | "es6", 28 | "eventemitter", 29 | "events", 30 | "pubsub" 31 | ], 32 | "dependencies": {}, 33 | "devDependencies": { 34 | "@babel/core": "^7.5.5", 35 | "@babel/preset-env": "^7.5.5", 36 | "coveralls": "latest", 37 | "git-dirty": "latest", 38 | "jest": "latest", 39 | "lodash": "latest", 40 | "merge": "latest", 41 | "microbundle": "latest", 42 | "prettier-standard": "latest", 43 | "standard": "latest" 44 | }, 45 | "engines": { 46 | "node": ">= 6" 47 | }, 48 | "scripts": { 49 | "build": "microbundle -o dist", 50 | "dev": "microbundle watch", 51 | "clean": "rm -rf node_modules", 52 | "coveralls": "cat ./coverage/lcov.info | coveralls", 53 | "lint": "standard", 54 | "prelint": "npm run pretty", 55 | "pretest": "npm run lint", 56 | "pretty": "prettier-standard index.js src/**/*.js test/**/*.js bin/**/*.js", 57 | "test": "jest --coverage" 58 | }, 59 | "license": "MIT", 60 | "jest": { 61 | "coverageThreshold": { 62 | "global": { 63 | "branches": 100, 64 | "functions": 100, 65 | "lines": 100, 66 | "statements": 100 67 | } 68 | }, 69 | "testEnvironment": "node" 70 | }, 71 | "standard": { 72 | "env": [ 73 | "jest" 74 | ] 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # es6-emitter 2 | 3 | [![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) 4 | 5 | [![Node version](https://img.shields.io/node/v/es6-emitter.svg?style=flat-square)](https://www.npmjs.org/package/es6-emitter) 6 | [![Build Status](https://img.shields.io/travis/kutyel/es6-emitter/master.svg?style=flat-square)](https://travis-ci.org/kutyel/es6-emitter) 7 | [![Coverage Status](https://img.shields.io/coveralls/kutyel/es6-emitter.svg?style=flat-square)](https://coveralls.io/github/kutyel/es6-emitter) 8 | [![Dependency status](https://img.shields.io/david/kutyel/es6-emitter.svg?style=flat-square)](https://david-dm.org/kutyel/es6-emitter) 9 | [![Dev Dependencies Status](https://img.shields.io/david/dev/kutyel/es6-emitter.svg?style=flat-square)](https://david-dm.org/kutyel/es6-emitter#info=devDependencies) 10 | [![NPM Status](https://img.shields.io/npm/dm/es6-emitter.svg?style=flat-square)](https://www.npmjs.org/package/es6-emitter) 11 | [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/flaviocorpa) 12 | 13 | > Smallest event emitter for JavaScript with all the power of ES6 Maps! 14 | 15 | ## Install 16 | 17 | ```bash 18 | $ npm install es6-emitter --save 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```javascript 24 | import Emitter from 'es6-emitter' 25 | 26 | const em = new Emitter() 27 | ``` 28 | 29 | ## API 30 | 31 | ### subscribe (name, callback) 32 | 33 | Allows you to add subscriptions to your emitter given a certain name, **multiple subscriptions** under the same name are allowed! 34 | 35 | ```js 36 | const sub1 = em.subscribe('myEvent', foo => console.log('a callback!')); 37 | 38 | const sub2 = em.subscribe('myEvent', (bar, baz) => console.log('another callback!')); 39 | 40 | sub1(); // releases the first subscription 41 | ``` 42 | 43 | **Returns** a function to release the subscription while the others remain intact. 44 | 45 | #### name 46 | 47 | *Required*
48 | Type: `any` 49 | 50 | Can be literally [any](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) type, and corresponds to the name of the event that you want the Emitter to subscribe to. 51 | 52 | #### callback 53 | 54 | *Required*
55 | Type: `function` 56 | 57 | Side effect that you want to associate with the name of the event. 58 | 59 | ### emit (name[, args]) 60 | 61 | Allows you to emit any subscription added to the emitter, with any number of arguments. 62 | 63 | ```js 64 | em.emit('myEvent', 1, '2', null, () => 4); 65 | ``` 66 | 67 | **Returns** an array with every return value for each subscription callback. 68 | 69 | ```js 70 | const s1 = em.subscribe('otherEvent', () => 1); 71 | const s2 = em.subscribe('otherEvent', () => 2); 72 | const s3 = em.subscribe('otherEvent', () => true); 73 | const result = em.emit('otherEvent'); // > [1, 2, true] 74 | ``` 75 | 76 | #### name 77 | 78 | *Required*
79 | Type: `any` 80 | 81 | Just as in the `subscribe` function, this is the `name` of the event you want to emit. 82 | 83 | #### args 84 | 85 | Any number of values of `any` type to be passed to the subscription functions. 86 | 87 | ## ES6 Map Awesomeness ✨ 88 | 89 | Thanks to **ES Maps** goodness, the `name` of the event subscribed can be literally *anything*, even another function! 😱 90 | 91 | ```js 92 | const em = new Emitter(); 93 | 94 | const eventObject = { name: 'click', debounce: 300 }; 95 | const eventFunction = e => console.log(e); 96 | 97 | const sub1 = em.subscribe(NaN, () => 'not a number'); 98 | const sub2 = em.subscribe(eventObject, () => 'called with an object!'); 99 | const sub3 = em.subscribe(eventFunction, () => 'called with a function!'); 100 | 101 | em.emit(NaN); // > ['not a number'] 102 | em.emit(eventObject); // > ['called with an object!'] 103 | em.emit(eventFunction); // > ['called with a function!'] 104 | ``` 105 | 106 | ## License 107 | 108 | MIT © [Flavio Corpa](https://github.com/kutyel). 109 | --------------------------------------------------------------------------------