This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compositedisposable.js
88 lines (80 loc) · 2.3 KB
/
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
'use strict';
/**
* Represents a group of disposable resources that are disposed together.
* @constructor
*/
function CompositeDisposable () {
var args = [], i, len;
if (Array.isArray(arguments[0])) {
args = arguments[0];
len = args.length;
} else {
len = arguments.length;
args = new Array(len);
for(i = 0; i < len; i++) { args[i] = arguments[i]; }
}
this._disposables = args;
this.isDisposed = false;
this.length = args.length;
}
/**
* Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
* @param {Any} item Disposable to add.
*/
CompositeDisposable.prototype.add = function (item) {
if (this.isDisposed) {
item.dispose();
} else {
this._disposables.push(item);
this.length++;
}
};
/**
* Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
* @param {Any} item Disposable to remove.
* @returns {Boolean} true if found; false otherwise.
*/
CompositeDisposable.prototype.remove = function (item) {
var shouldDispose = false;
if (!this.isDisposed) {
var idx = this._disposables.indexOf(item);
if (idx !== -1) {
shouldDispose = true;
this._disposables.splice(idx, 1);
this.length--;
item.dispose();
}
}
return shouldDispose;
};
/**
* Disposes all disposables in the group and removes them from the group but
* does not dispose the CompositeDisposable.
*/
CompositeDisposable.prototype.clear = function () {
if (!this.isDisposed) {
var len = this._disposables.length, currentDisposables = new Array(len);
for(var i = 0; i < len; i++) { currentDisposables[i] = this._disposables[i]; }
this._disposables = [];
this.length = 0;
for (i = 0; i < len; i++) {
currentDisposables[i].dispose();
}
}
};
/**
* Disposes all disposables in the group and removes them from the group.
*/
CompositeDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
var len = this._disposables.length, currentDisposables = new Array(len);
for(var i = 0; i < len; i++) { currentDisposables[i] = this._disposables[i]; }
this._disposables = [];
this.length = 0;
for (i = 0; i < len; i++) {
currentDisposables[i].dispose();
}
}
};
module.exports = CompositeDisposable;