Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 12, 2019
1 parent 3877264 commit a0a4c6e
Show file tree
Hide file tree
Showing 24 changed files with 472 additions and 614 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,3 @@ node_modules
yarn.lock
.nyc_output
coverage
/legacy.js
7 changes: 0 additions & 7 deletions .travis.yml
Expand Up @@ -3,11 +3,4 @@ node_js:
- '12'
- '10'
- '8'
- '6'
cache:
directories:
- $HOME/.npm
before_install:
- npm install --global npm@6.1.0
- npm --version
after_success: npx codecov --file=./coverage/lcov.info
4 changes: 2 additions & 2 deletions Emittery.d.ts β†’ index.d.ts
@@ -1,5 +1,3 @@
export = Emittery;

declare class Emittery {
/**
* Subscribe to an event.
Expand Down Expand Up @@ -129,3 +127,5 @@ declare namespace Emittery {
emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
}
}

export = Emittery;
39 changes: 39 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,39 @@
import {expectType, expectError} from 'tsd';
import Emittery = require('.');

// emit
{
const ee = new Emittery();
ee.emit('anEvent');
ee.emit('anEvent', 'some data');
}

// on
{
const ee = new Emittery();
ee.on('anEvent', () => undefined);
ee.on('anEvent', () => Promise.resolve());
ee.on('anEvent', data => undefined);
ee.on('anEvent', data => Promise.resolve());
const off = ee.on('anEvent', () => undefined);
off();
}

// off
{
const ee = new Emittery();
ee.off('anEvent', () => undefined);
ee.off('anEvent', () => Promise.resolve());
ee.off('anEvent', data => undefined);
ee.off('anEvent', data => Promise.resolve());
}

{
const ee = new Emittery();
expectError(ee.emit('anEvent', 'some data', 'and more'));
}

{
const ee = new Emittery();
expectError(ee.on('anEvent', (data: any, more: any) => undefined));
}
2 changes: 0 additions & 2 deletions legacy.d.ts

This file was deleted.

33 changes: 12 additions & 21 deletions package.json
Expand Up @@ -10,19 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"build": "babel --out-file=legacy.js index.js",
"build:watch": "npm run build -- --watch",
"prepare": "npm run build",
"test": "xo && tsc --noEmit && nyc ava"
"test": "xo && nyc ava && tsd"
},
"files": [
"Emittery.d.ts",
"index.js",
"legacy.js",
"legacy.d.ts"
"index.d.ts"
],
"keywords": [
"event",
Expand All @@ -46,24 +41,20 @@
"observer",
"trigger",
"await",
"promise"
"promise",
"typescript",
"ts",
"typed"
],
"devDependencies": {
"@sindresorhus/tsconfig": "^0.1.0",
"@types/node": "^10.12.10",
"ava": "^0.25.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"@types/node": "^12.0.8",
"ava": "^2.1.0",
"codecov": "^3.1.0",
"delay": "^4.1.0",
"glob": "^7.1.3",
"nyc": "^13.1.0",
"ts-node": "^7.0.1",
"typescript": "^3.2.1",
"xo": "^0.23.0"
"nyc": "^14.1.1",
"tsd": "^0.7.3",
"xo": "^0.24.0"
},
"types": "Emittery.d.ts",
"nyc": {
"reporter": [
"html",
Expand Down
21 changes: 6 additions & 15 deletions readme.md
Expand Up @@ -22,6 +22,7 @@ $ npm install emittery

```js
const Emittery = require('emittery');

const emitter = new Emittery();

emitter.on('πŸ¦„', data => {
Expand All @@ -32,10 +33,6 @@ emitter.on('πŸ¦„', data => {
emitter.emit('πŸ¦„', '🌈');
```

### Node.js 6

The above only works in Node.js 8 or newer. For older Node.js versions you can use `require('emittery/legacy')`.


## API

Expand Down Expand Up @@ -72,13 +69,13 @@ emitter.once('πŸ¦„').then(data => {
emitter.emit('πŸ¦„', '🌈');
```

#### emit(eventName, [data])
#### emit(eventName, data?)

Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but execute concurrently.

Returns a promise for when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.

#### emitSerial(eventName, [data])
#### emitSerial(eventName, data?)

Same as above, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.

Expand All @@ -102,15 +99,14 @@ Clear all event listeners on the instance.

If `eventName` is given, only the listeners for that event are cleared.

#### listenerCount([eventName])
#### listenerCount(eventName?)

The number of listeners for the `eventName` or all events if not specified.

## TypeScript

Definition for `emittery` and `emittery/legacy` are included. Use `import Emittery = require('emittery')` or `import Emittery = require('emittery/legacy')` to load the desired implementation.
## TypeScript

The default `Emittery` class does not let you type allowed event names and their associated data. However you can use `Emittery.Typed` with generics:
The default `Emittery` class does not let you type allowed event names and their associated data. However, you can use `Emittery.Typed` with generics:

```ts
import Emittery = require('emittery');
Expand Down Expand Up @@ -174,8 +170,3 @@ emitter.emit('πŸ¦„', [foo, bar]);
## Related

- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted


## License

MIT Β© [Sindre Sorhus](https://sindresorhus.com)

0 comments on commit a0a4c6e

Please sign in to comment.