diff --git a/lib/datastore.js b/lib/datastore.js index dd99058..337973a 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -3,7 +3,7 @@ var fs = require('fs') , customUtils = require('./customUtils') , model = require('./model') , async = require('async') - , executor = require('./executor') + , Executor = require('./executor') ; @@ -13,6 +13,7 @@ var fs = require('fs') function Datastore (filename) { this.filename = filename; this.data = []; + this.executor = new Executor(); // We keep internally the number of lines in the datafile // This will be used when/if I implement autocompacting when the datafile grows too big @@ -53,7 +54,7 @@ Datastore.prototype._loadDatabase = function (cb) { }; Datastore.prototype.loadDatabase = function () { - executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); + this.executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); }; @@ -143,7 +144,7 @@ Datastore.prototype._insert = function (newDoc, cb) { }; Datastore.prototype.insert = function () { - executor.push({ this: this, fn: this._insert, arguments: arguments }); + this.executor.push({ this: this, fn: this._insert, arguments: arguments }); }; @@ -285,7 +286,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { ]); }; Datastore.prototype.update = function () { - executor.push({ this: this, fn: this._update, arguments: arguments }); + this.executor.push({ this: this, fn: this._update, arguments: arguments }); }; @@ -332,7 +333,7 @@ Datastore.prototype._remove = function (query, options, cb) { }); }; Datastore.prototype.remove = function () { - executor.push({ this: this, fn: this._remove, arguments: arguments }); + this.executor.push({ this: this, fn: this._remove, arguments: arguments }); }; diff --git a/lib/executor.js b/lib/executor.js index d645765..2a219f8 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -5,34 +5,41 @@ */ var async = require('async') - , queue ; -executor = async.queue(function (task, cb) { - var callback - , lastArg = task.arguments[task.arguments.length - 1] - ; +function Executor () { + this.queue = async.queue(function (task, cb) { + var callback + , lastArg = task.arguments[task.arguments.length - 1] + ; - // Always tell the queue task is complete. Execute callback if any was given. - if (typeof lastArg === 'function') { - callback = function () { - lastArg.apply(null, arguments); - cb(); - }; + // Always tell the queue task is complete. Execute callback if any was given. + if (typeof lastArg === 'function') { + callback = function () { + lastArg.apply(null, arguments); + cb(); + }; - task.arguments[task.arguments.length - 1] = callback; - } else { - callback = function () { - cb(); - }; + task.arguments[task.arguments.length - 1] = callback; + } else { + callback = function () { + cb(); + }; - task.arguments.push(callback); - } + task.arguments.push(callback); + } + + task.fn.apply(task.this, task.arguments); + }, 1); +} + + +Executor.prototype.push = function () { + this.queue.push.apply(this, arguments); +}; - task.fn.apply(task.this, task.arguments); -}, 1); // Interface -module.exports = executor; +module.exports = Executor; diff --git a/package.json b/package.json index 4f5a7cd..d73b8f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nedb", - "version": "0.5.1", + "version": "0.5.2", "author": { "name": "tldr.io", "email": "hello@tldr.io"