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

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)
.success(function(oldInstance) {
if (oldInstance) { if (oldInstance) {
oldInstance[association.identifier] = null oldInstance[association.identifier] = null
oldInstance return oldInstance.save(Utils._.extend({}, options, {
.save(Utils._.extend({}, options, {
fields: [association.identifier], fields: [association.identifier],
allowNull: [association.identifier], allowNull: [association.identifier],
association: true association: true
})) }))
.success(function() {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
} }
}) }).then(function () {
} else {
if (associatedInstance) { if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier)) associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter) return associatedInstance.save(options)
} else {
emitter.emit('success', null)
}
} }
return 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
......
...@@ -524,16 +524,20 @@ module.exports = (function() { ...@@ -524,16 +524,20 @@ module.exports = (function() {
} }
} }
var emitter; return new Promise(function (resolve, reject) {
var tick = 0 var tick = 0
var iterate = function(err, i) { var iterate = function(err, i) {
if (!!err || i >= cascades.length) { if (err) {
return run(err) return reject(err)
}
if (i >= cascades.length) {
return resolve();
} }
dao[cascades[i]]().success(function(tasks) { dao[cascades[i]]().success(function(tasks) {
if (tasks === null || tasks.length < 1) { if (tasks === null || tasks.length < 1) {
return run() return resolve()
} }
tasks = Array.isArray(tasks) ? tasks : [tasks] tasks = Array.isArray(tasks) ? tasks : [tasks]
...@@ -563,24 +567,14 @@ module.exports = (function() { ...@@ -563,24 +567,14 @@ module.exports = (function() {
}) })
} }
var run = function(err) {
if (!!err) {
return emitter.reject(err);
}
if (emitter) {
self.queryAndEmit([sql, dao, options], 'delete').proxy(emitter);
} else {
return self.queryAndEmit([sql, dao, options], 'delete');
}
}
if (cascades.length > 0) { if (cascades.length > 0) {
emitter = new Promise();
iterate(null, tick) iterate(null, tick)
} else { } else {
return run() resolve();
} }
}).then(function () {
return self.queryAndEmit([sql, dao, options], 'delete');
});
} }
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!