Skip to content
Browse files

Changed `this` in callbacks to be the instance you manipulated. Closes

  • Loading branch information...
1 parent ee186b1 commit 0cf82efaf2f29ddc80602533d46b894643511b9d @maritz committed
Showing with 92 additions and 36 deletions.
  1. +4 −11 extra/models.js
  2. +10 −5 extra/stress.js
  3. +10 −10 lib/relations.js
  4. +3 −3 lib/retrieve.js
  5. +7 −7 lib/store.js
  6. +46 −0 test/featureTests.js
  7. +12 −0 test/relationTests.js
View
15 extra/models.js
@@ -4,21 +4,14 @@ var UserMockup = exports.user = nohm.model('UserMockup', {
properties: {
name: {
type: 'string',
- value: 'test',
+ defaultValue: 'test',
validations: [
'notEmpty'
]
},
- email: {
- type: 'string',
- unique: true,
- value: 'email@email.de',
- validations: [
- 'email'
- ]
- },
key: {
- type: 'integer'
+ type: 'integer',
+ index: true
}
}
-});
+});
View
15 extra/stress.js
@@ -1,5 +1,5 @@
var nohm = require(__dirname+'/../lib/nohm').Nohm,
- iterations = 1, // Around 10k total model inserts.
+ iterations = 100000,
user;
nohm.setPrefix('stress');
@@ -38,14 +38,19 @@ var save = function (err) {
process.exit();
}
counter++;
+ process.stdout.write("\r"+counter);
if (counter >= iterations) {
- update();
+ //update();
+ console.log('done');
+ console.log((+ new Date()) - start+' ms for '+counter+' User saves and an equal amount of updates.');
+ nohm.client.quit();
}
};
for (var i = 0; i < iterations; i++) {
- users[i] = new UserModel();
- users[i].p({name: 'Bob', key: i});
- users[i].save(save);
+ var user = new UserModel();
+ user.p({name: 'Bob', key: i});
+ user.save(save);
+ delete user;
}
View
20 lib/relations.js
@@ -22,7 +22,7 @@ exports.belongsTo = function belongsTo(obj, name) {
if (err) {
this.logError(err);
}
- callback(err, !!value);
+ callback.call(self, err, !!value);
});
};
@@ -57,7 +57,7 @@ exports.getAll = function getAll(objName, name) {
return parseInt(val.toString(), 10);
});
}
- callback(err, value);
+ callback.call(self, err, value);
});
};
@@ -73,7 +73,7 @@ exports.numLinks = function numLinks(objName, name) {
if (err) {
self.logError(err);
}
- callback(err, value);
+ callback.call(self, err, value);
});
};
@@ -102,20 +102,20 @@ exports.__linkProxied = function __linkProxied(type, obj, name, cb) {
client[type](val.key, val.id, callback);
},
function (err) {
- cb(err);
+ cb.call(self, err);
}
);
} else if (err && childFail) {
- cb(err, childFail, childName);
+ cb.call(self, err, childFail, childName);
} else {
- cb(err);
+ cb.call(self, err);
}
};
if (allowedLinkTypes.indexOf(type) === -1) {
- cb('link/unlink type invocation wrong');
+ cb.call(self, 'link/unlink type invocation wrong');
} else if (!this.id) {
- cb('save parent object before adding a child.');
+ cb.call(self, 'save parent object before adding a child.');
} else if (!obj.id) {
obj.save(redisExec);
} else {
@@ -194,7 +194,7 @@ exports.unlinkAll = function (client, callback) {
selfLinks = [];
client = client || normalClient;
- // this.getClient() here because client can (and should) be a multi()
+ // we usenormalClient for fetching data and client (which could be a provided client in multi mode) for manipulating data
normalClient.smembers(Nohm.prefix.relationKeys+this.modelName+':'+this.id, function (err, keys) {
var others = [];
keys.forEach(function (key) {
@@ -238,7 +238,7 @@ exports.unlinkAll = function (client, callback) {
if (selfLinks.length > 0) {
client.del.apply(client, selfLinks);
}
- callback(err);
+ callback.call(self, err);
});
});
};
View
6 lib/retrieve.js
@@ -52,10 +52,10 @@ exports.load = function (id, callback) {
err = 'not found';
}
if (typeof(callback) === 'function') {
- callback(err, return_props);
+ callback.call(self, err, return_props);
}
});
-}
+};
/**
@@ -76,7 +76,7 @@ exports.find = function find(searches, callback) {
}
});
}
- callback(err, found);
+ callback.call(self, err, found);
},
getSets = function (callback) {
self.getClient().sinter(sets, callback);
View
14 lib/store.js
@@ -38,7 +38,7 @@ exports.save = function save(callback) {
if (action === 'create') {
self.id = null;
}
- callback('invalid');
+ callback.call(self, 'invalid');
} else if (valid && action === 'create') {
__create.call(self, callback);
} else if (valid) {
@@ -78,7 +78,7 @@ var __create = function __create(callback) {
} else {
console.log('Nohm: Creating an object resulted in a client error: ' + util.inspect(err));
if (typeof callback === 'function') {
- callback(err);
+ callback.call(self, err);
} else {
throw err;
}
@@ -152,7 +152,8 @@ __update = function __update(all, callback) {
async.forEachSeries(self.relationChanges,
function (item, cb) {
self['__' + item.action](item.object, item.name, function (err, childFail, childName) {
- item.callback(item.action,
+ item.callback.call(self,
+ item.action,
self.modelName,
item.name,
item.object);
@@ -170,7 +171,7 @@ __update = function __update(all, callback) {
Nohm.logError('Nohm: Updating an object resulted in a client error: ' + err);
throw err;
} else if (err) {
- callback(err.err, true, err.modelName);
+ callback.call(self, err.err, true, err.modelName);
} else {
self.__inDB = true;
for (var p in self.properties) {
@@ -178,11 +179,10 @@ __update = function __update(all, callback) {
self.__resetProp(p);
}
}
- callback();
+ callback.call(self);
}
}
);
-
}
});
};
@@ -240,7 +240,7 @@ var __realDelete = function __realDelete(callback) {
multi.exec(function (err, values) {
self.id = 0;
if (typeof callback === 'function') {
- callback(err);
+ callback.call(self, err);
} else {
Nohm.logError(err);
}
View
46 test/featureTests.js
@@ -605,3 +605,49 @@ exports.allPropertiesJson = function (t) {
t.done();
});
};
+
+exports.thisInCallbacks = function (t) {
+ var user = new UserMockup();
+ var checkCounter = 0;
+ var checkSum = 13;
+ var checkThis = function (name, cb) {
+ return function () {
+ checkCounter++;
+ t.ok(this instanceof UserMockup, '`this` is not set to the instance in '+name);
+ if (checkCounter === checkSum) {
+ done();
+ } else if (typeof(cb) === 'function') {
+ cb();
+ }
+ };
+ };
+ t.expect(checkSum+1);
+
+ var done = function () {
+ user.remove(checkThis('remove', function () {
+ t.done();
+ }));
+ };
+
+ user.save(checkThis('createError', function () {
+ user.p({
+ name: 'thisInCallbacks',
+ email: 'thisInCallbacks@test.de'
+ });
+ user.link(user, checkThis('link'));
+ user.save(checkThis('create', function () {
+ user.load(user.id, checkThis('load'));
+ user.find({name: 'thisInCallbacks'}, checkThis('find'));
+ user.save(checkThis('update', function (){
+ user.p('email', 'asd');
+ user.save(checkThis('updateError'));
+ }));
+ user.belongsTo(user, checkThis('belongsTo'));
+ user.getAll('UserMockup', checkThis('getAll'));
+ user.numLinks('UserMockup', checkThis('numLinks'));
+ user.link(user, 'childa', true, checkThis('linkDirect'));
+ user.unlink(user, 'childa', true, checkThis('linkDirect'));
+ user.unlinkAll(null, checkThis('linkDirect'));
+ }));
+ }));
+};
View
12 test/relationTests.js
@@ -337,6 +337,18 @@ exports.deeplinkError = function (t) {
t.done();
});
};
+
+exports.linkToSelf = function (t) {
+ var user = new UserLinkMockup();
+ t.expect(1);
+
+ user.link(user);
+
+ user.save(function (err) {
+ t.ok(!err, 'Linking an object to itself failed.');
+ t.done();
+ });
+};
/* Maybe this isn't such a good idea. I like that model definitions are completely
lacking relation definitions.

0 comments on commit 0cf82ef

Please sign in to comment.
Something went wrong with that request. Please try again.