├── .npmignore ├── .gitignore ├── .eslintignore ├── test ├── .eslintrc └── test.js ├── .travis.yml ├── CHANGELOG.md ├── src └── index.js ├── .eslintrc ├── package.json ├── LICENSE.md └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | /test 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /node_modules 3 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | lib/** 2 | node_modules/** 3 | 4 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "mocha": true 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4.2' 4 | sudo: false 5 | 6 | before_script: 7 | - npm install 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `emits-change` will be documented in this file. 4 | 5 | ## 1.0.1 6 | - Dependency fix 7 | 8 | ## 1.0.0 9 | - Initial release 10 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import assign from 'object-assign' 2 | import { EventEmitter } from 'events' 3 | 4 | function emitsChange(object) { 5 | 6 | assign(object, EventEmitter.prototype) 7 | 8 | object.listen = function(callback) { 9 | this.on('change', callback) 10 | } 11 | 12 | object.unlisten = function(callback) { 13 | this.removeListener('change', callback) 14 | } 15 | 16 | object.emitChange = function() { 17 | this.emit('change') 18 | } 19 | } 20 | 21 | export default emitsChange 22 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "indent": [ 4 | 2, 5 | 4 6 | ], 7 | "quotes": [ 8 | 2, 9 | "single" 10 | ], 11 | "linebreak-style": [ 12 | 2, 13 | "unix" 14 | ], 15 | "semi": [ 16 | 2, 17 | "never" 18 | ] 19 | }, 20 | "ecmaFeatures": { 21 | "modules": true 22 | }, 23 | "env": { 24 | "es6": true, 25 | "browser": true 26 | }, 27 | "extends": "eslint:recommended" 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "emits-change", 3 | "version": "1.0.1", 4 | "description": "Plug-and-play node.js events integration to emit change events", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "babel src -d lib", 8 | "test": "eslint . && mocha --compilers js:mocha-babel test", 9 | "prepublish": "npm run test && npm run build" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/spatie/emits-events.git" 14 | }, 15 | "keywords": [ 16 | "events", 17 | "emit", 18 | "emitter", 19 | "change", 20 | "store" 21 | ], 22 | "author": "Sebastian De Deyne", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/spatie/emits-events/issues" 26 | }, 27 | "homepage": "https://github.com/spatie/emits-events#readme", 28 | "dependencies": { 29 | "object-assign": "^4.0.1" 30 | }, 31 | "devDependencies": { 32 | "babel": "^5.8.23", 33 | "babel-runtime": "^5.8.25", 34 | "chai": "^3.3.0", 35 | "eslint": "^7.1.0", 36 | "mocha": "^2.3.3", 37 | "mocha-babel": "^3.0.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sebastian De Deyne 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 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import { assert } from 'chai' 2 | import emitsChange from '../src/index' 3 | 4 | describe('emitsChange', () => { 5 | 6 | class TestEmitter { 7 | constructor() { 8 | emitsChange(this) 9 | } 10 | } 11 | 12 | it('can be bound to a class through composition', () => { 13 | 14 | let testEmitter = new TestEmitter 15 | 16 | assert.isFunction(testEmitter.listen) 17 | assert.isFunction(testEmitter.unlisten) 18 | assert.isFunction(testEmitter.emitChange) 19 | assert.notOk(testEmitter.random) 20 | }) 21 | 22 | it('accepts a listener and calls it on change', () => { 23 | 24 | let testEmitter = new TestEmitter 25 | let listenerHasBeenCalled = false 26 | let listener = () => { 27 | listenerHasBeenCalled = ! listenerHasBeenCalled 28 | } 29 | 30 | testEmitter.listen(listener) 31 | assert.isFalse(listenerHasBeenCalled) 32 | testEmitter.emitChange() 33 | assert.isTrue(listenerHasBeenCalled) 34 | }) 35 | 36 | it('can unlisten a callback', () => { 37 | 38 | let testEmitter = new TestEmitter 39 | let listenerHasBeenCalled = false 40 | let listener = () => { 41 | listenerHasBeenCalled = ! listenerHasBeenCalled 42 | } 43 | 44 | testEmitter.listen(listener) 45 | assert.isFalse(listenerHasBeenCalled) 46 | testEmitter.emitChange() 47 | assert.isTrue(listenerHasBeenCalled) 48 | testEmitter.unlisten(listener) 49 | testEmitter.emitChange() 50 | assert.isTrue(listenerHasBeenCalled) 51 | }) 52 | }) 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://supportukrainenow.org) 3 | 4 | # emits-change 5 | 6 | [![Latest Version on NPM](https://img.shields.io/npm/v/emits-change.svg?style=flat-square)](https://npmjs.com/package/emits-change) 7 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 8 | [![Build Status](https://img.shields.io/travis/spatie/emits-change.svg?style=flat-square)](https://travis-ci.org/spatie/emits-change) 9 | [![Code Climate](https://img.shields.io/codeclimate/github/spatie/emits-change.svg?style=flat-square)](https://img.shields.io/codeclimate/github/spatie/emits-change.svg) 10 | 11 | Plug-and-play node.js events integration to emit change events. 12 | 13 | ## Support us 14 | 15 | [](https://spatie.be/github-ad-click/emits-change) 16 | 17 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 18 | 19 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 20 | 21 | ## Installation 22 | 23 | ```bash 24 | npm install emits-change 25 | ``` 26 | 27 | This package uses the [node.js events API](https://nodejs.org/api/events.html). Make sure your javascript bundler can handles importing core node modules (e.g. Browserify does this). 28 | 29 | ## Postcardware 30 | 31 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. 32 | 33 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 34 | 35 | The best postcards will get published on the open source page on our website. 36 | 37 | ## Usage 38 | 39 | Calling `emitsChange` on an object will add node's EventEmitter, a `listen`, an `unlisten` and an `emitChange` function to the prototype. 40 | 41 | ```es6 42 | import emitsChange from 'emits-change' 43 | 44 | class Emitter { 45 | constructor() { 46 | emitsChange(this) 47 | } 48 | } 49 | 50 | let emitter = new Emitter 51 | 52 | function doSomethingOnChange() { 53 | console.log('hodor') 54 | } 55 | 56 | emitter.listen(doSomethingOnChange) 57 | 58 | emitter.emitChange() 59 | // => hodor 60 | 61 | emitter.unlisten(doSomethingOnChange) 62 | 63 | emitter.emitChange() 64 | // => *nothing* 65 | ``` 66 | 67 | ## Testing 68 | 69 | You can run the tests (ESLint & Mocha) with: 70 | 71 | ```bash 72 | npm run test 73 | ``` 74 | 75 | ## Contributing 76 | 77 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 78 | 79 | ## Security 80 | 81 | If you discover any security related issues, please email [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 82 | 83 | ## Credits 84 | 85 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne) 86 | - [All Contributors](../../contributors) 87 | 88 | ## About Spatie 89 | 90 | Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 91 | 92 | ## License 93 | 94 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 95 | --------------------------------------------------------------------------------