-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Exit Hook | ||
|
||
A utility for registering exit handlers in Node.js. | ||
|
||
## Installation | ||
|
||
```bash | ||
yarn add @alwatr/exit-hook | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import {exitHook} from '@alwatr/exit-hook'; | ||
|
||
const saveAllData = () => { | ||
// save all data | ||
}; | ||
|
||
exitHook(saveAllData); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"name": "@alwatr/exit-hook", | ||
"version": "0.0.0", | ||
"description": "A utility for registering exit handlers in Node.js.", | ||
"author": "S. Ali Mihandoost <[email protected]>", | ||
"keywords": [ | ||
"exit-hook", | ||
"hook", | ||
"exit", | ||
"cross-platform", | ||
"ECMAScript", | ||
"typescript", | ||
"javascript", | ||
"node", | ||
"nodejs", | ||
"esm", | ||
"module", | ||
"utility", | ||
"util", | ||
"utils", | ||
"nanolib", | ||
"alwatr" | ||
], | ||
"type": "module", | ||
"main": "./dist/main.cjs", | ||
"module": "./dist/main.mjs", | ||
"types": "./dist/main.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/main.mjs", | ||
"require": "./dist/main.cjs", | ||
"types": "./dist/main.d.ts" | ||
} | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"**/*.{js,mjs,cjs,map,d.ts,html,md}", | ||
"!demo/**/*" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https:/Alwatr/nanolib", | ||
"directory": "packages/exit-hook" | ||
}, | ||
"homepage": "https:/Alwatr/nanolib/tree/next/packages/exit-hook#readme", | ||
"bugs": { | ||
"url": "https:/Alwatr/nanolib/issues" | ||
}, | ||
"prettier": "@alwatr/prettier-config", | ||
"scripts": { | ||
"b": "yarn run build", | ||
"w": "yarn run watch", | ||
"c": "yarn run clean", | ||
"cb": "yarn run clean && yarn run build", | ||
"d": "yarn run build:es && ALWATR_DEBUG=1 yarn node", | ||
"build": "yarn run build:ts & yarn run build:es", | ||
"build:es": "nano-build --preset=module", | ||
"build:ts": "tsc --build", | ||
"watch": "yarn run watch:ts & yarn run watch:es", | ||
"watch:es": "yarn run build:es --watch", | ||
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput", | ||
"clean": "rm -rfv dist *.tsbuildinfo" | ||
}, | ||
"devDependencies": { | ||
"@alwatr/nano-build": "workspace:^", | ||
"@alwatr/prettier-config": "workspace:^", | ||
"@alwatr/tsconfig-base": "workspace:^", | ||
"@types/node": "^20.10.6", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Array of callback functions to be called when the process is exiting. | ||
*/ | ||
const callbacks: (() => void)[] = []; | ||
|
||
/** | ||
* True whether the process is exiting to prevent calling the callbacks more than once. | ||
*/ | ||
let exiting = false; | ||
|
||
/** | ||
* Add a callback function to be called when the process is exiting. | ||
* | ||
* @param callback The callback function to be called when the process is exiting. | ||
* | ||
* @example | ||
* ```typescript | ||
* const saveAllData = () => { | ||
* // save all data | ||
* }; | ||
* | ||
* existHook(saveAllData); | ||
* ``` | ||
*/ | ||
export function existHook(callback: () => void): void { | ||
callbacks.push(callback); | ||
} | ||
|
||
/** | ||
* A once callback to be called on process exit event. | ||
*/ | ||
function onExit_() { | ||
if (exiting === true) return; | ||
exiting = true; | ||
|
||
for (const callback of callbacks) { | ||
try { | ||
callback(); | ||
} | ||
catch (error) { | ||
console.error('Error in exit hook callback:', error); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This event emitted when Node.js empties its event loop and has no additional work to schedule. | ||
* Normally, the Node.js process will exit when there is no work scheduled, | ||
* but a listener registered on the 'beforeExit' event can make asynchronous calls, and thereby cause the Node.js process to continue. | ||
* | ||
* @see https://nodejs.org/api/process.html#event-beforeexit | ||
*/ | ||
process.once('beforeExit', onExit_); | ||
|
||
/** | ||
* This event is emitted when the Node.js process is about to exit as a result of either: | ||
* 1- The `process.exit()` method being called explicitly. | ||
* 2- The Node.js event loop no longer having any additional work to perform. | ||
* | ||
* @see https://nodejs.org/api/process.html#event-exit | ||
*/ | ||
process.once('exit', onExit_); | ||
|
||
/** | ||
* This event is emitted in terminal mode before exiting with code 128 + signal number. | ||
* | ||
* @see https://nodejs.org/api/process.html#signal-events | ||
*/ | ||
process.once('SIGTERM', onExit_); | ||
|
||
/** | ||
* This event is emitted when `Ctrl+C` is pressed. | ||
* | ||
* @see https://nodejs.org/api/process.html#signal-events | ||
*/ | ||
process.once('SIGINT', onExit_); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "@alwatr/tsconfig-base/tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"emitDeclarationOnly": true, | ||
"composite": true, | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters