Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adonisjs/events

Package Overview
Dependencies
Maintainers
2
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adonisjs/events

Event emitter with asynchronous events

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25K
decreased by-17.14%
Maintainers
2
Weekly downloads
 
Created
Source

Event emitter

Async event emitter with support for typed events

circleci-image npm-image license-image

This module is extracted from AdonisJs. It is an Async event emitter with support for Typed events when using typescript.

Features

  • Asynchronous
  • Support for Typed events (when using Typescript)
  • Comes with fake emitter for testing

Table of contents

Usage

Install the package from npm registry as follows:

npm i @adonisjs/events

# yarn
yarn add @adonisjs/events

And then use it as follows:

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.

const providers = [
  '@adonisjs/events'
]

And then also register the typings file inside tsconfig.json file.

{
  "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 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.

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.

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
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
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:

// 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.

Event.namespace('App/MyListeners')

Now AdonisJs will look inside the MyListeners directory vs Listeners.

API

Following are the autogenerated files via Typedoc

Audit report

Click here to see the latest npm audit report.

Keywords

FAQs

Package last updated on 05 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc