nanoevents
Advanced tools
Comparing version 4.0.3 to 5.0.0
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 5.0 | ||
* Use named export instead of default export. | ||
## 4.0.3 | ||
@@ -5,0 +8,0 @@ * Add `this` type definition (by Anton Khlynovskiy). |
107
index.d.ts
@@ -1,54 +0,56 @@ | ||
interface EventsMap { | ||
[event: string]: any | ||
} | ||
export namespace createNanoEvents { | ||
interface EventsMap { | ||
[event: string]: any | ||
} | ||
interface DefaultEvents extends EventsMap { | ||
[event: string]: (...args: any) => void | ||
} | ||
interface DefaultEvents extends EventsMap { | ||
[event: string]: (...args: any) => void | ||
} | ||
declare class Emitter<Events extends EventsMap> { | ||
/** | ||
* Event names in keys and arrays with listeners in values. | ||
* | ||
* ```js | ||
* emitter1.events = emitter2.events | ||
* emitter2.events = { } | ||
* ``` | ||
*/ | ||
events: Partial<{ [E in keyof Events]: Events[E][] }> | ||
class Emitter<Events extends EventsMap> { | ||
/** | ||
* Event names in keys and arrays with listeners in values. | ||
* | ||
* ```js | ||
* emitter1.events = emitter2.events | ||
* emitter2.events = { } | ||
* ``` | ||
*/ | ||
events: Partial<{ [E in keyof Events]: Events[E][] }> | ||
/** | ||
* Add a listener for a given event. | ||
* | ||
* ```js | ||
* const unbind = ee.on('tick', (tickType, tickDuration) => { | ||
* count += 1 | ||
* }) | ||
* | ||
* disable () { | ||
* unbind() | ||
* } | ||
* ``` | ||
* | ||
* @param event The event name. | ||
* @param cb The listener function. | ||
* @returns Unbind listener from event. | ||
*/ | ||
on <K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void | ||
/** | ||
* Add a listener for a given event. | ||
* | ||
* ```js | ||
* const unbind = ee.on('tick', (tickType, tickDuration) => { | ||
* count += 1 | ||
* }) | ||
* | ||
* disable () { | ||
* unbind() | ||
* } | ||
* ``` | ||
* | ||
* @param event The event name. | ||
* @param cb The listener function. | ||
* @returns Unbind listener from event. | ||
*/ | ||
on <K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void | ||
/** | ||
* Calls each of the listeners registered for a given event. | ||
* | ||
* ```js | ||
* ee.emit('tick', tickType, tickDuration) | ||
* ``` | ||
* | ||
* @param event The event name. | ||
* @param args The arguments for listeners. | ||
*/ | ||
emit <K extends keyof Events>( | ||
this: this, | ||
event: K, | ||
...args: Parameters<Events[K]> | ||
): void | ||
/** | ||
* Calls each of the listeners registered for a given event. | ||
* | ||
* ```js | ||
* ee.emit('tick', tickType, tickDuration) | ||
* ``` | ||
* | ||
* @param event The event name. | ||
* @param args The arguments for listeners. | ||
*/ | ||
emit <K extends keyof Events>( | ||
this: this, | ||
event: K, | ||
...args: Parameters<Events[K]> | ||
): void | ||
} | ||
} | ||
@@ -75,5 +77,4 @@ | ||
*/ | ||
declare function createNanoEvents<Events extends EventsMap = DefaultEvents> ( | ||
): Emitter<Events> | ||
export = createNanoEvents | ||
export function createNanoEvents< | ||
Events extends createNanoEvents.EventsMap = createNanoEvents.DefaultEvents | ||
> (): createNanoEvents.Emitter<Events> |
@@ -1,2 +0,2 @@ | ||
export default () => ({ | ||
let createNanoEvents = () => ({ | ||
events: { }, | ||
@@ -15,1 +15,3 @@ emit (event, ...args) { | ||
}) | ||
export { createNanoEvents } |
{ | ||
"name": "nanoevents", | ||
"version": "4.0.3", | ||
"description": "Simple and tiny (75 bytes) event emitter library", | ||
"version": "5.0.0", | ||
"description": "Simple and tiny (72 bytes) event emitter library", | ||
"keywords": [ | ||
@@ -25,3 +25,9 @@ "EventEmitter", | ||
"main": "index.cjs", | ||
"module": "index.js" | ||
"module": "index.js", | ||
"exports": { | ||
".": { | ||
"require": "./index.cjs", | ||
"import": "./index.js" | ||
} | ||
} | ||
} |
@@ -5,3 +5,3 @@ # Nano Events | ||
* Only **75 bytes** (minified and gzipped). | ||
* Only **72 bytes** (minified and gzipped). | ||
It uses [Size Limit] to control size. | ||
@@ -15,3 +15,3 @@ * `on` method returns `unbind` function. You don’t need to save | ||
```js | ||
const createNanoEvents = require('nanoevents') | ||
import { createNanoEvents } from 'nanoevents' | ||
@@ -44,3 +44,2 @@ const emitter = createNanoEvents() | ||
* [TypeScript](#typescript) | ||
* [ES Modules](#es-modules) | ||
* [Mixing to Object](#mixing-to-object) | ||
@@ -61,7 +60,5 @@ * [Add Listener](#add-listener) | ||
```ts | ||
import createNanoEvents = require('nanoevents') | ||
interface Events { | ||
'set': (name: string, count: number) => void, | ||
'tick': () => void | ||
set: (name: string, count: number) => void, | ||
tick: () => void | ||
} | ||
@@ -81,33 +78,24 @@ | ||
## ES Modules | ||
In Node.js 13 you can import ES module by manually added `index.mjs`. | ||
```js | ||
import createNanoEvents from 'nanoevents/index.js' | ||
``` | ||
For quick hacks you can load Nano Events from CDN. Do not use it in production | ||
because of low performance. | ||
```js | ||
import createNanoEvents from 'https://cdn.jsdelivr.net/npm/nanoevents/index.js' | ||
``` | ||
## Mixing to Object | ||
Because Nano Events API has only just 2 methods, | ||
you could just create proxy methods in your class. | ||
you could just create proxy methods in your class | ||
or encapsulate them entirely. | ||
```js | ||
class Ticker { | ||
constructor() { | ||
constructor () { | ||
this.emitter = createNanoEvents() | ||
this.internal = setInterval(() => { | ||
this.emitter.emit('tick') | ||
}, 100) | ||
} | ||
on() { | ||
return this.emitter.on.apply(this.emitter, arguments) | ||
stop () { | ||
clearInterval(this.internal) | ||
this.emitter.emit('stop') | ||
} | ||
tick() { | ||
this.emitter.emit('tick') | ||
on (event, callback) { | ||
return this.emitter.on(event, callback) | ||
} | ||
@@ -140,3 +128,3 @@ } | ||
userId: 1, | ||
getListener() { | ||
getListener () { | ||
return () => { | ||
@@ -204,3 +192,3 @@ console.log(this.userId) | ||
class Ticker { | ||
constructor() { | ||
constructor () { | ||
this.emitter = createNanoEvents() | ||
@@ -210,5 +198,5 @@ } | ||
once (event, callback) { | ||
const unbind = this.emitter.on(event, function () { | ||
const unbind = this.emitter.on(event, (...args) => { | ||
unbind() | ||
callback.apply(this, arguments) | ||
callback(...args) | ||
}) | ||
@@ -215,0 +203,0 @@ return unbind |
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
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
9645
104
209