All files / lib ChainHandler.js

17.14% Statements 6/35
0% Branches 0/14
14.29% Functions 1/7
19.35% Lines 6/31
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  1x   1x   1x                     1x 7x 7x                                                              
'use strict'
const async = require('async')
 
const ChainHandler = module.exports = function ChainHandler () { }
 
ChainHandler.prototype.chain = function (otherHandler) {
  const self = this
  if (self.otherHandler instanceof ChainHandler) {
    self.otherHandler.chain(otherHandler)
    return self
  }
  self.otherHandler = otherHandler
  self.ready = true
  return self
};
 
[ 'Initialise', 'Close', 'Search', 'Find', 'Create', 'Delete', 'Update' ].forEach(action => {
  const lowerAction = action.toLowerCase()
  ChainHandler.prototype[lowerAction] = function (request) {
    const self = this
    const argsIn = Array.prototype.slice.call(arguments)
    const callback = argsIn.pop()
    // This block catches invocations to synchronous functions (.initialise())
    if (!(callback instanceof Function)) {
      argsIn.push(callback)
      if (self[`before${action}`]) self[`before${action}`].apply(self, argsIn)
      if (typeof self.otherHandler[lowerAction] === 'function') {
        // sync functions like .initialise() and .close() are optional
        self.otherHandler[lowerAction].apply(self.otherHandler, argsIn)
      }
      if (self[`after${action}`]) self[`after${action}`].apply(self, argsIn)
      return
    }
    async.waterfall([
      callback => callback.apply(null, [null].concat(argsIn)),
      function () {
        const argsOut = Array.prototype.slice.call(arguments)
        if (!self[`before${action}`]) return argsOut.pop().apply(self, [null].concat(argsOut))
        self[`before${action}`].apply(self, argsOut)
      },
      self.otherHandler[lowerAction].bind.apply(self.otherHandler[lowerAction], [self.otherHandler]), // eslint-disable-line no-useless-call
      function () {
        const argsOut = Array.prototype.slice.call(arguments)
        if (!self[`after${action}`]) return argsOut.pop().apply(self, [null].concat(argsOut))
        self[`after${action}`].apply(self, [request].concat(argsOut))
      }
    ], callback)
  }
})