Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kucukkanat committed Apr 18, 2020
1 parent 46e34fd commit 9bd8f35
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
118 changes: 115 additions & 3 deletions README.md
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/)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@hackdonalds/emitter",
"description": "A simple better event emitter",
"keywords": ["event", "emitter", "isomorphic", "events", "nodejs", "Javascript", "mitt", "eventemitter"],
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type EventHandlerList = Array<EventHandler>;
type WildCardEventHandlerList = Array<WildCardEventHandler>;
// A map of event types and their corresponding event handlers.
type EventHandlerMap = {
'*'?: WildCardEventHandlerList,
[type: string]: EventHandlerList,
'*'?: WildCardEventHandlerList,
[type: string]: EventHandlerList,
};

export default class Emitter {
Expand Down Expand Up @@ -50,4 +50,8 @@ export default class Emitter {
(this.listeners[type] || []).slice().map((handler) => { handler(evt); });
(this.listeners['*'] || []).slice().map((handler) => { handler(type, evt); });
}
}

if (typeof window !== "undefined") {
(window as any).HackDonalds = { Emitter }
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"module": "umd", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down Expand Up @@ -39,7 +39,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand Down

0 comments on commit 9bd8f35

Please sign in to comment.