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

Commit ae6719b9 by sdepold

return results of emitters to the query chainer

1 parent a640bb6a
Showing with 65 additions and 12 deletions
...@@ -8,6 +8,8 @@ module.exports = (function() { ...@@ -8,6 +8,8 @@ module.exports = (function() {
this.emitters = [] this.emitters = []
this.serials = [] this.serials = []
this.fails = [] this.fails = []
this.serialResults = []
this.emitterResults = []
this.finished = false this.finished = false
this.wasRunning = false this.wasRunning = false
this.eventEmitter = null this.eventEmitter = null
...@@ -31,13 +33,14 @@ module.exports = (function() { ...@@ -31,13 +33,14 @@ module.exports = (function() {
var self = this var self = this
this.eventEmitter = new Utils.CustomEventEmitter(function() { this.eventEmitter = new Utils.CustomEventEmitter(function() {
self.wasRunning = true self.wasRunning = true
finish.call(self) finish.call(self, 'emitterResults')
}) })
return this.eventEmitter.run() return this.eventEmitter.run()
} }
QueryChainer.prototype.runSerially = function(options) { QueryChainer.prototype.runSerially = function(options) {
var self = this var self = this
, serialCopy = Utils._.clone(this.serials)
options = Utils._.extend({ options = Utils._.extend({
skipOnError: false skipOnError: false
...@@ -67,16 +70,20 @@ module.exports = (function() { ...@@ -67,16 +70,20 @@ module.exports = (function() {
onError('Skipped due to earlier error!') onError('Skipped due to earlier error!')
} else { } else {
var emitter = serial.klass[serial.method].apply(serial.klass, serial.params) var emitter = serial.klass[serial.method].apply(serial.klass, serial.params)
emitter.success(function() {
if(serial.options.success) emitter.success(function(result) {
self.serialResults[serialCopy.indexOf(serial)] = result
if(serial.options.success) {
serial.options.success(serial.klass, onSuccess) serial.options.success(serial.klass, onSuccess)
else } else {
onSuccess() onSuccess()
}
}).error(onError) }).error(onError)
} }
} else { } else {
self.wasRunning = true self.wasRunning = true
finish.call(self) finish.call(self, 'serialResults')
} }
} }
...@@ -91,14 +98,15 @@ module.exports = (function() { ...@@ -91,14 +98,15 @@ module.exports = (function() {
var self = this var self = this
emitter emitter
.success(function() { .success(function(result) {
self.emitterResults[self.emitters.indexOf(emitter)] = result
self.finishedEmits++ self.finishedEmits++
finish.call(self) finish.call(self, 'emitterResults')
}) })
.error(function(err) { .error(function(err) {
self.finishedEmits++ self.finishedEmits++
self.fails.push(err) self.fails.push(err)
finish.call(self) finish.call(self, 'emitterResults')
}) })
.on('sql', function(sql) { .on('sql', function(sql) {
if(self.eventEmitter) { if(self.eventEmitter) {
...@@ -107,17 +115,18 @@ module.exports = (function() { ...@@ -107,17 +115,18 @@ module.exports = (function() {
}) })
} }
var finish = function() { var finish = function(resultsName) {
this.finished = true this.finished = true
if(this.emitters.length > 0) 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) } else if(this.serials.length > 0) {
this.finished = (this.finishedEmits == this.serials.length) this.finished = (this.finishedEmits == this.serials.length)
}
if(this.finished && this.wasRunning) { if(this.finished && this.wasRunning) {
var status = (this.fails.length == 0 ? 'success' : 'error') var status = (this.fails.length == 0 ? 'success' : 'error')
, result = (this.fails.length == 0 ? result : this.fails) , result = (this.fails.length == 0 ? this[resultsName] : this.fails)
this.eventEmitter.emit(status, result) this.eventEmitter.emit(status, result)
} }
......
...@@ -77,4 +77,48 @@ describe('QueryChainer', function() { ...@@ -77,4 +77,48 @@ describe('QueryChainer', function() {
emitter3.run() emitter3.run()
}) })
}) })
describe('runSerially', function() {
it('finishes when all emitters are finished', function(done) {
var emitter1 = new CustomEventEmitter(function(e) { e.emit('success') })
var emitter2 = new CustomEventEmitter(function(e) { e.emit('success') })
this.queryChainer.add(emitter1, 'run')
this.queryChainer.add(emitter2, 'run')
this.queryChainer.runSerially().success(function() {
assert(true)
done()
})
})
it("returns the result of the passed emitters", function(done) {
var emitter1 = new CustomEventEmitter(function(e) { e.emit('success', 1) })
this.queryChainer.add(emitter1, 'run')
this.queryChainer.runSerially().success(function(results) {
expect(results).toBeDefined()
expect(results.length).toEqual(1)
expect(results[0]).toEqual(1)
done()
})
})
it("returns the result of the passed emitters in the order of the occurrence of adding the emitters", function(done) {
var emitter1 = new CustomEventEmitter(function(e) { e.emit('success', 1) })
, emitter2 = new CustomEventEmitter(function(e) { setTimeout(function() { e.emit('success', 2) }, 100) })
, emitter3 = new CustomEventEmitter(function(e) { e.emit('success', 3) })
this.queryChainer.add(emitter1, 'run')
this.queryChainer.add(emitter2, 'run')
this.queryChainer.add(emitter3, 'run')
this.queryChainer.runSerially().success(function(results) {
expect(results.length).toEqual(3)
expect(results).toEqual([1,2,3])
done()
})
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!