1.0.10 • Published 2 years ago

add-weak-listener v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Add weak Listener

npm version

Add weak event listeners from your components/classes based on WeakRefs. This package handles the boilerplate for you, which isn't too much anyways but not particularly good looking.

Typescript defintions are included.

Why ?

Event listeners are often a source of memory leaks. As the listener maintains a strong depedency to the callback which is in turn dependent on the object/closure, the closure/object is never Garbage collected.

See this awesome article by folks on v8 team, for a detailed explanation.

Usage

$ npm i add-weak-listener
import addWeakListener from 'add-weak-listener';

class MyComponent {
  constructor() {
    // This event listener will be cleaned by the garbage collector
    // when the an object of MyComponent is GC'd.
    addWeakListener(
      window, // Event Target window / element / socket etc
      'message', // Event type
      this.listener, // The listener callback
      { once: true } // (optional) addEventListener options
    )
  }

  private listener = (ev) => {
    // do something here.
  }
}
1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago