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

appolo-event-dispatcher

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appolo-event-dispatcher - npm Package Compare versions

Comparing version 6.0.14 to 7.0.0

lib/IEvent.js

1

index.ts

@@ -5,4 +5,5 @@ export {EventDispatcher, CallbacksSymbol} from './lib/eventDispatcher'

export {Event} from './lib/event'
export {IEvent} from './lib/IEvent'

24

lib/event.js

@@ -8,25 +8,31 @@ "use strict";

this.EVENT_NAME = "event";
this._eventEventDispatcher = new eventDispatcher_1.EventDispatcher();
this._dispatcher = new eventDispatcher_1.EventDispatcher();
this._opts = Object.assign({}, { await: false, parallel: true }, _opts);
}
on(fn, scope) {
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, this._opts);
this._dispatcher.on(this.EVENT_NAME, fn, scope, this._opts);
}
un(fn, scope) {
this._eventEventDispatcher.un(this.EVENT_NAME, fn, scope);
this._dispatcher.un(this.EVENT_NAME, fn, scope);
}
once(fn, scope) {
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope, this._opts);
once(fn, scope, options = {}) {
this._dispatcher.once(this.EVENT_NAME, fn, scope, Object.assign(Object.assign({}, options), this._opts));
}
iterator(event, options) {
return this._dispatcher.iterator(event, options);
}
fireEvent(payload) {
return this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload);
this._dispatcher.fireEvent(this.EVENT_NAME, payload);
}
fireEventAsync(payload) {
return this._dispatcher.fireEventAsync(this.EVENT_NAME, payload);
}
removeAllListeners() {
this._eventEventDispatcher.removeAllListeners();
this._dispatcher.removeAllListeners();
}
hasListener(fn, scope) {
return this._eventEventDispatcher.hasListener(this.EVENT_NAME, fn, scope);
return this._dispatcher.hasListener(this.EVENT_NAME, fn, scope);
}
listenerCount() {
return this._eventEventDispatcher.listenerCount(this.EVENT_NAME);
return this._dispatcher.listenerCount(this.EVENT_NAME);
}

@@ -33,0 +39,0 @@ }

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

