All files / es5 interpreters.js

98.3% Statements 173/176
79.63% Branches 43/54
96.15% Functions 50/52
98.8% Lines 165/167
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393    54x           54x   7x 7x     40x 40x 40x       14x 14x 14x   14x 2x         12x         13x 13x 13x 13x 13x 13x 9x 7x     9x     4x           10x 10x           3x     3x           1x   1x           1x   1x       76x           14x 14x 14x 14x 14x 14x 14x 14x     14x 28x 28x   14x 14x       14x         2x           14x 14x 14x 14x 14x 14x 14x 13x 13x     13x 26x 26x   13x 13x       13x         1x           1x 1x       4x 4x 4x 4x 4x 4x 4x 49x 49x     49x 98x 98x   49x 49x       49x         6x           1x 1x       7x 7x 7x         2x 2x 2x 2x 2x 1x       1x 5x 9x   1x 1x     4x 1x 1x     3x 3x     3x 3x     3x 3x 3x     1x       1x               1x 1x 1x 1x 1x     1x 3x 3x   1x 1x     1x       1x               2x 2x 2x 2x 2x     2x 5x 5x   2x 2x     1x       2x         1x         1x               2x 2x 2x 2x 2x 2x     2x 6x 6x   2x 2x 2x     1x     1x 1x 1x       2x               39x     7x     6x 6x 6x 3x 3x 2x       4x 4x 4x       4x     7x                                                
"use strict";
 
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
 
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
 
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
 
function _arrayWithoutHoles(arr) { Eif (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
 
var _require = require("./util"),
    delay = _require.delay;
 
function call(_ref, c) {
  var fn = _ref.fn,
      args = _ref.args;
  return c.call.apply(c, [c.context, c.interpreters, fn.eadFn || fn].concat(_toConsumableArray(args)));
}
 
function callFn(_ref2, c) {
  var fn = _ref2.fn,
      bindThis = _ref2.bindThis,
      args = _ref2.args;
 
  if (fn.eadFn) {
    return call({
      fn: fn,
      args: args
    }, c);
  } else {
    return fn.call.apply(fn, [bindThis].concat(_toConsumableArray(args)));
  }
}
 
function callCallback(_ref3, c) {
  var fn = _ref3.fn,
      bindThis = _ref3.bindThis,
      args = _ref3.args;
  return new Promise(function (resolve, reject) {
    try {
      fn.apply(bindThis, args.concat(function (err) {
        for (var _len = arguments.length, results = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
          results[_key - 1] = arguments[_key];
        }
 
        if (err) return reject(err);else resolve.apply(void 0, results);
      }));
    } catch (e) {
      reject(e);
    }
  });
}
 
function echo(_ref4) {
  var message = _ref4.message;
  return message;
}
 
function noop() {}
 
function globalVariable(_ref5) {
  var name = _ref5.name;
  // istanbul ignore next
  var g = typeof window === "undefined" ? global : window;
  return g[name];
}
 
function log(_ref6) {
  var _console;
 
  var args = _ref6.args;
 
  (_console = console).log.apply(_console, _toConsumableArray(args));
}
 
function logError(_ref7) {
  var _console2;
 
  var args = _ref7.args;
 
  (_console2 = console).error.apply(_console2, _toConsumableArray(args));
}
 
function getSubStack(context, resetStack) {
  return resetStack ? Object.assign({}, context, {
    stack: []
  }) : context;
}
 
function setImmediateInterpreter(_ref8, _ref9) {
  var cmd = _ref8.cmd,
      resetStack = _ref8.resetStack;
  var call = _ref9.call,
      context = _ref9.context,
      interpreters = _ref9.interpreters;
  delay(function () {
    var subcontext = getSubStack(context, resetStack);
    call(subcontext, interpreters,
    /*#__PURE__*/
    regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return cmd;
 
            case 2:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    })).catch(function (e) {
      return e;
    });
  });
}
 
function setTimeoutInterpreter(_ref10, _ref11) {
  var cmd = _ref10.cmd,
      time = _ref10.time,
      resetStack = _ref10.resetStack;
  var call = _ref11.call,
      context = _ref11.context,
      interpreters = _ref11.interpreters;
  return setTimeout(function () {
    var subcontext = getSubStack(context, resetStack);
    call(subcontext, interpreters,
    /*#__PURE__*/
    regeneratorRuntime.mark(function _callee2() {
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return cmd;
 
            case 2:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, this);
    })).catch(function (e) {
      return e;
    });
  }, time);
}
 
