Skip to content

Diagnostics

Vitaly Tomilov edited this page Oct 1, 2024 · 32 revisions

This library has embedded features for diagnosing event subscription leaks.

Dead subscriptions result in memory leaking, while emitting events into a growing number of zombie subscriptions may result in fast performance degradation.

First step toward easy subscriptions diagnostics is to name all your subscriptions:

const sub = event.subscribe(handler, {name: 'sub-name'});

e.g. when subscribing inside a class, you might use {name: MyClass.name}.

Then use method getStat on the event object to pull the usage statistics that relies on subscription names. In most cases, this will immediately tell you which subscriptions are leaked and where.


In addition, SubEventCount exposes onCount event to help you manage and track subscriptions.

In the example below we use event onCount to output named events counts:

const event = new SubEventCount();

event.onCount.subscribe((info: ISubCountChange) => {
    const stat = event.getStat();
    console.log('Named Subs:', stat.named);
});

// generating subscriptions...
const sub1 = event.subscribe(()=>{}, {name: 'one'});
const sub2 = event.subscribe(()=>{}, {name: 'two'});
const sub3 = event.subscribe(()=>{}, {name: 'two'});

This will log:

Named Subs: {'one': 1}
Named Subs: {'one': 1, 'two': 1}
Named Subs: {'one': 1, 'two': 2}

And you can use option minUse of getStat to avoid logging subscriptions with low use counts.

Clone this wiki locally