# all files / TypeScript.NET/dist/commonjs/System/Disposable/ ObjectPool.js

26.15% Statements 34/130
19.67% Branches 12/61
21.74% Functions 5/23
19.77% Lines 17/86

    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

    12×

    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
     * Based upon ObjectPool from Parallel Extension Extras and other ObjectPool implementations.
     * Uses .add(T) and .take():T
     */
    "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; }; }();

    var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    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; }

    var dispose_1 = require("./dispose");
    var DisposableBase_1 = require("./DisposableBase");
    var TaskHandler_1 = require("../Tasks/TaskHandler");
    var ArgumentOutOfRangeException_1 = require("../Exceptions/ArgumentOutOfRangeException");
    var OBJECT_POOL = "ObjectPool",
        _MAX_SIZE = "_maxSize",
        ABSOLUTE_MAX_SIZE = 65536,
        MUST_BE_GT1 = "Must be at valid number least 1.",
        MUST_BE_LTM = "Must be less than or equal to " + ABSOLUTE_MAX_SIZE + ".";

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

        function ObjectPool(_maxSize, _generator) {
            _classCallCheck(this, ObjectPool);

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

            _this._maxSize = _maxSize;
            _this._generator = _generator;
            _this.autoClearTimeout = 5000;
            if (isNaN(_maxSize) || _maxSize < 1) throw new ArgumentOutOfRangeException_1.default(_MAX_SIZE, _maxSize, MUST_BE_GT1);
            if (_maxSize > ABSOLUTE_MAX_SIZE) throw new ArgumentOutOfRangeException_1.default(_MAX_SIZE, _maxSize, MUST_BE_LTM);
            _this._localAbsMaxSize = Math.min(_maxSize * 2, ABSOLUTE_MAX_SIZE);
            var _ = _this;
            _._disposableObjectName = OBJECT_POOL;
            _._pool = [];
            _._trimmer = new TaskHandler_1.default(function () {
                return _._trim();
            });
            var clear = function clear() {
                return _._clear();
            };
            _._flusher = new TaskHandler_1.default(clear);
            _._autoFlusher = new TaskHandler_1.default(clear);
            return _this;
        }

        _createClass(ObjectPool, [{
            key: "_trim",
            value: function _trim() {
                var pool = this._pool;
                while (pool.length > this._maxSize) {
                    dispose_1.default.withoutException(pool.pop());
                }
            }
        }, {
            key: "trim",
            value: function trim(defer) {
                this.throwIfDisposed();
                this._trimmer.execute(defer);
            }
        }, {
            key: "_clear",
            value: function _clear() {
                var _ = this,
                    p = _._pool;
                _._trimmer.cancel();
                _._flusher.cancel();
                _._autoFlusher.cancel();
                dispose_1.default.these(p, true);
                p.length = 0;
            }
        }, {
            key: "clear",
            value: function clear(defer) {
                this.throwIfDisposed();
                this._flusher.execute(defer);
            }
        }, {
            key: "toArrayAndClear",
            value: function toArrayAndClear() {
                var _ = this;
                _.throwIfDisposed();
                _._trimmer.cancel();
                _._flusher.cancel();
                var p = _._pool;
                _._pool = [];
                return p;
            }
        }, {
            key: "dump",
            value: function dump() {
                return this.toArrayAndClear();
            }
        }, {
            key: "_onDispose",
            value: function _onDispose() {
                _get(Object.getPrototypeOf(ObjectPool.prototype), "_onDispose", this).call(this);
                var _ = this;
                _._generator = null;
                dispose_1.default(_._trimmer, _._flusher, _._autoFlusher);
                _._trimmer = null;
                _._flusher = null;
                _._autoFlusher = null;
                _._pool.length = 0;
                _._pool = null;
            }
        }, {
            key: "extendAutoClear",
            value: function extendAutoClear() {
                var _ = this;
                _.throwIfDisposed();
                var t = _.autoClearTimeout;
                if (isFinite(t) && !_._autoFlusher.isScheduled) _._autoFlusher.execute(t);
            }
        }, {
            key: "add",
            value: function add(o) {
                var _ = this;
                _.throwIfDisposed();
                if (_._pool.length >= _._localAbsMaxSize) {
                    dispose_1.default(o);
                } else {
                    _._pool.push(o);
                    var m = _._maxSize;
                    if (m < ABSOLUTE_MAX_SIZE && _._pool.length > m) _._trimmer.execute(500);
                }
                _.extendAutoClear();
            }
        }, {
            key: "take",
            value: function take() {
                var _ = this;
                _.throwIfDisposed();
                var e = _._pool.pop() || _._generator(),
                    len = _._pool.length;
                if (_._pool.length <= _._maxSize) _._trimmer.cancel();
                if (len) _.extendAutoClear();
                return e;
            }
        }, {
            key: "maxSize",
            get: function get() {
                return this._maxSize;
            }
        }, {
            key: "count",
            get: function get() {
                var p = this._pool;
                return p ? p.length : 0;
            }
        }]);

        return ObjectPool;
    }(DisposableBase_1.default);

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