@tanstack/db 0.4.14 → 0.4.16
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/cjs/index.cjs +8 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/indexes/auto-index.cjs +17 -8
- package/dist/cjs/indexes/auto-index.cjs.map +1 -1
- package/dist/cjs/local-storage.cjs +21 -18
- package/dist/cjs/local-storage.cjs.map +1 -1
- package/dist/cjs/local-storage.d.cts +9 -0
- package/dist/cjs/paced-mutations.cjs +52 -0
- package/dist/cjs/paced-mutations.cjs.map +1 -0
- package/dist/cjs/paced-mutations.d.cts +81 -0
- package/dist/cjs/query/optimizer.cjs +17 -2
- package/dist/cjs/query/optimizer.cjs.map +1 -1
- package/dist/cjs/strategies/debounceStrategy.cjs +21 -0
- package/dist/cjs/strategies/debounceStrategy.cjs.map +1 -0
- package/dist/cjs/strategies/debounceStrategy.d.cts +22 -0
- package/dist/cjs/strategies/index.d.cts +4 -0
- package/dist/cjs/strategies/queueStrategy.cjs +33 -0
- package/dist/cjs/strategies/queueStrategy.cjs.map +1 -0
- package/dist/cjs/strategies/queueStrategy.d.cts +43 -0
- package/dist/cjs/strategies/throttleStrategy.cjs +21 -0
- package/dist/cjs/strategies/throttleStrategy.cjs.map +1 -0
- package/dist/cjs/strategies/throttleStrategy.d.cts +39 -0
- package/dist/cjs/strategies/types.d.cts +103 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/indexes/auto-index.js +17 -8
- package/dist/esm/indexes/auto-index.js.map +1 -1
- package/dist/esm/local-storage.d.ts +9 -0
- package/dist/esm/local-storage.js +21 -18
- package/dist/esm/local-storage.js.map +1 -1
- package/dist/esm/paced-mutations.d.ts +81 -0
- package/dist/esm/paced-mutations.js +52 -0
- package/dist/esm/paced-mutations.js.map +1 -0
- package/dist/esm/query/optimizer.js +17 -2
- package/dist/esm/query/optimizer.js.map +1 -1
- package/dist/esm/strategies/debounceStrategy.d.ts +22 -0
- package/dist/esm/strategies/debounceStrategy.js +21 -0
- package/dist/esm/strategies/debounceStrategy.js.map +1 -0
- package/dist/esm/strategies/index.d.ts +4 -0
- package/dist/esm/strategies/queueStrategy.d.ts +43 -0
- package/dist/esm/strategies/queueStrategy.js +33 -0
- package/dist/esm/strategies/queueStrategy.js.map +1 -0
- package/dist/esm/strategies/throttleStrategy.d.ts +39 -0
- package/dist/esm/strategies/throttleStrategy.js +21 -0
- package/dist/esm/strategies/throttleStrategy.js.map +1 -0
- package/dist/esm/strategies/types.d.ts +103 -0
- package/package.json +4 -1
- package/src/index.ts +2 -0
- package/src/indexes/auto-index.ts +23 -10
- package/src/local-storage.ts +41 -17
- package/src/paced-mutations.ts +169 -0
- package/src/query/optimizer.ts +31 -4
- package/src/strategies/debounceStrategy.ts +45 -0
- package/src/strategies/index.ts +17 -0
- package/src/strategies/queueStrategy.ts +75 -0
- package/src/strategies/throttleStrategy.ts +62 -0
- package/src/strategies/types.ts +130 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { Transaction } from "../transactions"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base strategy interface that all strategy implementations must conform to
|
|
5
|
+
*/
|
|
6
|
+
export interface BaseStrategy<TName extends string = string> {
|
|
7
|
+
/** Type discriminator for strategy identification */
|
|
8
|
+
_type: TName
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Execute a function according to the strategy's timing rules
|
|
12
|
+
* @param fn - The function to execute
|
|
13
|
+
* @returns The result of the function execution (if applicable)
|
|
14
|
+
*/
|
|
15
|
+
execute: <T extends object = Record<string, unknown>>(
|
|
16
|
+
fn: () => Transaction<T>
|
|
17
|
+
) => void | Promise<void>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Clean up any resources held by the strategy
|
|
21
|
+
* Should be called when the strategy is no longer needed
|
|
22
|
+
*/
|
|
23
|
+
cleanup: () => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Options for debounce strategy
|
|
28
|
+
* Delays execution until after a period of inactivity
|
|
29
|
+
*/
|
|
30
|
+
export interface DebounceStrategyOptions {
|
|
31
|
+
/** Wait time in milliseconds before execution */
|
|
32
|
+
wait: number
|
|
33
|
+
/** Execute immediately on the first call */
|
|
34
|
+
leading?: boolean
|
|
35
|
+
/** Execute after the wait period on the last call */
|
|
36
|
+
trailing?: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Debounce strategy that delays execution until activity stops
|
|
41
|
+
*/
|
|
42
|
+
export interface DebounceStrategy extends BaseStrategy<`debounce`> {
|
|
43
|
+
options: DebounceStrategyOptions
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Options for queue strategy
|
|
48
|
+
* Processes all executions in order (FIFO/LIFO)
|
|
49
|
+
*/
|
|
50
|
+
export interface QueueStrategyOptions {
|
|
51
|
+
/** Wait time between processing queue items (milliseconds) */
|
|
52
|
+
wait?: number
|
|
53
|
+
/** Maximum queue size (items are dropped if exceeded) */
|
|
54
|
+
maxSize?: number
|
|
55
|
+
/** Where to add new items in the queue */
|
|
56
|
+
addItemsTo?: `front` | `back`
|
|
57
|
+
/** Where to get items from when processing */
|
|
58
|
+
getItemsFrom?: `front` | `back`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Queue strategy that processes all executions in order
|
|
63
|
+
* FIFO: { addItemsTo: 'back', getItemsFrom: 'front' }
|
|
64
|
+
* LIFO: { addItemsTo: 'back', getItemsFrom: 'back' }
|
|
65
|
+
*/
|
|
66
|
+
export interface QueueStrategy extends BaseStrategy<`queue`> {
|
|
67
|
+
options?: QueueStrategyOptions
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Options for throttle strategy
|
|
72
|
+
* Ensures executions are evenly spaced over time
|
|
73
|
+
*/
|
|
74
|
+
export interface ThrottleStrategyOptions {
|
|
75
|
+
/** Minimum wait time between executions (milliseconds) */
|
|
76
|
+
wait: number
|
|
77
|
+
/** Execute immediately on the first call */
|
|
78
|
+
leading?: boolean
|
|
79
|
+
/** Execute on the last call after wait period */
|
|
80
|
+
trailing?: boolean
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Throttle strategy that spaces executions evenly over time
|
|
85
|
+
*/
|
|
86
|
+
export interface ThrottleStrategy extends BaseStrategy<`throttle`> {
|
|
87
|
+
options: ThrottleStrategyOptions
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Options for batch strategy
|
|
92
|
+
* Groups multiple executions together
|
|
93
|
+
*/
|
|
94
|
+
export interface BatchStrategyOptions {
|
|
95
|
+
/** Maximum items per batch */
|
|
96
|
+
maxSize?: number
|
|
97
|
+
/** Maximum wait time before processing batch (milliseconds) */
|
|
98
|
+
wait?: number
|
|
99
|
+
/** Custom logic to determine when to execute batch */
|
|
100
|
+
getShouldExecute?: (items: Array<any>) => boolean
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Batch strategy that groups multiple executions together
|
|
105
|
+
*/
|
|
106
|
+
export interface BatchStrategy extends BaseStrategy<`batch`> {
|
|
107
|
+
options?: BatchStrategyOptions
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Union type of all available strategies
|
|
112
|
+
*/
|
|
113
|
+
export type Strategy =
|
|
114
|
+
| DebounceStrategy
|
|
115
|
+
| QueueStrategy
|
|
116
|
+
| ThrottleStrategy
|
|
117
|
+
| BatchStrategy
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Extract the options type from a strategy
|
|
121
|
+
*/
|
|
122
|
+
export type StrategyOptions<T extends Strategy> = T extends DebounceStrategy
|
|
123
|
+
? DebounceStrategyOptions
|
|
124
|
+
: T extends QueueStrategy
|
|
125
|
+
? QueueStrategyOptions
|
|
126
|
+
: T extends ThrottleStrategy
|
|
127
|
+
? ThrottleStrategyOptions
|
|
128
|
+
: T extends BatchStrategy
|
|
129
|
+
? BatchStrategyOptions
|
|
130
|
+
: never
|