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

Add inital async_hooks support #351

Merged
merged 3 commits into from
May 29, 2018
Merged
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
53 changes: 49 additions & 4 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ var util = require('util')
var thunky = require('thunky')
var Readable = require('readable-stream').Readable

try {
var hooks = require('async_hooks')
} catch (err) {}

var Cursor = function (getCursor) {
Readable.call(this, {objectMode: true, highWaterMark: 0})

this._opts = {}

var self = this

this._destroyed = false
this._hook = hooks && new hooks.AsyncResource('mongojs:cursor')
this._get = thunky(function (cb) {
getCursor(function (err, cursor) {
if (err) { return cb(err) }
Expand All @@ -27,10 +34,15 @@ var Cursor = function (getCursor) {
util.inherits(Cursor, Readable)

Cursor.prototype.next = function (cb) {
if (this._hook) cb = wrapHook(this, cb)

var self = this

this._get(function (err, cursor) {
if (err) return cb(err)

if (cursor.cursorState.dead || cursor.cursorState.killed) {
destroy(self)
return cb(null, null)
} else {
cursor.next(cb)
Expand All @@ -41,6 +53,8 @@ Cursor.prototype.next = function (cb) {
}

Cursor.prototype.rewind = function (cb) {
if (this._hook) cb = wrapHook(this, cb)

this._get(function (err, cursor) {
if (err) return cb(err)
cursor.rewind(cb)
Expand Down Expand Up @@ -115,6 +129,8 @@ opts.forEach(function (opt) {
})

Cursor.prototype.count = function (cb) {
if (this._hook) cb = wrapHook(this, cb)

var self = this

this._get(function (err, cursor) {
Expand All @@ -124,6 +140,8 @@ Cursor.prototype.count = function (cb) {
}

Cursor.prototype.size = function (cb) {
if (this._hook) cb = wrapHook(this, cb)

var self = this

this._get(function (err, cursor) {
Expand All @@ -133,6 +151,8 @@ Cursor.prototype.size = function (cb) {
}

Cursor.prototype.explain = function (cb) {
if (this._hook) cb = wrapHook(this, cb)

this._get(function (err, cursor) {
if (err) { return cb(err) }
cursor.explain(cb)
Expand All @@ -142,13 +162,16 @@ Cursor.prototype.explain = function (cb) {
Cursor.prototype.destroy = function () {
var self = this
this._get(function (err, cursor) {
if (err) return self.emit('error', err)
if (err) return done(err)
if (cursor.close) {
cursor.close(function (err) {
if (err) { self.emit('error', err) }
})
cursor.close(done)
}
})

function done (err) {
if (err) { self.emit('error', err) }
destroy(self)
}
}

Cursor.prototype._read = function () {
Expand All @@ -160,3 +183,25 @@ Cursor.prototype._read = function () {
}

module.exports = Cursor

function destroy (self) {
if (self._destroyed) return
self._destroyed = true
if (self._hook) self._hook.emitDestroy()
}

function runInAsyncScope (self, cb, err, val) {
if (self._hook.runInAsyncScope) {
self._hook.runInAsyncScope(cb, null, err, val)
} else {
self._hook.emitBefore()
cb(err, val)
self._hook.emitAfter()
}
}

function wrapHook (self, cb) {
return function (err, val) {
runInAsyncScope(self, cb, err, val)
}
}