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

promise.js 10.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
var util            = require("util")
  , Promise
  , EventEmitter    = require("events").EventEmitter
  , proxyEventKeys  = ['success', 'error', 'sql']
  , Utils           = require('./utils')
  , INTERNAL        = function() {}
  , async           = require("bluebird/js/main/async.js")

/**
 * A slightly modified version of bluebird promises. This means that, on top of the methods below, you can also call all the methods listed on the link below.
 *
 * The main difference is that sequelize promises allows you to attach a listener that will be called with the generated SQL, each time a query is run.
 * 
 * @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md
 * @class Promise
 */
var SequelizePromise = function(resolver) {
  var self = this;

  // Copied from Bluebird, bluebird doesn't like Promise.call(this)
  // mhansen wrote and is no fan of this, but sees no other way of making Promises first class while preserving SQL logging capabilities and BC.
  this._bitField = 0;
  this._fulfillmentHandler0 = void 0;
  this._rejectionHandler0 = void 0;
  this._promise0 = void 0;
  this._receiver0 = void 0;
  this._settledValue = void 0;
  this._boundTo = void 0;

  // Intercept the resolver so we can resolve with emit's
  this._resolveFromResolver(function resolverIntercept(resolve, reject) {
    self.seqResolve = resolve;
    self.seqReject = reject;

    if (resolver) {
      resolver.apply(this, arguments);
    }
  }.bind(this));

  // Sequelize speific
  this.$sql = [];
};

Promise = require("bluebird")

util.inherits(SequelizePromise, Promise)
Utils._.extend(SequelizePromise, Promise)

SequelizePromise.is = function (obj) {
  if (obj === void 0) return false;
  return obj instanceof Promise || obj instanceof SequelizePromise;
}

SequelizePromise.all = function(promises) {
  var resolved = SequelizePromise.resolve(Promise.all(promises));

  // Propagate sql events
  promises.forEach(function (promise) {
    if (SequelizePromise.is(promise)) {
      promise.on('sql', function (sql) {
        resolved.emit('sql', sql);
      });

      promise.$sql.forEach(function (sql) {
        resolved.emit('sql', sql);
      });  
    }    
  });

  return resolved;
};

SequelizePromise.settle = function (promises) {
  var settled = SequelizePromise.resolve(Promise.settle(promises))

  // Propagate sql events
  promises.forEach(function (promise) {
    if (SequelizePromise.is(promise)) {
      promise.on('sql', function (sql) {
        settled.emit('sql', sql);
      });

      promise.$sql.forEach(function (sql) {
        settled.emit('sql', sql);
      }); 
    }
  });

  return settled;
}

function tryCatchApply(fn, args, receiver) {
    try {
        return fn.apply(receiver, args);
    }
    catch (e) {
        return {e: e};
    }
}

SequelizePromise.method = function (fn) {
  if (typeof fn !== "function") {
      throw new TypeError(NOT_FUNCTION_ERROR);
  }
  return function Promise$_method() {
      var value = tryCatchApply(fn, Array.prototype.slice.apply(arguments), this);

      var ret = new SequelizePromise(INTERNAL);
      ret._setTrace(void 0);
      ret._resolveFromSyncValue(value);
      return ret;
  };
};

Promise.prototype._resolveFromSyncValue = function(value) {
    if (value && value.hasOwnProperty('e')) {
        this._cleanValues();
        this._setRejected();
        this._settledValue = value.e;
        this._ensurePossibleRejectionHandled();
    }
    else {
        var maybePromise = Promise._cast(value, void 0);
        if (maybePromise instanceof Promise) {
            this._follow(maybePromise);
        }
        else {
            this._cleanValues();
            this._setFulfilled();
            this._settledValue = value;
        }
    }
};

SequelizePromise.attempt = SequelizePromise["try"] = function (fn, args, ctx) {
    var value = tryCatchApply(fn, args, ctx)

    var ret = new SequelizePromise(INTERNAL);
    ret._setTrace(void 0);
    ret._resolveFromSyncValue(value);
    return ret;
};

// Need to hack resolve cause we can't hack all directrly
SequelizePromise.resolve = SequelizePromise.fulfilled = function(value) {
  var ret = new SequelizePromise(INTERNAL);

  if (ret._tryFollow(value)) {
    return ret;
  }
  ret._cleanValues();
  ret._setFulfilled();
  ret._settledValue = value;
  return ret;
};

SequelizePromise.promisify = function (callback, receiver) {
  function promisified() {
    var _receiver = receiver;
    var promise = new SequelizePromise(INTERNAL);
    promise._setTrace(void 0);
    var fn = function PromiseResolver$_callback(err, value) {
        if (err) {
            // var wrapped = wrapAsRejectionError(maybeWrapAsError(err));
            var wrapped = err
            promise._attachExtraTrace(wrapped);
            promise._reject(wrapped);
        }
        else {
            if (arguments.length > 2) {
                promise._fulfill(Array.prototype.slice.call(arguments, 1));
            }
            else {
                promise._fulfill(value);
            }
        }
    }
    try {
        callback.apply(_receiver, withAppended(arguments, fn));
    }
    catch(e) {
        // var wrapped = maybeWrapAsError(e);
        var wrapped = e
        promise._attachExtraTrace(wrapped);
        promise._reject(wrapped);
    }
    return promise;
  }
  promisified.__isPromisified__ = true;
  return promisified;
}

