All files / build index.js

66.26% Statements 108/163
55.84% Branches 43/77
28.57% Functions 2/7
66.26% Lines 108/163
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    1x   1x 1x 1x                       1x 1x 1x 1x   1x                 1x                                             4x 4x 4x 4x       4x 35x 62x   4x     4x 4x 4x 4x 4x 4x 4x     9x 3x 3x     6x 6x       6x 6x     5x   5x   5x     5x 5x 5x     3x 3x                 3x 3x   3x         3x   3x 3x 3x           3x     3x         6x     6x   6x 6x     6x   6x 4x 4x     2x 2x     4x 4x 4x 4x     4x 4x 4x     4x   4x 3x 3x       1x 1x 1x 1x 1x     2x         2x   2x 1x 1x     1x 1x     2x         2x 2x     1x 1x 1x           1x 1x 1x                                                                               3x         3x 3x     3x 3x 3x           3x               2x                           1x                                                         1x                   1x 1x 1x 1x 1x 1x 1x    
'use strict';
 
Object.defineProperty(exports, '__esModule', { value: true });
 
var reduxSaga = require('redux-saga');
var reduxSaga_effects = require('redux-saga/effects');
var ramda = require('ramda');
 
/**
 * OTP
 *
 * Docs {@link http://erlang.org/doc/design_principles/des_princ.html Here}.
 * @namespace OTP
 *
 */
 
/* eslint-disable no-continue */
 
var ONE_FOR_ONE = 'ONE_FOR_ONE';
var ONE_FOR_MANY = 'ONE_FOR_MANY';
var PERMANENT = 'PERMANENT';
var TEMPORARY = 'TEMPORARY';
 
var debug = require('debug');
 
/**
 * Supervisor OTP behavior
 *
 * Docs {@link http://erlang.org/documentation/doc-4.9.1/doc/design_principles/sup_princ.html Here}.
 * @memberof OTP
 *
 */
