All files / src index.js

100% Statements 34/34
100% Branches 6/6
100% Functions 10/10
100% Lines 33/33
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 915x 5x 5x   5x 5x     67x 66x 66x 55x 1x 1x   1x     54x             66x 66x         66x   66x 2x 1x 1x   1x     1x     66x   66x       3x       4x       2x       1x       4x       4x       2x 2x     5x                        
const core = require("./core");
const coreCmds = require("./cmds");
const coreInterpreters = require("./interpreters");
 
let interpreters = Object.assign({}, coreInterpreters);
let context = {};
 
function promisify(fn) {
  if (fn.eadPromisified) return fn;
  const validator = fn.validator;
  const promised = function(...args) {
    if (validator) {
      try {
        validator(...args);
      } catch (e) {
        return Promise.reject(e);
      }
    }
    return core.call(context, interpreters, fn, ...args);
  };
  // try/catch because this is nice for reporting, but not
  // necessary for the system to function
  // Note: there is a unit test to validate this behavior
  // so errors, although swallowed here, are picked
  // up in the unit test.
  try {
    Object.defineProperty(promised, "name", {
      value: fn.name,
      writable: false
    });
  } catch (e) {}
  promised.eadFn = fn;
 
  promised.callWithContext = function(c, ...args) {
    if (validator) {
      try {
        validator(...args);
      } catch (e) {
        return Promise.reject(e);
      }
    }
    return core.call(Object.assign({}, context, c), interpreters, fn, ...args);
  };
 
  promised.eadPromisified = true;
 
  return promised;
}
 
function setContext(c) {
  context = c;
}
 
function getContext() {
  return context;
}
 
function addToContext(c) {
  context = Object.assign({}, context, c);
}
 
function setInterpreters(h) {
  interpreters = h;
}
 
function getInterpreters() {
  return interpreters;
}
 
function addInterpreters(h) {
  interpreters = Object.assign({}, interpreters, h);
}
 
function reset() {
  interpreters = {};
  context = {};
}
 
module.exports = Object.assign({}, coreCmds, {
  cmds: coreCmds,
  interpreters: coreInterpreters,
  promisify,
  setContext,
  getContext,
  addToContext,
  setInterpreters,
  getInterpreters,
  addInterpreters,
  reset
});