|
1 | 1 | # signal-exit |
2 | 2 |
|
3 | | -[](https://travis-ci.org/tapjs/signal-exit) |
4 | | -[](https://coveralls.io/r/tapjs/signal-exit?branch=master) |
5 | | -[](https://www.npmjs.com/package/signal-exit) |
6 | | -[](https://github.com/conventional-changelog/standard-version) |
7 | | - |
8 | 3 | When you want to fire an event no matter how a process exits: |
9 | 4 |
|
10 | | -* reaching the end of execution. |
11 | | -* explicitly having `process.exit(code)` called. |
12 | | -* having `process.kill(pid, sig)` called. |
13 | | -* receiving a fatal signal from outside the process |
| 5 | +- reaching the end of execution. |
| 6 | +- explicitly having `process.exit(code)` called. |
| 7 | +- having `process.kill(pid, sig)` called. |
| 8 | +- receiving a fatal signal from outside the process |
14 | 9 |
|
15 | 10 | Use `signal-exit`. |
16 | 11 |
|
17 | 12 | ```js |
18 | | -var onExit = require('signal-exit') |
| 13 | +// Hybrid module, either works |
| 14 | +import { onExit } from 'signal-exit' |
| 15 | +// or: |
| 16 | +// const { onExit } = require('signal-exit') |
19 | 17 |
|
20 | | -onExit(function (code, signal) { |
21 | | - console.log('process exited!') |
| 18 | +onExit((code, signal) => { |
| 19 | + console.log('process exited!', code, signal) |
22 | 20 | }) |
23 | 21 | ``` |
24 | 22 |
|
25 | 23 | ## API |
26 | 24 |
|
27 | | -`var remove = onExit(function (code, signal) {}, options)` |
| 25 | +`remove = onExit((code, signal) => {}, options)` |
| 26 | + |
| 27 | +The return value of the function is a function that will remove |
| 28 | +the handler. |
| 29 | + |
| 30 | +Note that the function _only_ fires for signals if the signal |
| 31 | +would cause the process to exit. That is, there are no other |
| 32 | +listeners, and it is a fatal signal. |
| 33 | + |
| 34 | +If the global `process` object is not suitable for this purpose |
| 35 | +(ie, it's unset, or doesn't have an `emit` method, etc.) then the |
| 36 | +`onExit` function is a no-op that returns a no-op `remove` method. |
| 37 | + |
| 38 | +### Options |
28 | 39 |
|
29 | | -The return value of the function is a function that will remove the |
30 | | -handler. |
| 40 | +- `alwaysLast`: Run this handler after any other signal or exit |
| 41 | + handlers. This causes `process.emit` to be monkeypatched. |
31 | 42 |
|
32 | | -Note that the function *only* fires for signals if the signal would |
33 | | -cause the process to exit. That is, there are no other listeners, and |
34 | | -it is a fatal signal. |
| 43 | +### Browser Fallback |
35 | 44 |
|
36 | | -## Options |
| 45 | +The `'signal-exit/browser'` module is the same fallback shim that |
| 46 | +just doesn't do anything, but presents the same function |
| 47 | +interface. |
37 | 48 |
|
38 | | -* `alwaysLast`: Run this handler after any other signal or exit |
39 | | - handlers. This causes `process.emit` to be monkeypatched. |
| 49 | +Patches welcome to add something that hooks onto |
| 50 | +`window.onbeforeunload` or similar, but it might just not be a |
| 51 | +thing that makes sense there. |
0 commit comments