@stacksjs/stx 0.2.189 → 0.2.190

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.
@@ -153,19 +153,24 @@ export declare function useDebouncedValue<T>(getter: () => T, delay: number): {
153
153
  */
154
154
  export declare function useThrottle<T extends (...args: any[]) => any>(fn: T, limit: number): (...args: Parameters<T>) => void;
155
155
  /**
156
- * Reactive interval.
156
+ * Interval with controls, in both call forms.
157
+ *
158
+ * This is the module-import twin of the client runtime's `useInterval`, and the
159
+ * two must agree — a caller cannot tell which one their bundle resolved. It had
160
+ * drifted on three axes at once: no callback form (so `useInterval(fn, 1000)`
161
+ * passed the function as the delay, which coerces to `NaN`, which the platform
162
+ * clamps to 0 — a runaway timer incrementing a counter nobody could read); no
163
+ * `subscribe`; and `counter` wrapped in a `{ value }` object the runtime does
164
+ * not use. `immediate` was accepted, documented, and used as an initial "is
165
+ * paused" flag rather than "tick right away" (#1941).
157
166
  *
158
167
  * @example
159
168
  * ```typescript
160
169
  * const { counter, pause, resume, reset } = useInterval(1000)
170
+ * const poll = useInterval(() => refresh(), 60_000)
161
171
  * ```
162
172
  */
163
- export declare function useInterval(interval: number, options?: { immediate?: boolean }): {
164
- counter: { readonly value: number }
165
- pause: () => void
166
- resume: () => void
167
- reset: () => void
168
- };
173
+ export declare function useInterval(interval?: number | ((count: number) => void), options?: number | IntervalOptions, thirdOptions?: IntervalOptions): IntervalControls;
169
174
  /**
170
175
  * One-time timeout with controls.
171
176
  *
@@ -310,6 +315,18 @@ export declare interface BreakpointsRef {
310
315
  isDesktop: { readonly value: boolean }
311
316
  isLargeDesktop: { readonly value: boolean }
312
317
  }
318
+ export declare interface IntervalOptions {
319
+ immediate?: boolean
320
+ enabled?: boolean | (() => boolean)
321
+ whileVisible?: boolean
322
+ }
323
+ export declare interface IntervalControls {
324
+ readonly counter: number
325
+ pause: () => void
326
+ resume: () => void
327
+ reset: () => void
328
+ subscribe: (fn: (count: number) => void) => () => void
329
+ }
313
330
  // =============================================================================
314
331
  // Async Composables
315
332
  // =============================================================================
@@ -272,36 +272,77 @@ export function useThrottle(fn, limit) {
272
272
  });
273
273
  return throttledFn;
274
274
  }
275
- export function useInterval(interval, options = {}) {
276
- let count = 0, isActive = options.immediate !== !1, intervalId = null;
277
- const start = () => {
278
- if (intervalId)
275
+ export function useInterval(interval, options, thirdOptions) {
276
+ let callback = null, delay, opts;
277
+ if (typeof interval === "function") {
278
+ callback = interval;
279
+ if (typeof options === "number") {
280
+ delay = options;
281
+ opts = thirdOptions || {};
282
+ } else {
283
+ delay = 1000;
284
+ opts = options && typeof options === "object" ? options : {};
285
+ }
286
+ } else {
287
+ delay = interval || 1000;
288
+ opts = typeof options === "object" && options ? options : {};
289
+ }
290
+ let count = 0, id = null, running = !1, listeners = [];
291
+ const shouldTick = () => {
292
+ if (opts.whileVisible && typeof document < "u" && document.hidden)
293
+ return !1;
294
+ const enabled = opts.enabled;
295
+ if (typeof enabled === "function")
296
+ return !!enabled();
297
+ return enabled !== !1;
298
+ }, tick = () => {
299
+ if (!shouldTick())
279
300
  return;
280
- intervalId = setInterval(() => {
281
- if (isActive)
282
- count++;
283
- }, interval);
284
- }, pause = () => {
285
- isActive = !1;
301
+ count++;
302
+ listeners.forEach((fn) => fn(count));
286
303
  }, resume = () => {
287
- isActive = !0;
304
+ if (running)
305
+ return;
306
+ running = !0;
307
+ id = setInterval(tick, delay);
308
+ if (opts.immediate)
309
+ tick();
310
+ }, pause = () => {
311
+ if (!running)
312
+ return;
313
+ running = !1;
314
+ if (id !== null) {
315
+ clearInterval(id);
316
+ id = null;
317
+ }
288
318
  }, reset = () => {
319
+ pause();
289
320
  count = 0;
321
+ listeners.forEach((fn) => fn(count));
322
+ resume();
290
323
  };
324
+ if (callback)
325
+ listeners.push(callback);
291
326
  if (typeof window < "u") {
292
- start();
327
+ resume();
293
328
  onDestroy(() => {
294
- if (intervalId)
295
- clearInterval(intervalId);
329
+ pause();
330
+ listeners = [];
296
331
  });
297
332
  }
298
333
  return {
299
- counter: { get value() {
334
+ get counter() {
300
335
  return count;
301
- } },
336
+ },
302
337
  pause,
303
338
  resume,
304
- reset
339
+ reset,
340
+ subscribe(fn) {
341
+ listeners.push(fn);
342
+ return () => {
343
+ listeners = listeners.filter((f) => f !== fn);
344
+ };
345
+ }
305
346
  };
306
347
  }
307
348
  export function useTimeout(callback, delay) {