Code coverage report for RxJS/dist/cjs/schedulers/VirtualTimeScheduler.js

Statements: 94.95% (94 / 99)      Branches: 77.5% (31 / 40)      Functions: 100% (15 / 15)      Lines: 97.78% (88 / 90)      Ignored: none     

All files » RxJS/dist/cjs/schedulers/ » VirtualTimeScheduler.js
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    1   1   1   6043   1   1   1 1 817   817 817 817 817 817 817 817     1 2162     1 821 821 821 4940 4940 4940 4934   6     817 817     1 5259 5259 5259 5259 5259 5259 34521 1536 1 1535 165   1370   32985 9781   23204         1 5183   5183 5183     1     1   1   1 1   1 5226   5226 5226 5226 5226 5226     1 5259   5259     5259 5259 5259   5216       43 43   5259 5259 5259 5259     1 4934     4934     1 1187 1187 1187 1187 1187 1187 318   1187     1     1
'use strict';
 
exports.__esModule = true;
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
 
var _Subscription2 = require('../Subscription');
 
var _Subscription3 = _interopRequireDefault(_Subscription2);
 
var VirtualTimeScheduler = (function () {
    function VirtualTimeScheduler() {
        _classCallCheck(this, VirtualTimeScheduler);
 
        this.actions = [];
        this.active = false;
        this.scheduled = false;
        this.index = 0;
        this.sorted = false;
        this.frame = 0;
        this.maxFrames = 750;
    }
 
    VirtualTimeScheduler.prototype.now = function now() {
        return this.frame;
    };
 
    VirtualTimeScheduler.prototype.flush = function flush() {
        var actions = this.actions;
        var maxFrames = this.maxFrames;
        while (actions.length > 0) {
            var action = actions.shift();
            this.frame = action.delay;
            if (this.frame <= maxFrames) {
                action.execute();
            } else {
                break;
            }
        }
        actions.length = 0;
        this.frame = 0;
    };
 
    VirtualTimeScheduler.prototype.addAction = function addAction(action) {
        var findDelay = action.delay;
        var actions = this.actions;
        var len = actions.length;
        var vaction = action;
        actions.push(action);
        actions.sort(function (a, b) {
            if (a.delay === b.delay) {
                if (a.index === b.index) {
                    return 0;
                } else if (a.index > b.index) {
                    return 1;
                } else {
                    return -1;
                }
            } else if (a.delay > b.delay) {
                return 1;
            } else {
                return -1;
            }
        });
    };
 
    VirtualTimeScheduler.prototype.schedule = function schedule(work, delay, state) {
        Iif (delay === undefined) delay = 0;
 
        this.sorted = false;
        return new VirtualAction(this, work, this.index++).schedule(state, delay);
    };
 
    return VirtualTimeScheduler;
})();
 
exports['default'] = VirtualTimeScheduler;
 
VirtualTimeScheduler.frameTimeFactor = 10;
 
var VirtualAction = (function (_Subscription) {
    _inherits(VirtualAction, _Subscription);
 
    function VirtualAction(scheduler, work, index) {
        _classCallCheck(this, VirtualAction);
 
        _Subscription.call(this);
        this.scheduler = scheduler;
        this.work = work;
        this.index = index;
        this.calls = 0;
    }
 
    VirtualAction.prototype.schedule = function schedule(state) {
        var delay = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
 
        Iif (this.isUnsubscribed) {
            return this;
        }
        var scheduler = this.scheduler;
        var action = undefined;
        if (this.calls++ === 0) {
            // the action is not being rescheduled.
            action = this;
        } else {
            // the action is being rescheduled, and we can't mutate the one in the actions list
            // in the scheduler, so we'll create a new one.
            action = new VirtualAction(scheduler, this.work, scheduler.index += 1);
            this.add(action);
        }
        action.state = state;
        action.delay = scheduler.frame + delay;
        scheduler.addAction(action);
        return this;
    };
 
    VirtualAction.prototype.execute = function execute() {
        Iif (this.isUnsubscribed) {
            throw new Error('How did did we execute a canceled Action?');
        }
        this.work(this.state);
    };
 
    VirtualAction.prototype.unsubscribe = function unsubscribe() {
        var actions = this.scheduler.actions;
        var index = actions.indexOf(this);
        this.work = void 0;
        this.state = void 0;
        this.scheduler = void 0;
        if (index !== -1) {
            actions.splice(index, 1);
        }
        _Subscription.prototype.unsubscribe.call(this);
    };
 
    return VirtualAction;
})(_Subscription3['default']);
 
module.exports = exports['default'];