Skip to content

Commit

Permalink
core: refactor signal handling
Browse files Browse the repository at this point in the history
Replace the current signal API with one following a similar model to
EventTarget, a la Deno.

Before

```
const sh = tjs.signal('SIGINT', handleSigInt);
// sh needs to be alive or the signal handler is destroyed.
```

After

```
tjs.addSignalListener('SIGINT', handleSigInt);
// No need to use a handle, it can be removed with
// tjs.removeSignalListener.
```

Fixes: #476
Ref: #477
  • Loading branch information
saghul committed Apr 9, 2024
1 parent 9c4ea9c commit 184ecdd
Show file tree
Hide file tree
Showing 6 changed files with 10,104 additions and 9,952 deletions.
43 changes: 21 additions & 22 deletions docs/types/txikijs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,34 @@ declare global {
| 'SIGPOLL' | 'SIGPWR' | 'SIGSYS';

/**
* Signal handler function.
* Signal listener function.
*/
type SignalHandlerFunction = () => void;

interface SignalHandler {
/**
* The signal that this signal handler was registered for.
*/
signal: Signal;

/**
* Stop the signal handler. The registered signal handler function
* will no longer be called.
*/
close(): void;
}

type SignalListener = () => void;

/**
* Registers a handler for the given signal.
* Registers a listener for the given signal.
*
* ```js
* const h = tjs.signal('SIGINT', handleSigint);
* tjs.addSignalListener('SIGINT', handleSigint);
* ```
*
* @param sig Which signal to register a handler for.
* @param handler Handler function.
* @param sig Which signal to register a listener for.
* @param listener Listener function.
*/
function signal(sig: Signal, handler: SignalHandlerFunction): SignalHandler;

function addSignalListener(sig: Signal, listener: SignalListener): void;

/**
* Un-registers a listener for the given signal.
*
* ```js
* tjs.removeSignalListener('SIGINT', handleSigint);
* ```
*
* @param sig Which signal to un-register a listener for.
* @param listener Listener function.
*/
function removeSignalListener(sig: Signal, listener: SignalListener): void;

/**
* Send a signal to a process.
*
Expand Down
Loading

0 comments on commit 184ecdd

Please sign in to comment.