@duxcore/eventra
Advanced tools
Comparing version 1.0.8 to 1.1.0
import { DefaultListener, ListenerSignature } from "./types/events"; | ||
import { AnyListenerCallback } from "./types/types"; | ||
export declare class Eventra<Events extends ListenerSignature<Events> = DefaultListener> { | ||
@@ -34,2 +35,6 @@ constructor(); | ||
/** | ||
* Adds a listener that will callback after every single event execution. | ||
*/ | ||
any(listener: AnyListenerCallback): this; | ||
/** | ||
* Adds the listener function to the end of the listeners array for the event. | ||
@@ -70,4 +75,4 @@ * | ||
* Returns a reference to the eventra instance, so that calls can be chained. | ||
*/ | ||
*/ | ||
removeListener<E extends keyof Events>(eventName: E, listener: Events[E]): this; | ||
} |
@@ -49,3 +49,3 @@ "use strict"; | ||
const singular = this._singularListeners.countListeners(eventName); | ||
return (recurring + singular); | ||
return recurring + singular; | ||
} | ||
@@ -60,6 +60,13 @@ /** | ||
recurring, | ||
singular | ||
singular, | ||
}; | ||
} | ||
/** | ||
* Adds a listener that will callback after every single event execution. | ||
*/ | ||
any(listener) { | ||
this._listeners.addAny(listener); | ||
return this; | ||
} | ||
/** | ||
* Adds the listener function to the end of the listeners array for the event. | ||
@@ -109,3 +116,3 @@ * | ||
const listeners = [...eventName]; | ||
listeners.map(en => { | ||
listeners.map((en) => { | ||
this._listeners.removeEvent(en); | ||
@@ -120,3 +127,3 @@ this._singularListeners.removeEvent(en); | ||
* Returns a reference to the eventra instance, so that calls can be chained. | ||
*/ | ||
*/ | ||
removeListener(eventName, listener) { | ||
@@ -123,0 +130,0 @@ this._listeners.removeListener(eventName, listener); |
@@ -7,2 +7,3 @@ import { ListenerArrayMode, ListenerArrayOptions, ListenerCallback } from "../types/types"; | ||
private _internalStorage; | ||
private _anyListeners; | ||
constructor(options?: ListenerArrayOptions); | ||
@@ -14,2 +15,3 @@ get mode(): ListenerArrayMode; | ||
add(eventName: string | any, listener: ListenerCallback): this; | ||
addAny(listener: ListenerCallback): this; | ||
prepend(eventName: string | any, listener: ListenerCallback): this; | ||
@@ -16,0 +18,0 @@ removeListener(eventName: string | any, listener: ListenerCallback): this; |
@@ -10,2 +10,3 @@ "use strict"; | ||
this._internalStorage = new Collection_1.default(); | ||
this._anyListeners = []; | ||
if (!options) | ||
@@ -16,4 +17,8 @@ this._options = { mode: "recurring" }; | ||
} | ||
get mode() { return this._options.mode; } | ||
get storage() { return this._internalStorage; } | ||
get mode() { | ||
return this._options.mode; | ||
} | ||
get storage() { | ||
return this._internalStorage; | ||
} | ||
_updateInternalStorage(collection) { | ||
@@ -41,2 +46,6 @@ if (!collection) | ||
} | ||
addAny(listener) { | ||
this._anyListeners.push(listener); | ||
return this; | ||
} | ||
prepend(eventName, listener) { | ||
@@ -66,3 +75,2 @@ let carbonCopy = this._cloneInternalStorage(); | ||
} | ||
; | ||
const index = event.indexOf(listener); | ||
@@ -98,2 +106,3 @@ if (index > -1) | ||
let event = carbonCopy.get(eventName); | ||
this._anyListeners.map((listener) => listener(eventName, ...args)); | ||
if (!event) | ||
@@ -105,3 +114,3 @@ return this; | ||
}); | ||
if (this.mode == 'once') | ||
if (this.mode == "once") | ||
carbonCopy.delete(eventName); | ||
@@ -108,0 +117,0 @@ this._updateInternalStorage(carbonCopy); |
export declare type ListenerCallback = (...args: any[]) => void; | ||
export declare type AnyListenerCallback = (eventName: string, ...args: any[]) => void; | ||
export declare type ListenerArrayMode = "recurring" | "once"; | ||
@@ -3,0 +4,0 @@ export interface ListenerArrayOptions { |
@@ -16,3 +16,2 @@ export interface CollectionConstructor { | ||
static readonly default: typeof Collection; | ||
['constructor']: CollectionConstructor; | ||
/** | ||
@@ -19,0 +18,0 @@ * Identical to [Map.get()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get). |
@@ -11,2 +11,3 @@ "use strict"; | ||
class Collection extends Map { | ||
// public ['constructor']: CollectionConstructor; | ||
/** | ||
@@ -74,3 +75,3 @@ * Identical to [Map.get()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get). | ||
first(amount) { | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return this.values().next().value; | ||
@@ -84,3 +85,3 @@ if (amount < 0) | ||
firstKey(amount) { | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return this.keys().next().value; | ||
@@ -95,3 +96,3 @@ if (amount < 0) | ||
const arr = [...this.values()]; | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return arr[arr.length - 1]; | ||
@@ -106,3 +107,3 @@ if (amount < 0) | ||
const arr = [...this.keys()]; | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return arr[arr.length - 1]; | ||
@@ -117,3 +118,3 @@ if (amount < 0) | ||
const arr = [...this.values()]; | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
@@ -126,3 +127,3 @@ if (!arr.length || !amount) | ||
const arr = [...this.keys()]; | ||
if (typeof amount === 'undefined') | ||
if (typeof amount === "undefined") | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
@@ -134,3 +135,3 @@ if (!arr.length || !amount) | ||
find(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -144,3 +145,3 @@ for (const [key, val] of this) { | ||
findKey(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -154,3 +155,3 @@ for (const [key, val] of this) { | ||
sweep(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -165,3 +166,3 @@ const previousSize = this.size; | ||
filter(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -176,3 +177,3 @@ const results = new this.constructor[Symbol.species](); | ||
partition(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -198,3 +199,3 @@ const results = [ | ||
map(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -208,3 +209,3 @@ const iter = this.entries(); | ||
mapValues(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -217,3 +218,3 @@ const coll = new this.constructor[Symbol.species](); | ||
some(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -227,3 +228,3 @@ for (const [key, val] of this) { | ||
every(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -247,3 +248,3 @@ for (const [key, val] of this) { | ||
let accumulator; | ||
if (typeof initialValue !== 'undefined') { | ||
if (typeof initialValue !== "undefined") { | ||
accumulator = initialValue; | ||
@@ -265,3 +266,3 @@ for (const [key, val] of this) | ||
if (first) { | ||
throw new TypeError('Reduce of empty collection with no initial value'); | ||
throw new TypeError("Reduce of empty collection with no initial value"); | ||
} | ||
@@ -275,3 +276,3 @@ return accumulator; | ||
tap(fn, thisArg) { | ||
if (typeof thisArg !== 'undefined') | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
@@ -390,3 +391,3 @@ fn(this); | ||
static defaultSort(firstValue, secondValue) { | ||
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1; | ||
return (Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1); | ||
} | ||
@@ -393,0 +394,0 @@ } |
{ | ||
"name": "@duxcore/eventra", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A lightweight, but powerful JavaScript event manger that will work in both in the browser and node.js.", |
116215
1733