function clearTimeoutInterpreter(_ref12) {
  var id = _ref12.id;
  return clearTimeout(id);
}
 
function setIntervalInterpreter(_ref13, _ref14) {
  var cmd = _ref13.cmd,
      time = _ref13.time,
      resetStack = _ref13.resetStack;
  var call = _ref14.call,
      context = _ref14.context,
      interpreters = _ref14.interpreters;
  return setInterval(function () {
    var subcontext = getSubStack(context, resetStack);
    call(subcontext, interpreters,
    /*#__PURE__*/
    regeneratorRuntime.mark(function _callee3() {
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return cmd;
 
            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3, this);
    })).catch(function (e) {
      return e;
    });
  }, time);
}
 
function clearIntervalInterpreter(_ref15) {
  var id = _ref15.id;
  return clearInterval(id);
}
 
function sleep(_ref16) {
  var time = _ref16.time;
  return new Promise(function (resolve) {
    return setTimeout(resolve, time);
  });
}
 
function series(_ref17, _ref18) {
  var cmdList = _ref17.cmdList;
  var call = _ref18.call,
      context = _ref18.context,
      interpreters = _ref18.interpreters;
  if (cmdList.length === 0) return [];
  return call(context, interpreters,
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee4() {
    var results, i, result;
    return regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            results = [];
            i = 0;
 
          case 2:
            if (!(i < cmdList.length)) {
              _context4.next = 10;
              break;
            }
 
            _context4.next = 5;
            return cmdList[i];
 
          case 5:
            result = _context4.sent;
            results.push(result);
 
          case 7:
            i++;
            _context4.next = 2;
            break;
 
          case 10:
            return _context4.abrupt("return", results);
 
          case 11:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4, this);
  }));
}
 
function parallel(_ref19, _ref20) {
  var cmdList = _ref19.cmdList;
  var call = _ref20.call,
      context = _ref20.context,
      interpreters = _ref20.interpreters;
  return call(context, interpreters,
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee5() {
    return regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.next = 2;
            return cmdList;
 
          case 2:
            return _context5.abrupt("return", _context5.sent);
 
          case 3:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5, this);
  }));
}
 
function envelope(_ref21, _ref22) {
  var cmd = _ref21.cmd;
  var call = _ref22.call,
      context = _ref22.context,
      interpreters = _ref22.interpreters;
  return call(context, interpreters,
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee6() {
    return regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            _context6.next = 2;
            return cmd;
 
          case 2:
            return _context6.abrupt("return", _context6.sent);
 
          case 3:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6, this);
  })).then(function (result) {
    return {
      success: true,
      result: result
    };
  }).catch(function (e) {
    return {
      success: false,
      result: e
    };
  });
}
 
function either(_ref23, _ref24) {
  var cmd = _ref23.cmd,
      defaultValue = _ref23.defaultValue;
  var call = _ref24.call,
      context = _ref24.context,
      interpreters = _ref24.interpreters;
  return call(context, interpreters,
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee7() {
    return regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            _context7.prev = 0;
            _context7.next = 3;
            return cmd;
 
          case 3:
            return _context7.abrupt("return", _context7.sent);
 
          case 6:
            _context7.prev = 6;
            _context7.t0 = _context7["catch"](0);
            return _context7.abrupt("return", defaultValue);
 
          case 9:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7, this, [[0, 6]]);
  }));
}
 
function now() {
  return Date.now();
}
 
var state = {};
 
function getState(_ref25) {
  var path = _ref25.path,
      defaultValue = _ref25.defaultValue;
  if (!path) return state;
  var value = state[path];
  if (value === undefined) return defaultValue;
  return value;
}
 
function setState(_ref26) {
  var path = _ref26.path,
      value = _ref26.value;
  state[path] = value;
}
 
function clearState() {
  state = {};
}
 
module.exports = {
  call: call,
  callFn: callFn,
  callCallback: callCallback,
  echo: echo,
  noop: noop,
  globalVariable: globalVariable,
  log: log,
  logError: logError,
  setImmediate: setImmediateInterpreter,
  setTimeout: setTimeoutInterpreter,
  clearTimeout: clearTimeoutInterpreter,
  setInterval: setIntervalInterpreter,
  clearInterval: clearIntervalInterpreter,
  sleep: sleep,
  series: series,
  parallel: parallel,
  envelope: envelope,
  either: either,
  now: now,
  getState: getState,
  setState: setState,
  clearState: clearState
};
//# sourceMappingURL=interpreters.js.map