@crup/react-timer-hook 0.0.1-alpha.11 → 0.0.1-alpha.12

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 +81 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @crup/react-timer-hook
2
2
 
3
- > ~1.2 kB timer core for React, with opt-in batteries for schedules, diagnostics, duration math, and many independent timers.
3
+ > A lightweight React hooks library for building timers, stopwatches, and real-time clocks with minimal boilerplate.
4
4
 
5
5
  [![npm alpha](https://img.shields.io/npm/v/%40crup%2Freact-timer-hook/alpha?label=npm%20alpha&color=00b894)](https://www.npmjs.com/package/@crup/react-timer-hook?activeTab=versions)
6
6
  [![npm downloads](https://img.shields.io/npm/dm/%40crup%2Freact-timer-hook?color=0f766e)](https://www.npmjs.com/package/@crup/react-timer-hook)
@@ -17,7 +17,7 @@
17
17
 
18
18
  Timer hooks look simple until real apps need pause/resume semantics, Strict Mode cleanup, async callbacks, polling that does not overlap, and lists with dozens of independent timers.
19
19
 
20
- `@crup/react-timer-hook` starts with a tiny core and lets your app compose the heavier pieces only when it needs them:
20
+ `@crup/react-timer-hook` starts with a ~1.2 kB timer core and lets your app compose the heavier pieces only when it needs them:
21
21
 
22
22
  - ⏱️ `useTimer()` from the root package for one lifecycle: stopwatch, countdown, clock, or custom flow.
23
23
  - 🔋 Batteries are optional: schedules, timer groups, duration helpers, and diagnostics live in subpath imports.
@@ -143,6 +143,85 @@ const timers = useTimerGroup({
143
143
  });
144
144
  ```
145
145
 
146
+ ## API tables
147
+
148
+ ### `useTimer()` settings
149
+
150
+ | Key | Type | Required | Description |
151
+ | --- | --- | --- | --- |
152
+ | `autoStart` | `boolean` | No | Starts the lifecycle after mount. Defaults to `false`. |
153
+ | `updateIntervalMs` | `number` | No | Render/update cadence in milliseconds. Defaults to `1000`. This does not define elapsed time; elapsed time is calculated from timestamps. Use a smaller value like `100` or `20` when the UI needs finer updates. |
154
+ | `endWhen` | `(snapshot) => boolean` | No | Ends the lifecycle when it returns `true`. Use this for countdowns, timeouts, and custom stop conditions. |
155
+ | `onEnd` | `(snapshot, controls) => void \| Promise<void>` | No | Called once per generation when `endWhen` ends the lifecycle. `restart()` creates a new generation. |
156
+
157
+ ### `useScheduledTimer()` settings
158
+
159
+ Import from `@crup/react-timer-hook/schedules` when you need polling or scheduled side effects.
160
+
161
+ | Key | Type | Required | Description |
162
+ | --- | --- | --- | --- |
163
+ | `autoStart` | `boolean` | No | Starts the lifecycle after mount. Defaults to `false`. |
164
+ | `updateIntervalMs` | `number` | No | Render/update cadence in milliseconds. Defaults to `1000`. Scheduled callbacks can run on their own cadence. |
165
+ | `endWhen` | `(snapshot) => boolean` | No | Ends the lifecycle when it returns `true`. |
166
+ | `onEnd` | `(snapshot, controls) => void \| Promise<void>` | No | Called once per generation when `endWhen` ends the lifecycle. |
167
+ | `schedules` | `TimerSchedule[]` | No | Scheduled side effects that run while the timer is active. Async overlap defaults to `skip`. |
168
+ | `debug` | `TimerDebug` | No | Opt-in semantic diagnostics. No logs are emitted by default. |
169
+
170
+ ### `TimerSchedule`
171
+
172
+ | Key | Type | Required | Description |
173
+ | --- | --- | --- | --- |
174
+ | `id` | `string` | No | Stable identifier used in debug events and schedule context. Falls back to the array index. |
175
+ | `everyMs` | `number` | Yes | Schedule cadence in milliseconds. Must be positive and finite. |
176
+ | `leading` | `boolean` | No | Runs the schedule immediately when the timer starts or resumes into a new generation. Defaults to `false`. |
177
+ | `overlap` | `'skip' \| 'allow'` | No | Controls async overlap. Defaults to `skip`, so a pending callback prevents another run. |
178
+ | `callback` | `(snapshot, controls, context) => void \| Promise<void>` | Yes | Scheduled side effect. Receives timing context with `scheduledAt`, `firedAt`, `nextRunAt`, `overdueCount`, and `effectiveEveryMs`. |
179
+
180
+ ### `useTimerGroup()` settings
181
+
182
+ Import from `@crup/react-timer-hook/group` when many keyed items need independent lifecycle control.
183
+
184
+ | Key | Type | Required | Description |
185
+ | --- | --- | --- | --- |
186
+ | `updateIntervalMs` | `number` | No | Shared scheduler cadence for the group. Defaults to `1000`. |
187
+ | `items` | `TimerGroupItem[]` | No | Initial/synced timer item definitions. Each item has its own lifecycle state. |
188
+ | `debug` | `TimerDebug` | No | Opt-in semantic diagnostics for group lifecycle and schedule events. |
189
+
190
+ ### `TimerGroupItem`
191
+
192
+ | Key | Type | Required | Description |
193
+ | --- | --- | --- | --- |
194
+ | `id` | `string` | Yes | Stable key for the item. Duplicate IDs throw. |
195
+ | `autoStart` | `boolean` | No | Starts the item automatically when it is added or synced. Defaults to `false`. |
196
+ | `endWhen` | `(snapshot) => boolean` | No | Ends that item when it returns `true`. |
197
+ | `onEnd` | `(snapshot, controls) => void \| Promise<void>` | No | Called once per item generation when that item ends naturally. |
198
+ | `schedules` | `TimerSchedule[]` | No | Per-item schedules with the same contract as `useScheduledTimer()`. |
199
+
200
+ ### Values and controls
201
+
202
+ | Key | Type | Description |
203
+ | --- | --- | --- |
204
+ | `status` | `'idle' \| 'running' \| 'paused' \| 'ended' \| 'cancelled'` | Current lifecycle state. |
205
+ | `now` | `number` | Wall-clock timestamp from `Date.now()`. Use for clocks and absolute deadlines. |
206
+ | `tick` | `number` | Number of render/update ticks produced in the current generation. |
207
+ | `startedAt` | `number \| null` | Wall-clock timestamp when the current generation started. |
208
+ | `pausedAt` | `number \| null` | Wall-clock timestamp for the current pause, or `null`. |
209
+ | `endedAt` | `number \| null` | Wall-clock timestamp when `endWhen` ended the lifecycle. |
210
+ | `cancelledAt` | `number \| null` | Wall-clock timestamp when `cancel()` ended the lifecycle early. |
211
+ | `cancelReason` | `string \| null` | Optional reason passed to `cancel(reason)`. |
212
+ | `elapsedMilliseconds` | `number` | Active elapsed duration calculated from monotonic time, excluding paused time. |
213
+ | `isIdle` | `boolean` | Convenience flag for `status === 'idle'`. |
214
+ | `isRunning` | `boolean` | Convenience flag for `status === 'running'`. |
215
+ | `isPaused` | `boolean` | Convenience flag for `status === 'paused'`. |
216
+ | `isEnded` | `boolean` | Convenience flag for `status === 'ended'`. |
217
+ | `isCancelled` | `boolean` | Convenience flag for `status === 'cancelled'`. |
218
+ | `start()` | `function` | Starts an idle timer. No-op if it is already started. |
219
+ | `pause()` | `function` | Pauses a running timer. |
220
+ | `resume()` | `function` | Resumes a paused timer from the paused elapsed value. |
221
+ | `reset(options?)` | `function` | Resets to idle and zero elapsed time. Pass `{ autoStart: true }` to reset directly into running. |
222
+ | `restart()` | `function` | Starts a new running generation from zero elapsed time. |
223
+ | `cancel(reason?)` | `function` | Terminal early stop. Does not call `onEnd`. |
224
+
146
225
  ## Bundle size
147
226
 
148
227
  The core import stays small. Extra capabilities are opt-in batteries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@crup/react-timer-hook",
3
- "version": "0.0.1-alpha.11",
4
- "description": "React timer lifecycle hooks for countdowns, stopwatches, schedules, and many independent timers.",
3
+ "version": "0.0.1-alpha.12",
4
+ "description": "A lightweight React hooks library for building timers, stopwatches, and real-time clocks with minimal boilerplate.",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",