-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Async before & after hooks for all methods except find
- Loading branch information
Bero
committed
Apr 23, 2024
1 parent
5534b93
commit aa97b23
Showing
11 changed files
with
832 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
import { CollectionHooks } from './collection-hooks' | ||
|
||
CollectionHooks.defineAdvice('findOne', function (userId, _super, instance, aspects, getTransform, args, suppressAspects) { | ||
CollectionHooks.defineAdvice('findOne', async function (userId, _super, instance, aspects, getTransform, args, suppressAspects) { | ||
const ctx = { context: this, _super, args } | ||
const selector = CollectionHooks.normalizeSelector(instance._getFindSelector(args)) | ||
const options = instance._getFindOptions(args) | ||
let abort | ||
|
||
// before | ||
if (!suppressAspects) { | ||
aspects.before.forEach((o) => { | ||
const r = o.aspect.call(ctx, userId, selector, options) | ||
if (r === false) abort = true | ||
}) | ||
for (const o of aspects.before) { | ||
const r = await o.aspect.call(ctx, userId, selector, options) | ||
if (r === false) { | ||
abort = true | ||
break | ||
} | ||
} | ||
|
||
if (abort) return | ||
} | ||
|
||
function after (doc) { | ||
async function after (doc) { | ||
if (!suppressAspects) { | ||
aspects.after.forEach((o) => { | ||
for (const o of aspects.after) { | ||
o.aspect.call(ctx, userId, selector, options, doc) | ||
}) | ||
} | ||
} | ||
|
||
// return because of callAfterValueOrPromise | ||
return doc | ||
} | ||
|
||
const ret = _super.call(this, selector, options) | ||
return CollectionHooks.callAfterValueOrPromise(ret, (ret) => after(ret)) | ||
const ret = await _super.call(this, selector, options) | ||
await after(ret) | ||
return ret | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,16 @@ | |
|
||
Package.describe({ | ||
name: 'matb33:collection-hooks', | ||
summary: 'Extends Mongo.Collection with before/after hooks for insert/update/upsert/remove/find/findOne', | ||
summary: | ||
'Extends Mongo.Collection with before/after hooks for insert/update/upsert/remove/find/findOne', | ||
version: '1.3.1', | ||
git: 'https://github.com/Meteor-Community-Packages/meteor-collection-hooks' | ||
}) | ||
|
||
Package.onUse(function (api) { | ||
api.versionsFrom(['2.3', '2.8.1', '3.0-beta.0']) | ||
api.versionsFrom(['2.13', '3.0-beta.0']) | ||
|
||
api.use([ | ||
'mongo', | ||
'tracker', | ||
'ejson', | ||
'minimongo', | ||
'ecmascript' | ||
]) | ||
api.use(['mongo', 'tracker', 'ejson', 'minimongo', 'ecmascript']) | ||
|
||
api.use('zodern:[email protected]', 'server') | ||
|
||
|
@@ -31,7 +26,7 @@ Package.onUse(function (api) { | |
Package.onTest(function (api) { | ||
// var isTravisCI = process && process.env && process.env.TRAVIS | ||
|
||
api.versionsFrom(['1.12', '2.3', '3.0-beta.0']) | ||
api.versionsFrom(['2.13', '3.0-beta.0']) | ||
|
||
api.use([ | ||
'matb33:collection-hooks', | ||
|
Oops, something went wrong.
aa97b23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested it in a simple app and this is working nice : https://github.com/dokithonon/meteor-test-async/tree/3.0-rc.0
thanks a lot