# Code coverage report for modules/SerialDisposable.js

## Statements: 97.44% (38 / 39) Branches: 91.67% (22 / 24) Functions: 100% (7 / 7) Lines: 100% (36 / 36) Ignored: none

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

    1

    7

    1

    1

    1

    1
    1
    7

    7
    7

    1
    5

    1
    18

    18
    4

    14
    14

    14
    11
    11

    14
    4

    14
    2

    1
    4
    1

    3
    3
    3

    3
    2

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

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

        this.isDisposed = false;
        this.current = null;
      }

      /**
       * Gets the underlying disposable.
       * @return The underlying disposable.
       */

      SerialDisposable.prototype.getDisposable = function getDisposable() {
        return this.current;
      };

      /**
       * Sets the underlying disposable.
       * @param {Disposable} value The new underlying disposable.
       */

      SerialDisposable.prototype.setDisposable = function setDisposable() {
        var value = arguments[0] === undefined ? null : arguments[0];

        if (value != null && !_isDisposable2['default'](value)) {
          throw new Error('Expected either an empty value or a valid disposable');
        }

        var isDisposed = this.isDisposed;
        var previous = undefined;

        if (!isDisposed) {
          previous = this.current;
          this.current = value;
        }

        if (previous) {
          previous.dispose();
        }

        if (isDisposed && value) {
          value.dispose();
        }
      };

      /**
       * Disposes the underlying disposable as well as all future replacements.
       */

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

        this.isDisposed = true;
        var previous = this.current;
        this.current = null;

        if (previous) {
          previous.dispose();
        }
      };

      return SerialDisposable;
    })();

    exports['default'] = SerialDisposable;
    module.exports = exports['default'];
    //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9kYW4vRG9jdW1lbnRzL1Byb2plY3RzL2Rpc3Bvc2FibGVzL21vZHVsZXMvU2VyaWFsRGlzcG9zYWJsZS5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs0QkFBeUIsZ0JBQWdCOzs7O0lBRXBCLGdCQUFnQjtBQUN4QixXQURRLGdCQUFnQixHQUNyQjswQkFESyxnQkFBZ0I7O0FBRWpDLFFBQUksQ0FBQyxVQUFVLEdBQUcsS0FBSyxDQUFDO0FBQ3hCLFFBQUksQ0FBQyxPQUFPLEdBQUcsSUFBSSxDQUFDO0dBQ3JCOzs7Ozs7O0FBSmtCLGtCQUFnQixXQVVuQyxhQUFhLEdBQUEseUJBQUc7QUFDZCxXQUFPLElBQUksQ0FBQyxPQUFPLENBQUM7R0FDckI7Ozs7Ozs7QUFaa0Isa0JBQWdCLFdBa0JuQyxhQUFhLEdBQUEseUJBQWU7UUFBZCxLQUFLLGdDQUFHLElBQUk7O0FBQ3hCLFFBQUksS0FBSyxJQUFJLElBQUksSUFBSSxDQUFDLDBCQUFhLEtBQUssQ0FBQyxFQUFFO0FBQ3pDLFlBQU0sSUFBSSxLQUFLLENBQUMsc0RBQXNELENBQUMsQ0FBQztLQUN6RTs7QUFFRCxRQUFNLFVBQVUsR0FBRyxJQUFJLENBQUMsVUFBVSxDQUFDO0FBQ25DLFFBQUksUUFBUSxZQUFBLENBQUM7O0FBRWIsUUFBSSxDQUFDLFVBQVUsRUFBRTtBQUNmLGNBQVEsR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