var supervisor = {
  debug: debug('otp/supervisor'),
 
  start_link: regeneratorRuntime.mark(function start_link(task, id) {
    return regeneratorRuntime.wrap(function start_link$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            supervisor.debug('start_link', { task: task, id: id });
            _context.next = 3;
            return reduxSaga_effects.call(task);
 
          case 3:
            return _context.abrupt('return', id);
 
          case 4:
          case 'end':
            return _context.stop();
        }
      }
    }, start_link, this);
  }),
  init: regeneratorRuntime.mark(function init(_ref) {
    var _ref$supFlags = _ref.supFlags,
        supFlags = _ref$supFlags === undefined ? {} : _ref$supFlags,
        _ref$childSpecs = _ref.childSpecs,
        childSpecs = _ref$childSpecs === undefined ? [] : _ref$childSpecs;
 
    var _supFlags$strategy, strategy, _supFlags$maxR, maxR, _supFlags$maxT, maxT, children, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _child, id, start, _child$restart, restart, task, tasks, waitedLongEnough, terminatedId, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, child;
 
    return regeneratorRuntime.wrap(function init$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _supFlags$strategy = supFlags.strategy, strategy = _supFlags$strategy === undefined ? ONE_FOR_ONE : _supFlags$strategy, _supFlags$maxR = supFlags.maxR, maxR = _supFlags$maxR === undefined ? 1 : _supFlags$maxR, _supFlags$maxT = supFlags.maxT, maxT = _supFlags$maxT === undefined ? 1000 : _supFlags$maxT;
 
 
            supervisor.debug('init', { supFlags: supFlags, childSpecs: childSpecs });
            children = {};
            _iteratorNormalCompletion = true;
            _didIteratorError = false;
            _iteratorError = undefined;
            _context2.prev = 6;
            _iterator = childSpecs[Symbol.iterator]();
 
          case 8:
            if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
              _context2.next = 19;
              break;
            }
 
            _child = _step.value;
            id = _child.id, start = _child.start, _child$restart = _child.restart, restart = _child$restart === undefined ? PERMANENT : _child$restart;
 
            // fork worker
 
            _context2.next = 13;
            return reduxSaga_effects.fork(start);
 
          case 13:
            task = _context2.sent;
 
            supervisor.debug('init: forked', id);
 
            children[id] = { id: id, task: task, start: start, restart: restart, terminated: false };
 
          case 16:
            _iteratorNormalCompletion = true;
            _context2.next = 8;
            break;
 
          case 19:
            _context2.next = 25;
            break;
 
          case 21:
            _context2.prev = 21;
            _context2.t0 = _context2['catch'](6);
            _didIteratorError = true;
            _iteratorError = _context2.t0;
 
          case 25:
            _context2.prev = 25;
            _context2.prev = 26;
 
            Iif (!_iteratorNormalCompletion && _iterator.return) {
              _iterator.return();
            }
 
          case 28:
            _context2.prev = 28;
 
            Eif (!_didIteratorError) {
              _context2.next = 31;
              break;
            }
 
            throw _iteratorError;
 
          case 31:
            return _context2.finish(28);
 
          case 32:
            return _context2.finish(25);
 
          case 33:
            
 
            tasks = ramda.compose(ramda.map(reduxSaga_effects.join), ramda.map(ramda.prop('task')), ramda.reject(ramda.prop('terminated')))(children);
 
 
            supervisor.debug('init: loop', { tasks: tasks });
 
            _context2.next = 38;
            return reduxSaga_effects.race(ramda.merge(tasks, { waitedLongEnough: reduxSaga_effects.call(reduxSaga.delay, maxT / maxR, true) }));
 
          case 38:
            waitedLongEnough = _context2.sent;
 
            if (ramda.has('waitedLongEnough', waitedLongEnough)) {
              _context2.next = 42;
              break;
            }
 
            supervisor.debug('init: not waited long enough', { waitedLongEnough: waitedLongEnough });
            throw new Error('Restart frequency limit reached');
 
          case 42:
            _context2.t1 = ramda.head;
            _context2.t2 = ramda.values;
            _context2.next = 46;
            return reduxSaga_effects.race(tasks);
 
          case 46:
            _context2.t3 = _context2.sent;
            _context2.t4 = (0, _context2.t2)(_context2.t3);
            terminatedId = (0, _context2.t1)(_context2.t4);
 
 
            supervisor.debug('init: %s terminated', terminatedId);
 
            if (!(strategy === ONE_FOR_MANY)) {
              _context2.next = 88;
              break;
            }
 
            // restart all workers
            _iteratorNormalCompletion2 = true;
            _didIteratorError2 = false;
            _iteratorError2 = undefined;
            _context2.prev = 54;
            _iterator2 = ramda.values(children)[Symbol.iterator]();
 
          case 56:
            Iif (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
              _context2.next = 71;
              break;
            }
 
            child = _step2.value;
 
            if (!(child.id !== terminatedId)) {
              _context2.next = 61;
              break;
            }
 
            _context2.next = 61;
            return reduxSaga_effects.cancel(child.task);
 
          case 61:
            Iif (!(child.restart === PERMANENT)) {
              _context2.next = 67;
              break;
            }
 
            _context2.next = 64;
            return reduxSaga_effects.fork(child.start);
 
          case 64:
            children[child.id].task = _context2.sent;
            _context2.next = 68;
            break;
 
          case 67:
            children[child.id].terminated = true;
 
          case 68:
            _iteratorNormalCompletion2 = true;
            _context2.next = 56;
            break;
 
          case 71:
            _context2.next = 77;
            break;
 
          case 73:
            _context2.prev = 73;
            _context2.t5 = _context2['catch'](54);
            _didIteratorError2 = true;
            _iteratorError2 = _context2.t5;
 
          case 77:
            _context2.prev = 77;
            _context2.prev = 78;
 
            if (!_iteratorNormalCompletion2 && _iterator2.return) {
              _iterator2.return();
            }
 
          case 80:
            _context2.prev = 80;
 
            if (!_didIteratorError2) {
              _context2.next = 83;
              break;
            }
 
            throw _iteratorError2;
 
          case 83:
            return _context2.finish(80);
 
          case 84:
            return _context2.finish(77);
 
          case 85:
            return _context2.abrupt('continue', 33);
 
          case 88:
            Iif (!(children[terminatedId].restart === PERMANENT)) {
              _context2.next = 94;
              break;
            }
 
            _context2.next = 91;
            return reduxSaga_effects.fork(children[terminatedId].start);
 
          case 91:
            children[terminatedId].task = _context2.sent;
            _context2.next = 95;
            break;
 
          case 94:
            children[terminatedId].terminated = true;
 
          case 95:
            return _context2.abrupt('continue', 33);
 
          case 96:
            _context2.next = 33;
            break;
 
          case 98:
          case 'end':
            return _context2.stop();
        }
      }
    }, init, this, [[6, 21, 25, 33], [26,, 28, 32], [54, 73, 77, 85], [78,, 80, 84]]);
  })
};
 
/**
 * Testing utils
 *
 * @namespace TestUtils
 *
 */
 
var resumeIteratorAndCheckOutput = function resumeIteratorAndCheckOutput(t, iterator) {
  return function (into, outof) {
    var value = void 0;
    if (!iterator) {
      throw 'iterator is undefined';
    }
 
    try {
      value = iterator.next(into).value;
      t.deepEqual(value, outof);
    } catch (e) {
      t.deepEqual(e.message, outof.error.message);
    }
  };
};
 
/**
 * checks generator scenario
 *
 * this is equivalent to:
 * asserting that two iterators are equal, when given the same input
 *
 * @param {NodeTap} t node-tap instance
 * @param {Iterator} iter
 * @param {[]} scenario Scenario data
 * @returns {void}
 *
 * @memberof TestUtils
 */
var checkGeneratorScenario = function checkGeneratorScenario(t, iter, scenario) {
  var next = resumeIteratorAndCheckOutput(t, iter);
 
  for (var i = 0; i < scenario.length - 1; i += 2) {
    var input = scenario[i];
    var output = scenario[i + 1];
    next(input, output);
  }
};
 
exports.ONE_FOR_ONE = ONE_FOR_ONE;
exports.ONE_FOR_MANY = ONE_FOR_MANY;
exports.PERMANENT = PERMANENT;
exports.TEMPORARY = TEMPORARY;
exports.supervisor = supervisor;
exports.resumeIteratorAndCheckOutput = resumeIteratorAndCheckOutput;
exports.checkGeneratorScenario = checkGeneratorScenario;
//# sourceMappingURL=index.js.map