{"id":13657555,"url":"https://github.com/pubkey/unload","last_synced_at":"2025-05-16T08:04:21.540Z","repository":{"id":40778925,"uuid":"75946645","full_name":"pubkey/unload","owner":"pubkey","description":"Run a piece of code when the javascript process stops. Works in all environments (browsers, nodejs..)","archived":false,"fork":false,"pushed_at":"2025-05-02T01:35:26.000Z","size":206,"stargazers_count":55,"open_issues_count":3,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-02T02:36:48.821Z","etag":null,"topics":["browser","exitcode","nodejs","unload"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pubkey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-12-08T14:49:54.000Z","updated_at":"2025-05-02T01:35:11.000Z","dependencies_parsed_at":"2024-04-12T00:22:13.137Z","dependency_job_id":"b4c9d000-284b-4e7c-91bc-ea3074797aaa","html_url":"https://github.com/pubkey/unload","commit_stats":{"total_commits":279,"total_committers":6,"mean_commits":46.5,"dds":"0.31182795698924726","last_synced_commit":"1bcfbb4205535f78d6086269c69dd4e8a8ed9704"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Funload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Funload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Funload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Funload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pubkey","download_url":"https://codeload.github.com/pubkey/unload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254493379,"owners_count":22080126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browser","exitcode","nodejs","unload"],"created_at":"2024-08-02T05:00:45.058Z","updated_at":"2025-05-16T08:04:20.629Z","avatar_url":"https://github.com/pubkey.png","language":"JavaScript","readme":"# javascript: unload\n\nRun 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**.\n\nYou should use this module when your write a npm library where you dont know in which environments the users will run it.\n\n# What does `unload` handle?\n\nWhen nodejs:\n```js\nprocess.on('beforeExit');\nprocess.on('exit');\nprocess.on('SIGINT'); // catches ctrl+c event\nprocess.on('uncaughtException'); // catches uncaught exceptions\n```\n\nWhen browser:\n```js\nwindow.addEventListener('beforeunload'); // closing of normal browser-window\nwindow.addEventListener('unload'); // closed inside of iframe\n```\n\n# Usage\n\nInstallation:\n\n`npm install unload --save`\n\nAdd a function which runs when the process exits:\n\n```javascript\nvar unload = require('unload');\nunload.add(function(){\n    console.log('Ouch, I\\'m dying.');\n});\n```\n\nAdd and remove the function (It will no longer run when the process exits):\n\n```javascript\nvar unload = require('unload');\nvar ret = unload.add(function(){\n    console.log('Ouch, I\\'m dying.');\n});\n\nret.remove(); // removes the event-handler\n```\n\nRun all previously added functions:\n\n```javascript\nvar unload = require('unload');\nunload.add(function(){\n    console.log('Ouch, I\\'m dying.');\n});\n\nunload.runAll();\n```\n\nRemove all added functions (They will no longer run when the process exits):\n```javascript\nvar unload = require('unload');\nunload.add(function(){\n    console.log('Ouch, I\\'m dying.');\n});\n\nunload.removeAll();\n```\n\n\n# Sponsored by\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://rxdb.info/?utm_source=github\u0026utm_medium=repo\u0026utm_campaign=github-unload\"\u003e\n        \u003cimg\n            src=\"https://rxdb.info/files/logo/logo_text.svg\"\n            alt=\"Sponsored by RxDB\"\n            width=\"300\"\n         /\u003e\n         \u003cbr /\u003e\n         \u003cbr /\u003e\n         \u003cspan\u003eThe \u003cb\u003eJavaScript Database\u003c/b\u003e\u003c/span\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Funload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpubkey%2Funload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Funload/lists"}