New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

add-weak-listener

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

add-weak-listener

Add weak event listeners based on WeakRefs

  • 1.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.
  }
}

Keywords

FAQs

Package last updated on 24 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc