detritus-utils
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -5,5 +5,5 @@ "use strict"; | ||
URL: 'https://github.com/detritusjs/utils', | ||
VERSION: '0.2.3', | ||
VERSION: '0.2.4', | ||
}); | ||
exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000; | ||
exports.DISCORD_TOKEN_EPOCH = 1293840000000; |
@@ -8,6 +8,15 @@ /// <reference types="node" /> | ||
*/ | ||
_events: any; | ||
_events: { | ||
[key: string]: any; | ||
} | undefined; | ||
/** | ||
* @ignore | ||
*/ | ||
_subscriptions: Array<EventSubscription> | undefined; | ||
constructor(); | ||
hasEventListener(name: string): boolean; | ||
subscribe(event: string | symbol, listener: (...args: any[]) => void): EventSubscription; | ||
hasEventListener(name: string | symbol): boolean; | ||
subscribe(name: string | symbol, listener: (...args: any[]) => void): EventSubscription; | ||
removeSubscription(subscription: EventSubscription): void; | ||
removeAllListeners(name: string | symbol | undefined): this; | ||
removeAllSubscriptions(): this; | ||
} | ||
@@ -14,0 +23,0 @@ export declare class EventSubscription { |
@@ -12,2 +12,6 @@ "use strict"; | ||
super(); | ||
/** | ||
* @ignore | ||
*/ | ||
this._subscriptions = undefined; | ||
Object.defineProperties(this, { | ||
@@ -17,2 +21,3 @@ _events: { enumerable: false }, | ||
_maxListeners: { enumerable: false }, | ||
_subscriptions: { enumerable: false }, | ||
}); | ||
@@ -23,7 +28,62 @@ } | ||
} | ||
subscribe(event, listener) { | ||
const subscription = new EventSubscription(this, event, listener); | ||
this.on(event, listener); | ||
subscribe(name, listener) { | ||
const subscription = new EventSubscription(this, name, listener); | ||
this.on(name, listener); | ||
if (!this._subscriptions) { | ||
this._subscriptions = []; | ||
} | ||
this._subscriptions.push(subscription); | ||
return subscription; | ||
} | ||
removeSubscription(subscription) { | ||
if (subscription.listener) { | ||
this.removeListener(subscription.name, subscription.listener); | ||
} | ||
if (this._subscriptions) { | ||
const index = this._subscriptions.indexOf(subscription); | ||
if (index !== 1) { | ||
if (this._subscriptions.length === 1) { | ||
this._subscriptions.pop(); | ||
} | ||
else { | ||
this._subscriptions.splice(index, 1); | ||
} | ||
} | ||
if (!this._subscriptions.length) { | ||
this._subscriptions = undefined; | ||
} | ||
} | ||
} | ||
removeAllListeners(name) { | ||
if (this._subscriptions) { | ||
if (name !== undefined) { | ||
for (let subscription of this._subscriptions) { | ||
if (subscription.name === name) { | ||
subscription.remove(); | ||
} | ||
} | ||
} | ||
else { | ||
while (this._subscriptions.length) { | ||
const subscription = this._subscriptions.shift(); | ||
if (subscription) { | ||
subscription.listener = null; | ||
subscription.spewer = null; | ||
} | ||
} | ||
} | ||
} | ||
return super.removeAllListeners(name); | ||
} | ||
removeAllSubscriptions() { | ||
if (this._subscriptions) { | ||
while (this._subscriptions.length) { | ||
const subscription = this._subscriptions.shift(); | ||
if (subscription) { | ||
subscription.remove(); | ||
} | ||
} | ||
} | ||
return this; | ||
} | ||
} | ||
@@ -38,4 +98,4 @@ exports.EventSpewer = EventSpewer; | ||
remove() { | ||
if (this.listener && this.spewer) { | ||
this.spewer.removeListener(this.name, this.listener); | ||
if (this.spewer) { | ||
this.spewer.removeSubscription(this); | ||
} | ||
@@ -42,0 +102,0 @@ this.listener = null; |
{ | ||
"name": "detritus-utils", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Detritus Utils", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26115
865