Skip to content

Commit b5702a4

Browse files
committed
Simplify Instance.save control flow (and lint some stuff)
1 parent ed6140f commit b5702a4

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

lib/Instance.js

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function Instance(Model, opts) {
9797
cb(err, instance);
9898
}
9999
};
100-
var saveInstance = function (cb, saveOptions) {
100+
var saveInstance = function (saveOptions, cb) {
101101
// what this condition means:
102102
// - If the instance is in state mode
103103
// - AND it's not an association that is asking it to save
@@ -118,29 +118,20 @@ function Instance(Model, opts) {
118118
return saveError(cb, err);
119119
}
120120

121-
return saveNew(cb, saveOptions, getInstanceData());
121+
return saveNew(saveOptions, getInstanceData(), cb);
122122
});
123123
} else {
124124
waitHooks([ "beforeSave" ], function (err) {
125125
if (err) {
126126
return saveError(cb, err);
127127
}
128128

129-
if (opts.changes.length === 0) {
130-
if (saveOptions.saveAssociations === false) {
131-
return saveInstanceExtra(cb);
132-
}
133-
return saveAssociations(function (err) {
134-
return afterSave(cb, false, err);
135-
});
136-
}
137-
138-
return savePersisted(cb, saveOptions, getInstanceData());
129+
return savePersisted(saveOptions, getInstanceData(), cb);
139130
});
140131
}
141132
});
142133
};
143-
var afterSave = function (cb, create, err) {
134+
var runAfterSaveActions = function (cb, create, err) {
144135
instance_saving = false;
145136

146137
emitEvent("save", err, instance);
@@ -150,9 +141,7 @@ function Instance(Model, opts) {
150141
}
151142
Hook.trigger(instance, opts.hooks.afterSave, !err);
152143

153-
if (!err) {
154-
saveInstanceExtra(cb);
155-
}
144+
cb();
156145
};
157146
var getInstanceData = function () {
158147
var data = {}, prop;
@@ -190,8 +179,13 @@ function Instance(Model, opts) {
190179

191180
return nextHook();
192181
};
193-
var saveNew = function (cb, saveOptions, data) {
194-
var next = afterSave.bind(this, cb, true);
182+
var saveNew = function (saveOptions, data, cb) {
183+
var finish = function (err) {
184+
runAfterSaveActions(function () {
185+
if (err) return cb(err);
186+
saveInstanceExtra(cb);
187+
}, true);
188+
}
195189

196190
opts.driver.insert(opts.table, data, opts.id, function (save_err, info) {
197191
if (save_err) {
@@ -205,36 +199,56 @@ function Instance(Model, opts) {
205199
opts.is_new = false;
206200

207201
if (saveOptions.saveAssociations === false) {
208-
return next();
202+
return finish();
209203
}
210204

211-
return saveAssociations(next);
205+
return saveAssociations(finish);
212206
});
213207
};
214-
var savePersisted = function (cb, saveOptions, data) {
215-
var next = afterSave.bind(this, cb, false);
208+
var savePersisted = function (saveOptions, data, cb) {
216209
var changes = {}, conditions = {};
217210

218-
for (var i = 0; i < opts.changes.length; i++) {
219-
changes[opts.changes[i]] = data[opts.changes[i]];
220-
}
221-
for (i = 0; i < opts.id.length; i++) {
222-
conditions[opts.id[i]] = data[opts.id[i]];
223-
}
224-
225-
opts.driver.update(opts.table, changes, conditions, function (save_err) {
226-
if (save_err) {
227-
return saveError(cb, save_err);
211+
var next = function (saved) {
212+
var finish = function () {
213+
saveInstanceExtra(cb);
228214
}
229215

230-
opts.changes.length = 0;
216+
if(!saved && saveOptions.saveAssociations === false) {
217+
finish();
218+
} else {
219+
if (saveOptions.saveAssociations === false) {
220+
runAfterSaveActions(function () {
221+
finish();
222+
}, false);
223+
} else {
224+
saveAssociations(function (err) {
225+
runAfterSaveActions(function () {
226+
if (err) return cb(err);
227+
finish();
228+
}, false, err);
229+
});
230+
}
231+
}
232+
}
231233

232-
if (saveOptions.saveAssociations === false) {
233-
return next();
234+
if (opts.changes.length === 0) {
235+
next(false);
236+
} else {
237+
for (var i = 0; i < opts.changes.length; i++) {
238+
changes[opts.changes[i]] = data[opts.changes[i]];
234239
}
240+
for (i = 0; i < opts.id.length; i++) {
241+
conditions[opts.id[i]] = data[opts.id[i]];
242+
}
243+
opts.driver.update(opts.table, changes, conditions, function (err) {
244+
if (err) {
245+
return saveError(cb, err);
246+
}
247+
opts.changes.length = 0;
235248

236-
return saveAssociations(next);
237-
});
249+
next(true);
250+
});
251+
}
238252
};
239253
var saveAssociations = function (cb) {
240254
var pending = 1, errored = false, i, j;
@@ -333,7 +347,7 @@ function Instance(Model, opts) {
333347
}
334348

335349
opts.driver.update(opts.extra_info.table, data, conditions, function (err) {
336-
if (cb) return cb(err, instance);
350+
return cb(err);
337351
});
338352
};
339353
var removeInstance = function (cb) {
@@ -512,7 +526,7 @@ function Instance(Model, opts) {
512526
Object.defineProperty(instance, "save", {
513527
value: function () {
514528
var arg = null, objCount = 0;
515-
var data = {}, saveOptions = {}, callback = null;
529+
var data = {}, saveOptions = {}, cb = null;
516530

517531
while (arguments.length > 0) {
518532
arg = Array.prototype.shift.call(arguments);
@@ -530,7 +544,7 @@ function Instance(Model, opts) {
530544
objCount++;
531545
break;
532546
case 'function':
533-
callback = arg;
547+
cb = arg;
534548
break;
535549
default:
536550
var err = new Error("Unknown parameter type '" + (typeof arg) + "' in Instance.save()");
@@ -545,7 +559,12 @@ function Instance(Model, opts) {
545559
}
546560
}
547561

548-
saveInstance(callback, saveOptions);
562+
saveInstance(saveOptions, function (err) {
563+
if (!cb) return;
564+
if (err) return cb(err);
565+
566+
return cb(null, instance);
567+
});
549568

550569
return this;
551570
},

0 commit comments

Comments
 (0)