function withAppended(target, appendee) {
    var len = target.length;
    var ret = new Array(len + 1);
    var i;
    for (i = 0; i < len; ++i) {
        ret[i] = target[i];
    }
    ret[i] = appendee;
    return ret;
}

// Need to hack _then to make sure our promise is chainable
SequelizePromise.prototype._then = function (
  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);
  }

  // Start of sequelize specific 
  // Needed to transfer sql events accross .then() calls
  if (this.proxySql && ret && ret.emit) {
    this.proxySql(ret);
  }
  // End of sequelize specific 

  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._settlePromiseAt = function (index) {
  var receiver = this._receiverAt(index);

  if (this.$sql && receiver && receiver.emit) {
    this.$sql.forEach(function (sql) {
      receiver.emit('sql', sql);
    });
  }

  return Promise.prototype._settlePromiseAt.apply(this, arguments);
};

/**
 * Listen for events, event emitter style. Mostly for backwards compat. with EventEmitter
 * 
 * @param {String} evt
 * @param {Function} fct
 */
SequelizePromise.prototype.on = function(evt, fct) {
  if (evt === 'success') {
    this.then(fct);
  }
  else if (evt === 'error') {
    this.then(null, fct);
  }
  else {
    EventEmitter.prototype.on.call(this, evt, fct);
  }

  return this;
}

/**
 * Emit an event from the emitter
 * @param {string} type The type of event
 * @param {any}    value(s)* All other arguments will be passed to the event listeners
 */
SequelizePromise.prototype.emit = function(evt) {
  var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : [];

  if (evt === 'success') {
    this.seqResolve.apply(this, args);
  } else if (evt === 'error') {
    this.seqReject.apply(this, args);
  } else {
    // Needed to transfer sql across .then() calls
    if (evt === 'sql') {
      this.$sql.push(args[0]);
    }

    EventEmitter.prototype.emit.apply(this, [evt].concat(args));
  }

  return this;
};

/**
 * Listen for success events.
 * 
 * ```js
 * promise.success(function (result) {
 *  //...
 * });
 * ```
 *
 * @param {function} onSuccess
 * @method success
 * @alias ok
 * @return this
 */
SequelizePromise.prototype.success =
SequelizePromise.prototype.ok = function(fct) {
  if (fct.length > 1) {
    return this.spread(fct);
  } else {
    return this.then(fct);
  }
}

/**
 * Listen for error events
 * 
 * ```js
 * promise.error(function (err) {
 *  //...
 * });
 * ```
 *
 * @param {function} onError
 * @method error
 * @alias fail
 * @alias failure
 * @return this
 */
SequelizePromise.prototype.failure =
SequelizePromise.prototype.fail =
SequelizePromise.prototype.error = function(fct) {
  return this.then(null, fct);
}

/**
* Listen for both success and error events.
* 
* ```js
* promise.done(function (err, result) {
*  //...
* });
* ```
* 
* @param {function} onDone
* @method done
* @alias complete
* @return this
*/
SequelizePromise.prototype.done =
SequelizePromise.prototype.complete = function(fct) {
  if (fct.length > 2) {
    return this.spread(function () {
      fct.apply(null, [null].concat(Array.prototype.slice.call(arguments)));
    }, fct);
  } else {
    return this.then(function () {
      fct.apply(null, [null].concat(Array.prototype.slice.call(arguments)));
    }, fct);
  }
};

/*
 * Attach a function that is called every time the function that created this emitter executes a query.
 * @param {function} onSQL
 * @return this
 */
SequelizePromise.prototype.sql = function(fct) {
  this.on('sql', fct)
  return this;
}

/**
 * Proxy every event of this promise to another one.
 *
 * @param  {SequelizePromise} promise The promise that should receive the events.
 * @param  {Object}           [options]
 * @param  {Array}            [options.events] An array of the events to proxy. Defaults to sql, error and success
 * @return this
 */
SequelizePromise.prototype.proxy = function(promise, options) {
  options = Utils._.extend({
    events:     proxyEventKeys,
    skipEvents: []
  }, options || {})

  options.events = Utils._.difference(options.events, options.skipEvents)

  options.events.forEach(function (eventKey) {
    this.on(eventKey, function () {
      var args = [ eventKey ].concat([].slice.apply(arguments))
      promise.emit.apply(promise, args)
    })
  }.bind(this))

  return this
}

SequelizePromise.prototype.proxySql = function(promise) {
  return this.proxy(promise, {
    events: ['sql']
  });
};

module.exports = SequelizePromise;