@@ -46,8 +46,8 @@ export function currentStateAsChanges<
4646 TKey extends string | number ,
4747> (
4848 collection : CollectionLike < T , TKey > ,
49- options : CurrentStateAsChangesOptions < T > = { }
50- ) : Array < ChangeMessage < T > > {
49+ options : CurrentStateAsChangesOptions = { }
50+ ) : Array < ChangeMessage < T > > | void {
5151 // Helper function to collect filtered results
5252 const collectFilteredResults = (
5353 filterFn ?: ( value : T ) => boolean
@@ -66,31 +66,17 @@ export function currentStateAsChanges<
6666 return result
6767 }
6868
69- if ( ! options . where && ! options . whereExpression ) {
69+ // TODO: handle orderBy and limit options
70+ // by calling optimizeOrderedLimit
71+
72+ if ( ! options . where ) {
7073 // No filtering, return all items
7174 return collectFilteredResults ( )
7275 }
7376
7477 // There's a where clause, let's see if we can use an index
7578 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
9480
9581 // Try to optimize the query using indexes
9682 const optimizationResult = optimizeExpressionWithIndexes (
@@ -113,11 +99,11 @@ export function currentStateAsChanges<
11399 }
114100 return result
115101 } 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+ }
120105
106+ const filterFn = createFilterFunctionFromExpression ( expression )
121107 return collectFilteredResults ( filterFn )
122108 }
123109 } catch ( error ) {
@@ -127,9 +113,11 @@ export function currentStateAsChanges<
127113 error
128114 )
129115
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+ }
133121
134122 return collectFilteredResults ( filterFn )
135123 }
@@ -201,11 +189,9 @@ export function createFilterFunctionFromExpression<T extends object>(
201189 */
202190export function createFilteredCallback < T extends object > (
203191 originalCallback : ( changes : Array < ChangeMessage < T > > ) => void ,
204- options : SubscribeChangesOptions < T >
192+ options : SubscribeChangesOptions
205193) : ( changes : Array < ChangeMessage < T > > ) => void {
206- const filterFn = options . whereExpression
207- ? createFilterFunctionFromExpression ( options . whereExpression )
208- : createFilterFunction ( options . where ! )
194+ const filterFn = createFilterFunctionFromExpression ( options . whereExpression ! )
209195
210196 return ( changes : Array < ChangeMessage < T > > ) => {
211197 const filteredChanges : Array < ChangeMessage < T > > = [ ]
0 commit comments