Coder Social home page Coder Social logo

adonisjs / events Goto Github PK

View Code? Open in Web Editor NEW
19.0 7.0 5.0 558 KB

Event emitter with asynchronous events.

Home Page: https://docs.adonisjs.com/guides/emitter

License: MIT License

TypeScript 99.91% Shell 0.09%
emitter emittery bundled-with-core

events's Introduction

@adonisjs/events


gh-workflow-image npm-image license-image

Introduction

AdonisJS events is an implementation of the event emitter built on top of emittery. Alongside defining event listeners as inline callbacks, you can also bind modules to events and define events as classes.

Official Documentation

The documentation is available on the AdonisJS website

Contributing

One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.

We encourage you to read the contribution guide before contributing to the framework.

Code of Conduct

In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the Code of Conduct.

License

AdonisJS events is open-sourced software licensed under the MIT license.

events's People

Contributors

ammezie avatar dependabot-preview[bot] avatar dependabot[bot] avatar julien-r44 avatar snyk-bot avatar thetutlage avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

events's Issues

Unhandled promise rejection on events

Package version

5.0.5 and previous

Node.js and npm version

Warning before Node.js 15.0.0
Error since Node.js 15.0.0

NPM 7

Sample Code (to reproduce the issue)

Create a Listener that throws.

export default class BoomListener {
  public async handleBoom() {
    throw new Error('boom');
  }
}

Register it in start/events.ts

Event.on('i-boom-ur-app', 'BoomListener.handleBoom');

Emit an event since anywhere:

Event.emit('i-boom-ur-app')

Previous Node.js 15, the app just throw a warning.
Since Node.js 15, the app crashes.

Support emittery.emitSerial

Why this feature is required (specific use-cases will be appreciated)?

emittery.emitSerial(eventName, data?)

This can be useful if your events depend on each other. Although ideally they should not. Prefer emit() whenever possible.
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will not be called.

In some cases we need guarantee the order of listeners, this API is quite usefull.

Have you tried any other work arounds?

the other ways are not elegance as events

APIs to wait for event listeners to finish

Why this feature is required (specific use-cases will be appreciated)?

Within a test, if one wants to assert for actions performed inside an event listener, then cannot do that reliably, since events are async in nature.

For example: The UsersController.store method fires an event after the user is created. That event performs an action, and I want to assert that in my code that action was executed successfully.

test('create user', ({ client }) => {
  await client.post('users').json({})

  // Here assert if action within event listener was successful.
  // However, I do not know when that listener gets executed.
})

Have you tried any other work arounds?

The workaround is to wrap the assert code inside a setTimeout or process.nextTick. But, that is merely a hack and will cause some flakiness.

Are you willing to work on it with little guidance?

We need first class support to monitor events and run code after the all the listeners for a specific/all events have been executed successfully.

The last line in events.ts rewrites namespace of all previous lines

Prerequisites

The last line in events.ts rewrites namespace of all previous lines:

Event.on('new:organization', 'ChecklistListener.onNewOrganization').namespace('App/Checklist/Listeners')
Event.on('user:deactivated', 'StudyGroupsListener.onUserDeactivate').namespace('App/StudyGroups/Listeners')
[1698769693344] ERROR (myproject/14527 on Samats-MacBook-Pro-2.local): Cannot find module '/myproject/app/StudyGroups/Listeners/ChecklistListener'
Require stack:
- /myproject/node_modules/@adonisjs/fold/build/src/Ioc/ImportAliases.js
- /myproject/node_modules/@adonisjs/fold/build/src/Ioc/index.js
- /myproject/node_modules/@adonisjs/fold/build/index.js
- /myproject/node_modules/@adonisjs/application/build/index.js
- /myproject/node_modules/@adonisjs/assembler/build/src/requireHook/ioc-transformer.js
- /myproject/node_modules/@adonisjs/require-ts/node_modules/@poppinss/utils/build/src/esmRequire.js
- /myproject/node_modules/@adonisjs/require-ts/node_modules/@poppinss/utils/build/index.js
- /myproject/node_modules/@adonisjs/require-ts/build/src/Compiler/index.js
- /myproject/node_modules/@adonisjs/require-ts/build/index.js
- /myproject/node_modules/@adonisjs/assembler/build/src/requireHook/index.js
- /myproject/node_modules/@adonisjs/assembler/build/register.js
- internal/preload
    Error: Cannot find module '/myproject/app/StudyGroups/Listeners/ChecklistListener'
    Require stack:
    - /myproject/node_modules/@adonisjs/fold/build/src/Ioc/ImportAliases.js
    - /myproject/node_modules/@adonisjs/fold/build/src/Ioc/index.js
    - /myproject/node_modules/@adonisjs/fold/build/index.js
    - /myproject/node_modules/@adonisjs/application/build/index.js
    - /myproject/node_modules/@adonisjs/assembler/build/src/requireHook/ioc-transformer.js
    - /myproject/node_modules/@adonisjs/require-ts/node_modules/@poppinss/utils/build/src/esmRequire.js
    - /myproject/node_modules/@adonisjs/require-ts/node_modules/@poppinss/utils/build/index.js
    - /myproject/node_modules/@adonisjs/require-ts/build/src/Compiler/index.js
    - /myproject/node_modules/@adonisjs/require-ts/build/index.js
    - /myproject/node_modules/@adonisjs/assembler/build/src/requireHook/index.js
    - /myproject/node_modules/@adonisjs/assembler/build/register.js
    - internal/preload
        at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
        at Function.Module._load (node:internal/modules/cjs/loader:778:27)
        at Module.require (node:internal/modules/cjs/loader:1005:19)
        at require (node:internal/modules/cjs/helpers:102:18)
        at ImportAliases.resolve (/myproject/node_modules/@adonisjs/fold/build/src/Ioc/ImportAliases.js:99:23)
        at ImportAliases.resolveAsync (/myproject/node_modules/@adonisjs/fold/build/src/Ioc/ImportAliases.js:117:25)
        at Ioc.import (/myproject/node_modules/@adonisjs/fold/build/src/Ioc/index.js:280:42)
        at Ioc.makeAsync (/myproject/node_modules/@adonisjs/fold/build/src/Ioc/index.js:391:40)
        at IocResolver.call (/myproject/node_modules/@adonisjs/fold/build/src/Resolver/index.js:90:47)
        at /myproject/node_modules/@adonisjs/events/build/src/IocResolver/index.js:39:43  

Package version

5.9

Node.js and npm version

18

Sample Code (to reproduce the issue)

// events.ts
Event.on('new:organization', 'ChecklistListener.onNewOrganization').namespace('App/Checklist/Listeners')
Event.on('user:deactivated', 'StudyGroupsListener.onUserDeactivate').namespace('App/StudyGroups/Listeners')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.