Code coverage report for upstream/lib/emitters/custom-event-emitter.js

Statements: 91.11% (41 / 45)      Branches: 75% (6 / 8)      Functions: 87.5% (14 / 16)      Lines: 93.18% (41 / 44)     

All files » upstream/lib/emitters/ » custom-event-emitter.js
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 1211           1 28249       28249     1 1 6555   1   1 6554 6554 6554       6554                       1     11558 11558                       1       10072 10072     1     48 48   48 48 48   48     1                     1 577         577   577 1167 409       577     1 8   8 8   8 8           1      
var util           = require("util")
  , EventEmitter   = require("events").EventEmitter
  , Promise        = require("bluebird")
  , proxyEventKeys = ['success', 'error', 'sql']
  , Utils          = require('../utils')
 
var bindToProcess = function(fct) {
  Iif (fct && process.domain) {
    return process.domain.bind(fct)
  }
 
  return fct
};
 
module.exports = (function() {
  var CustomEventEmitter = function(fct) {
    this.fct = bindToProcess(fct)
  }
  util.inherits(CustomEventEmitter, EventEmitter)
 
  CustomEventEmitter.prototype.run = function() {
    Utils.tick(function() {
      Eif (this.fct) {
        this.fct.call(this, this)
      }
    }.bind(this))
 
    return this
  }
 
  /**
    Shortcut methods (success, ok) for listening for success events.
 
    Params:
      - fct: A function that gets executed once the *success* event was triggered.
 
    Result:
      The function returns the instance of the query.
  */
  CustomEventEmitter.prototype.success =
  CustomEventEmitter.prototype.ok =
  function(fct) {
    this.on('success', bindToProcess(fct))
    return this
  }
 
  /**
    Shortcut methods (failure, fail, error) for listening for error events.
 
    Params:
      - fct: A function that gets executed once the *error* event was triggered.
 
    Result:
      The function returns the instance of the query.
  */
  CustomEventEmitter.prototype.failure =
  CustomEventEmitter.prototype.fail =
  CustomEventEmitter.prototype.error =
  function(fct) {
    this.on('error', bindToProcess(fct))
    return this;
  }
 
  CustomEventEmitter.prototype.done =
  CustomEventEmitter.prototype.complete =
  function(fct) {
    fct = bindToProcess(fct);
    this.on('error', function(err) { fct(err, null) })
        .on('success', function() {
          var args = Array.prototype.slice.call(arguments);
          args.unshift(null);
          fct.apply(fct, args);
        })
    return this
  }
 
  CustomEventEmitter.prototype.sql = function(fct) {
    this.on('sql', bindToProcess(fct))
    return this;
  }
 
  /**
   * Proxy every event of this custom event emitter to another one.
   *
   * @param  {CustomEventEmitter} emitter The event emitter that should receive the events.
   * @return {void}
   */
  CustomEventEmitter.prototype.proxy = function(emitter, 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 (result) {
        emitter.emit(eventKey, result)
      })
    }.bind(this))
 
    return this
  }
 
  CustomEventEmitter.prototype.then = function(onFulfilled, onRejected) {
    var self = this
 
    onFulfilled = bindToProcess(onFulfilled)
    onRejected = bindToProcess(onRejected)
 
    return new Promise(function (resolve, reject) {
      self.on('error', reject)
          .on('success', resolve);
    }).then(onFulfilled, onRejected)
  }
 
 
  return CustomEventEmitter;
})()