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 6
/
singleassignmentdisposable.js
46 lines (41 loc) · 1.37 KB
/
singleassignmentdisposable.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
'use strict';
/**
* @constructor
* Represents a disposable resource which only allows a single assignment of
* its underlying disposable resource. If an underlying disposable resource has
* already been set, future attempts to set the underlying disposable resource
* will throw an Error.
*/
function SingleAssignmentDisposable () {
this.isDisposed = false;
this._current = null;
}
/**
* Gets the underlying disposable. After disposal, the result of getting
* this method is null.
* @returns {Any} the underlying disposable.
*/
SingleAssignmentDisposable.prototype.getDisposable = function () {
return this._current;
};
/**
* Sets the underlying disposable. Throws an error if the
* SingleAssignmentDisposable has already been assigned to.
* @param {Any} value the underlying disposable to set.
*/
SingleAssignmentDisposable.prototype.setDisposable = function (value) {
if (this._current) { throw new Error('Disposable has already been assigned'); }
var shouldDispose = this.isDisposed;
!shouldDispose && (this._current = value);
shouldDispose && value && value.dispose();
};
/** Performs the task of cleaning up resources. */
SingleAssignmentDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
var old = this._current;
this._current = null;
old && old.dispose();
}
};
module.exports = SingleAssignmentDisposable;