All files / src interpreters.js

100% Statements 52/52
100% Branches 8/8
100% Functions 36/36
100% Lines 49/49
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 1585x     18x       13x 2x   11x         13x 13x 13x     9x 5x       4x           8x           2x       1x       1x       2x 2x 2x 1x         3x 2x 2x 1x         1x             3x 10x 10x 5x         1x       7x       2x 1x 1x 1x 3x 3x   1x         1x 1x         2x 2x     1x           1x               2x 2x 2x   1x           1x     5x                                        
const { delay } = require("./util");
 
function call({ fn, args }, c) {
  return c.call(c.context, c.interpreters, fn.eadFn || fn, ...args);
}
 
function callFn({ fn, bindThis, args }, c) {
  if (fn.eadFn) {
    return call({ fn, args }, c);
  } else {
    return fn.call(bindThis, ...args);
  }
}
 
function callCallback({ fn, bindThis, args }, c) {
  return new Promise((resolve, reject) => {
    try {
      fn.apply(
        bindThis,
        args.concat((err, ...results) => {
          if (err) return reject(err);
          else resolve(...results);
        })
      );
    } catch (e) {
      reject(e);
    }
  });
}
 
function echo({ message }) {
  return message;
}
 
function globalVariable({ name }) {
  // istanbul ignore next
  const g = typeof window === undefined ? global : window;
  return g[name];
}
 
function log({ args }) {
  console.log(...args);
}
 
function logError({ args }) {
  console.error(...args);
}
 
function setImmediateInterpreter({ cmd }, { call, context, interpreters }) {
  delay(() => {
    call(context, interpreters, function*() {
      yield cmd;
    }).catch(e => e);
  });
}
 
function setTimeoutInterpreter({ cmd, time }, { call, context, interpreters }) {
  return setTimeout(() => {
    call(context, interpreters, function*() {
      yield cmd;
    }).catch(e => e);
  }, time);
}
 
function clearTimeoutInterpreter({ id }) {
  return clearTimeout(id);
}
 
function setIntervalInterpreter(
  { cmd, time },
  { call, context, interpreters }
) {
  return setInterval(() => {
    call(context, interpreters, function*() {
      yield cmd;
    }).catch(e => e);
  }, time);
}
 
function clearIntervalInterpreter({ id }) {
  return clearInterval(id);
}
 
function sleep({ time }) {
  return new Promise(resolve => setTimeout(resolve, time));
}
 
function series({ cmdList }, { call, context, interpreters }) {
  if (cmdList.length === 0) return [];
  return call(context, interpreters, function*() {
    const results = [];
    for (let i = 0; i < cmdList.length; i++) {
      const result = yield cmdList[i];
      results.push(result);
    }
    return results;
  });
}
 
function parallel({ cmdList }, { call, context, interpreters }) {
  return call(context, interpreters, function*() {
    return yield cmdList;
  });
}
 
function envelope({ cmd }, { call, context, interpreters }) {
  return call(context, interpreters, function*() {
    return yield cmd;
  })
    .then(result => {
      return {
        success: true,
        result
      };
    })
    .catch(e => {
      return {
        success: false,
        result: e
      };
    });
}
 
function either({ cmd, defaultValue }, { call, context, interpreters }) {
  return call(context, interpreters, function*() {
    try {
      return yield cmd;
    } catch (e) {
      return defaultValue;
    }
  });
}
 
function now() {
  return Date.now();
}
 
module.exports = {
  call,
  callFn,
  callCallback,
  echo,
  globalVariable,
  log,
  logError,
  setImmediate: setImmediateInterpreter,
  setTimeout: setTimeoutInterpreter,
  clearTimeout: clearTimeoutInterpreter,
  setInterval: setIntervalInterpreter,
  clearInterval: clearIntervalInterpreter,
  sleep,
  series,
  parallel,
  envelope,
  either,
  now
};