GithubHelp home page GithubHelp logo

linusu / unload Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pubkey/unload

0.0 2.0 0.0 44 KB

Run a piece of code when the javascript-process stops. Works in all environments (browsers, nodejs..)

License: Apache License 2.0

JavaScript 100.00%

unload's Introduction

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

unload's People

Contributors

etiktin avatar pubkey avatar zdrr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.