# all files / TypeScript.NET/dist/commonjs/System/Collections/Enumeration/ EnumeratorBase.js

40% Statements 42/105
29.17% Branches 14/48
30.43% Functions 7/23
33.33% Lines 25/75

    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

    10×

    1×

    1×

    1×

    1×
    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×
    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    1×
    1×

     /*!
     * @author electricessence / https://github.com/electricessence/
     * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
     */
    'use strict';

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

    function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

    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) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var DisposableBase_1 = require("../../Disposable/DisposableBase");
    var ObjectPool_1 = require("../../Disposable/ObjectPool");
    var VOID0 = void 0;
    var yielderPool;
    function yielder(recycle) {
        if (!yielderPool) yielderPool = new ObjectPool_1.default(40, function () {
            return new Yielder();
        });
        if (!recycle) return yielderPool.take();
        recycle.yieldBreak();
        yielderPool.add(recycle);
    }

    var Yielder = function () {
        function Yielder() {
            _classCallCheck(this, Yielder);

            this._current = VOID0;
        }

        _createClass(Yielder, [{
            key: "yieldReturn",
            value: function yieldReturn(value) {
                this._current = value;
                return true;
            }
        }, {
            key: "yieldBreak",
            value: function yieldBreak() {
                this._current = VOID0;
                return false;
            }
        }, {
            key: "dispose",
            value: function dispose() {
                this.yieldBreak();
            }
        }, {
            key: "current",
            get: function get() {
                return this._current;
            }
        }]);

        return Yielder;
    }();

    var EnumeratorState;
    (function (EnumeratorState) {
        EnumeratorState[EnumeratorState["Before"] = 0] = "Before";
        EnumeratorState[EnumeratorState["Running"] = 1] = "Running";
        EnumeratorState[EnumeratorState["After"] = 2] = "After";
    })(EnumeratorState || (EnumeratorState = {}));

    var EnumeratorBase = function (_DisposableBase_1$def) {
        _inherits(EnumeratorBase, _DisposableBase_1$def);

        function EnumeratorBase(initializer, tryGetNext, disposer) {
            _classCallCheck(this, EnumeratorBase);

            var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(EnumeratorBase).call(this));

            _this.initializer = initializer;
            _this.tryGetNext = tryGetNext;
            _this.disposer = disposer;
            _this.reset();
            return _this;
        }

        _createClass(EnumeratorBase, [{
            key: "reset",
            value: function reset() {
                var _ = this;
                _.throwIfDisposed();
                var y = _._yielder;
                if (y) y.yieldBreak();else _._yielder = yielder();
                _._state = EnumeratorState.Before;
            }
        }, {
            key: "moveNext",
            value: function moveNext() {
                var _ = this;
                try {
                    switch (_._state) {
                        case EnumeratorState.Before:
                            _._state = EnumeratorState.Running;
                            var initializer = _.initializer;
                            if (initializer) initializer();
                        case EnumeratorState.Running:
                            if (_.tryGetNext(_._yielder)) {
                                return true;
                            } else {
                                this.dispose();
                                return false;
                            }
                        case EnumeratorState.After:
                            return false;
                    }
                } catch (e) {
                    this.dispose();
                    throw e;
                }
            }
        }, {
            key: "nextValue",
            value: function nextValue() {
                return this.moveNext() ? this._yielder.current : VOID0;
            }
        }, {
            key: "next",
            value: function next() {
                return this.moveNext() ? {
                    value: this._yielder.current,
                    done: false
                } : {
                    value: VOID0,
                    done: true
                };
            }
        }, {
            key: "_onDispose",
            value: function _onDispose() {
                var _ = this,
                    disposer = _.disposer;
                _.initializer = null;
                _.disposer = null;
                var y = _._yielder;
                _._yielder = null;
                yielder(y);
                try {
                    if (disposer) disposer();
                } finally {
                    this._state = EnumeratorState.After;
                }
            }
        }, {
            key: "current",
            get: function get() {
                return this._yielder.current;
            }
        }]);

        return EnumeratorBase;
    }(DisposableBase_1.default);

    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = EnumeratorBase;
    //# sourceMappingURL=EnumeratorBase.js.map