import {EventDispatcher} from "./eventDispatcher";
import {IEvent} from "./IEvent";
export class Event<T> {
export class Event<T> implements IEvent<T> {

@@ -16,34 +17,44 @@ constructor(private readonly _opts?: { await?: boolean, parallel?: boolean }) {

private _eventEventDispatcher: EventDispatcher = new EventDispatcher();
private _dispatcher: EventDispatcher = new EventDispatcher();
public on(fn: (payload: T) => any, scope?: any): void {
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, this._opts)
this._dispatcher.on(this.EVENT_NAME, fn, scope, this._opts)
}
public un(fn: (payload: T) => any, scope?: any): void {
this._eventEventDispatcher.un(this.EVENT_NAME, fn, scope)
this._dispatcher.un(this.EVENT_NAME, fn, scope)
}
public once(fn?: (payload: T) => any, scope?: any): Promise<any> | void {
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope, this._opts);
public once(fn?: (payload: T) => any, scope?: any, options: { timeout?: number } = {}): Promise<any> | void {
this._dispatcher.once(this.EVENT_NAME, fn, scope, {...options, ...this._opts});
}
public fireEvent(payload: T): Promise<any> {
return this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload)
public iterator<T>(event: string | string[], options?: { limit?: number }): AsyncIterableIterator<T> {
return this._dispatcher.iterator(event, options);
}
public fireEvent(payload: T): void {
this._dispatcher.fireEvent(this.EVENT_NAME, payload)
}
public fireEventAsync(payload: T): Promise<any> {
return this._dispatcher.fireEventAsync(this.EVENT_NAME, payload)
}
public removeAllListeners() {
this._eventEventDispatcher.removeAllListeners();
this._dispatcher.removeAllListeners();
}
public hasListener(fn?: (...args: any[]) => any, scope?: any): boolean {
return this._eventEventDispatcher.hasListener(this.EVENT_NAME, fn, scope);
return this._dispatcher.hasListener(this.EVENT_NAME, fn, scope);
}
public listenerCount(): number {
return this._eventEventDispatcher.listenerCount(this.EVENT_NAME);
return this._dispatcher.listenerCount(this.EVENT_NAME);
}
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const routingKey_1 = require("./routingKey");
const CallbacksSymbol = Symbol('eventDispatcherCallbacks');
const iterator_1 = require("./iterator");
const CallbacksSymbol = '__eventDispatcherCallbacks__';
exports.CallbacksSymbol = CallbacksSymbol;
const RoutingKeysSymbol = Symbol('eventDispatcherRoutingKeys');
const RoutingKeysSymbol = '__eventDispatcherRoutingKeys__';
exports.RoutingKeysSymbol = RoutingKeysSymbol;
const RoutingKeysCacheSymbol = Symbol('eventDispatcherRoutingKeysCache');
const RoutingKeysCacheSymbol = '__eventDispatcherRoutingKeysCache__';
class EventDispatcher {

@@ -41,5 +42,12 @@ constructor(_eventDispatcherOptions) {

}
return new Promise((resolve) => {
fn = (...args) => resolve(args.length > 1 ? args : args[0]);
return new Promise((resolve, reject) => {
let timeout = null;
fn = (...args) => {
clearTimeout(timeout);
resolve(args.length > 1 ? args : args[0]);
};
this.on(event, fn, scope, Object.assign(Object.assign({}, options), { once: true }));
if (options.timeout) {
timeout = setTimeout(() => reject(new Error("timeout")), options.timeout);
}
});

@@ -69,3 +77,33 @@ }

}
async fireEvent(event, ...args) {
async fireEventAsync(event, ...args) {
let result = this._fireEvent(event, args);
if (!result) {
return;
}
if (result.serialPromises.length) {
for (let callback of result.serialPromises) {
await callback.callback.fn.apply(callback.callback.scope, callback.args);
}
}
if (result.parallelPromises.length) {
await Promise.all(result.parallelPromises.map(callback => callback.callback.fn.apply(callback.callback.scope, callback.args)));
}
if (result.callbacks.length) {
for (let callback of result.callbacks) {
callback.callback.fn.apply(callback.callback.scope, callback.args);
}
}
}
fireEvent(event, ...args) {
let result = this._fireEvent(event, args);
if (!result) {
return;
}
let callbacks = result.callbacks.concat(result.parallelPromises, result.serialPromises);
for (let i = 0; i < callbacks.length; i++) {
let callback = callbacks[i];
callback.callback.fn.apply(callback.callback.scope, callback.args);
}
}
_fireEvent(event, args) {
if (!this[CallbacksSymbol]) {

@@ -75,36 +113,43 @@ return;

let handler = this[CallbacksSymbol][event];
let parallelPromises = [];
let parallelPromises = [], serialPromises = [], callbacks = [];
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event);
if (routingKeys.length) {
for (let i = 0, len = routingKeys.length; i < len; i++) {
parallelPromises.push(this.fireEvent(routingKeys[i], ...args));
parallelPromises.push({
callback: {
fn: this.fireEvent,
scope: this
}, args: [routingKeys[i], ...args]
});
}
}
if (handler) {
for (let i = handler.callbacks.length - 1; i >= 0; i--) {
let callback = handler.callbacks[i];
if (!callback || !callback.fn) {
continue;
if (!handler) {
return { callbacks, serialPromises, parallelPromises };
}
for (let i = handler.callbacks.length - 1; i >= 0; i--) {
let callback = handler.callbacks[i];
if (!callback || !callback.fn) {
continue;
}
//callback.fn.apply((callback.scope || null), args);
if (callback.options.await) {
if (callback.options.parallel) {
parallelPromises.push({ callback, args });
}
let result = callback.fn.apply((callback.scope || null), args);
if (callback.options.once) {
handler.callbacks.splice(i, 1);
if (!handler.callbacks.length && this[RoutingKeysSymbol] && this[RoutingKeysSymbol][event]) {
this[RoutingKeysSymbol][event] = undefined;
this[RoutingKeysCacheSymbol] = Object.keys(this[RoutingKeysSymbol]);
}
else {
serialPromises.push({ callback, args });
}
if (callback.options.await) {
if (callback.options.parallel) {
parallelPromises.push(result);
}
else {
await result;
}
}
else {
callbacks.push({ callback, args });
}
if (callback.options.once) {
handler.callbacks.splice(i, 1);
if (!handler.callbacks.length && this[RoutingKeysSymbol] && this[RoutingKeysSymbol][event]) {
this[RoutingKeysSymbol][event] = undefined;
this[RoutingKeysCacheSymbol] = Object.keys(this[RoutingKeysSymbol]);
}
}
}
if (parallelPromises.length) {
await Promise.all(parallelPromises);
}
return { callbacks, serialPromises, parallelPromises };
}

@@ -194,4 +239,8 @@ _eventDispatcherGetRoutingKeys(handler, event) {

}
iterator(event, options) {
let iterator = new iterator_1.Iterator(this, event, options);
return iterator.iterate();
}
}
exports.EventDispatcher = EventDispatcher;
//# sourceMappingURL=eventDispatcher.js.map

@@ -6,6 +6,7 @@ "use strict";

import {RoutingKey} from "./routingKey";
import {Iterator} from "./iterator";
const CallbacksSymbol: unique symbol = Symbol('eventDispatcherCallbacks');
const RoutingKeysSymbol: unique symbol = Symbol('eventDispatcherRoutingKeys');
const RoutingKeysCacheSymbol: unique symbol = Symbol('eventDispatcherRoutingKeysCache');
const CallbacksSymbol = '__eventDispatcherCallbacks__';
const RoutingKeysSymbol = '__eventDispatcherRoutingKeys__';
const RoutingKeysCacheSymbol = '__eventDispatcherRoutingKeysCache__';
export {CallbacksSymbol, RoutingKeysSymbol};

@@ -63,5 +64,13 @@

return new Promise((resolve) => {
fn = (...args: any[]) => resolve(args.length > 1 ? args : args[0]);
this.on(event, fn, scope, {...options, ...{once: true}})
return new Promise((resolve, reject) => {
let timeout = null;
fn = (...args: any[]) => {
clearTimeout(timeout);
resolve(args.length > 1 ? args : args[0])
};
this.on(event, fn, scope, {...options, ...{once: true}});
if (options.timeout) {
timeout = setTimeout(() => reject(new Error("timeout")), options.timeout)
}
})

@@ -100,4 +109,47 @@

public async fireEvent(event: string, ...args: any[]): Promise<any> {
public async fireEventAsync(event: string, ...args: any[]): Promise<any> {
let result = this._fireEvent(event, args);
if (!result) {
return;
}
if (result.serialPromises.length) {
for (let callback of result.serialPromises) {
await callback.callback.fn.apply(callback.callback.scope, callback.args)
}
}
if (result.parallelPromises.length) {
await Promise.all(result.parallelPromises.map(callback => callback.callback.fn.apply(callback.callback.scope, callback.args)))
}
if (result.callbacks.length) {
for (let callback of result.callbacks) {
callback.callback.fn.apply(callback.callback.scope, callback.args)
}
}
}
public fireEvent(event: string, ...args: any[]): void {
let result = this._fireEvent(event, args);
if (!result) {
return;
}
let callbacks = result.callbacks.concat(result.parallelPromises, result.serialPromises)
for (let i = 0; i < callbacks.length; i++) {
let callback = callbacks[i];
callback.callback.fn.apply(callback.callback.scope, callback.args)
}
}
private _fireEvent(event: string, args: any[]): {
parallelPromises: { callback: ICallback, args: any[] }[],
serialPromises: { callback: ICallback, args: any[] }[],
callbacks: { callback: ICallback, args: any[] }[]
} {
if (!this[CallbacksSymbol]) {

@@ -108,3 +160,5 @@ return;

let handler = this[CallbacksSymbol][event];
let parallelPromises: Promise<any>[] = [];
let parallelPromises: { callback: ICallback, args: any[] }[] = [],
serialPromises: { callback: ICallback, args: any[] }[] = [],
callbacks: { callback: ICallback, args: any[] }[] = [];

@@ -115,40 +169,47 @@ let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event);

for (let i = 0, len = routingKeys.length; i < len; i++) {
parallelPromises.push(this.fireEvent(routingKeys[i], ...args))
parallelPromises.push({
callback: {
fn: this.fireEvent,
scope: this
}, args: [routingKeys[i], ...args]
})
}
}
if (!handler) {
return {callbacks, serialPromises, parallelPromises}
}
for (let i = handler.callbacks.length - 1; i >= 0; i--) {
let callback = handler.callbacks[i];
if (handler) {
for (let i = handler.callbacks.length - 1; i >= 0; i--) {
let callback = handler.callbacks[i];
if (!callback || !callback.fn) {
continue;
}
if (!callback || !callback.fn) {
continue;
//callback.fn.apply((callback.scope || null), args);
if (callback.options.await) {
if (callback.options.parallel) {
parallelPromises.push({callback, args})
} else {
serialPromises.push({callback, args});
}
} else {
callbacks.push({callback, args})
}
let result = callback.fn.apply((callback.scope || null), args);
if (callback.options.once) {
handler.callbacks.splice(i, 1);
if (callback.options.once) {
handler.callbacks.splice(i, 1);
if (!handler.callbacks.length && this[RoutingKeysSymbol] && this[RoutingKeysSymbol][event]) {
this[RoutingKeysSymbol][event] = undefined;
this[RoutingKeysCacheSymbol] = Object.keys(this[RoutingKeysSymbol]);
}
if (!handler.callbacks.length && this[RoutingKeysSymbol] && this[RoutingKeysSymbol][event]) {
this[RoutingKeysSymbol][event] = undefined;
this[RoutingKeysCacheSymbol] = Object.keys(this[RoutingKeysSymbol]);
}
}
if (callback.options.await) {
if (callback.options.parallel) {
parallelPromises.push(result)
} else {
await result;
}
}
}
}
if (parallelPromises.length) {
await Promise.all(parallelPromises)
}
return {callbacks, serialPromises, parallelPromises}
}

@@ -274,3 +335,9 @@

}
public iterator<T>(event: string | string[], options?: { limit?: number }): AsyncIterableIterator<T> {
let iterator = new Iterator<T>(this, event, options);
return iterator.iterate()
}
}

@@ -5,2 +5,3 @@ export interface IEventOptions {

parallel?: boolean
timeout?: number
}

@@ -7,0 +8,0 @@

@@ -23,3 +23,3 @@ {

"main": "./index.js",
"version": "6.0.14",
"version": "7.0.0",
"license": "MIT",

@@ -34,5 +34,7 @@ "repository": {

"devDependencies": {
"tslib": "^1.11.1",
"@types/bluebird": "^3.5.27",
"@types/chai": "^4.2.1",
"@types/mocha": "^5.2.7",
"@types/node": "^13.9.1",
"chai": "^4.2.0",

@@ -39,0 +41,0 @@ "mocha": "^6.2.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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