Skip to content

Commit

Permalink
perf(user): update only on change
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Oct 8, 2015
1 parent b4af9e0 commit 53fabd5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion api/services/user.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

import _ from 'lodash';
import { propsDiffer } from './utils.service';

import { User } from '../models';

exports.getById = id => {
Expand All @@ -14,8 +17,12 @@ exports.updateOrCreate = (githubId, name, avatar) => {

return exports.getByGithub(githubId)
.then(user => {

if (!user) { return User.create({ githubId, name, avatar }); }
return user;
if (!propsDiffer(user, { name, avatar })) { return user; }

_.assign(user, { name, avatar });
return user.save();
})
.then(user => {
if (user.banned) { throw new Error('You are banned from KeyCode.'); }
Expand Down
10 changes: 10 additions & 0 deletions api/services/utils.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

export function propsDiffer (one, two, arr) {
if (!arr) { arr = Object.keys(two); }
const len = arr.length;
for (let i = 0; i < len; ++i) {
if (one[i] !== two[i]) { return true; }
}
return false;
};

0 comments on commit 53fabd5

Please sign in to comment.