@hackdonalds/emitter
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,44 +0,57 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Emitter { | ||
constructor(listeners = {}) { | ||
this.listeners = listeners; | ||
this.listeners = listeners; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
/** | ||
* Register an event handler for the given type. | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Emitter { | ||
constructor(listeners = {}) { | ||
this.listeners = listeners; | ||
this.listeners = listeners; | ||
} | ||
/** | ||
* Register an event handler for the given type. | ||
* | ||
* @param {String} type Type of event to listen for, or `"*"` for all events | ||
* @param {Function} handler Function to call in response to given event | ||
*/ | ||
on(type, handler) { | ||
this.listeners[type] = this.listeners[type] || []; | ||
this.listeners[type].push(handler); | ||
} | ||
/** | ||
* Remove an event handler for the given type. | ||
* | ||
* @param {String} type Type of event to listen for, or `"*"` for all events | ||
* @param {Function} handler Function to call in response to given event | ||
* @param {String} type Type of event to unregister `handler` from, or `"*"` | ||
* @param {Function} handler Handler function to remove | ||
*/ | ||
on(type, handler) { | ||
this.listeners[type] = this.listeners[type] || []; | ||
this.listeners[type].push(handler); | ||
} | ||
/** | ||
* Remove an event handler for the given type. | ||
* | ||
* @param {String} type Type of event to unregister `handler` from, or `"*"` | ||
* @param {Function} handler Handler function to remove | ||
*/ | ||
off(type, handler) { | ||
if (this.listeners[type]) { | ||
this.listeners[type].splice(this.listeners[type].indexOf(handler) >>> 0, 1); | ||
off(type, handler) { | ||
if (this.listeners[type]) { | ||
this.listeners[type].splice(this.listeners[type].indexOf(handler) >>> 0, 1); | ||
} | ||
} | ||
/** | ||
* Invoke this.listeners handlers for the given type. | ||
* If present, `"*"` handlers are invoked after type-matched handlers. | ||
* | ||
* Note: Manually firing "*" handlers is not supported. | ||
* | ||
* @param {String} type The event type to invoke | ||
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler | ||
*/ | ||
emit(type, evt) { | ||
(this.listeners[type] || []).slice().map((handler) => { handler(evt); }); | ||
(this.listeners['*'] || []).slice().map((handler) => { handler(type, evt); }); | ||
} | ||
} | ||
/** | ||
* Invoke this.listeners handlers for the given type. | ||
* If present, `"*"` handlers are invoked after type-matched handlers. | ||
* | ||
* Note: Manually firing "*" handlers is not supported. | ||
* | ||
* @param {String} type The event type to invoke | ||
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler | ||
*/ | ||
emit(type, evt) { | ||
(this.listeners[type] || []).slice().map((handler) => { handler(evt); }); | ||
(this.listeners['*'] || []).slice().map((handler) => { handler(type, evt); }); | ||
exports.default = Emitter; | ||
if (typeof window !== "undefined") { | ||
window.HackDonalds = { Emitter }; | ||
} | ||
} | ||
exports.default = Emitter; | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -5,5 +5,4 @@ { | ||
"keywords": ["event", "emitter", "isomorphic", "events", "nodejs", "Javascript", "mitt", "eventemitter"], | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "dist/index.js", | ||
"files": ["dist","README.md","src"], | ||
@@ -13,3 +12,3 @@ "scripts": { | ||
}, | ||
"repository": "https://github.com/kucukkanat/emitter.git", | ||
"repository": "https://github.com/hackdonalds/emitter.git", | ||
"license": "MIT", | ||
@@ -16,0 +15,0 @@ "devDependencies": { |
118
README.md
@@ -1,4 +0,116 @@ | ||
# emitter | ||
Event Emitter | ||
# HackDonald's Emitter | ||
A better event emitter than `mitt` that you can extend your classes with. | ||
## Table of Contents | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Examples & Demos](#examples--demos) | ||
- [API](#api) | ||
- [Contribute](#contribute) | ||
- [License](#license) | ||
## Install | ||
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed. | ||
```sh | ||
$ npm install --save @hackdonalds/emitter | ||
``` | ||
```javascript | ||
// using ES6 modules | ||
import Emitter from '@hackdonalds/emitter' | ||
// using CommonJS modules | ||
var Emitter = require('@hackdonals/emitter').default | ||
``` | ||
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com/@hackdonalds/emitter@0.2.0/dist/index.js): | ||
```html | ||
<script src="https://unpkg.com/@hackdonalds/emitter@0.2.0/dist/index.js"></script> | ||
``` | ||
You can find the library on `window.HackDonalds.Emitter`. | ||
## Usage | ||
```js | ||
import Emitter from '@hackdonalds/emitter' | ||
// OR | ||
const Emitter = require('@hackdonalds/emitter').default | ||
const emitter = new Emitter() | ||
// listen to an event | ||
emitter.on('foo', e => console.log('foo', e) ) | ||
// listen to all events | ||
emitter.on('*', (type, e) => console.log(type, e) ) | ||
// fire an event | ||
emitter.emit('foo', { a: 'b' }) | ||
// working with handler references: | ||
function onFoo() {} | ||
emitter.on('foo', onFoo) // listen | ||
emitter.off('foo', onFoo) // unlisten | ||
``` | ||
* * * | ||
## API | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
### Emitter | ||
**Parameters** | ||
- `all` **EventHandlerMap** | ||
Returns **Mitt** | ||
### on | ||
Register an event handler for the given type. | ||
**Parameters** | ||
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events | ||
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event | ||
### off | ||
Remove an event handler for the given type. | ||
**Parameters** | ||
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"` | ||
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove | ||
### emit | ||
Invoke all handlers for the given type. | ||
If present, `"*"` handlers are invoked after type-matched handlers. | ||
_Note: Manually firing "*" handlers is not supported._ | ||
**Parameters** | ||
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke | ||
- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler | ||
### Reporting Issues | ||
Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues). | ||
If don't, just open a [new clear and descriptive issue](../../issues/new). | ||
## License | ||
[MIT License](https://opensource.org/licenses/MIT) © [Hilmi Tolga SAHIN](https://kucukkanat.com/) |
@@ -9,4 +9,4 @@ type EventHandler = (event?: any) => void; | ||
type EventHandlerMap = { | ||
'*'?: WildCardEventHandlerList, | ||
[type: string]: EventHandlerList, | ||
'*'?: WildCardEventHandlerList, | ||
[type: string]: EventHandlerList, | ||
}; | ||
@@ -54,2 +54,6 @@ | ||
} | ||
} | ||
if (typeof window !== "undefined") { | ||
(window as any).HackDonalds = { Emitter } | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
11193
144
116