Skip to content

Commit

Permalink
Merge pull request #760 from dresende/async_loop_cleanup
Browse files Browse the repository at this point in the history
Rerwite some async loops using async lib
  • Loading branch information
dxg authored Dec 19, 2016
2 parents f5fc5bf + 0da4b8a commit 7ab2bba
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 87 deletions.
74 changes: 37 additions & 37 deletions lib/ChainFind.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require("lodash");
var async = require("async");
var Utilities = require("./Utilities");
var ChainInstance = require("./ChainInstance");
var Promise = require("./Promise").Promise;
Expand All @@ -18,7 +19,7 @@ function ChainFind(Model, opts) {
);
};

var chainRun = function (cb) {
var chainRun = function (done) {
var order, conditions;

conditions = Utilities.transformPropertyNames(
Expand All @@ -34,58 +35,57 @@ function ChainFind(Model, opts) {
merge : opts.merge,
offset : opts.offset,
exists : opts.exists
}, function (err, data) {
}, function (err, dataItems) {
if (err) {
return cb(err);
return done(err);
}
if (data.length === 0) {
return cb(null, []);
if (dataItems.length === 0) {
return done(null, []);
}
var pending = data.length;
var pending = dataItems.length;

var createInstance = function (idx) {
opts.newInstance(data[idx], function (err, instance) {
data[idx] = instance;

if (--pending === 0) {
return (opts.__eager && opts.__eager.length ? eagerLoading : cb)(null, data);
}
});
};

var eagerLoading = function (err, data) {
var eagerLoad = function (err, items) {
var pending = opts.__eager.length;
var idMap = {};
var count = 0;

var keys = _.map(data, function (instance) {
var key = instance[opts.keys[0]];
var keys = _.map(items, function (item, index) {
var key = item[opts.keys[0]];
// Create the association arrays
for (var i = 0, association; association = opts.__eager[i]; i++) {
instance[association.name] = [];
item[association.name] = [];
}
idMap[key] = index;

idMap[key] = count++;
return key;
});

_.map(opts.__eager, function (association) {
opts.driver.eagerQuery(association, opts, keys, function (err, instances) {
for (var i = 0, instance; instance = instances[i]; i++) {
// Perform a parent lookup with $p, and initialize it as an instance.
data[idMap[instance.$p]][association.name].push(association.model(instance));
}

if (--pending === 0) {
return cb(null, data);
}
});
});
async.eachSeries(opts.__eager,
function (association, cb) {
opts.driver.eagerQuery(association, opts, keys, function (err, instances) {
if (err) return cb(err)

for (var i = 0, instance; instance = instances[i]; i++) {
// Perform a parent lookup with $p, and initialize it as an instance.
items[idMap[instance.$p]][association.name].push(association.model(instance));
}
cb();
});
},
function (err) {
if (err) done(err);
else done(null, items);
}
);
};

for (var i = 0; i < data.length; i++) {
createInstance(i);
}
async.map(dataItems, opts.newInstance, function (err, items) {
if (err) return done(err);

var shouldEagerLoad = opts.__eager && opts.__eager.length;
var completeFn = shouldEagerLoad ? eagerLoad : done;

return completeFn(null, items);
});
});
}

Expand Down
72 changes: 38 additions & 34 deletions lib/Model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require("lodash");
var async = require("async");
var ChainFind = require("./ChainFind");
var Instance = require("./Instance").Instance;
var LazyLoad = require("./LazyLoad");
Expand Down Expand Up @@ -560,57 +561,60 @@ function Model(opts) {
};

model.create = function () {
var Instances = [];
var options = {};
var cb = null, idx = 0, single = false;
var createNext = function () {
if (idx >= Instances.length) {
return cb(null, single ? Instances[0] : Instances);
var itemsParams = []
var items = [];
var options = {};
var done = null;
var single = false;

for (var i = 0; i < arguments.length; i++) {
switch (typeof arguments[i]) {
case "object":
if ( !single && Array.isArray(arguments[i]) ) {
itemsParams = itemsParams.concat(arguments[i]);
} else if (i === 0) {
single = true;
itemsParams.push(arguments[i]);
} else {
options = arguments[i];
}
break;
case "function":
done = arguments[i];
break;
}
}

Instances[idx] = createInstance(Instances[idx], {
var iterator = function (params, index, cb) {
createInstance(params, {
is_new : true,
autoSave : opts.autoSave,
autoFetch : false
}, function (err) {
}, function (err, item) {
if (err) {
err.index = idx;
err.instance = Instances[idx];
err.index = index;
err.instance = item;

return cb(err);
}
Instances[idx].save(function (err) {
item.save(function (err) {
if (err) {
err.index = idx;
err.instance = Instances[idx];
err.index = index;
err.instance = item;

return cb(err);
}

idx += 1;
createNext();
items[index] = item;
cb();
});
});
};

for (var i = 0; i < arguments.length; i++) {
switch (typeof arguments[i]) {
case "object":
if ( !single && Array.isArray(arguments[i]) ) {
Instances = Instances.concat(arguments[i]);
} else if (i === 0) {
single = true;
Instances.push(arguments[i]);
} else {
options = arguments[i];
}
break;
case "function":
cb = arguments[i];
break;
}
}

createNext();
async.eachOfSeries(itemsParams, iterator, function (err) {
if (err) return done(err);
done(null, single ? items[0] : items);
});

return this;
};
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@
},
"analyse" : false,
"dependencies": {
"async" : "2.1.4",
"enforce" : "0.1.6",
"sql-query" : "0.1.26",
"sql-ddl-sync" : "0.3.12",
"hat" : "0.0.3",
"lodash" : "4.17.2",
"path-is-absolute" : "1.0.1"
"path-is-absolute" : "1.0.1",
"sql-query" : "0.1.26",
"sql-ddl-sync" : "0.3.12"
},
"devDependencies": {
"mysql" : "2.12.0",
"pg" : "6.1.0",
"sqlite3" : "3.1.8",
"async" : "1.5.2",
"mocha" : "3.2.0",
"should" : "11.1.1",
"mongodb" : "1.4.10",
Expand Down
24 changes: 12 additions & 12 deletions test/integration/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe("Hook", function() {
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function () {
beforeCreate.should.be.true;
Expand All @@ -200,7 +200,7 @@ describe("Hook", function() {
}));

it("should trigger error", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err) {
err.should.be.a.Object();
Expand Down Expand Up @@ -301,7 +301,7 @@ describe("Hook", function() {
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function () {
beforeSave.should.be.true;
Expand All @@ -324,7 +324,7 @@ describe("Hook", function() {
}));

it("should trigger error when creating", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "Jane Doe" }], function (err) {
err.should.be.a.Object();
Expand All @@ -335,7 +335,7 @@ describe("Hook", function() {
});

it("should trigger error when saving", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, John) {
should.equal(err, null);
Expand Down Expand Up @@ -429,7 +429,7 @@ describe("Hook", function() {

describe("if hook method has 1 argument", function () {
var beforeValidation = false;
this.timeout(500);
this.timeout(800);

before(setup({
beforeValidation : function (next) {
Expand Down Expand Up @@ -508,7 +508,7 @@ describe("Hook", function() {
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
afterLoad.should.be.true;
Expand All @@ -525,7 +525,7 @@ describe("Hook", function() {
}));

it("should return error", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
err.should.exist;
Expand Down Expand Up @@ -569,7 +569,7 @@ describe("Hook", function() {
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
afterAutoFetch.should.be.true;
Expand All @@ -586,7 +586,7 @@ describe("Hook", function() {
}));

it("should return error", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
err.should.exist;
Expand Down Expand Up @@ -628,7 +628,7 @@ describe("Hook", function() {
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
items[0].remove(function () {
Expand All @@ -650,7 +650,7 @@ describe("Hook", function() {
}));

it("should trigger error", function (done) {
this.timeout(500);
this.timeout(800);

Person.create([{ name: "John Doe" }], function (err, items) {
items[0].remove(function (err) {
Expand Down

0 comments on commit 7ab2bba

Please sign in to comment.