Skip to content

Commit

Permalink
Minimize code for browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Apr 10, 2019
1 parent 548c3ee commit 3445479
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 50 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

---

## [v1.1.1](https://github.com/foxifyjs/events/releases/tag/v1.1.1) - _(2019-04-10)_

- :star2: Minimize code for browsers.

## [v1.1.0](https://github.com/foxifyjs/events/releases/tag/v1.1.0) - _(2019-04-04)_

- :star2: Now we `throw` an error when you emit an `error` event and nobody is listening.
- :zap: Now we `throw` an error when you emit an `error` event and nobody is listening.

## [v1.0.0](https://github.com/foxifyjs/events/releases/tag/v1.0.0) - _(2019-03-24)_

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ We use [SemVer](http://semver.org) for versioning. For the versions available, s

## Authors

- **Ardalan Amini** - *Owner/Developer* - [@ardalanamini](https://github.com/ardalanamini)
- **Ardalan Amini** - *Core Maintainer* - [@ardalanamini](https://github.com/ardalanamini)

See also the list of [contributors](https://github.com/foxifyjs/events/contributors) who participated in this project.

Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/events",
"version": "1.1.0",
"version": "1.1.1",
"description": "An EventEmitter alternative for Node.js and browser",
"author": "Ardalan Amini <ardalanamini22@gmail.com> (https://ardalanamini.com)",
"license": "MIT",
Expand Down Expand Up @@ -38,22 +38,22 @@
"test": "jest",
"coverage": "npm test -- --coverage",
"codecov": "npm run build && npm run coverage -- --runInBand && codecov",
"benchmark": "find benchmarks -name '*.js' -exec benchmarks/start.sh {} \\;"
"benchmarks": "find benchmarks -name '*.js' -exec benchmarks/start.sh {} \\;"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/node": "^11.13.2",
"benchmark": "^2.1.4",
"codecov": "^3.3.0",
"drip": "^1.4.0",
"eventemitter2": "^5.0.1",
"eventemitter3": "^3.1.0",
"jest": "^24.7.1",
"ts-jest": "^24.0.1",
"ts-jest": "^24.0.2",
"tslint": "^5.15.0",
"tslint-config-airbnb": "^5.11.1",
"typescript": "^3.4.1"
"typescript": "^3.4.3"
},
"jest": {
"moduleFileExtensions": [
Expand Down
46 changes: 16 additions & 30 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,20 @@ class EventEmitter {
);
}

public listenerCount(event: string | symbol) {
public rawListeners(event: string | symbol) {
assertEvent(event);

const listeners = this._listeners[event as string];

return listeners ? listeners.length : 0;
return this._listeners[event as string] || [];
}

public listeners(event: string | symbol) {
assertEvent(event);

const listeners = this._listeners[event as string];
const listeners = this.rawListeners(event);
const length = listeners.length;

if (!listeners) return [];
if (!length) return [];

const length = listeners.length;
const ret = [];
// tslint:disable-next-line:prefer-array-literal
const ret = new Array(length);

for (let i = 0; i < length; i++) {
ret[i] = listeners[i].listener;
Expand All @@ -89,25 +86,14 @@ class EventEmitter {
return ret;
}

public rawListeners(event: string | symbol) {
assertEvent(event);

return this._listeners[event as string] || [];
public listenerCount(event: string | symbol) {
return this.rawListeners(event).length;
}

public emit(event: "error", error: Error): boolean;
public emit(event: string | symbol, ...args: any[]): boolean;
public emit(event: string | symbol, ...args: any[]) {
assertEvent(event);

const listeners = this._listeners[event as string];

if (!listeners) {
if (event === "error") throw args[0];

return false;
}

const listeners = this.rawListeners(event);
const length = listeners.length;

if (!length) {
Expand Down Expand Up @@ -184,24 +170,24 @@ class EventEmitter {
event: string | symbol,
listener: (...args: any[]) => void,
) {
assertEvent(event);
assertListener(listener);

let listeners = this._listeners[event as string];
let listeners = this.rawListeners(event);
const length = listeners.length;

if (!listeners) return this;
if (!length) return this;

if (listeners.length === 1) {
if (length === 1) {
if (listener !== listeners[0].listener) return this;

this._listeners[event as string] = undefined;

return this;
}

listeners = [...listeners];
listeners = listeners.slice(0);

let index = listeners.length - 1;
let index = length - 1;
for (; index >= 0; index--) {
if (listeners[index].listener === listener) break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Item.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Item {
constructor(
public listener: (...args: any[]) => void,
public context: any,
public once: boolean,
public readonly listener: (...args: any[]) => void,
public readonly context: any,
public readonly once: boolean,
) {}
}

Expand Down

0 comments on commit 3445479

Please sign in to comment.