Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate/3.0 - fix: proposal to keep before.find operations sync #318

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ test.before.find(function (userId, selector, options) {
});
```

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

--------------------------------------------------------------------------------

Expand Down
10 changes: 3 additions & 7 deletions find.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
hooks.before.forEach(hook => {
if (!hook.hook.constructor.name.includes('Async')) {
hook.hook.call(this, userId, selector, options)
} else {
throw new Error('Cannot use async function as before.find hook')
}
})

Expand All @@ -27,13 +29,7 @@ CollectionHooks.defineWrapper('find', function (userId, _super, instance, hooks,
if (cursor[method]) {
const originalMethod = cursor[method]
cursor[method] = async function (...args) {
// Apply asynchronous before hooks
for (const hook of hooks.before) {
if (hook.hook.constructor.name.includes('Async')) {
await hook.hook.call(this, userId, selector, options)
}
}

// Do not try to apply asynchronous before hooks here because they act on the cursor which is already defined
const result = await originalMethod.apply(this, args)

// Apply after hooks
Expand Down
3 changes: 2 additions & 1 deletion tests/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Tinytest.addAsync('find - selector should be {} when called without arguments',
const collection = new Mongo.Collection(null)

let findSelector = null
collection.before.find(async function (userId, selector, options) {
collection.before.find(function (userId, selector, options) {
findSelector = selector
return true
})

// hooks won't be triggered on find() alone, we must call fetchAsync()
Expand Down
2 changes: 1 addition & 1 deletion tests/find_after_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Tinytest.addAsync('issue #296 - after insert hook always finds all inserted', as
test.equal(afterCalled, true)
})

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

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