@climba03003/event-emitter
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -1,29 +0,40 @@ | ||
declare class Listener { | ||
declare const MODE_ONCE: unique symbol; | ||
declare const MODE_ALWAYS: unique symbol; | ||
export declare type ListenerMode = typeof MODE_ONCE | typeof MODE_ALWAYS; | ||
export declare class Listener { | ||
executed: boolean; | ||
listener: Function; | ||
mode: symbol; | ||
executed: boolean; | ||
constructor(listener: Function, mode: symbol); | ||
execute(...args: any): Promise<any>; | ||
mode: ListenerMode; | ||
constructor(listener: Function, mode: ListenerMode); | ||
execute(...args: any[]): Promise<void>; | ||
} | ||
declare const kDefaultMaxListeners: unique symbol; | ||
declare const kCheckMaxListenter: unique symbol; | ||
declare const kFindEventStack: unique symbol; | ||
export declare type EventName = string | symbol; | ||
export declare class EventEmitter { | ||
private events; | ||
private defaultMaxListeners; | ||
addListener(eventName: string | symbol, listener: Function): this; | ||
emit(eventName: string | symbol, ...args: any): Promise<boolean>; | ||
eventNames(): string[]; | ||
private static [kDefaultMaxListeners]; | ||
static get defaultMaxListeners(): number; | ||
static set defaultMaxListeners(n: number); | ||
private readonly events; | ||
private maxListeners; | ||
constructor(); | ||
addListener(eventName: EventName, listener: Function): this; | ||
emit(eventName: EventName, ...args: any[]): Promise<boolean>; | ||
eventNames(): EventName[]; | ||
getMaxListeners(): number; | ||
listenerCount(eventName: string | symbol): number; | ||
listeners(eventName: string | symbol): Listener[]; | ||
off(eventName: string | symbol, listener: Function): this; | ||
on(eventName: string | symbol, listener: Function): this; | ||
once(eventName: string | symbol, listener: Function): this; | ||
listenerCount(eventName: EventName): number; | ||
listeners(eventName: EventName): Listener[]; | ||
off(eventName: EventName, listener: Function): this; | ||
on(eventName: EventName, listener: Function): this; | ||
once(eventName: EventName, listener: Function): this; | ||
prependListener(eventName: string | symbol, listener: Function): this; | ||
prependOnceListener(eventName: string | symbol, listener: Function): this; | ||
removeAllListeners(eventName: string | symbol): this; | ||
removeListener(eventName: string | symbol, listener: Function): this; | ||
removeListener(eventName: EventName, listener: Function): this; | ||
setMaxListeners(n: number): void; | ||
rawListeners(eventName: string | symbol): Function[]; | ||
private ensureStack; | ||
private indexOfListener; | ||
rawListeners(eventName: EventName): Function[]; | ||
private [kCheckMaxListenter]; | ||
private [kFindEventStack]; | ||
} | ||
export default EventEmitter; |
144
lib/index.js
@@ -1,3 +0,21 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
@@ -10,4 +28,5 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
} | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
@@ -38,11 +57,4 @@ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
} | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
} | ||
var MODE_ONCE = Symbol.for('[[Once Event Listener]]'); | ||
@@ -65,7 +77,11 @@ var MODE_ALWAYS = Symbol.for('[[Always Event Listener]]'); | ||
case 0: | ||
if (this.mode === MODE_ONCE && this.executed) | ||
return [2, true]; | ||
if (!(this.mode === MODE_ONCE && this.executed)) return [3, 1]; | ||
return [2, undefined]; | ||
case 1: | ||
this.executed = true; | ||
return [4, this.listener.apply(this.listener, args)]; | ||
case 1: return [2, _a.sent()]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: return [2]; | ||
} | ||
@@ -77,12 +93,27 @@ }); | ||
}()); | ||
var kDefaultMaxListeners = Symbol.for('ee.defaultMaxListeners'); | ||
var kCheckMaxListenter = Symbol.for('ee.checkMaxListenter'); | ||
var kFindEventStack = Symbol.for('ee.findEventStack'); | ||
var EventEmitter = (function () { | ||
function EventEmitter() { | ||
this.events = {}; | ||
this.defaultMaxListeners = 10; | ||
this.events = new Map(); | ||
this.maxListeners = EventEmitter.defaultMaxListeners; | ||
} | ||
Object.defineProperty(EventEmitter, "defaultMaxListeners", { | ||
get: function () { | ||
return EventEmitter[kDefaultMaxListeners]; | ||
}, | ||
set: function (n) { | ||
if (isNaN(n)) | ||
throw new Error('MaxListerners must be a number.'); | ||
if (!isNaN(n) && n < 0) { | ||
throw new RangeError('MaxListerners must be a positive number.'); | ||
} | ||
EventEmitter[kDefaultMaxListeners] = n; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
EventEmitter.prototype.addListener = function (eventName, listener) { | ||
if (this.ensureStack.length > this.defaultMaxListeners) | ||
throw new Error('Maximum Number of Event Listener exceed.'); | ||
this.ensureStack(eventName).push(new Listener(listener, MODE_ALWAYS)); | ||
return this; | ||
return this.on(eventName, listener); | ||
}; | ||
@@ -96,7 +127,6 @@ EventEmitter.prototype.emit = function (eventName) { | ||
var stack, i, listener; | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
stack = this.ensureStack(eventName); | ||
stack = this[kFindEventStack](eventName); | ||
i = 0; | ||
@@ -107,3 +137,3 @@ _b.label = 1; | ||
listener = stack[i]; | ||
return [4, (_a = listener.execute).call.apply(_a, __spreadArrays([listener], args))]; | ||
return [4, listener.execute.apply(listener, args)]; | ||
case 2: | ||
@@ -121,12 +151,12 @@ _b.sent(); | ||
EventEmitter.prototype.eventNames = function () { | ||
return Object.keys(this.events); | ||
return Array.from(this.events.keys()); | ||
}; | ||
EventEmitter.prototype.getMaxListeners = function () { | ||
return this.defaultMaxListeners; | ||
return this.maxListeners; | ||
}; | ||
EventEmitter.prototype.listenerCount = function (eventName) { | ||
return this.ensureStack(eventName).length; | ||
return this[kFindEventStack](eventName).length; | ||
}; | ||
EventEmitter.prototype.listeners = function (eventName) { | ||
return this.ensureStack(eventName); | ||
return this[kFindEventStack](eventName); | ||
}; | ||
@@ -137,25 +167,31 @@ EventEmitter.prototype.off = function (eventName, listener) { | ||
EventEmitter.prototype.on = function (eventName, listener) { | ||
return this.addListener(eventName, listener); | ||
this[kFindEventStack](eventName).push(new Listener(listener, MODE_ALWAYS)); | ||
this[kCheckMaxListenter](eventName); | ||
return this; | ||
}; | ||
EventEmitter.prototype.once = function (eventName, listener) { | ||
this.ensureStack(eventName).push(new Listener(listener, MODE_ONCE)); | ||
this[kFindEventStack](eventName).push(new Listener(listener, MODE_ONCE)); | ||
this[kCheckMaxListenter](eventName); | ||
return this; | ||
}; | ||
EventEmitter.prototype.prependListener = function (eventName, listener) { | ||
this.ensureStack(eventName).unshift(new Listener(listener, MODE_ALWAYS)); | ||
this[kFindEventStack](eventName).unshift(new Listener(listener, MODE_ALWAYS)); | ||
this[kCheckMaxListenter](eventName); | ||
return this; | ||
}; | ||
EventEmitter.prototype.prependOnceListener = function (eventName, listener) { | ||
this.ensureStack(eventName).unshift(new Listener(listener, MODE_ONCE)); | ||
this[kFindEventStack](eventName).unshift(new Listener(listener, MODE_ONCE)); | ||
this[kCheckMaxListenter](eventName); | ||
return this; | ||
}; | ||
EventEmitter.prototype.removeAllListeners = function (eventName) { | ||
delete this.events[eventName]; | ||
this.events.delete(eventName); | ||
return this; | ||
}; | ||
EventEmitter.prototype.removeListener = function (eventName, listener) { | ||
var stack = this.ensureStack(eventName); | ||
var index = this.indexOfListener(stack, listener); | ||
while (index >= 0) { | ||
index = this.indexOfListener(stack, listener); | ||
var stack = this[kFindEventStack](eventName); | ||
var index = stack.findIndex(function (l) { | ||
return l.listener === listener; | ||
}); | ||
if (index !== -1) { | ||
stack.splice(index, 1); | ||
@@ -168,23 +204,31 @@ } | ||
throw new Error('MaxListerners must be a number.'); | ||
this.defaultMaxListeners = parseInt(String(n), 10); | ||
if (!isNaN(n) && n < 0) { | ||
throw new RangeError('MaxListerners must be a positive number.'); | ||
} | ||
this.maxListeners = n; | ||
}; | ||
EventEmitter.prototype.rawListeners = function (eventName) { | ||
return this.ensureStack(eventName).map(function (listener) { | ||
return listener.listener; | ||
return this[kFindEventStack](eventName).map(function (l) { | ||
return l.listener; | ||
}); | ||
}; | ||
EventEmitter.prototype.ensureStack = function (eventName) { | ||
if (!Array.isArray(this.events[eventName])) | ||
this.events[eventName] = []; | ||
return this.events[eventName]; | ||
EventEmitter.prototype[(_a = kDefaultMaxListeners, kCheckMaxListenter)] = function (eventName) { | ||
if (this.listenerCount(eventName) > this.maxListeners) { | ||
process.emitWarning('', 'MaxListenersExceededWarning'); | ||
} | ||
}; | ||
EventEmitter.prototype.indexOfListener = function (stack, listener) { | ||
return stack.findIndex(function (stackListener, index, self) { | ||
return stackListener.listener === listener; | ||
}); | ||
EventEmitter.prototype[kFindEventStack] = function (eventName) { | ||
if (!this.events.has(eventName)) { | ||
this.events.set(eventName, []); | ||
} | ||
return this.events.get(eventName); | ||
}; | ||
var _a; | ||
EventEmitter[_a] = 10; | ||
return EventEmitter; | ||
}()); | ||
exports.EventEmitter = EventEmitter; | ||
exports.Listener = Listener; | ||
exports.default = EventEmitter; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@climba03003/event-emitter", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "NodeJS EventEmitter which ensure all the event listener run sequentially. The API is follow the NodeJS API Documents.", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./lib/mjs/index.js", | ||
"require": "./lib/index.js" | ||
} | ||
}, | ||
"directories": { | ||
"lib": "lib", | ||
"test": "test" | ||
"lib": "lib" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.12.0", | ||
"@typescript-eslint/eslint-plugin": "^2.8.0", | ||
"@typescript-eslint/parser": "^2.8.0", | ||
"coveralls": "^3.0.8", | ||
"eslint": "^6.6.0", | ||
"eslint-config-prettier": "^6.7.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^6.2.2", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"prettier": "^1.19.1", | ||
"should": "^13.2.3", | ||
"typescript": "^3.6.4" | ||
}, | ||
"scripts": { | ||
"preeslint": "npm install -D eslint prettier eslint-config-prettier eslint-plugin-prettier @typescript-eslint/parser @typescript-eslint/eslint-plugin", | ||
"eslint": "eslint --fix --ext .ts lib test", | ||
"prebuild": "npm install", | ||
"build": "npx tsc", | ||
"pretest": "npm run build", | ||
"test": "npx istanbul cover _mocha -- --recursive", | ||
"precoveralls": "npm run build", | ||
"coveralls": "npx istanbul cover _mocha --report lcovonly -- --recursive -R spec && cat ./coverage/lcov.info | npx coveralls && rm -rf ./coverage" | ||
"clean": "npx rimraf \"lib/**/*.js\" \"lib/**/*.map\" \"lib/**/*.d.ts\" \"test/**/*.js\" \"test/**/*.map\" \"test/**/*.d.ts\"", | ||
"lint": "eslint --ext .ts lib test", | ||
"lint:fix": "npm run lint -- --fix", | ||
"build": "rollup -c", | ||
"build:tsc": "tsc -b", | ||
"unit": "jest", | ||
"test": "npm run lint && npm run unit", | ||
"coverage": "jest --coverage", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
@@ -46,3 +36,22 @@ "repository": { | ||
"author": "KaKa", | ||
"license": "GPL-3.0" | ||
"license": "GPL-3.0", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^8.2.1", | ||
"@types/bcrypt": "^3.0.0", | ||
"@types/jest": "^26.0.23", | ||
"@typescript-eslint/eslint-plugin": "4", | ||
"eslint": "7", | ||
"eslint-config-standard-with-typescript": "^20.0.0", | ||
"eslint-plugin-import": "2", | ||
"eslint-plugin-node": "11", | ||
"eslint-plugin-promise": "5", | ||
"eslint-plugin-standard": "5", | ||
"husky": "^6.0.0", | ||
"jest": "^27.0.1", | ||
"prettier": "^2.3.0", | ||
"rollup": "^2.50.3", | ||
"ts-jest": "^27.0.1", | ||
"typescript": "^4.3.2" | ||
} | ||
} |
# EventEmitter | ||
[![Build Status](https://travis-ci.com/climba03003/EventEmitter.svg?branch=master)](https://travis-ci.com/climba03003/Validator) | ||
[![GitHub package.json version](https://img.shields.io/github/package-json/v/climba03003/EventEmitter)](https://github.com/climba03003/EventEmitter) | ||
[![Coverage Status](https://coveralls.io/repos/github/climba03003/EventEmitter/badge.svg?branch=master)](https://coveralls.io/github/climba03003/EventEmitter?branch=master) | ||
[![GitHub](https://img.shields.io/github/license/climba03003/EventEmitter)](https://github.com/climba03003/EventEmitter) | ||
[![Continuous Integration](https://github.com/climba03003/eventemitter/actions/workflows/ci.yml/badge.svg)](https://github.com/climba03003/eventemitter/actions/workflows/ci.yml) | ||
[![Package Manager CI](https://github.com/climba03003/eventemitter/actions/workflows/package-manager-ci.yml/badge.svg)](https://github.com/climba03003/eventemitter/actions/workflows/package-manager-ci.yml) | ||
[![NPM version](https://img.shields.io/npm/v/@climba03003/eventemitter.svg?style=flat)](https://www.npmjs.com/package/@climba03003/eventemitter) | ||
[![GitHub package.json version](https://img.shields.io/github/package-json/v/climba03003/eventemitter)](https://github.com/climba03003/eventemitter) | ||
[![Coverage Status](https://coveralls.io/repos/github/climba03003/eventemitter/badge.svg?branch=main)](https://coveralls.io/github/climba03003/eventemitter?branch=master) | ||
[![GitHub](https://img.shields.io/github/license/climba03003/eventemitter)](https://github.com/climba03003/eventemitter) | ||
@@ -20,3 +22,3 @@ NodeJS EventEmitter which ensure all the event listener run sequentially. | ||
```javascript | ||
const EventEmitter = require('@climba03003/event-emitter').EventEmitter; | ||
const EventEmitter = require("@climba03003/event-emitter").EventEmitter; | ||
@@ -27,5 +29,5 @@ class Foo extends EventEmitter {} | ||
```typescript | ||
import EventEmitter from '@climba03003/event-emitter'; | ||
import EventEmitter from "@climba03003/event-emitter"; | ||
class Foo extends EventEmitter {} | ||
``` |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
100773
32
16
11
549
1