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 - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

89

build/adonis-typings/events.d.ts
declare module '@ioc:Adonis/Core/Event' {
/**
* Returns the data type for a given key
*/
export type DataForEvent<K extends string> = K extends keyof EventsList ? EventsList[K] : any;
/**
* Shape of event handler

@@ -9,5 +13,14 @@ */

*/
export type AnyHandler<T extends any = any> = (event: string | symbol, data: T) => Promise<void> | void;
export type EventData<T extends any, K extends any> = K extends keyof T ? T[K] : any;
export type AnyHandler = (event: keyof EventsList, data: {
[P in keyof EventsList]: EventsList[P];
}[keyof EventsList]) => Promise<void> | void;
/**
* Shape of event trap handler
*/
export type TrapHandler<T extends any = any> = EventHandler<T>;
/**
* Shape of trap all events handler
*/
export type TrapAllHandler = AnyHandler;
/**
* The shape of emitter transport. This has to be same as

@@ -19,6 +32,6 @@ * `emittery`.

once(event: string | symbol): Promise<any>;
onAny(handler: AnyHandler): any;
onAny(handler: (event: any, data: any) => Promise<void> | void): any;
emit(event: string | symbol, data: any): Promise<any>;
off(event: string | symbol, handler: EventHandler): any;
offAny(handler: AnyHandler): any;
offAny(handler: (event: any, data: any) => Promise<void> | void): any;
off(event: string | symbol, handler: EventHandler): any;

@@ -31,6 +44,6 @@ clearListeners(event?: string | symbol): any;

*/
export interface EmitterContract<T extends any = any> {
export interface EmitterContract {
transport: EmitterTransportContract;
/**
* Define a custom Ioc Container base namespace for resolving
* Define a custom IoC Container base namespace for resolving
* the listener bindings.

@@ -42,34 +55,22 @@ */

*/
on<K extends keyof T>(event: K, handler: EventHandler<T[K]> | string): this;
on<K extends keyof EventsList>(event: K, handler: EventHandler<EventsList[K]> | string): this;
on<K extends string>(event: K, handler: EventHandler<DataForEvent<K>> | string): this;
/**
* Listen for an event
*/
on<K extends string>(event: K, handler: EventHandler<EventData<T, K>> | string): this;
/**
* Listen for an event only once
*/
once<K extends keyof T>(event: K, handler: EventHandler<T[K]> | string): this;
once<K extends keyof EventsList>(event: K, handler: EventHandler<EventsList[K]> | string): this;
once<K extends string>(event: K, handler: EventHandler<DataForEvent<K>> | string): this;
/**
* Listen for an event only once
*/
once<K extends string>(event: K, handler: EventHandler<EventData<T, K>> | string): this;
/**
* Listen for all events
*/
onAny(handler: AnyHandler): this;
onAny(handler: AnyHandler | string): this;
/**
* Emit an event
*/
emit<K extends keyof T>(event: K, data: T[K]): Promise<void>;
emit<K extends keyof EventsList>(event: K, data: EventsList[K]): Promise<void>;
emit<K extends string>(event: K, data: DataForEvent<K>): Promise<void>;
/**
* Emit an event
*/
emit<K extends string>(event: K, data: EventData<T, K>): Promise<void>;
/**
* Remove event listener
*/
off<K extends keyof T>(event: K, handler: EventHandler | string): void;
/**
* Remove event listener
*/
off<K extends keyof EventsList>(event: K, handler: EventHandler | string): void;
off<K extends string>(event: K, handler: EventHandler | string): void;

@@ -79,10 +80,7 @@ /**

*/
offAny(handler: AnyHandler): void;
offAny(handler: AnyHandler | string): void;
/**
* Clear a given listener for a given event
*/
clearListener<K extends keyof T>(event: K, handler: EventHandler | string): void;
/**
* Clear a given listener for a given event
*/
clearListener<K extends keyof EventsList>(event: K, handler: EventHandler | string): void;
clearListener<K extends string>(event: K, handler: EventHandler | string): void;

@@ -92,6 +90,3 @@ /**

*/
clearListeners<K extends keyof T>(event: K): void;
/**
* Clear all listeners for a given event
*/
clearListeners<K extends keyof EventsList>(event: K): void;
clearListeners<K extends string>(event: K): void;

@@ -101,6 +96,3 @@ /**

*/
listenerCount<K extends keyof T>(event?: K): number;
/**
* Returns count of listeners listening for a given event
*/
listenerCount<K extends keyof EventsList>(event?: K): number;
listenerCount<K extends string>(event?: K): number;

@@ -110,7 +102,18 @@ /**

*/
hasListeners<K extends keyof T>(event?: K): boolean;
hasListeners<K extends keyof EventsList>(event?: K): boolean;
hasListeners<K extends string>(event?: K): boolean;
/**
* Returns true when an event has one or more listeners
* Trap a specific event. The event listener won't be executed during
* the trap. Call [[this.restore]] to remove traps
*/
hasListeners<K extends string>(event?: K): boolean;
trap<K extends keyof EventsList>(event: K, handler: TrapHandler<EventsList[K]>): this;
trap<K extends string>(event: K, handler: TrapHandler<DataForEvent<K>>): this;
/**
* Trap all the events, which are not trapped using the [[this.trap]] method
*/
trapAll(handler: AnyHandler): this;
/**
* Restore traps
*/
restore(): this;
}

@@ -122,4 +125,4 @@ /**

}
const Event: EmitterContract<EventsList>;
const Event: EmitterContract;
export default Event;
}

@@ -11,3 +11,2 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const Emitter_1 = require("../src/Emitter");
class EventProvider {

@@ -22,3 +21,4 @@ constructor(container) {

this.container.singleton('Adonis/Core/Event', () => {
return new Emitter_1.Emitter(this.container);
const { Emitter } = require('../src/Emitter');
return new Emitter(this.container);
});

@@ -25,0 +25,0 @@ }

/// <reference path="../../adonis-typings/events.d.ts" />
import { IocContract } from '@adonisjs/fold';
import { EventData, AnyHandler, EventHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event';
import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, TrapAllHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event';
/**

@@ -9,5 +9,8 @@ * Emitter class exposes the API for async event emitter built on top of

*/
export declare class Emitter<T extends any = any> implements EmitterContract<T> {
export declare class Emitter implements EmitterContract {
transport: EmitterTransportContract;
private iocResolver?;
private trappingEvents;
private traps;
private trapAllHandler?;
constructor(container?: IocContract);

@@ -22,4 +25,3 @@ /**

*/
on<K extends keyof T>(event: K, handler: EventHandler<T[K]> | string): this;
on<K extends string>(event: K, handler: EventHandler<EventData<T, K>> | string): this;
on<K extends keyof EventsList | string>(event: K, handler: EventHandler<DataForEvent<K>> | string): this;
/**

@@ -29,4 +31,3 @@ * Define event handler for a given event and to be called

*/
once<K extends keyof T>(event: K, handler: EventHandler<T[K]> | string): this;
once<K extends string>(event: K, handler: EventHandler<EventData<T, K>> | string): this;
once<K extends keyof EventsList | string>(event: K, handler: EventHandler<DataForEvent<K>> | string): this;
/**

@@ -39,9 +40,7 @@ * Define catch all event handler to listen for all events.

*/
emit<K extends keyof T>(event: K, data: T[K]): Promise<void>;
emit<K extends string>(event: K, data: EventData<T, K>): Promise<void>;
emit<K extends keyof EventsList | string>(event: K, data: DataForEvent<K>): Promise<any>;
/**
* Remove existing event listener
*/
off<K extends keyof T>(event: K, handler: EventHandler | string): void;
off<K extends string>(event: K, handler: EventHandler | string): void;
off<K extends keyof EventsList>(event: K | string, handler: EventHandler | string): void;
/**

@@ -55,9 +54,7 @@ * Remove existing event listener for catch all handler

*/
clearListener<K extends keyof T>(event: K, handler: EventHandler | string): void;
clearListener<K extends string>(event: K, handler: EventHandler | string): void;
clearListener<K extends keyof EventsList | string>(event: K, handler: EventHandler | string): void;
/**
* Clear all listeners for a given event
*/
clearListeners<K extends keyof T>(event: K): void;
clearListeners<K extends string>(event: K): void;
clearListeners<K extends keyof EventsList | string>(event: K): void;
/**

@@ -71,4 +68,3 @@ * Clear all listeners for all events

*/
listenerCount<K extends keyof T>(event?: K): number;
listenerCount<K extends string>(event?: K): number;
listenerCount<K extends keyof EventsList | string>(event?: K): number;
/**

@@ -78,4 +74,3 @@ * Returns a boolean telling if listeners count for a given

*/
hasListeners<K extends keyof T>(event?: K): boolean;
hasListeners<K extends string>(event?: K): boolean;
hasListeners<K extends keyof EventsList | string>(event?: K): boolean;
/**

@@ -86,2 +81,14 @@ * Define custom namespace for event listeners. It is set to `App/Listeners`

namespace(namespace: string): this;
/**
* Trap event instead of emitting it
*/
trap<K extends keyof EventsList | string>(event: K, handler: TrapHandler<DataForEvent<K>>): this;
/**
* Trap all events instead of emitting them
*/
trapAll(handler: TrapAllHandler): this;
/**
* Restore trap
*/
restore(): this;
}

@@ -26,2 +26,4 @@ "use strict";

this.transport = new emittery_1.default();
this.trappingEvents = false;
this.traps = new Map();
if (container) {

@@ -41,2 +43,5 @@ this.iocResolver = new IocResolver_1.IocResolver(container);

}
/**
* Define event handler for a given event
*/
on(event, handler) {

@@ -49,2 +54,6 @@ if (typeof handler === 'string') {

}
/**
* Define event handler for a given event and to be called
* only once.
*/
once(event, handler) {

@@ -72,5 +81,25 @@ this.transport.once(event).then((data) => {

}
emit(event, data) {
return this.transport.emit(event, data);
/**
* Emit event
*/
async emit(event, data) {
if (!this.trappingEvents) {
return this.transport.emit(event, data);
}
/**
* Give preference to the handler for a specific event
*/
if (this.traps.has(event)) {
return this.traps.get(event)(data);
}
/**
* Invoke catch all (if defined)
*/
if (this.trapAllHandler) {
return this.trapAllHandler(event, data);
}
}
/**
* Remove existing event listener
*/
off(event, handler) {

@@ -99,5 +128,12 @@ if (typeof handler === 'string') {

}
/**
* Remove existing event listener.
* @alias off
*/
clearListener(event, handler) {
this.off(event, handler);
}
/**
* Clear all listeners for a given event
*/
clearListeners(event) {

@@ -112,5 +148,13 @@ this.transport.clearListeners(event);

}
/**
* Returns count of listeners for a given event or all
* events.
*/
listenerCount(event) {
return this.transport.listenerCount(event ? event : undefined);
}
/**
* Returns a boolean telling if listeners count for a given
* event or all events is greater than 0 or not.
*/
hasListeners(event) {

@@ -129,3 +173,28 @@ return this.listenerCount(event) > 0;

}
/**
* Trap event instead of emitting it
*/
trap(event, handler) {
this.trappingEvents = true;
this.traps.set(event, handler);
return this;
}
/**
* Trap all events instead of emitting them
*/
trapAll(handler) {
this.trappingEvents = true;
this.trapAllHandler = handler;
return this;
}
/**
* Restore trap
*/
restore() {
this.trappingEvents = false;
this.traps.clear();
this.trapAllHandler = undefined;
return this;
}
}
exports.Emitter = Emitter;

@@ -18,4 +18,5 @@ /// <reference path="../../adonis-typings/events.d.ts" />

/**
* A reference to the catch all event handlers. They can be plain callbacks or reference
* to an IoC container namespace
* A reference to the catch all event handlers. It is a map of
*
* [namespace, resolvedHandler]
*/

@@ -22,0 +23,0 @@ private anyHandlers;

@@ -27,4 +27,5 @@ "use strict";

/**
* A reference to the catch all event handlers. They can be plain callbacks or reference
* to an IoC container namespace
* A reference to the catch all event handlers. It is a map of
*
* [namespace, resolvedHandler]
*/

@@ -31,0 +32,0 @@ this.anyHandlers = new Map();

export { Emitter } from './src/Emitter';
export { FakeEmitter } from './src/FakeEmitter';

@@ -13,3 +13,1 @@ "use strict";

Object.defineProperty(exports, "Emitter", { enumerable: true, get: function () { return Emitter_1.Emitter; } });
var FakeEmitter_1 = require("./src/FakeEmitter");
Object.defineProperty(exports, "FakeEmitter", { enumerable: true, get: function () { return FakeEmitter_1.FakeEmitter; } });
# The MIT License
Copyright 2019 Harminder virk, contributors
Copyright 2020 Harminder virk, contributors

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

{
"name": "@adonisjs/events",
"version": "3.0.0",
"version": "4.0.0",
"description": "Event emitter with asynchronous events",

@@ -17,3 +17,2 @@ "main": "build/providers/EventProvider.js",

"test": "node japaFile.js",
"lint": "eslint . --ext=.ts",
"clean": "del build",

@@ -25,3 +24,6 @@ "compile": "npm run lint && npm run clean && tsc",

"version": "npm run build",
"format": "prettier --write ."
"format": "prettier --write .",
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json adonisjs/events"
},

@@ -36,4 +38,4 @@ "keywords": [

"@adonisjs/fold": "^6.3.5",
"@adonisjs/mrm-preset": "^2.3.6",
"@types/node": "^14.0.14",
"@adonisjs/mrm-preset": "^2.4.0",
"@types/node": "^14.0.23",
"commitizen": "^4.1.2",

@@ -43,14 +45,15 @@ "cz-conventional-changelog": "^3.2.0",

"doctoc": "^1.4.0",
"eslint": "^7.4.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-adonis": "^1.0.14",
"eslint-plugin-prettier": "^3.1.4",
"github-label-sync": "^2.0.0",
"husky": "^4.2.5",
"japa": "^3.1.1",
"mrm": "^2.3.3",
"np": "^6.2.5",
"np": "^6.3.2",
"npm-audit-html": "^1.4.1",
"prettier": "^2.0.5",
"ts-node": "^8.10.2",
"typescript": "^3.9.6"
"typescript": "^3.9.7"
},

@@ -81,3 +84,3 @@ "nyc": {

"dependencies": {
"emittery": "^0.7.0"
"emittery": "^0.7.1"
},

@@ -84,0 +87,0 @@ "directories": {

@@ -8,3 +8,3 @@ <div align="center">

[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![audit-report-image]][audit-report-url]

@@ -164,3 +164,3 @@ This module is extracted from [AdonisJs](https://adonisjs.com). It is an Async event emitter with support for Typed events when using typescript.

Now AdonisJs will look inside the `MyListeners` directory vs `Listeners`.
Now AdonisJs will look inside the `app/MyListeners` directory vs `app/Listeners`.

@@ -186,1 +186,4 @@ ## API

[license-url]: LICENSE.md "license"
[audit-report-image]: https://img.shields.io/badge/-Audit%20Report-blueviolet?style=for-the-badge
[audit-report-url]: https://htmlpreview.github.io/?https://github.com/adonisjs/events/blob/develop/npm-audit.html "audit-report"
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