@@ -6,8 +6,8 @@ const ASYNC_METHODS = ['countAsync', 'fetchAsync', 'forEachAsync', 'mapAsync']
66 * With Meteor v3 this behaves differently than with Meteor v2.
77 * We cannot use async hooks on find() directly because in Meteor it is a sync method that returns cursor instance.
88 *
9- * Modified to preserve v2 behavior: after.find hooks fire immediately when find() is called,
10- * while also maintaining async method wrapping for backward compatibility .
9+ * That's why we need to wrap all async methods of cursor instance. We're doing this by creating another cursor
10+ * within these wrapped methods with selector and options updated by before hooks .
1111 */
1212CollectionHooks . defineWrapper ( 'find' , function ( userId , _super , instance , hooks , getTransform , args , suppressHooks ) {
1313 const selector = CollectionHooks . normalizeSelector ( instance . _getFindSelector ( args ) )
@@ -24,35 +24,19 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
2424
2525 const cursor = _super . call ( this , selector , options )
2626
27- // PRESERVE V2 BEHAVIOR: Apply synchronous after hooks immediately
28- hooks . after . forEach ( hook => {
29- if ( ! hook . hook . constructor . name . includes ( 'Async' ) ) {
30- hook . hook . call ( this , userId , selector , options , cursor )
31- }
32- } )
33-
34- // Track which hooks have been called to avoid double execution
35- const immediateHooksExecuted = new Set ( )
36- hooks . after . forEach ( ( hook , index ) => {
37- if ( ! hook . hook . constructor . name . includes ( 'Async' ) ) {
38- immediateHooksExecuted . add ( index )
39- }
40- } )
41-
42- // Wrap async cursor methods (for backward compatibility and async hooks)
27+ // Wrap async cursor methods
4328 ASYNC_METHODS . forEach ( ( method ) => {
4429 if ( cursor [ method ] ) {
4530 const originalMethod = cursor [ method ]
4631 cursor [ method ] = async function ( ...args ) {
4732 // Do not try to apply asynchronous before hooks here because they act on the cursor which is already defined
4833 const result = await originalMethod . apply ( this , args )
4934
50- // Apply after hooks (skip already executed synchronous hooks)
51- for ( const [ index , hook ] of hooks . after . entries ( ) ) {
35+ // Apply after hooks
36+ for ( const hook of hooks . after ) {
5237 if ( hook . hook . constructor . name . includes ( 'Async' ) ) {
5338 await hook . hook . call ( this , userId , selector , options , this )
54- } else if ( ! immediateHooksExecuted . has ( index ) ) {
55- // Only call sync hooks that weren't already executed immediately
39+ } else {
5640 hook . hook . call ( this , userId , selector , options , this )
5741 }
5842 }
@@ -63,4 +47,4 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
6347 } )
6448
6549 return cursor
66- } )
50+ } )
0 commit comments