# Code coverage report for modules/CompositeDisposable.js

## Statements: 97.92% (47 / 48) Branches: 90% (18 / 20) Functions: 100% (7 / 7) Lines: 100% (45 / 45) Ignored: none

All files » modules/ » CompositeDisposable.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

    1

    15

    1

    1

    1

    1
    1
    15
    20

    15

    15
    1

    15
    21
    2

    13
    13

    1
    11
    3

    8

    1
    10
    3

    7
    7
    2

    5
    5
    5

    1
    10
    1

    9
    9
    9
    14

    9
    9
    9

    9
    14

    1

    1
    1
     'use strict';

    var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };

    var _classCallCheck = function (instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };

    exports.__esModule = true;

    var _isDisposable = require('./isDisposable');

    var _isDisposable2 = _interopRequireWildcard(_isDisposable);

    /**
     * Represents a group of disposable resources that are disposed together.
     */

    var CompositeDisposable = (function () {
      function CompositeDisposable() {
        for (var _len = arguments.length, disposables = Array(_len), _key = 0; _key < _len; _key++) {
          disposables[_key] = arguments[_key];
        }

        _classCallCheck(this, CompositeDisposable);

        if (Array.isArray(disposables[0]) && disposables.length === 1) {
          disposables = disposables[0];
        }

        for (var i = 0; i < disposables.length; i++) {
          if (!_isDisposable2['default'](disposables[i])) {
            throw new Error('Expected a disposable');
          }
        }

        this.disposables = disposables;
        this.isDisposed = false;
      }

      /**
       * Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
       * @param {Disposable} item Disposable to add.
       */

      CompositeDisposable.prototype.add = function add(item) {
        if (this.isDisposed) {
          item.dispose();
        } else {
          this.disposables.push(item);
        }
      };

      /**
       * Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
       * @param {Disposable} item Disposable to remove.
       * @returns {Boolean} true if found; false otherwise.
       */

      CompositeDisposable.prototype.remove = function remove(item) {
        if (this.isDisposed) {
          return false;
        }

        var index = this.disposables.indexOf(item);
        if (index === -1) {
          return false;
        }

        this.disposables.splice(index, 1);
        item.dispose();
        return true;
      };

      /**
       * Disposes all disposables in the group and removes them from the group.
       */

      CompositeDisposable.prototype.dispose = function dispose() {
        if (this.isDisposed) {
          return;
        }

        var len = this.disposables.length;
        var currentDisposables = new Array(len);
        for (var i = 0; i < len; i++) {
          currentDisposables[i] = this.disposables[i];
        }

        this.isDisposed = true;
        this.disposables = [];
        this.length = 0;

        for (var i = 0; i < len; i++) {
          currentDisposables[i].dispose();
        }
      };

      return CompositeDisposable;
    })();

    exports['default'] = CompositeDisposable;
    module.exports = exports['default'];
    //# sourceMappingURL=data:application/json;base64,[encoded data, 5016 characters]