@adonisjs/events
Advanced tools
Comparing version 4.0.1 to 5.0.0
@@ -1,5 +0,5 @@ | ||
import { IocContract } from '@adonisjs/fold'; | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application'; | ||
export default class EventProvider { | ||
protected container: IocContract; | ||
constructor(container: IocContract); | ||
protected app: ApplicationContract; | ||
constructor(app: ApplicationContract); | ||
/** | ||
@@ -6,0 +6,0 @@ * Register `Event emitter` to the container. |
@@ -12,4 +12,4 @@ "use strict"; | ||
class EventProvider { | ||
constructor(container) { | ||
this.container = container; | ||
constructor(app) { | ||
this.app = app; | ||
} | ||
@@ -20,5 +20,5 @@ /** | ||
register() { | ||
this.container.singleton('Adonis/Core/Event', () => { | ||
this.app.container.singleton('Adonis/Core/Event', () => { | ||
const { Emitter } = require('../src/Emitter'); | ||
return new Emitter(this.container); | ||
return new Emitter(this.app); | ||
}); | ||
@@ -25,0 +25,0 @@ } |
/// <reference path="../../adonis-typings/events.d.ts" /> | ||
import { IocContract } from '@adonisjs/fold'; | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application'; | ||
import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, TrapAllHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event'; | ||
@@ -15,3 +15,3 @@ /** | ||
private trapAllHandler?; | ||
constructor(container?: IocContract); | ||
constructor(app?: ApplicationContract); | ||
/** | ||
@@ -18,0 +18,0 @@ * Returns reference to the IoC resolver. Do not call this method until |
@@ -24,8 +24,8 @@ "use strict"; | ||
class Emitter { | ||
constructor(container) { | ||
constructor(app) { | ||
this.transport = new emittery_1.default(); | ||
this.trappingEvents = false; | ||
this.traps = new Map(); | ||
if (container) { | ||
this.iocResolver = new IocResolver_1.IocResolver(container); | ||
if (app) { | ||
this.iocResolver = new IocResolver_1.IocResolver(app); | ||
} | ||
@@ -32,0 +32,0 @@ } |
/// <reference path="../../adonis-typings/events.d.ts" /> | ||
import { IocContract } from '@adonisjs/fold'; | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application'; | ||
import { AnyHandler, EventHandler } from '@ioc:Adonis/Core/Event'; | ||
@@ -33,3 +33,3 @@ /** | ||
private listenersBaseNamespace?; | ||
constructor(container: IocContract); | ||
constructor(app: ApplicationContract); | ||
/** | ||
@@ -36,0 +36,0 @@ * Returns the listener by resolving the namespace from the IoC container |
@@ -18,3 +18,3 @@ "use strict"; | ||
class IocResolver { | ||
constructor(container) { | ||
constructor(app) { | ||
/** | ||
@@ -33,3 +33,3 @@ * A reference to the event handlers resolved from the IoC container and | ||
this.anyHandlers = new Map(); | ||
this.containerResolver = container.getResolver(undefined, 'eventListeners', 'App/Listeners'); | ||
this.containerResolver = app.container.getResolver(undefined, 'eventListeners', 'App/Listeners'); | ||
} | ||
@@ -36,0 +36,0 @@ /** |
@@ -11,3 +11,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Emitter = void 0; | ||
var Emitter_1 = require("./src/Emitter"); | ||
Object.defineProperty(exports, "Emitter", { enumerable: true, get: function () { return Emitter_1.Emitter; } }); |
{ | ||
"name": "@adonisjs/events", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "Event emitter with asynchronous events", | ||
@@ -35,22 +35,22 @@ "main": "build/providers/EventProvider.js", | ||
"devDependencies": { | ||
"@adonisjs/fold": "^6.3.5", | ||
"@adonisjs/application": "^3.0.7", | ||
"@adonisjs/mrm-preset": "^2.4.0", | ||
"@types/node": "^14.0.23", | ||
"commitizen": "^4.1.2", | ||
"cz-conventional-changelog": "^3.2.0", | ||
"@adonisjs/require-ts": "^1.0.0", | ||
"@poppinss/dev-utils": "^1.0.10", | ||
"@types/node": "^14.11.2", | ||
"commitizen": "^4.2.1", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"del-cli": "^3.0.1", | ||
"doctoc": "^1.4.0", | ||
"eslint": "^7.5.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-adonis": "^1.0.14", | ||
"eslint": "^7.10.0", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint-plugin-adonis": "^1.0.15", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"github-label-sync": "^2.0.0", | ||
"husky": "^4.2.5", | ||
"husky": "^4.3.0", | ||
"japa": "^3.1.1", | ||
"mrm": "^2.3.3", | ||
"np": "^6.3.2", | ||
"npm-audit-html": "^1.4.1", | ||
"prettier": "^2.0.5", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.7" | ||
"mrm": "^2.5.1", | ||
"np": "^6.5.0", | ||
"npm-audit-html": "^1.4.3", | ||
"prettier": "^2.1.2", | ||
"typescript": "^4.0.3" | ||
}, | ||
@@ -67,3 +67,3 @@ "nyc": { | ||
"hooks": { | ||
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md && npm audit --production --json | ./node_modules/.bin/npm-audit-html && git add npm-audit.html", | ||
"pre-commit": "npm audit --production --json | ./node_modules/.bin/npm-audit-html && git add npm-audit.html", | ||
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js" | ||
@@ -84,2 +84,5 @@ } | ||
}, | ||
"peerDependencies": { | ||
"@adonisjs/application": "^3.0.0" | ||
}, | ||
"directories": { | ||
@@ -96,3 +99,7 @@ "doc": "docs", | ||
}, | ||
"homepage": "https://github.com/adonisjs/events#readme" | ||
"homepage": "https://github.com/adonisjs/events#readme", | ||
"publishConfig": { | ||
"access": "public", | ||
"tag": "next" | ||
} | ||
} |
189
README.md
<div align="center"> | ||
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1564392111/adonis-banner_o9lunk.png" width="600px"> | ||
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px"> | ||
</div> | ||
# Event emitter | ||
> Async event emitter with support for typed events | ||
<br /> | ||
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![audit-report-image]][audit-report-url] | ||
<div align="center"> | ||
<h3>AdonisJS Event Emitter</h3> | ||
<p>Asynchronous Event Emitter built on top of <a href="https://github.com/sindresorhus/emittery">emittery</a> with first class for <strong>typed events</strong> and ability to <strong>trap events during tests</strong>.</p> | ||
</div> | ||
This module is extracted from [AdonisJs](https://adonisjs.com). It is an Async event emitter with support for Typed events when using typescript. | ||
<br /> | ||
## Features | ||
- Asynchronous | ||
- Support for Typed events (when using Typescript) | ||
- Comes with fake emitter for testing | ||
<div align="center"> | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Table of contents | ||
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![audit-report-image]][audit-report-url] | ||
- [Usage](#usage) | ||
- [Using with AdonisJs](#using-with-adonisjs) | ||
- [Asynchronous](#asynchronous) | ||
- [Typed events](#typed-events) | ||
- [Fake emitter](#fake-emitter) | ||
- [Application code](#application-code) | ||
- [Test](#test) | ||
- [AdonisJs usage](#adonisjs-usage) | ||
- [API](#api) | ||
- [Audit report](#audit-report) | ||
</div> | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
<div align="center"> | ||
<h3> | ||
<a href="https://preview.adonisjs.com"> | ||
Website | ||
</a> | ||
<span> | </span> | ||
<a href="https://preview.adonisjs.com/guides/events"> | ||
Guides | ||
</a> | ||
<span> | </span> | ||
<a href="CONTRIBUTING.md"> | ||
Contributing | ||
</a> | ||
</h3> | ||
</div> | ||
## Usage | ||
Install the package from npm registry as follows: | ||
<div align="center"> | ||
<sub>Built with ❤︎ by <a href="https://twitter.com/AmanVirk1">Harminder Virk</a> | ||
</div> | ||
```sh | ||
npm i @adonisjs/events | ||
# yarn | ||
yarn add @adonisjs/events | ||
``` | ||
And then use it as follows: | ||
```ts | ||
import { Emitter } from '@adonisjs/events/build/standalone' | ||
const emitter = new Emitter() | ||
emitter.on('signup', async (data) => { | ||
// | ||
}) | ||
emitter.emit('signup', { id: 1, email: 'foo@bar.com' }) | ||
``` | ||
## Using with AdonisJs | ||
The `@adonisjs/core` module includes this module by default. However, here's how you can set it up manually. | ||
```ts | ||
const providers = [ | ||
'@adonisjs/events' | ||
] | ||
``` | ||
And then also register the typings file inside `tsconfig.json` file. | ||
```json | ||
{ | ||
"files": ["./node_modules/@adonisjs/events/build/adonis-typings/events.d.ts"] | ||
} | ||
``` | ||
## Asynchronous | ||
Node.js inbuilt event emitter is synchronous and blocks the event loop while emitting events. On the other hand, this module relies on [emittery](https://github.com/sindresorhus/emittery) to emit events asynchronously. | ||
## Typed events | ||
When using Typescript, you can attach data types to a event name and the Typescript compiler will complain, if types aren't the same. | ||
```ts | ||
import { Emitter } from '@adonisjs/events/build/standalone' | ||
type EventsMap = { | ||
'new:user': { id: number }, | ||
} | ||
const emitter = new Emitter<EventsMap>() | ||
emitter.emit('new:user', { id: 1 }) // works | ||
emitter.emit('new:user', 1) // compiler error | ||
``` | ||
Just like the `emit` method, all other methods enforce types on typed events. | ||
```ts | ||
const emitter = new Emitter<EventsMap>() | ||
emitter.on('new:user', (user) => { | ||
}) | ||
emitter.once('new:user', (user) => { | ||
}) | ||
``` | ||
## Fake emitter | ||
When writing tests, you may want to suppress the events and instead run assertions to ensure that your code is emitting right events with correct data. | ||
You can do this by using the `FakeEmitter` instance during tests. | ||
#### Application code | ||
```ts | ||
import { EmitterContract } from '@adonisjs/events/build/standalone' | ||
export default class UserController { | ||
constructor (protected emitter) {} | ||
create () { | ||
const user = makeSomeDbCall() | ||
this.emitter.emit('new:user', user) | ||
} | ||
} | ||
``` | ||
When writing tests, you can pass the `FakeEmitter`, instead of the real emitter instance. | ||
#### Test | ||
```ts | ||
import { FakeEmitter } from '@adonisjs/events/build/standalone' | ||
import UserController from './UserController' | ||
const emitter = new FakeEmitter() | ||
new UserController(emitter).create() | ||
assert.deepEqual(emitter.events, [{ | ||
event: 'new:user', | ||
data: user, | ||
}]) | ||
// Clear stored events inside memory | ||
emitter.clear() | ||
``` | ||
## AdonisJs usage | ||
When using with AdonisJs, you can also define listeners as a reference to the IoC container binding. For example: | ||
```ts | ||
// Add event listener | ||
Event.on('new:user', 'User.signup') | ||
// Remove event listener | ||
Event.off('new:user', 'User.signup') | ||
``` | ||
AdonisJs will make an instance of `app/Listeners/User.ts` file and will call the signup method on it. This keeps your events file clean, since you can abstract the listeners code to dedicated files. | ||
Also, you can define a custom namespace to lookup listeners from. | ||
```ts | ||
Event.namespace('App/MyListeners') | ||
``` | ||
Now AdonisJs will look inside the `app/MyListeners` directory vs `app/Listeners`. | ||
## API | ||
Following are the autogenerated files via Typedoc | ||
* [API](docs/README.md) | ||
## Audit report | ||
[Click here](https://htmlpreview.github.io/?https://github.com/adonisjs/events/blob/develop/npm-audit.html) to see the latest npm audit report. | ||
[circleci-image]: https://img.shields.io/circleci/project/github/adonisjs/events/master.svg?style=for-the-badge&logo=circleci | ||
@@ -174,0 +41,0 @@ [circleci-url]: https://circleci.com/gh/adonisjs/events "circleci" |
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
17
663
26931
2
54