@hardlydifficult/daemon 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +57 -6
  2. package/package.json +3 -2
package/README.md CHANGED
@@ -88,7 +88,38 @@ untrap();
88
88
  ### Behavior Notes
89
89
 
90
90
  - Errors in teardown functions are caught and logged, allowing remaining functions to complete.
91
- - Signal handlers are added only once per process and cleaned up automatically when untrap() is called.
91
+ - Signal handlers are added only once per process and cleaned up automatically when `untrap()` is called.
92
+
93
+ ### Example: Basic Teardown
94
+
95
+ ```typescript
96
+ import { createTeardown } from "@hardlydifficult/daemon";
97
+
98
+ const teardown = createTeardown();
99
+
100
+ teardown.add(() => console.log("Closing database"));
101
+ teardown.add(() => console.log("Stopping server"));
102
+ teardown.trapSignals();
103
+
104
+ await teardown.run();
105
+ // Output:
106
+ // Stopping server
107
+ // Closing database
108
+ ```
109
+
110
+ ### Unregistering Teardown Functions
111
+
112
+ Teardown functions can be unregistered before `run()` is called.
113
+
114
+ ```typescript
115
+ const teardown = createTeardown();
116
+
117
+ const unregister = teardown.add(() => cleanupA());
118
+ teardown.add(() => cleanupB());
119
+
120
+ unregister(); // Removes cleanupA from the teardown list
121
+ await teardown.run(); // Only cleanupB runs
122
+ ```
92
123
 
93
124
  ## Continuous Loop with `runContinuousLoop`
94
125
 
@@ -99,11 +130,11 @@ Runs a recurring task in a loop with built-in signal handling, dynamic delays, a
99
130
  | Option | Type | Description |
100
131
  |--------|------|-------------|
101
132
  | `intervalSeconds` | `number` | Default delay between cycles in seconds |
102
- | `runCycle` | `() => Promise<RunCycleResult>` | Main function executed per cycle |
103
- | `getNextDelayMs` | `() => Delay \| undefined` | Optional custom delay resolver |
104
- | `onCycleError` | `ErrorHandler` | Custom error handling strategy |
105
- | `onShutdown` | `() => Promise<void>` | Cleanup hook called on shutdown |
106
- | `logger` | `ContinuousLoopLogger` | Optional logger (defaults to console) |
133
+ | `runCycle` | `(isShutdownRequested: () => boolean) => Promise<RunCycleResult>` | Main function executed per cycle |
134
+ | `getNextDelayMs?` | `(result, context) => ContinuousLoopDelay \| undefined` | Optional custom delay resolver |
135
+ | `onCycleError?` | `ContinuousLoopErrorHandler` | Custom error handling strategy |
136
+ | `onShutdown?` | `() => void \| Promise<void>` | Cleanup hook called on shutdown |
137
+ | `logger?` | `ContinuousLoopLogger` | Optional logger (defaults to console) |
107
138
 
108
139
  ### Return Values from `runCycle`
109
140
 
@@ -158,6 +189,26 @@ await runContinuousLoop({
158
189
  });
159
190
  ```
160
191
 
192
+ ### Dynamic Delays
193
+
194
+ Use `getNextDelayMs` to derive delays from domain results:
195
+
196
+ ```typescript
197
+ type CycleResult = { delaySeconds: number } | { stop: true };
198
+
199
+ await runContinuousLoop({
200
+ intervalSeconds: 60, // default fallback
201
+ async runCycle() {
202
+ const result = await fetchStatus();
203
+ if (result.stopped) return { stop: true };
204
+ return { delaySeconds: result.delay };
205
+ },
206
+ getNextDelayMs(result) {
207
+ return "delaySeconds" in result ? result.delaySeconds * 1000 : undefined;
208
+ }
209
+ });
210
+ ```
211
+
161
212
  ### Shutdown Signals
162
213
 
163
214
  The loop responds to `SIGINT` and `SIGTERM` by stopping after the current cycle completes. Use `isShutdownRequested()` inside `runCycle` to abort long-running work:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/daemon",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [
@@ -27,7 +27,8 @@
27
27
  ".": {
28
28
  "types": "./dist/index.d.ts",
29
29
  "import": "./dist/index.js",
30
- "require": "./dist/index.js"
30
+ "require": "./dist/index.js",
31
+ "default": "./dist/index.js"
31
32
  }
32
33
  }
33
34
  }