Skip to content

Commit 02b4493

Browse files
committed
fix: proposal to keep before.find sync
1 parent f60a25a commit 02b4493

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ test.before.find(function (userId, selector, options) {
195195
});
196196
```
197197

198-
__Important:__ This hook does not get called for `after.update` hooks (see https://github.com/Meteor-Community-Packages/meteor-collection-hooks/pull/297).
198+
__Important:__
199+
- The function used as `before.find` hook cannot be async
200+
- This hook does not get called for `after.update` hooks (see https://github.com/Meteor-Community-Packages/meteor-collection-hooks/pull/297).
199201

200202
--------------------------------------------------------------------------------
201203

find.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
1717
hooks.before.forEach(hook => {
1818
if (!hook.hook.constructor.name.includes('Async')) {
1919
hook.hook.call(this, userId, selector, options)
20+
} else {
21+
throw new Error('Cannot use async function as before.find hook')
2022
}
2123
})
2224

@@ -27,13 +29,7 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
2729
if (cursor[method]) {
2830
const originalMethod = cursor[method]
2931
cursor[method] = async function (...args) {
30-
// Apply asynchronous before hooks
31-
for (const hook of hooks.before) {
32-
if (hook.hook.constructor.name.includes('Async')) {
33-
await hook.hook.call(this, userId, selector, options)
34-
}
35-
}
36-
32+
// Do not try to apply asynchronous before hooks here because they act on the cursor which is already defined
3733
const result = await originalMethod.apply(this, args)
3834

3935
// Apply after hooks

tests/find.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Tinytest.addAsync('find - selector should be {} when called without arguments',
66
const collection = new Mongo.Collection(null)
77

88
let findSelector = null
9-
collection.before.find(async function (userId, selector, options) {
9+
collection.before.find(function (userId, selector, options) {
1010
findSelector = selector
11+
return true
1112
})
1213

1314
// hooks won't be triggered on find() alone, we must call fetchAsync()

tests/find_after_hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Tinytest.addAsync('issue #296 - after insert hook always finds all inserted', as
5353
test.equal(afterCalled, true)
5454
})
5555

56-
Tinytest.addAsync('async find hook - after insert hook always finds all inserted', async function (test, next) {
56+
Tinytest.addAsync('find hook - after insert hook always finds all inserted', async function (test, next) {
5757
const collection = new Mongo.Collection(null)
5858

5959
collection.before.find((userId, selector) => {

0 commit comments

Comments
 (0)