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

Commit ae6719b9 by sdepold

return results of emitters to the query chainer

1 parent a640bb6a
Showing with 73 additions and 20 deletions
......@@ -4,13 +4,15 @@ module.exports = (function() {
var QueryChainer = function(emitters) {
var self = this
this.finishedEmits = 0
this.emitters = []
this.serials = []
this.fails = []
this.finished = false
this.wasRunning = false
this.eventEmitter = null
this.finishedEmits = 0
this.emitters = []
this.serials = []
this.fails = []
this.serialResults = []
this.emitterResults = []
this.finished = false
this.wasRunning = false
this.eventEmitter = null
emitters = emitters || []
emitters.forEach(function(emitter) { self.add(emitter) })
......@@ -31,13 +33,14 @@ module.exports = (function() {
var self = this
this.eventEmitter = new Utils.CustomEventEmitter(function() {
self.wasRunning = true
finish.call(self)
finish.call(self, 'emitterResults')
})
return this.eventEmitter.run()
}
QueryChainer.prototype.runSerially = function(options) {
var self = this
var self = this
, serialCopy = Utils._.clone(this.serials)
options = Utils._.extend({
skipOnError: false
......@@ -67,16 +70,20 @@ module.exports = (function() {
onError('Skipped due to earlier error!')
} else {
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)
else
} else {
onSuccess()
}
}).error(onError)
}
} else {
self.wasRunning = true
finish.call(self)
finish.call(self, 'serialResults')
}
}
......@@ -91,14 +98,15 @@ module.exports = (function() {
var self = this
emitter
.success(function() {
.success(function(result) {
self.emitterResults[self.emitters.indexOf(emitter)] = result
self.finishedEmits++
finish.call(self)
finish.call(self, 'emitterResults')
})
.error(function(err) {
self.finishedEmits++
self.fails.push(err)
finish.call(self)
finish.call(self, 'emitterResults')
})
.on('sql', function(sql) {
if(self.eventEmitter) {
......@@ -107,17 +115,18 @@ module.exports = (function() {
})
}
var finish = function() {
var finish = function(resultsName) {
this.finished = true
if(this.emitters.length > 0)
if(this.emitters.length > 0) {
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)
}
if(this.finished && this.wasRunning) {
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)
}
......
......@@ -77,4 +77,48 @@ describe('QueryChainer', function() {
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!