不要怂,就是干,撸起袖子干!

Commit 434b85a1 by sdepold

added options for running query chainer serialized

1 parent 058666ae
Showing with 52 additions and 8 deletions
...@@ -40,11 +40,11 @@ module.exports = (function() { ...@@ -40,11 +40,11 @@ module.exports = (function() {
migrations.reverse() migrations.reverse()
migrations.forEach(function(migration) { migrations.forEach(function(migration) {
chainer.add(migration.execute(options)) chainer.add(migration, 'execute', [options])
}) })
chainer chainer
.run() .runSerial()
.success(function() { emitter.emit('success', null) }) .success(function() { emitter.emit('success', null) })
.error(function(err) { emitter.emit('failure', err) }) .error(function(err) { emitter.emit('failure', err) })
} }
...@@ -70,6 +70,10 @@ module.exports = (function() { ...@@ -70,6 +70,10 @@ module.exports = (function() {
return new Migration(self, self.options.path + '/' + file) return new Migration(self, self.options.path + '/' + file)
}) })
migrations = migrations.sort(function(a,b){
return parseInt(a.filename.split('-')[0]) - parseInt(b.filename.split('-')[0])
})
if(this.options.from) { if(this.options.from) {
filterFrom(migrations, this.options.from, function(err, migrations) { filterFrom(migrations, this.options.from, function(err, migrations) {
if(self.options.to) if(self.options.to)
......
...@@ -6,6 +6,7 @@ module.exports = (function() { ...@@ -6,6 +6,7 @@ module.exports = (function() {
this.finishedEmits = 0 this.finishedEmits = 0
this.emitters = [] this.emitters = []
this.serials = []
this.fails = [] this.fails = []
this.finished = false this.finished = false
this.wasRunning = false this.wasRunning = false
...@@ -16,9 +17,14 @@ module.exports = (function() { ...@@ -16,9 +17,14 @@ module.exports = (function() {
} }
Utils.addEventEmitter(QueryChainer) Utils.addEventEmitter(QueryChainer)
QueryChainer.prototype.add = function(emitter) { QueryChainer.prototype.add = function(emitterOrKlass, method, params) {
observeEmitter.call(this, emitter) if(!!method) {
this.emitters.push(emitter) this.serials.push({ klass: emitterOrKlass, method: method, params: params })
} else {
observeEmitter.call(this, emitterOrKlass)
this.emitters.push(emitterOrKlass)
}
return this return this
} }
...@@ -31,6 +37,33 @@ module.exports = (function() { ...@@ -31,6 +37,33 @@ module.exports = (function() {
return this.eventEmitter.run() return this.eventEmitter.run()
} }
QueryChainer.prototype.runSerial = function() {
var self = this
var exec = function() {
var serial = self.serials.pop()
if(serial) {
var emitter = serial.klass[serial.method].apply(serial.klass, serial.params)
emitter.success(function() {
self.finishedEmits++
exec()
}).error(function(err) {
self.finishedEmits++
self.fails.push(err)
exec()
})
} else {
self.wasRunning = true
finish.call(self)
}
}
this.serials.reverse()
this.eventEmitter = new Utils.CustomEventEmitter(exec)
return this.eventEmitter.run()
}
// private // private
var observeEmitter = function(emitter) { var observeEmitter = function(emitter) {
...@@ -47,11 +80,18 @@ module.exports = (function() { ...@@ -47,11 +80,18 @@ module.exports = (function() {
}) })
} }
var finish = function(result) { var finish = function() {
this.finished = true
if(this.emitters.length > 0)
this.finished = (this.finishedEmits == this.emitters.length) this.finished = (this.finishedEmits == this.emitters.length)
else if(this.serials.length > 0)
this.finished = (this.finishedEmits == this.serials.length)
if(this.finished && this.wasRunning) { if(this.finished && this.wasRunning) {
var status = this.fails.length == 0 ? 'success' : 'failure' var status = (this.fails.length == 0 ? 'success' : 'failure')
result = this.fails.length == 0 ? result : this.fails , result = (this.fails.length == 0 ? result : this.fails)
this.eventEmitter.emit(status, result) this.eventEmitter.emit(status, result)
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!