Skip to content

Commit

Permalink
feat(exit-hook): new package (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored Jan 3, 2024
2 parents 96633d3 + b6dd6dd commit 83a888a
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Here is a brief overview of the included libraries:
8. [`nano-build`](./packages/nano-build/README.md): This is a tool for building/bundling ECMAScript, TypeScript, and JavaScript libraries. It's user-friendly, requires no setup, follows best practices, has no dependencies, and uses esbuild for improved performance.
9. [`type-helper`](./packages/type-helper/README.md): Collection of useful typescript type helpers.
10. [`wait`](./packages/wait/README.md): Comprehensive toolkit for managing asynchronous operations.
11. [`exit-hook`](./packages/exit-hook/README.md): A utility for registering exit handlers in Node.js.
<!-- 9. [`async-queue`](./packages/async-queue/README.md): This is a utility for managing asynchronous operations in a queue. -->

For more detailed information and guidelines on how to use each package, please refer to to each package's README.
21 changes: 21 additions & 0 deletions packages/exit-hook/README.md
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);
```
74 changes: 74 additions & 0 deletions packages/exit-hook/package.json
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"
}
}
76 changes: 76 additions & 0 deletions packages/exit-hook/src/main.ts
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_);
10 changes: 10 additions & 0 deletions packages/exit-hook/tsconfig.json
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"]
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ __metadata:
languageName: unknown
linkType: soft

"@alwatr/exit-hook@workspace:packages/exit-hook":
version: 0.0.0-use.local
resolution: "@alwatr/exit-hook@workspace:packages/exit-hook"
dependencies:
"@alwatr/nano-build": "workspace:^"
"@alwatr/prettier-config": "workspace:^"
"@alwatr/tsconfig-base": "workspace:^"
"@types/node": "npm:^20.10.6"
typescript: "npm:^5.3.3"
languageName: unknown
linkType: soft

"@alwatr/flat-string@workspace:packages/flat-string":
version: 0.0.0-use.local
resolution: "@alwatr/flat-string@workspace:packages/flat-string"
Expand Down

0 comments on commit 83a888a

Please sign in to comment.