All files / src interpreters.js

100% Statements 65/65
100% Branches 14/14
100% Functions 41/41
100% Lines 60/60
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 1947x     40x       14x 2x   12x         13x 13x 13x     9x 5x       4x           10x               3x       1x       1x       74x             14x 14x 14x 14x 2x               14x 13x 13x 13x 1x         1x             4x 47x 47x 47x 6x         1x       7x       2x 1x 1x 1x 3x 3x   1x         1x 1x         2x 2x     1x           1x               2x 2x 2x   1x           37x     7x     6x 3x 3x 2x       4x       4x     7x                                                
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 noop() {}
 
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 getSubStack(context, resetStack) {
  return resetStack ? Object.assign({}, context, { stack: [] }) : context;
}
 
function setImmediateInterpreter(
  { cmd, resetStack },
  { call, context, interpreters }
) {
  delay(() => {
    const subcontext = getSubStack(context, resetStack);
    call(subcontext, interpreters, function*() {
      yield cmd;
    }).catch(e => e);
  });
}
 
function setTimeoutInterpreter(
  { cmd, time, resetStack },
  { call, context, interpreters }
) {
  return setTimeout(() => {
    const subcontext = getSubStack(context, resetStack);
    call(subcontext, interpreters, function*() {
      yield cmd;
    }).catch(e => e);
  }, time);
}
 
function clearTimeoutInterpreter({ id }) {
  return clearTimeout(id);
}
 
function setIntervalInterpreter(
  { cmd, time, resetStack },
  { call, context, interpreters }
) {
  return setInterval(() => {
    const subcontext = getSubStack(context, resetStack);
    call(subcontext, 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();
}
 
let state = {};
 
function getState({ path, defaultValue }) {
  if (!path) return state;
  const value = state[path];
  if (value === undefined) return defaultValue;
  return value;
}
 
function setState({ path, value }) {
  state[path] = value;
}
 
function clearState() {
  state = {};
}
 
module.exports = {
  call,
  callFn,
  callCallback,
  echo,
  noop,
  globalVariable,
  log,
  logError,
  setImmediate: setImmediateInterpreter,
  setTimeout: setTimeoutInterpreter,
  clearTimeout: clearTimeoutInterpreter,
  setInterval: setIntervalInterpreter,
  clearInterval: clearIntervalInterpreter,
  sleep,
  series,
  parallel,
  envelope,
  either,
  now,
  getState,
  setState,
  clearState
};