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

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() {
instancePrototype[this.accessors.set] = function(associatedInstance, options) {
var instance = this
return new Utils.CustomEventEmitter(function(emitter) {
instance[association.accessors.get](options)
.success(function(oldInstance) {
if (oldInstance) {
oldInstance[association.identifier] = null
oldInstance
.save(Utils._.extend({}, options, {
fields: [association.identifier],
allowNull: [association.identifier],
association: true
}))
.success(function() {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} 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 instance[association.accessors.get](options).then(function(oldInstance) {
if (oldInstance) {
oldInstance[association.identifier] = null
return oldInstance.save(Utils._.extend({}, options, {
fields: [association.identifier],
allowNull: [association.identifier],
association: true
}))
}
}).then(function () {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
return associatedInstance.save(options)
}
return null;
})
}
return this
......
......@@ -39,7 +39,15 @@ util.inherits(SequelizePromise, Promise)
Utils._.extend(SequelizePromise, Promise)
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
......
......@@ -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) {
if (tasks === null || tasks.length < 1) {
return run()
return new Promise(function (resolve, reject) {
var tick = 0
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
var next = function(err, ii) {
if (!!err || ii >= tasks.length) {
return iterate(err)
dao[cascades[i]]().success(function(tasks) {
if (tasks === null || tasks.length < 1) {
return resolve()
}
tasks[ii].destroy().error(function(err) {
return iterate(err)
})
.success(function() {
ii++
tasks = Array.isArray(tasks) ? tasks : [tasks]
if (ii >= tasks.length) {
tick++
return iterate(null, tick)
var ii = 0
var next = function(err, ii) {
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) {
if (!!err) {
return emitter.reject(err);
next(null, ii)
})
}
next(null, ii)
})
}
if (emitter) {
self.queryAndEmit([sql, dao, options], 'delete').proxy(emitter);
if (cascades.length > 0) {
iterate(null, tick)
} else {
return self.queryAndEmit([sql, dao, options], 'delete');
resolve();
}
}
if (cascades.length > 0) {
emitter = new Promise();
iterate(null, tick)
} else {
return run()
}
}).then(function () {
return self.queryAndEmit([sql, dao, options], 'delete');
});
}
QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) {
......
......@@ -507,6 +507,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
it('produce 3 errors', function(done) {
this.Project.create({}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
delete err.stack // longStackTraces
expect(Object.keys(err)).to.have.length(3)
done()
})
......
......@@ -536,24 +536,6 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
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) {
var promise = 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!