Skip to content

Commit 46bb0fd

Browse files
author
emmanuel
committed
refactor
1 parent 03cd45e commit 46bb0fd

File tree

2 files changed

+39
-57
lines changed

2 files changed

+39
-57
lines changed

lib/roller.js

+17-24
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ var Roller = {
3232
roll: function(){
3333
var chain = Promise.resolve();
3434

35-
return TaskMdl.find().exec()
36-
.then(function(tasks){
37-
38-
tasks.forEach(function(task){
39-
chain = chain.then(function(){
40-
return rollBackTask(task);
41-
});
35+
return TaskMdl.find().exec().then(function (tasks) {
36+
tasks.forEach(function (task) {
37+
chain = chain.then(function () {
38+
return rollBackTask(task);
4239
});
43-
44-
return chain;
4540
});
41+
return chain;
42+
});
4643
}
4744

4845
// for internal use only
@@ -87,11 +84,9 @@ function getRollbackFunc(step){
8784
function rollbackSave(save, task) {
8885
var Collection = getModel(save.name);
8986

90-
return Collection.remove({_id: save.dataStore[0]._id})
91-
.exec()
92-
.then(function(){
93-
return updateState(task, save.index, ROLLED);
94-
});
87+
return Collection.remove({_id: save.dataStore[0]._id}).exec().then(function () {
88+
return updateState(task, save.index, ROLLED);
89+
});
9590
}
9691

9792
function rollbackDeleteOrUpdate(step, task){
@@ -100,16 +95,14 @@ function rollbackDeleteOrUpdate(step, task){
10095

10196
step.dataStore.forEach(function(data){
10297
chain = chain.then(function(){
103-
return Collection.findById(data._id)
104-
.exec()
105-
.then(function(doc){
106-
if(doc) {
107-
return doc.update(data).exec();
108-
}
109-
110-
doc = new Collection(data);
111-
return doc.save();
112-
});
98+
return Collection.findById(data._id).exec().then(function (doc) {
99+
if (doc) {
100+
return doc.update(data).exec();
101+
}
102+
103+
doc = new Collection(data);
104+
return doc.save();
105+
});
113106
});
114107
});
115108

lib/task.js

+22-33
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,21 @@ var Task = function(){
151151
steps = [];
152152
index = 0;
153153

154-
return dbTask
155-
.save()
156-
.then(function(_task){
157-
_task.steps.forEach(function(step){
158-
chain = chain.then(function(){
159-
return getResolveFunc(step)(step, _task);
160-
});
154+
return dbTask.save().then(function (_task) {
155+
_task.steps.forEach(function (step) {
156+
chain = chain.then(function () {
157+
return getResolveFunc(step)(step, _task);
161158
});
159+
});
162160

163-
return chain
164-
.then(function(){
165-
return _task.remove();
166-
})
167-
.catch(function(err){
168-
return Roller.rollOne(_task)
169-
.then(function(){
170-
throw err;
171-
});
172-
});
161+
return chain.then(function () {
162+
return _task.remove();
163+
}).catch(function (err) {
164+
return Roller.rollOne(_task).then(function () {
165+
throw err;
166+
});
173167
});
168+
});
174169
};
175170
};
176171

@@ -214,31 +209,25 @@ function performSave(save, task){
214209
task.steps[save.index].dataStore = dataStore;
215210
task.steps[save.index].markModified("dataStore");
216211

217-
return updateState(task, save.index, PENDING)
218-
.then(function(_task){
219-
return doc.save()
220-
.then(function(){
221-
return updateState(_task, save.index, DONE);
222-
});
212+
return updateState(task, save.index, PENDING).then(function (_task) {
213+
return doc.save().then(function () {
214+
return updateState(_task, save.index, DONE);
223215
});
216+
});
224217
}
225218

226219
function performRemove(remove, task){
227220
var Collection = getModel(remove.name);
228221

229222
handle$Token(remove.condition, true);
230223

231-
return storeOldData(remove, task)
232-
.then(function(){
233-
return updateState(task, remove.index, PENDING)
234-
.then(function(_task){
235-
return Collection.remove(remove.condition)
236-
.exec()
237-
.then(function(){
238-
return updateState(_task, remove.index, DONE);
239-
});
240-
});
224+
return storeOldData(remove, task).then(function () {
225+
return updateState(task, remove.index, PENDING).then(function (_task) {
226+
return Collection.remove(remove.condition).exec().then(function () {
227+
return updateState(_task, remove.index, DONE);
228+
});
241229
});
230+
});
242231
}
243232

244233
function storeOldData(step, task){

0 commit comments

Comments
 (0)