Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes memory-leaks #103

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function assertListener(listener) {
function getListeners(instance, eventName) {
const events = eventsMap.get(instance);
if (!events.has(eventName)) {
events.set(eventName, new Set());
return null;
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

return events.get(eventName);
Expand All @@ -38,7 +38,7 @@ function getEventProducers(instance, eventName) {
const key = typeof eventName === 'string' || typeof eventName === 'symbol' || typeof eventName === 'number' ? eventName : anyProducer;
const producers = producersMap.get(instance);
if (!producers.has(key)) {
producers.set(key, new Set());
return null;
}

return producers.get(key);
Expand Down Expand Up @@ -79,7 +79,14 @@ function iterator(instance, eventNames) {
};

for (const eventName of eventNames) {
getEventProducers(instance, eventName).add(producer);
let set = getEventProducers(instance, eventName);
if (!set) {
set = new Set();
const producers = producersMap.get(instance);
producers.set(eventName, set);
}

set.add(producer);
}

return {
Expand Down Expand Up @@ -111,7 +118,14 @@ function iterator(instance, eventNames) {
queue = undefined;

for (const eventName of eventNames) {
getEventProducers(instance, eventName).delete(producer);
const set = getEventProducers(instance, eventName);
if (set) {
set.delete(producer);
if (set.size === 0) {
const producers = producersMap.get(instance);
producers.delete(eventName);
}
}
}

flush();
Expand Down Expand Up @@ -221,6 +235,9 @@ class Emittery {
anyMap.set(this, new Set());
eventsMap.set(this, new Map());
producersMap.set(this, new Map());

producersMap.get(this).set(anyProducer, new Set());

this.debug = options.debug || {};

if (this.debug.enabled === undefined) {
Expand Down Expand Up @@ -259,7 +276,14 @@ class Emittery {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
for (const eventName of eventNames) {
assertEventName(eventName);
getListeners(this, eventName).add(listener);
let set = getListeners(this, eventName);
if (!set) {
set = new Set();
const events = eventsMap.get(this);
events.set(eventName, set);
}

set.add(listener);

this.logIfDebugEnabled('subscribe', eventName, undefined);

Expand All @@ -277,7 +301,14 @@ class Emittery {
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
for (const eventName of eventNames) {
assertEventName(eventName);
getListeners(this, eventName).delete(listener);
const set = getListeners(this, eventName);
if (set) {
set.delete(listener);
if (set.size === 0) {
const events = eventsMap.get(this);
events.delete(eventName);
}
}

this.logIfDebugEnabled('unsubscribe', eventName, undefined);

Expand Down Expand Up @@ -321,7 +352,7 @@ class Emittery {

enqueueProducers(this, eventName, eventData);

const listeners = getListeners(this, eventName);
const listeners = getListeners(this, eventName) || new Set();
const anyListeners = anyMap.get(this);
const staticListeners = [...listeners];
const staticAnyListeners = isMetaEvent(eventName) ? [] : [...anyListeners];
Expand Down Expand Up @@ -350,7 +381,7 @@ class Emittery {

this.logIfDebugEnabled('emitSerial', eventName, eventData);

const listeners = getListeners(this, eventName);
const listeners = getListeners(this, eventName) || new Set();
const anyListeners = anyMap.get(this);
const staticListeners = [...listeners];
const staticAnyListeners = [...anyListeners];
Expand Down Expand Up @@ -401,28 +432,34 @@ class Emittery {
this.logIfDebugEnabled('clear', eventName, undefined);

if (typeof eventName === 'string' || typeof eventName === 'symbol' || typeof eventName === 'number') {
getListeners(this, eventName).clear();
const set = getListeners(this, eventName);
if (set) {
set.clear();
}

const producers = getEventProducers(this, eventName);
if (producers) {
for (const producer of producers) {
producer.finish();
}

for (const producer of producers) {
producer.finish();
producers.clear();
}

producers.clear();
} else {
anyMap.get(this).clear();

for (const listeners of eventsMap.get(this).values()) {
for (const [eventName, listeners] of eventsMap.get(this).entries()) {
listeners.clear();
eventsMap.get(this).delete(eventName);
}

for (const producers of producersMap.get(this).values()) {
for (const [eventName, producers] of producersMap.get(this).entries()) {
for (const producer of producers) {
producer.finish();
}

producers.clear();
producersMap.get(this).delete(eventName);
}
}
}
Expand All @@ -434,8 +471,8 @@ class Emittery {

for (const eventName of eventNames) {
if (typeof eventName === 'string') {
count += anyMap.get(this).size + getListeners(this, eventName).size +
getEventProducers(this, eventName).size + getEventProducers(this).size;
count += anyMap.get(this).size + (getListeners(this, eventName) || new Set()).size +
(getEventProducers(this, eventName) || new Set()).size + (getEventProducers(this) || new Set()).size;
continue;
}

Expand Down