JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2049920
  • Score
    100M100P100Q195821F
  • License Apache-2.0

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

Package Exports

  • unload

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (unload) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

javascript: unload

Run a piece of code whenever the javascript-process stops/exits/quits. On browsers, nodejs, electron, react-native, .net-core. 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')
process.on('uncaughtException')

When browser:

window.addEventListener('beforeunload')

When electron:

app.on('window-all-closed')

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 stopListen = unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

stopListen(); // removes the event-listeners

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();