Socket
Socket
Sign inDemoInstall

unload

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    unload

Execute code when the js-process exits. On all javascript-environments


Version published
Weekly downloads
1.9M
increased by5.11%
Maintainers
1
Install size
36.8 kB
Created
Weekly downloads
 

Readme

Source

javascript: unload

Run a piece of code whenever the javascript process stops/exits/quits. On browsers, nodejs, electron, react-native, workers, iframes. It also ensures that the exit-function is called only once.

You should use this module when your write a npm library where you dont know in which environments the users will run it.

What does unload handle?

When nodejs:

process.on('beforeExit');
process.on('exit');
process.on('SIGINT'); // catches ctrl+c event
process.on('uncaughtException'); // catches uncaught exceptions

When browser:

window.addEventListener('beforeunload'); // closing of normal browser-window
window.addEventListener('unload'); // closed inside of iframe

Usage

Installation:

npm install unload --save

Add a function which runs when the process exits:

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

Add and remove the function (It will no longer run when the process exits):

var unload = require('unload');
var ret = unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

ret.remove(); // removes the event-handler

Run all previously added functions:

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.runAll();

Remove all added functions (They will no longer run when the process exits):

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.removeAll();

Sponsored by

Sponsored by RxDB

The JavaScript Database

Keywords

FAQs

Last updated on 28 Dec 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc