-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (40 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
const asyncHooks = require('async_hooks');
const SCOPE_MODE = 'scope';
const EMIT_MODE = 'emit';
module.exports = (schema, options) => {
options = options || {};
schema.statics.$wrapCallback = function(callback) {
var _this = this;
const resourceName = `mongoose.${this.modelName}`;
const resource = new asyncHooks.AsyncResource(resourceName);
// add "mode" option for testing
if (resource.runInAsyncScope) {
options.mode = options.mode || SCOPE_MODE;
} else {
options.mode = EMIT_MODE;
}
return function() {
let emittedAfter = false;
const args = Array.prototype.slice.call(arguments, 0);
try {
if (options.mode === SCOPE_MODE) {
args.unshift(callback, null);
emittedAfter = true;
resource.runInAsyncScope.apply(resource, args);
return;
}
// asyncResource.emitBefore() and asyncResource.emittedAfter() are already deprecated since node v9.6.0
resource.emitBefore();
callback.apply(null, args);
emittedAfter = true;
resource.emitAfter();
} catch (error) {
if (!emittedAfter) {
resource.emitAfter();
}
_this.emit('error', error);
}
};
};
};