@tanstack/solid-pacer 0.16.0 → 0.16.1
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.
- package/dist/async-batcher/createAsyncBatcher.cjs +122 -0
- package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -0
- package/dist/{cjs/async-batcher → async-batcher}/createAsyncBatcher.d.cts +22 -17
- package/dist/{esm/async-batcher → async-batcher}/createAsyncBatcher.d.ts +22 -17
- package/dist/async-batcher/createAsyncBatcher.js +122 -0
- package/dist/async-batcher/createAsyncBatcher.js.map +1 -0
- package/dist/async-batcher/index.cjs +10 -0
- package/dist/async-batcher/index.d.cts +3 -0
- package/dist/async-batcher/index.d.ts +3 -0
- package/dist/async-batcher/index.js +5 -0
- package/dist/async-debouncer/createAsyncDebouncer.cjs +114 -0
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -0
- package/dist/{cjs/async-debouncer → async-debouncer}/createAsyncDebouncer.d.cts +23 -18
- package/dist/{esm/async-debouncer → async-debouncer}/createAsyncDebouncer.d.ts +23 -18
- package/dist/async-debouncer/createAsyncDebouncer.js +114 -0
- package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -0
- package/dist/async-debouncer/index.cjs +10 -0
- package/dist/async-debouncer/index.d.cts +3 -0
- package/dist/async-debouncer/index.d.ts +3 -0
- package/dist/async-debouncer/index.js +5 -0
- package/dist/async-queuer/createAsyncQueuer.cjs +114 -0
- package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -0
- package/dist/{cjs/async-queuer → async-queuer}/createAsyncQueuer.d.cts +22 -17
- package/dist/{esm/async-queuer → async-queuer}/createAsyncQueuer.d.ts +22 -17
- package/dist/async-queuer/createAsyncQueuer.js +114 -0
- package/dist/async-queuer/createAsyncQueuer.js.map +1 -0
- package/dist/async-queuer/index.cjs +10 -0
- package/dist/async-queuer/index.d.cts +3 -0
- package/dist/async-queuer/index.d.ts +3 -0
- package/dist/async-queuer/index.js +5 -0
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +120 -0
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -0
- package/dist/{cjs/async-rate-limiter → async-rate-limiter}/createAsyncRateLimiter.d.cts +23 -18
- package/dist/{esm/async-rate-limiter → async-rate-limiter}/createAsyncRateLimiter.d.ts +23 -18
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +120 -0
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -0
- package/dist/async-rate-limiter/index.cjs +10 -0
- package/dist/async-rate-limiter/index.d.cts +3 -0
- package/dist/async-rate-limiter/index.d.ts +3 -0
- package/dist/async-rate-limiter/index.js +5 -0
- package/dist/async-throttler/createAsyncThrottler.cjs +108 -0
- package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -0
- package/dist/{cjs/async-throttler → async-throttler}/createAsyncThrottler.d.cts +23 -18
- package/dist/{esm/async-throttler → async-throttler}/createAsyncThrottler.d.ts +23 -18
- package/dist/async-throttler/createAsyncThrottler.js +108 -0
- package/dist/async-throttler/createAsyncThrottler.js.map +1 -0
- package/dist/async-throttler/index.cjs +10 -0
- package/dist/async-throttler/index.d.cts +3 -0
- package/dist/async-throttler/index.d.ts +3 -0
- package/dist/async-throttler/index.js +5 -0
- package/dist/batcher/createBatcher.cjs +97 -0
- package/dist/batcher/createBatcher.cjs.map +1 -0
- package/dist/{cjs/batcher → batcher}/createBatcher.d.cts +22 -17
- package/dist/{esm/batcher → batcher}/createBatcher.d.ts +22 -17
- package/dist/batcher/createBatcher.js +97 -0
- package/dist/batcher/createBatcher.js.map +1 -0
- package/dist/batcher/index.cjs +10 -0
- package/dist/batcher/index.d.cts +3 -0
- package/dist/batcher/index.d.ts +3 -0
- package/dist/batcher/index.js +5 -0
- package/dist/debouncer/createDebouncedSignal.cjs +88 -0
- package/dist/debouncer/createDebouncedSignal.cjs.map +1 -0
- package/dist/{cjs/debouncer → debouncer}/createDebouncedSignal.d.cts +10 -8
- package/dist/{esm/debouncer → debouncer}/createDebouncedSignal.d.ts +10 -8
- package/dist/debouncer/createDebouncedSignal.js +88 -0
- package/dist/debouncer/createDebouncedSignal.js.map +1 -0
- package/dist/debouncer/createDebouncedValue.cjs +77 -0
- package/dist/debouncer/createDebouncedValue.cjs.map +1 -0
- package/dist/{cjs/debouncer → debouncer}/createDebouncedValue.d.cts +10 -4
- package/dist/{esm/debouncer → debouncer}/createDebouncedValue.d.ts +10 -4
- package/dist/debouncer/createDebouncedValue.js +77 -0
- package/dist/debouncer/createDebouncedValue.js.map +1 -0
- package/dist/debouncer/createDebouncer.cjs +101 -0
- package/dist/debouncer/createDebouncer.cjs.map +1 -0
- package/dist/{cjs/debouncer → debouncer}/createDebouncer.d.cts +23 -18
- package/dist/{esm/debouncer → debouncer}/createDebouncer.d.ts +23 -18
- package/dist/debouncer/createDebouncer.js +101 -0
- package/dist/debouncer/createDebouncer.js.map +1 -0
- package/dist/debouncer/index.cjs +14 -0
- package/dist/debouncer/index.d.cts +5 -0
- package/dist/debouncer/index.d.ts +5 -0
- package/dist/{esm/debouncer → debouncer}/index.js +5 -8
- package/dist/index.cjs +46 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/{esm/index.js → index.js} +6 -26
- package/dist/provider/PacerProvider.cjs +27 -0
- package/dist/provider/PacerProvider.cjs.map +1 -0
- package/dist/provider/PacerProvider.d.cts +29 -0
- package/dist/provider/PacerProvider.d.ts +29 -0
- package/dist/provider/PacerProvider.js +25 -0
- package/dist/provider/PacerProvider.js.map +1 -0
- package/dist/provider/index.cjs +5 -0
- package/dist/provider/index.d.cts +2 -0
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +3 -0
- package/dist/queuer/createQueuedSignal.cjs +129 -0
- package/dist/queuer/createQueuedSignal.cjs.map +1 -0
- package/dist/{cjs/queuer → queuer}/createQueuedSignal.d.cts +9 -7
- package/dist/{esm/queuer → queuer}/createQueuedSignal.d.ts +9 -7
- package/dist/queuer/createQueuedSignal.js +129 -0
- package/dist/queuer/createQueuedSignal.js.map +1 -0
- package/dist/queuer/createQueuer.cjs +98 -0
- package/dist/queuer/createQueuer.cjs.map +1 -0
- package/dist/{cjs/queuer → queuer}/createQueuer.d.cts +22 -17
- package/dist/{esm/queuer → queuer}/createQueuer.d.ts +22 -17
- package/dist/queuer/createQueuer.js +98 -0
- package/dist/queuer/createQueuer.js.map +1 -0
- package/dist/queuer/index.cjs +12 -0
- package/dist/queuer/index.d.cts +4 -0
- package/dist/queuer/index.d.ts +4 -0
- package/dist/queuer/index.js +6 -0
- package/dist/rate-limiter/createRateLimitedSignal.cjs +102 -0
- package/dist/rate-limiter/createRateLimitedSignal.cjs.map +1 -0
- package/dist/{cjs/rate-limiter → rate-limiter}/createRateLimitedSignal.d.cts +10 -8
- package/dist/{esm/rate-limiter → rate-limiter}/createRateLimitedSignal.d.ts +10 -8
- package/dist/rate-limiter/createRateLimitedSignal.js +102 -0
- package/dist/rate-limiter/createRateLimitedSignal.js.map +1 -0
- package/dist/rate-limiter/createRateLimitedValue.cjs +102 -0
- package/dist/rate-limiter/createRateLimitedValue.cjs.map +1 -0
- package/dist/{cjs/rate-limiter → rate-limiter}/createRateLimitedValue.d.cts +10 -4
- package/dist/{esm/rate-limiter → rate-limiter}/createRateLimitedValue.d.ts +10 -4
- package/dist/rate-limiter/createRateLimitedValue.js +102 -0
- package/dist/rate-limiter/createRateLimitedValue.js.map +1 -0
- package/dist/rate-limiter/createRateLimiter.cjs +95 -0
- package/dist/rate-limiter/createRateLimiter.cjs.map +1 -0
- package/dist/{cjs/rate-limiter → rate-limiter}/createRateLimiter.d.cts +23 -18
- package/dist/{esm/rate-limiter → rate-limiter}/createRateLimiter.d.ts +23 -18
- package/dist/rate-limiter/createRateLimiter.js +95 -0
- package/dist/rate-limiter/createRateLimiter.js.map +1 -0
- package/dist/rate-limiter/index.cjs +14 -0
- package/dist/rate-limiter/index.d.cts +5 -0
- package/dist/rate-limiter/index.d.ts +5 -0
- package/dist/{esm/rate-limiter → rate-limiter}/index.js +4 -7
- package/dist/throttler/createThrottledSignal.cjs +79 -0
- package/dist/throttler/createThrottledSignal.cjs.map +1 -0
- package/dist/{cjs/throttler → throttler}/createThrottledSignal.d.cts +10 -8
- package/dist/{esm/throttler → throttler}/createThrottledSignal.d.ts +10 -8
- package/dist/throttler/createThrottledSignal.js +79 -0
- package/dist/throttler/createThrottledSignal.js.map +1 -0
- package/dist/throttler/createThrottledValue.cjs +74 -0
- package/dist/throttler/createThrottledValue.cjs.map +1 -0
- package/dist/{cjs/throttler → throttler}/createThrottledValue.d.cts +10 -4
- package/dist/{esm/throttler → throttler}/createThrottledValue.d.ts +10 -4
- package/dist/throttler/createThrottledValue.js +74 -0
- package/dist/throttler/createThrottledValue.js.map +1 -0
- package/dist/throttler/createThrottler.cjs +97 -0
- package/dist/throttler/createThrottler.cjs.map +1 -0
- package/dist/{cjs/throttler → throttler}/createThrottler.d.cts +23 -18
- package/dist/{esm/throttler → throttler}/createThrottler.d.ts +23 -18
- package/dist/throttler/createThrottler.js +97 -0
- package/dist/throttler/createThrottler.js.map +1 -0
- package/dist/throttler/index.cjs +14 -0
- package/dist/throttler/index.d.cts +5 -0
- package/dist/throttler/index.d.ts +5 -0
- package/dist/{esm/throttler → throttler}/index.js +5 -8
- package/dist/types/index.cjs +9 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/index.cjs +9 -0
- package/dist/utils/index.d.cts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +37 -106
- package/src/async-batcher/createAsyncBatcher.ts +4 -2
- package/src/async-queuer/createAsyncQueuer.ts +4 -2
- package/src/batcher/createBatcher.ts +4 -2
- package/src/debouncer/createDebouncer.ts +4 -2
- package/src/provider/index.ts +1 -0
- package/src/queuer/createQueuer.ts +4 -2
- package/src/rate-limiter/createRateLimiter.ts +4 -2
- package/src/throttler/createThrottler.ts +4 -2
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs +0 -19
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +0 -1
- package/dist/cjs/async-batcher/index.cjs +0 -12
- package/dist/cjs/async-batcher/index.cjs.map +0 -1
- package/dist/cjs/async-batcher/index.d.cts +0 -2
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +0 -25
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +0 -1
- package/dist/cjs/async-debouncer/index.cjs +0 -12
- package/dist/cjs/async-debouncer/index.cjs.map +0 -1
- package/dist/cjs/async-debouncer/index.d.cts +0 -2
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +0 -19
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +0 -1
- package/dist/cjs/async-queuer/index.cjs +0 -12
- package/dist/cjs/async-queuer/index.cjs.map +0 -1
- package/dist/cjs/async-queuer/index.d.cts +0 -2
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +0 -19
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +0 -1
- package/dist/cjs/async-rate-limiter/index.cjs +0 -12
- package/dist/cjs/async-rate-limiter/index.cjs.map +0 -1
- package/dist/cjs/async-rate-limiter/index.d.cts +0 -2
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +0 -19
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +0 -1
- package/dist/cjs/async-throttler/index.cjs +0 -12
- package/dist/cjs/async-throttler/index.cjs.map +0 -1
- package/dist/cjs/async-throttler/index.d.cts +0 -2
- package/dist/cjs/batcher/createBatcher.cjs +0 -19
- package/dist/cjs/batcher/createBatcher.cjs.map +0 -1
- package/dist/cjs/batcher/index.cjs +0 -12
- package/dist/cjs/batcher/index.cjs.map +0 -1
- package/dist/cjs/batcher/index.d.cts +0 -2
- package/dist/cjs/debouncer/createDebouncedSignal.cjs +0 -11
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +0 -1
- package/dist/cjs/debouncer/createDebouncedValue.cjs +0 -17
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +0 -1
- package/dist/cjs/debouncer/createDebouncer.cjs +0 -25
- package/dist/cjs/debouncer/createDebouncer.cjs.map +0 -1
- package/dist/cjs/debouncer/index.cjs +0 -16
- package/dist/cjs/debouncer/index.cjs.map +0 -1
- package/dist/cjs/debouncer/index.d.cts +0 -4
- package/dist/cjs/index.cjs +0 -48
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -22
- package/dist/cjs/provider/PacerProvider.cjs +0 -28
- package/dist/cjs/provider/PacerProvider.cjs.map +0 -1
- package/dist/cjs/provider/PacerProvider.d.cts +0 -25
- package/dist/cjs/queuer/createQueuedSignal.cjs +0 -9
- package/dist/cjs/queuer/createQueuedSignal.cjs.map +0 -1
- package/dist/cjs/queuer/createQueuer.cjs +0 -19
- package/dist/cjs/queuer/createQueuer.cjs.map +0 -1
- package/dist/cjs/queuer/index.cjs +0 -14
- package/dist/cjs/queuer/index.cjs.map +0 -1
- package/dist/cjs/queuer/index.d.cts +0 -3
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +0 -19
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +0 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +0 -13
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +0 -1
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +0 -19
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +0 -1
- package/dist/cjs/rate-limiter/index.cjs +0 -16
- package/dist/cjs/rate-limiter/index.cjs.map +0 -1
- package/dist/cjs/rate-limiter/index.d.cts +0 -4
- package/dist/cjs/throttler/createThrottledSignal.cjs +0 -11
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +0 -1
- package/dist/cjs/throttler/createThrottledValue.cjs +0 -17
- package/dist/cjs/throttler/createThrottledValue.cjs.map +0 -1
- package/dist/cjs/throttler/createThrottler.cjs +0 -25
- package/dist/cjs/throttler/createThrottler.cjs.map +0 -1
- package/dist/cjs/throttler/index.cjs +0 -16
- package/dist/cjs/throttler/index.cjs.map +0 -1
- package/dist/cjs/throttler/index.d.cts +0 -4
- package/dist/cjs/types/index.cjs +0 -10
- package/dist/cjs/types/index.cjs.map +0 -1
- package/dist/cjs/types/index.d.cts +0 -1
- package/dist/cjs/utils/index.cjs +0 -10
- package/dist/cjs/utils/index.cjs.map +0 -1
- package/dist/cjs/utils/index.d.cts +0 -1
- package/dist/esm/async-batcher/createAsyncBatcher.js +0 -19
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +0 -1
- package/dist/esm/async-batcher/index.d.ts +0 -2
- package/dist/esm/async-batcher/index.js +0 -6
- package/dist/esm/async-batcher/index.js.map +0 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +0 -25
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +0 -1
- package/dist/esm/async-debouncer/index.d.ts +0 -2
- package/dist/esm/async-debouncer/index.js +0 -6
- package/dist/esm/async-debouncer/index.js.map +0 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js +0 -19
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +0 -1
- package/dist/esm/async-queuer/index.d.ts +0 -2
- package/dist/esm/async-queuer/index.js +0 -6
- package/dist/esm/async-queuer/index.js.map +0 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +0 -19
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +0 -1
- package/dist/esm/async-rate-limiter/index.d.ts +0 -2
- package/dist/esm/async-rate-limiter/index.js +0 -6
- package/dist/esm/async-rate-limiter/index.js.map +0 -1
- package/dist/esm/async-throttler/createAsyncThrottler.js +0 -19
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +0 -1
- package/dist/esm/async-throttler/index.d.ts +0 -2
- package/dist/esm/async-throttler/index.js +0 -6
- package/dist/esm/async-throttler/index.js.map +0 -1
- package/dist/esm/batcher/createBatcher.js +0 -19
- package/dist/esm/batcher/createBatcher.js.map +0 -1
- package/dist/esm/batcher/index.d.ts +0 -2
- package/dist/esm/batcher/index.js +0 -6
- package/dist/esm/batcher/index.js.map +0 -1
- package/dist/esm/debouncer/createDebouncedSignal.js +0 -11
- package/dist/esm/debouncer/createDebouncedSignal.js.map +0 -1
- package/dist/esm/debouncer/createDebouncedValue.js +0 -17
- package/dist/esm/debouncer/createDebouncedValue.js.map +0 -1
- package/dist/esm/debouncer/createDebouncer.js +0 -25
- package/dist/esm/debouncer/createDebouncer.js.map +0 -1
- package/dist/esm/debouncer/index.d.ts +0 -4
- package/dist/esm/debouncer/index.js.map +0 -1
- package/dist/esm/index.d.ts +0 -22
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/provider/PacerProvider.d.ts +0 -25
- package/dist/esm/provider/PacerProvider.js +0 -28
- package/dist/esm/provider/PacerProvider.js.map +0 -1
- package/dist/esm/queuer/createQueuedSignal.js +0 -9
- package/dist/esm/queuer/createQueuedSignal.js.map +0 -1
- package/dist/esm/queuer/createQueuer.js +0 -19
- package/dist/esm/queuer/createQueuer.js.map +0 -1
- package/dist/esm/queuer/index.d.ts +0 -3
- package/dist/esm/queuer/index.js +0 -8
- package/dist/esm/queuer/index.js.map +0 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.js +0 -19
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +0 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.js +0 -13
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +0 -1
- package/dist/esm/rate-limiter/createRateLimiter.js +0 -19
- package/dist/esm/rate-limiter/createRateLimiter.js.map +0 -1
- package/dist/esm/rate-limiter/index.d.ts +0 -4
- package/dist/esm/rate-limiter/index.js.map +0 -1
- package/dist/esm/throttler/createThrottledSignal.js +0 -11
- package/dist/esm/throttler/createThrottledSignal.js.map +0 -1
- package/dist/esm/throttler/createThrottledValue.js +0 -17
- package/dist/esm/throttler/createThrottledValue.js.map +0 -1
- package/dist/esm/throttler/createThrottler.js +0 -25
- package/dist/esm/throttler/createThrottler.js.map +0 -1
- package/dist/esm/throttler/index.d.ts +0 -4
- package/dist/esm/throttler/index.js.map +0 -1
- package/dist/esm/types/index.d.ts +0 -1
- package/dist/esm/types/index.js +0 -2
- package/dist/esm/types/index.js.map +0 -1
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -2
- package/dist/esm/utils/index.js.map +0 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createThrottledSignal } from "./createThrottledSignal.js";
|
|
2
|
+
import { createEffect } from "solid-js";
|
|
3
|
+
|
|
4
|
+
//#region src/throttler/createThrottledValue.ts
|
|
5
|
+
/**
|
|
6
|
+
* A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.
|
|
7
|
+
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
8
|
+
*
|
|
9
|
+
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
10
|
+
* This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.
|
|
11
|
+
*
|
|
12
|
+
* The hook returns a tuple containing:
|
|
13
|
+
* - An accessor function that provides the throttled value
|
|
14
|
+
* - The throttler instance with control methods
|
|
15
|
+
*
|
|
16
|
+
* The throttled value will update according to the leading/trailing edge behavior specified in the options.
|
|
17
|
+
*
|
|
18
|
+
* For more direct control over throttling behavior without Solid state management,
|
|
19
|
+
* consider using the lower-level createThrottler hook instead.
|
|
20
|
+
*
|
|
21
|
+
* ## State Management and Selector
|
|
22
|
+
*
|
|
23
|
+
* The hook uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
24
|
+
* The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,
|
|
25
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
26
|
+
*
|
|
27
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
28
|
+
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
29
|
+
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
30
|
+
* the reactive system track the selected state values.
|
|
31
|
+
*
|
|
32
|
+
* Available throttler state properties:
|
|
33
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
34
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
35
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
36
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution
|
|
37
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
38
|
+
* - `lastExecutionTime`: Unix timestamp of the last execution
|
|
39
|
+
* - `nextExecutionTime`: Unix timestamp of the next allowed execution
|
|
40
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* // Default behavior - no reactive state subscriptions
|
|
45
|
+
* const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });
|
|
46
|
+
*
|
|
47
|
+
* // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
|
|
48
|
+
* const [throttledValue, throttler] = createThrottledValue(
|
|
49
|
+
* rawValue,
|
|
50
|
+
* { wait: 1000 },
|
|
51
|
+
* (state) => ({ isPending: state.isPending })
|
|
52
|
+
* );
|
|
53
|
+
*
|
|
54
|
+
* // Use the throttled value
|
|
55
|
+
* console.log(throttledValue()); // Access the current throttled value
|
|
56
|
+
*
|
|
57
|
+
* // Access throttler state via signals
|
|
58
|
+
* console.log('Is pending:', throttler.state().isPending);
|
|
59
|
+
*
|
|
60
|
+
* // Control the throttler
|
|
61
|
+
* throttler.cancel(); // Cancel any pending updates
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function createThrottledValue(value, initialOptions, selector) {
|
|
65
|
+
const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(value(), initialOptions, selector);
|
|
66
|
+
createEffect(() => {
|
|
67
|
+
setThrottledValue(value());
|
|
68
|
+
});
|
|
69
|
+
return [throttledValue, throttler];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
export { createThrottledValue };
|
|
74
|
+
//# sourceMappingURL=createThrottledValue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottledValue.js","names":[],"sources":["../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createThrottledSignal } from './createThrottledSignal'\nimport type { SolidThrottler } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\n/**\n * A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.\n * This hook uses Solid's createSignal internally to manage the throttled state.\n *\n * Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.\n * This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the throttled value\n * - The throttler instance with control methods\n *\n * The throttled value will update according to the leading/trailing edge behavior specified in the options.\n *\n * For more direct control over throttling behavior without Solid state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Unix timestamp of the last execution\n * - `nextExecutionTime`: Unix timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)\n * const [throttledValue, throttler] = createThrottledValue(\n * rawValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Access throttler state via signals\n * console.log('Is pending:', throttler.state().isPending);\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<TValue, TSelected = {}>(\n value: Accessor<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n selector,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n })\n\n return [throttledValue, throttler]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,SAAgB,qBACd,OACA,gBACA,UAC+D;CAC/D,MAAM,CAAC,gBAAgB,mBAAmB,aAAa,sBACrD,OAAO,EACP,gBACA,SACD;AAED,oBAAmB;AACjB,oBAAkB,OAAO,CAAQ;GACjC;AAEF,QAAO,CAAC,gBAAgB,UAAU"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
+
let solid_js = require("solid-js");
|
|
3
|
+
let __tanstack_solid_store = require("@tanstack/solid-store");
|
|
4
|
+
let __tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/createThrottler.ts
|
|
7
|
+
/**
|
|
8
|
+
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
9
|
+
*
|
|
10
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
11
|
+
* you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
12
|
+
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
13
|
+
*
|
|
14
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
15
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
16
|
+
* expensive operations or UI updates.
|
|
17
|
+
*
|
|
18
|
+
* ## State Management and Selector
|
|
19
|
+
*
|
|
20
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
21
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
22
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
23
|
+
*
|
|
24
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
26
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
27
|
+
* component re-render when the selected state values change.
|
|
28
|
+
*
|
|
29
|
+
* Available state properties:
|
|
30
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
31
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
32
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
34
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
+
* - `lastExecutionTime`: Timestamp of the last execution
|
|
36
|
+
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
37
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Default behavior - no reactive state subscriptions
|
|
42
|
+
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
43
|
+
*
|
|
44
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
45
|
+
* const throttler = createThrottler(
|
|
46
|
+
* setValue,
|
|
47
|
+
* { wait: 1000 },
|
|
48
|
+
* (state) => ({ isPending: state.isPending })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
52
|
+
* const throttler = createThrottler(
|
|
53
|
+
* setValue,
|
|
54
|
+
* { wait: 1000 },
|
|
55
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* // Multiple state properties - re-render when any of these change
|
|
59
|
+
* const throttler = createThrottler(
|
|
60
|
+
* setValue,
|
|
61
|
+
* {
|
|
62
|
+
* wait: 2000,
|
|
63
|
+
* leading: true, // Execute immediately on first call
|
|
64
|
+
* trailing: false // Skip trailing edge updates
|
|
65
|
+
* },
|
|
66
|
+
* (state) => ({
|
|
67
|
+
* isPending: state.isPending,
|
|
68
|
+
* executionCount: state.executionCount,
|
|
69
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
70
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
71
|
+
* })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
75
|
+
* const { isPending, executionCount } = throttler.state();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function createThrottler(fn, options, selector = () => ({})) {
|
|
79
|
+
const asyncThrottler = new __tanstack_pacer_throttler.Throttler(fn, {
|
|
80
|
+
...require_PacerProvider.useDefaultPacerOptions().throttler,
|
|
81
|
+
...options
|
|
82
|
+
});
|
|
83
|
+
const state = (0, __tanstack_solid_store.useStore)(asyncThrottler.store, selector);
|
|
84
|
+
(0, solid_js.createEffect)(() => {
|
|
85
|
+
(0, solid_js.onCleanup)(() => {
|
|
86
|
+
asyncThrottler.cancel();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
...asyncThrottler,
|
|
91
|
+
state
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.createThrottler = createThrottler;
|
|
97
|
+
//# sourceMappingURL=createThrottler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottler.cjs","names":["Throttler","useDefaultPacerOptions"],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Opt-in to re-render when isPending changes (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - re-render when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const asyncThrottler = new Throttler<TFn>(fn, mergedOptions)\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,iBAAiB,IAAIA,qCAAe,IALpB;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAE2D;CAE5D,MAAM,6CAAiB,eAAe,OAAO,SAAS;AAEtD,kCAAmB;AACjB,gCAAgB;AACd,kBAAe,QAAQ;IACvB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { Store } from "@tanstack/solid-store";
|
|
2
|
+
import { Accessor } from "solid-js";
|
|
3
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/createThrottler.d.ts
|
|
7
|
+
interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* Reactive state that will be updated when the throttler state changes
|
|
10
|
+
*
|
|
11
|
+
* Use this instead of `throttler.store.state`
|
|
12
|
+
*/
|
|
13
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
16
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
17
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
18
|
+
*/
|
|
19
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
22
|
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
@@ -87,4 +89,7 @@ export interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends
|
|
|
87
89
|
* const { isPending, executionCount } = throttler.state();
|
|
88
90
|
* ```
|
|
89
91
|
*/
|
|
90
|
-
|
|
92
|
+
declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
|
|
93
|
+
//#endregion
|
|
94
|
+
export { SolidThrottler, createThrottler };
|
|
95
|
+
//# sourceMappingURL=createThrottler.d.cts.map
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Store } from
|
|
3
|
-
import {
|
|
4
|
-
import { AnyFunction } from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { Accessor } from "solid-js";
|
|
2
|
+
import { Store } from "@tanstack/solid-store";
|
|
3
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
4
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/createThrottler.d.ts
|
|
7
|
+
interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* Reactive state that will be updated when the throttler state changes
|
|
10
|
+
*
|
|
11
|
+
* Use this instead of `throttler.store.state`
|
|
12
|
+
*/
|
|
13
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
16
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
17
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
18
|
+
*/
|
|
19
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
22
|
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
@@ -87,4 +89,7 @@ export interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends
|
|
|
87
89
|
* const { isPending, executionCount } = throttler.state();
|
|
88
90
|
* ```
|
|
89
91
|
*/
|
|
90
|
-
|
|
92
|
+
declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
|
|
93
|
+
//#endregion
|
|
94
|
+
export { SolidThrottler, createThrottler };
|
|
95
|
+
//# sourceMappingURL=createThrottler.d.ts.map
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useDefaultPacerOptions } from "../provider/PacerProvider.js";
|
|
2
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
3
|
+
import { useStore } from "@tanstack/solid-store";
|
|
4
|
+
import { Throttler } from "@tanstack/pacer/throttler";
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/createThrottler.ts
|
|
7
|
+
/**
|
|
8
|
+
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
9
|
+
*
|
|
10
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
11
|
+
* you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
12
|
+
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
13
|
+
*
|
|
14
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
15
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
16
|
+
* expensive operations or UI updates.
|
|
17
|
+
*
|
|
18
|
+
* ## State Management and Selector
|
|
19
|
+
*
|
|
20
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
21
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
22
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
23
|
+
*
|
|
24
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
26
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
27
|
+
* component re-render when the selected state values change.
|
|
28
|
+
*
|
|
29
|
+
* Available state properties:
|
|
30
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
31
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
32
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
34
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
+
* - `lastExecutionTime`: Timestamp of the last execution
|
|
36
|
+
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
37
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Default behavior - no reactive state subscriptions
|
|
42
|
+
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
43
|
+
*
|
|
44
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
45
|
+
* const throttler = createThrottler(
|
|
46
|
+
* setValue,
|
|
47
|
+
* { wait: 1000 },
|
|
48
|
+
* (state) => ({ isPending: state.isPending })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
52
|
+
* const throttler = createThrottler(
|
|
53
|
+
* setValue,
|
|
54
|
+
* { wait: 1000 },
|
|
55
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* // Multiple state properties - re-render when any of these change
|
|
59
|
+
* const throttler = createThrottler(
|
|
60
|
+
* setValue,
|
|
61
|
+
* {
|
|
62
|
+
* wait: 2000,
|
|
63
|
+
* leading: true, // Execute immediately on first call
|
|
64
|
+
* trailing: false // Skip trailing edge updates
|
|
65
|
+
* },
|
|
66
|
+
* (state) => ({
|
|
67
|
+
* isPending: state.isPending,
|
|
68
|
+
* executionCount: state.executionCount,
|
|
69
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
70
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
71
|
+
* })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
75
|
+
* const { isPending, executionCount } = throttler.state();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function createThrottler(fn, options, selector = () => ({})) {
|
|
79
|
+
const asyncThrottler = new Throttler(fn, {
|
|
80
|
+
...useDefaultPacerOptions().throttler,
|
|
81
|
+
...options
|
|
82
|
+
});
|
|
83
|
+
const state = useStore(asyncThrottler.store, selector);
|
|
84
|
+
createEffect(() => {
|
|
85
|
+
onCleanup(() => {
|
|
86
|
+
asyncThrottler.cancel();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
...asyncThrottler,
|
|
91
|
+
state
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
export { createThrottler };
|
|
97
|
+
//# sourceMappingURL=createThrottler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottler.js","names":[],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Opt-in to re-render when isPending changes (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - re-render when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const asyncThrottler = new Throttler<TFn>(fn, mergedOptions)\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,iBAAiB,IAAI,UAAe,IALpB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAE2D;CAE5D,MAAM,QAAQ,SAAS,eAAe,OAAO,SAAS;AAEtD,oBAAmB;AACjB,kBAAgB;AACd,kBAAe,QAAQ;IACvB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const require_createThrottler = require('./createThrottler.cjs');
|
|
2
|
+
const require_createThrottledSignal = require('./createThrottledSignal.cjs');
|
|
3
|
+
const require_createThrottledValue = require('./createThrottledValue.cjs');
|
|
4
|
+
|
|
5
|
+
exports.createThrottledSignal = require_createThrottledSignal.createThrottledSignal;
|
|
6
|
+
exports.createThrottledValue = require_createThrottledValue.createThrottledValue;
|
|
7
|
+
exports.createThrottler = require_createThrottler.createThrottler;
|
|
8
|
+
var __tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
9
|
+
Object.keys(__tanstack_pacer_throttler).forEach(function (k) {
|
|
10
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () { return __tanstack_pacer_throttler[k]; }
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SolidThrottler, createThrottler } from "./createThrottler.cjs";
|
|
2
|
+
import { createThrottledSignal } from "./createThrottledSignal.cjs";
|
|
3
|
+
import { createThrottledValue } from "./createThrottledValue.cjs";
|
|
4
|
+
export * from "@tanstack/pacer/throttler";
|
|
5
|
+
export { SolidThrottler, createThrottledSignal, createThrottledValue, createThrottler };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SolidThrottler, createThrottler } from "./createThrottler.js";
|
|
2
|
+
import { createThrottledSignal } from "./createThrottledSignal.js";
|
|
3
|
+
import { createThrottledValue } from "./createThrottledValue.js";
|
|
4
|
+
export * from "@tanstack/pacer/throttler";
|
|
5
|
+
export { SolidThrottler, createThrottledSignal, createThrottledValue, createThrottler };
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { createThrottler } from "./createThrottler.js";
|
|
2
2
|
import { createThrottledSignal } from "./createThrottledSignal.js";
|
|
3
3
|
import { createThrottledValue } from "./createThrottledValue.js";
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
createThrottler
|
|
9
|
-
};
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
4
|
+
|
|
5
|
+
export * from "@tanstack/pacer/throttler"
|
|
6
|
+
|
|
7
|
+
export { createThrottledSignal, createThrottledValue, createThrottler };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
var __tanstack_pacer_types = require("@tanstack/pacer/types");
|
|
4
|
+
Object.keys(__tanstack_pacer_types).forEach(function (k) {
|
|
5
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () { return __tanstack_pacer_types[k]; }
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
var __tanstack_pacer_utils = require("@tanstack/pacer/utils");
|
|
4
|
+
Object.keys(__tanstack_pacer_utils).forEach(function (k) {
|
|
5
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () { return __tanstack_pacer_utils[k]; }
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|