ilusm.dev

sig

In-process logical signals - create a signal registry, bind a handler thunk to a named signal, unbind it, emit a signal by name (calling its handler with an argument), check whether a signal has a handler, list all registered signal names, and clear all handlers. For application-level events, not OS signal delivery.

Load with: use sig

What this module does

sig is a lightweight in-process signal bus. Each named signal has at most one handler at a time (last registration wins). Emitting an unregistered signal is a no-op - no crash. This makes it safe to emit events speculatively and let components opt-in by registering handlers.

OS-level signal delivery (SIGTERM, SIGHUP, etc.) is host-specific and outside the syscall table. For OS signals use the daem module. sig is for intra-process application events.

Quick example

use sig

reg = signe()

# Register handlers
reg = sigon(reg, "shutdown", \(arg) prn("shutting down: " + str(arg)))
reg = sigon(reg, "reload",   \(arg) load_config())

# Check
prn(sigha(reg, "shutdown"))  # tru
prn(signm(reg))              # ["shutdown", "reload"]

# Emit
sigem(reg, "shutdown", "user request")
sigem(reg, "missing", nil)  # no-op

# Unregister
reg = sigof(reg, "reload")

# Clear all
reg = sigcl(reg)

Functions

Registry

signe()

Creates a new empty signal registry {h:{}}.

sigcl(reg)

Removes all handlers from the registry. Returns the cleared registry.

Registration

sigon(reg, name, thunk)

Registers a handler function for signal name. The previous handler (if any) is replaced. thunk must be a function.

sigof(reg, name)

Unregisters the handler for name. Returns the updated registry.

Emission and inspection

sigem(reg, name, arg)

Calls the handler registered for name with arg. Returns the handler's return value, or nil if no handler is registered. Never throws for missing signals.

sigha(reg, name)

Returns tru if a handler is registered for name.

signm(reg)

Returns the list of all registered signal names.

Notes

  • One handler per signal name - use obs if you need multiple subscribers.
  • Requires eru.