# all files / TypeScript.NET/source/System/Disposable/ ObjectPool.js

62.16% Statements 69/111
68.29% Branches 28/41
45% Functions 9/20
64.42% Lines 67/104

    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×
    1×

    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    5×
    5×
    5×
    5×
    5×

    5×

    5×
    5×
    5×
    5×
    5×
    5×
    5×
    5×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
    6889×
    6889×
    6889×
    6889×
    4×

    1×
    5858×
    5858×
    5858×
    4687×

    1171×
    1171×
    1171×
    285×

    5858×

    1×
    7950×
    7950×
    7950×
    7950×
    7708×
    7950×
    1031×
    7950×

    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
     */
    var __extends = (this && this.__extends) || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
    (function (factory) {
        Eif (typeof module === 'object' && typeof module.exports === 'object') {
            var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
        }
        else if (typeof define === 'function' && define.amd) {
            define(["require", "exports", "./dispose", "./DisposableBase", "../Tasks/TaskHandler", "../Exceptions/ArgumentOutOfRangeException"], factory);
        }
    })(function (require, exports) {
        "use strict";
        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 (_super) {
            __extends(ObjectPool, _super);
            function ObjectPool(_maxSize, _generator) {
                _super.call(this);
                this._maxSize = _maxSize;
                this._generator = _generator;
                this.autoClearTimeout = 5000;
                Iif (isNaN(_maxSize) || _maxSize < 1)
                    throw new ArgumentOutOfRangeException_1.ArgumentOutOfRangeException(_MAX_SIZE, _maxSize, MUST_BE_GT1);
                Iif (_maxSize > ABSOLUTE_MAX_SIZE)
                    throw new ArgumentOutOfRangeException_1.ArgumentOutOfRangeException(_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.TaskHandler(function () { return _._trim(); });
                var clear = function () { return _._clear(); };
                _._flusher = new TaskHandler_1.TaskHandler(clear);
                _._autoFlusher = new TaskHandler_1.TaskHandler(clear);
            }
            Object.defineProperty(ObjectPool.prototype, "maxSize", {
                get: function () {
                    return this._maxSize;
                },
                enumerable: true,
                configurable: true
            });
            Object.defineProperty(ObjectPool.prototype, "count", {
                get: function () {
                    var p = this._pool;
                    return p ? p.length : 0;
                },
                enumerable: true,
                configurable: true
            });
            ObjectPool.prototype._trim = function () {
                var pool = this._pool;
                while (pool.length > this._maxSize) {
                    dispose_1.dispose.withoutException(pool.pop());
                }
            };
            ObjectPool.prototype.trim = function (defer) {
                this.throwIfDisposed();
                this._trimmer.execute(defer);
            };
            ObjectPool.prototype._clear = function () {
                var _ = this, p = _._pool;
                _._trimmer.cancel();
                _._flusher.cancel();
                _._autoFlusher.cancel();
                dispose_1.dispose.these(p, true);
                p.length = 0;
            };
            ObjectPool.prototype.clear = function (defer) {
                this.throwIfDisposed();
                this._flusher.execute(defer);
            };
            ObjectPool.prototype.toArrayAndClear = function () {
                var _ = this;
                _.throwIfDisposed();
                _._trimmer.cancel();
                _._flusher.cancel();
                var p = _._pool;
                _._pool = [];
                return p;
            };
            ObjectPool.prototype.dump = function () {
                return this.toArrayAndClear();
            };
            ObjectPool.prototype._onDispose = function () {
                _super.prototype._onDispose.call(this);
                var _ = this;
                _._generator = null;
                dispose_1.dispose(_._trimmer, _._flusher, _._autoFlusher);
                _._trimmer = null;
                _._flusher = null;
                _._autoFlusher = null;
                _._pool.length = 0;
                _._pool = null;
            };
            ObjectPool.prototype.extendAutoClear = function () {
                var _ = this;
                _.throwIfDisposed();
                var t = _.autoClearTimeout;
                if (isFinite(t) && !_._autoFlusher.isScheduled)
                    _._autoFlusher.execute(t);
            };
            ObjectPool.prototype.add = function (o) {
                var _ = this;
                _.throwIfDisposed();
                if (_._pool.length >= _._localAbsMaxSize) {
                    dispose_1.dispose(o);
                }
                else {
                    _._pool.push(o);
                    var m = _._maxSize;
                    if (m < ABSOLUTE_MAX_SIZE && _._pool.length > m)
                        _._trimmer.execute(500);
                }
                _.extendAutoClear();
            };
            ObjectPool.prototype.take = function () {
                var _ = this;
                _.throwIfDisposed();
                var e = _._pool.pop() || _._generator(), len = _._pool.length;
                if (_._pool.length <= _._maxSize)
                    _._trimmer.cancel();
                if (len)
                    _.extendAutoClear();
                return e;
            };
            return ObjectPool;
        }(DisposableBase_1.DisposableBase));
        exports.ObjectPool = ObjectPool;
        Object.defineProperty(exports, "__esModule", { value: true });
        exports.default = ObjectPool;
    });
    //# sourceMappingURL=ObjectPool.js.map