This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46e34fd
commit 9bd8f35
Showing
4 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,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/[email protected]/dist/index.js): | ||
|
||
```html | ||
<script src="https://unpkg.com/@hackdonalds/[email protected]/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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters