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

Commit 932fb177 by Mick Hansen

feat(promises): fix more tests/implementation and make sure Promise.all propagates all sql events

1 parent ef3727ad
...@@ -86,36 +86,22 @@ module.exports = (function() { ...@@ -86,36 +86,22 @@ module.exports = (function() {
instancePrototype[this.accessors.set] = function(associatedInstance, options) { instancePrototype[this.accessors.set] = function(associatedInstance, options) {
var instance = this var instance = this
return new Utils.CustomEventEmitter(function(emitter) { return instance[association.accessors.get](options).then(function(oldInstance) {
instance[association.accessors.get](options) if (oldInstance) {
.success(function(oldInstance) { oldInstance[association.identifier] = null
if (oldInstance) { return oldInstance.save(Utils._.extend({}, options, {
oldInstance[association.identifier] = null fields: [association.identifier],
oldInstance allowNull: [association.identifier],
.save(Utils._.extend({}, options, { association: true
fields: [association.identifier], }))
allowNull: [association.identifier], }
association: true }).then(function () {
})) if (associatedInstance) {
.success(function() { associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
if (associatedInstance) { return associatedInstance.save(options)
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier)) }
associatedInstance.save(options).proxy(emitter) return null;
} else { })
emitter.emit('success', null)
}
})
} else {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
}
})
.proxy(emitter, { events: ['sql'] })
}).run()
} }
return this return this
......
...@@ -39,7 +39,15 @@ util.inherits(SequelizePromise, Promise) ...@@ -39,7 +39,15 @@ util.inherits(SequelizePromise, Promise)
Utils._.extend(SequelizePromise, Promise) Utils._.extend(SequelizePromise, Promise)
SequelizePromise.all = function(promises) { SequelizePromise.all = function(promises) {
return SequelizePromise.resolve(Promise.all(promises)); var resolved = SequelizePromise.resolve(Promise.all(promises));
promises.forEach(function (promise) {
promise.on('sql', function (sql) {
resolved.emit('sql', sql);
});
});
return resolved;
}; };
// Need to hack resolve cause we can't hack all directrly // Need to hack resolve cause we can't hack all directrly
......
...@@ -522,65 +522,59 @@ module.exports = (function() { ...@@ -522,65 +522,59 @@ module.exports = (function() {
} }
} }
} }
} }
var emitter;
var tick = 0
var iterate = function(err, i) {
if (!!err || i >= cascades.length) {
return run(err)
}
dao[cascades[i]]().success(function(tasks) { return new Promise(function (resolve, reject) {
if (tasks === null || tasks.length < 1) { var tick = 0
return run() var iterate = function(err, i) {
if (err) {
return reject(err)
} }
tasks = Array.isArray(tasks) ? tasks : [tasks] if (i >= cascades.length) {
return resolve();
}
var ii = 0 dao[cascades[i]]().success(function(tasks) {
var next = function(err, ii) { if (tasks === null || tasks.length < 1) {
if (!!err || ii >= tasks.length) { return resolve()
return iterate(err)
} }
tasks[ii].destroy().error(function(err) { tasks = Array.isArray(tasks) ? tasks : [tasks]
return iterate(err)
})
.success(function() {
ii++
if (ii >= tasks.length) { var ii = 0
tick++ var next = function(err, ii) {
return iterate(null, tick) if (!!err || ii >= tasks.length) {
return iterate(err)
} }
next(null, ii) tasks[ii].destroy().error(function(err) {
}) return iterate(err)
} })
.success(function() {
ii++
next(null, ii) if (ii >= tasks.length) {
}) tick++
} return iterate(null, tick)
}
var run = function(err) { next(null, ii)
if (!!err) { })
return emitter.reject(err); }
next(null, ii)
})
} }
if (emitter) { if (cascades.length > 0) {
self.queryAndEmit([sql, dao, options], 'delete').proxy(emitter); iterate(null, tick)
} else { } else {
return self.queryAndEmit([sql, dao, options], 'delete'); resolve();
} }
} }).then(function () {
return self.queryAndEmit([sql, dao, options], 'delete');
if (cascades.length > 0) { });
emitter = new Promise();
iterate(null, tick)
} else {
return run()
}
} }
QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) { QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) {
......
...@@ -507,6 +507,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -507,6 +507,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
it('produce 3 errors', function(done) { it('produce 3 errors', function(done) {
this.Project.create({}).error(function(err) { this.Project.create({}).error(function(err) {
expect(err).to.be.an.instanceOf(Error) expect(err).to.be.an.instanceOf(Error)
delete err.stack // longStackTraces
expect(Object.keys(err)).to.have.length(3) expect(Object.keys(err)).to.have.length(3)
done() done()
}) })
......
...@@ -536,24 +536,6 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -536,24 +536,6 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
proxy.emit('success') proxy.emit('success')
}) })
it("should correctly work with error listeners", function(done) {
var emitter = new SequelizePromise(function () {})
, proxy = new SequelizePromise(function () {})
, error = sinon.spy()
emitter.error(error)
proxy.error(function() {
process.nextTick(function() {
expect(error.called).to.be.true
expect(error.firstCall.args[0]).to.be.an.instanceof(Error)
done()
})
})
proxy.proxy(emitter)
proxy.emit('error', new Error('reason'))
})
it("should correctly work with complete/done listeners", function(done) { it("should correctly work with complete/done listeners", function(done) {
var promise = new SequelizePromise(function () {}) var promise = new SequelizePromise(function () {})
, proxy = new SequelizePromise(function () {}) , proxy = new SequelizePromise(function () {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!