@@ -46,8 +46,8 @@ export function currentStateAsChanges<
46
46
TKey extends string | number ,
47
47
> (
48
48
collection : CollectionLike < T , TKey > ,
49
- options : CurrentStateAsChangesOptions < T > = { }
50
- ) : Array < ChangeMessage < T > > {
49
+ options : CurrentStateAsChangesOptions = { }
50
+ ) : Array < ChangeMessage < T > > | void {
51
51
// Helper function to collect filtered results
52
52
const collectFilteredResults = (
53
53
filterFn ?: ( value : T ) => boolean
@@ -66,31 +66,17 @@ export function currentStateAsChanges<
66
66
return result
67
67
}
68
68
69
- if ( ! options . where && ! options . whereExpression ) {
69
+ // TODO: handle orderBy and limit options
70
+ // by calling optimizeOrderedLimit
71
+
72
+ if ( ! options . where ) {
70
73
// No filtering, return all items
71
74
return collectFilteredResults ( )
72
75
}
73
76
74
77
// There's a where clause, let's see if we can use an index
75
78
try {
76
- let expression : BasicExpression < boolean >
77
-
78
- if ( options . whereExpression ) {
79
- // Use the pre-compiled expression directly
80
- expression = options . whereExpression
81
- } else if ( options . where ) {
82
- // Create the single-row refProxy for the callback
83
- const singleRowRefProxy = createSingleRowRefProxy < T > ( )
84
-
85
- // Execute the callback to get the expression
86
- const whereExpression = options . where ( singleRowRefProxy )
87
-
88
- // Convert the result to a BasicExpression
89
- expression = toExpression ( whereExpression )
90
- } else {
91
- // This should never happen due to the check above, but TypeScript needs it
92
- return [ ]
93
- }
79
+ const expression : BasicExpression < boolean > = options . where
94
80
95
81
// Try to optimize the query using indexes
96
82
const optimizationResult = optimizeExpressionWithIndexes (
@@ -113,11 +99,11 @@ export function currentStateAsChanges<
113
99
}
114
100
return result
115
101
} else {
116
- // No index found or complex expression, fall back to full scan with filter
117
- const filterFn = options . where
118
- ? createFilterFunction ( options . where )
119
- : createFilterFunctionFromExpression ( expression )
102
+ if ( options . optimizedOnly ) {
103
+ return
104
+ }
120
105
106
+ const filterFn = createFilterFunctionFromExpression ( expression )
121
107
return collectFilteredResults ( filterFn )
122
108
}
123
109
} catch ( error ) {
@@ -127,9 +113,11 @@ export function currentStateAsChanges<
127
113
error
128
114
)
129
115
130
- const filterFn = options . where
131
- ? createFilterFunction ( options . where )
132
- : createFilterFunctionFromExpression ( options . whereExpression ! )
116
+ const filterFn = createFilterFunctionFromExpression ( options . where )
117
+
118
+ if ( options . optimizedOnly ) {
119
+ return
120
+ }
133
121
134
122
return collectFilteredResults ( filterFn )
135
123
}
@@ -201,11 +189,9 @@ export function createFilterFunctionFromExpression<T extends object>(
201
189
*/
202
190
export function createFilteredCallback < T extends object > (
203
191
originalCallback : ( changes : Array < ChangeMessage < T > > ) => void ,
204
- options : SubscribeChangesOptions < T >
192
+ options : SubscribeChangesOptions
205
193
) : ( changes : Array < ChangeMessage < T > > ) => void {
206
- const filterFn = options . whereExpression
207
- ? createFilterFunctionFromExpression ( options . whereExpression )
208
- : createFilterFunction ( options . where ! )
194
+ const filterFn = createFilterFunctionFromExpression ( options . whereExpression ! )
209
195
210
196
return ( changes : Array < ChangeMessage < T > > ) => {
211
197
const filteredChanges : Array < ChangeMessage < T > > = [ ]
0 commit comments