Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Commit

Permalink
Making synchronous functions not use optional callback for result
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Skillington committed Nov 12, 2014
1 parent 800f4ed commit 0f97e91
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 57 deletions.
9 changes: 8 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ Client.prototype.sendScopeStateMessage = function(scope, scopeIndex, callback) {
function getSyncFragments(modelObjects, nextCallback) {
// TODO: sliced map to avoid blocking CPU
async.map(modelObjects, function(modelObject, doneCallback) {
modelObject.getAddSyncFragment(doneCallback);
var syncFragment;
try {

This comment has been minimized.

Copy link
@Raynos

Raynos Nov 13, 2014

why try catch ?

syncFragment = modelObject.getAddSyncFragment();
} catch (err) {
return doneCallback(err);
}
doneCallback(null, syncFragment);

}, nextCallback);
}

Expand Down
27 changes: 9 additions & 18 deletions lib/model_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ ModelObject.prototype.setScopeAndMakeRootModel = function(scope, callback) {
}.bind(this));
};

ModelObject.prototype.getChildModelObjectUUIDs = function(callback) {
ModelObject.prototype.getChildModelObjectUUIDs = function() {
var uuids = [];
this.getProperties().forEach(function(property) {
if (property.isModelObjectType) {
Expand All @@ -449,7 +449,6 @@ ModelObject.prototype.getChildModelObjectUUIDs = function(callback) {
}
}
}.bind(this));
maybeCallback(callback)(null, uuids);
return uuids;
};

Expand Down Expand Up @@ -480,7 +479,7 @@ ModelObject.prototype.getChildModelObjects = function(callback) {
});
};

ModelObject.prototype.getValues = function(callback) {
ModelObject.prototype.getValues = function() {
var results = {};
this.getProperties().forEach(function(property) {
if (!property.isModelObjectType) {
Expand All @@ -497,28 +496,20 @@ ModelObject.prototype.getValues = function(callback) {
} else {
var modelObject = this[property.name];
if (modelObject) {
results[property.name] = modelObject.uuid;
results[property.name] = modelObject.uuid;
}
}
}
}.bind(this));
maybeCallback(callback)(null, results);
return results;
};

ModelObject.prototype.getAddSyncFragment = function(callback) {
var syncFragment;
try {
syncFragment = new SyncFragment({
type: 'add',
modelObject: this,
properties: this.getValues()
});
} catch (err) {
return callback(err);
}

callback(null, syncFragment);
ModelObject.prototype.getAddSyncFragment = function() {
return new SyncFragment({
type: 'add',
modelObject: this,
properties: this.getValues()
});
};

ModelObject.prototype.getProperty = function(propertyName) {
Expand Down
27 changes: 11 additions & 16 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,23 @@ Scope.prototype.getAllModelObjects = function(callback) {

results.push(modelObject);

modelObject.getChildModelObjectUUIDs(function(err, uuids) {
var uuids = modelObject.getChildModelObjectUUIDs();
if (uuids.length < 1) {
return doneCallback(null, results);
}

async.map(uuids, getRecursively, function(err, modelObjectArrays) {
if (err) {
return doneCallback(err);
}

if (uuids.length < 1) {
return doneCallback(null, results);
if (modelObjectArrays.length > 0) {
modelObjectArrays.forEach(function(modelObjects) {
results = results.concat(modelObjects);
});
}

async.map(uuids, getRecursively, function(err, modelObjectArrays) {
if (err) {
return doneCallback(err);
}

if (modelObjectArrays.length > 0) {
modelObjectArrays.forEach(function(modelObjects) {
results = results.concat(modelObjects);
});
}

doneCallback(null, results);
});
doneCallback(null, results);
});
});
}
Expand Down
39 changes: 17 additions & 22 deletions test/model_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,11 @@ describe(method('getChildModelObjectUUIDs'), 'when getting child UUIDs', functio
parent.partner = otherParent;
parent.children = [child1, child2];

parent.getChildModelObjectUUIDs(function(err, uuids) {
assert.ifError(err);
assert.ok(_.isEqual(_.map([otherParent, child1, child2], function(modelObject) {
return modelObject.uuid;
}), uuids));
assert.end();
});
var uuids = parent.getChildModelObjectUUIDs();
assert.ok(_.isEqual(_.map([otherParent, child1, child2], function(modelObject) {
return modelObject.uuid;
}), uuids));
assert.end();
});

});
Expand Down Expand Up @@ -749,23 +747,20 @@ describe(method('getAddSyncFragment'), 'when getting ModelObject as an add fragm
parent.partner = otherParent;
parent.children = [child1, child2];

parent.getAddSyncFragment(function(err, syncFragment) {
assert.ifError(err);
var syncFragment = parent.getAddSyncFragment();
assert.equal(syncFragment.type, 'add');
assert.equal(syncFragment.objectUUID, parent.uuid);
assert.equal(syncFragment.clsName, 'Person');

assert.equal(syncFragment.type, 'add');
assert.equal(syncFragment.objectUUID, parent.uuid);
assert.equal(syncFragment.clsName, 'Person');
var values = syncFragment.properties;
assert.equal(Object.keys(values).length, 5);
assert.equal(values.name, 'Sam');
assert.equal(values.age, 28);
assert.ok(_.isEqual(values.nicknames, ['samster', 'samsonite']));
assert.equal(values.partner, otherParent.uuid);
assert.ok(_.isEqual(values.children, [child1.uuid, child2.uuid]));

var values = syncFragment.properties;
assert.equal(Object.keys(values).length, 5);
assert.equal(values.name, 'Sam');
assert.equal(values.age, 28);
assert.ok(_.isEqual(values.nicknames, ['samster', 'samsonite']));
assert.equal(values.partner, otherParent.uuid);
assert.ok(_.isEqual(values.children, [child1.uuid, child2.uuid]));

assert.end();
});
assert.end();
});

});
Expand Down

0 comments on commit 0f97e91

Please sign in to comment.