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

Commit 2f4d0f13 by Mick Hansen

feat(promises): implement promise in dao.destroy()

1 parent e41a2a67
...@@ -654,39 +654,29 @@ module.exports = (function() { ...@@ -654,39 +654,29 @@ module.exports = (function() {
options.force = options.force === undefined ? false : Boolean(options.force) options.force = options.force === undefined ? false : Boolean(options.force)
var self = this var self = this
, query = null , promise
return new Utils.CustomEventEmitter(function(emitter) { // This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self, function(err) { promise = self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self).then(function () {
if (!!err) { var query
return emitter.emit('error', err) , identifier
}
if (self.Model._timestampAttributes.deletedAt && options.force === false) { if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date() self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date()
query = self.save(options) query = self.save(options)
} else { } else {
var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id }; identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options) query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options)
} }
query.on('sql', function(sql) { return query.proxySql(promise);
emitter.emit('sql', sql) }).then(function (results) {
}) return self.Model.runHooks(self.Model.options.hooks.afterDestroy, self).then(function () {
.error(function(err) { return results;
emitter.emit('error', err) });
}) });
.success(function(results) {
self.Model.runHooks(self.Model.options.hooks.afterDestroy, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
emitter.emit('success', results) return promise
})
})
})
}).run()
} }
/** /**
......
...@@ -214,6 +214,12 @@ module.exports = (function() { ...@@ -214,6 +214,12 @@ module.exports = (function() {
return this return this
} }
CustomEventEmitter.prototype.proxySql = function(promise) {
return this.proxy(promise, {
events: ['sql']
})
}
/** /**
* Attach listeners to the emitter, promise style. * Attach listeners to the emitter, promise style.
* *
......
...@@ -56,11 +56,12 @@ Hooks.replaceHookAliases = function(hooks) { ...@@ -56,11 +56,12 @@ Hooks.replaceHookAliases = function(hooks) {
} }
Hooks.runHooks = function() { Hooks.runHooks = function() {
var self = this var self = this
, tick = 0 , tick = 0
, hooks = arguments[0] , hooks = arguments[0]
, args = Array.prototype.slice.call(arguments, 1, arguments.length-1) , lastIndex = arguments.length-1
, fn = arguments[arguments.length-1] , fn = typeof arguments[lastIndex] === "function" ? arguments[lastIndex] : null
, args = Array.prototype.slice.call(arguments, 1, fn ? lastIndex : arguments.length)
if (typeof hooks === "string") { if (typeof hooks === "string") {
hooks = this.options.hooks[hooks] || [] hooks = this.options.hooks[hooks] || []
...@@ -98,7 +99,9 @@ Hooks.runHooks = function() { ...@@ -98,7 +99,9 @@ Hooks.runHooks = function() {
run(hooks[tick]) run(hooks[tick])
}).spread(function () { }).spread(function () {
fn.apply(self, [null].concat(Array.prototype.slice.apply(arguments))); if (fn) {
fn.apply(self, [null].concat(Array.prototype.slice.apply(arguments)));
}
}, fn); }, fn);
} }
......
...@@ -4,6 +4,7 @@ var util = require("util") ...@@ -4,6 +4,7 @@ var util = require("util")
, proxyEventKeys = ['success', 'error', 'sql'] , proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils') , Utils = require('./utils')
, INTERNAL = function() {} , INTERNAL = function() {}
, async = require("bluebird/js/main/async.js")
var SequelizePromise = function(resolver) { var SequelizePromise = function(resolver) {
var self = this; var self = this;
...@@ -31,9 +32,33 @@ var SequelizePromise = function(resolver) { ...@@ -31,9 +32,33 @@ var SequelizePromise = function(resolver) {
util.inherits(SequelizePromise, Promise) util.inherits(SequelizePromise, Promise)
// Need to hack then to make sure our promise is chainable // Need to hack _then to make sure our promise is chainable
SequelizePromise.prototype.then = function (didFulfill, didReject, didProgress) { Promise.prototype._then = function (
return this._then(didFulfill, didReject, didProgress, void 0, new SequelizePromise(function () {}), this.then); didFulfill,
didReject,
didProgress,
receiver,
internalData
) {
var haveInternalData = internalData !== void 0;
var ret = haveInternalData ? internalData : new SequelizePromise(INTERNAL); // The relevant line, rest is fine
if (!haveInternalData && this._isBound()) {
ret._setBoundTo(this._boundTo);
}
var callbackIndex = this._addCallbacks(didFulfill, didReject, didProgress, ret, receiver);
if (!haveInternalData && this._cancellable()) {
ret._setCancellable();
ret._cancellationParent = this;
}
if (this.isResolved()) {
async.invoke(this._queueSettleAt, this, callbackIndex);
}
return ret;
}; };
SequelizePromise.prototype.on = function(evt, fct) { SequelizePromise.prototype.on = function(evt, fct) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!