@parcel/events 2.8.4-nightly.0 → 2.9.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/Disposable.js +1 -16
- package/lib/ValueEmitter.js +5 -20
- package/lib/errors.js +0 -2
- package/lib/index.js +0 -4
- package/package.json +2 -2
package/lib/Disposable.js
CHANGED
@@ -4,21 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
function _assert() {
|
9
8
|
const data = _interopRequireDefault(require("assert"));
|
10
|
-
|
11
9
|
_assert = function () {
|
12
10
|
return data;
|
13
11
|
};
|
14
|
-
|
15
12
|
return data;
|
16
13
|
}
|
17
|
-
|
18
14
|
var _errors = require("./errors");
|
19
|
-
|
20
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
-
|
22
16
|
/*
|
23
17
|
* A general-purpose disposable class. It can normalize disposable-like values
|
24
18
|
* (such as single functions or IDisposables), as well as hold multiple
|
@@ -26,37 +20,28 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
26
20
|
*/
|
27
21
|
class Disposable {
|
28
22
|
disposed = false;
|
29
|
-
#disposables
|
30
|
-
/*: ?Set<DisposableLike> */
|
31
|
-
;
|
23
|
+
#disposables /*: ?Set<DisposableLike> */;
|
32
24
|
|
33
25
|
constructor(...disposables) {
|
34
26
|
this.#disposables = new Set(disposables);
|
35
27
|
}
|
36
|
-
|
37
28
|
add(...disposables) {
|
38
29
|
if (this.disposed) {
|
39
30
|
throw new _errors.AlreadyDisposedError('Cannot add new disposables after disposable has been disposed');
|
40
31
|
}
|
41
|
-
|
42
32
|
(0, _assert().default)(this.#disposables != null);
|
43
|
-
|
44
33
|
for (let disposable of disposables) {
|
45
34
|
this.#disposables.add(disposable);
|
46
35
|
}
|
47
36
|
}
|
48
|
-
|
49
37
|
async dispose() {
|
50
38
|
if (this.disposed) {
|
51
39
|
return;
|
52
40
|
}
|
53
|
-
|
54
41
|
this.disposed = true;
|
55
42
|
(0, _assert().default)(this.#disposables != null);
|
56
43
|
await Promise.all([...this.#disposables].map(disposable => typeof disposable === 'function' ? disposable() : disposable.dispose()));
|
57
44
|
this.#disposables = null;
|
58
45
|
}
|
59
|
-
|
60
46
|
}
|
61
|
-
|
62
47
|
exports.default = Disposable;
|
package/lib/ValueEmitter.js
CHANGED
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _errors = require("./errors");
|
9
|
-
|
10
8
|
// Like an EventEmitter, but for only a single "event". This provides type-safety
|
11
9
|
// for the values emitted. Rather than passing predetermined strings (which can
|
12
10
|
// be misspelled), create an instance of ValueEmitter for every logical "event"
|
@@ -17,70 +15,57 @@ class ValueEmitter {
|
|
17
15
|
// in emit) is far faster than a Set copy: https://github.com/atom/event-kit/pull/39
|
18
16
|
_listeners = [];
|
19
17
|
_disposed = false;
|
20
|
-
|
21
18
|
addListener(listener) {
|
22
19
|
if (this._disposed) {
|
23
20
|
throw new _errors.AlreadyDisposedError('Cannot add a listener since this ValueEmitter has been disposed');
|
24
21
|
}
|
22
|
+
this._listeners.push(listener);
|
25
23
|
|
26
|
-
|
24
|
+
// Close over a reference to this emitter in the disposable below, rather
|
27
25
|
// than referencing `this` directly. This allows us to set it to null after
|
28
26
|
// slicing out the listener.
|
29
27
|
// This prevents anyone holding onto the disposable after disposal from
|
30
28
|
// unintentionally retaining a reference to this emitter.
|
31
|
-
|
32
|
-
|
33
29
|
let emitter = this;
|
34
30
|
return {
|
35
31
|
dispose() {
|
36
32
|
if (emitter == null) {
|
37
33
|
return;
|
38
34
|
}
|
39
|
-
|
40
35
|
if (emitter._disposed) {
|
41
36
|
emitter = null;
|
42
37
|
return;
|
43
38
|
}
|
44
|
-
|
45
39
|
let listeners = emitter._listeners;
|
46
40
|
let listenerIndex = listeners.indexOf(listener);
|
47
|
-
|
48
41
|
if (listenerIndex > -1) {
|
49
42
|
listeners.splice(listenerIndex, 1);
|
50
43
|
}
|
51
|
-
|
52
44
|
emitter = null;
|
53
45
|
}
|
54
|
-
|
55
46
|
};
|
56
47
|
}
|
57
|
-
|
58
48
|
emit(value) {
|
59
49
|
if (this._disposed) {
|
60
50
|
throw new _errors.AlreadyDisposedError('Cannot emit since this ValueEmitter has been disposed');
|
61
|
-
}
|
51
|
+
}
|
52
|
+
|
53
|
+
// Iterate over a copy of listeners. This prevents the following cases:
|
62
54
|
// * When a listener callback can itself register a new listener and be
|
63
55
|
// emitted as part of this iteration.
|
64
56
|
// * When a listener callback disposes of this emitter mid-emit, preventing
|
65
57
|
// other listeners from receiving the event.
|
66
|
-
|
67
|
-
|
68
58
|
let listeners = this._listeners.slice();
|
69
|
-
|
70
59
|
for (let i = 0; i < listeners.length; i++) {
|
71
60
|
listeners[i](value);
|
72
61
|
}
|
73
62
|
}
|
74
|
-
|
75
63
|
dispose() {
|
76
64
|
if (this._disposed) {
|
77
65
|
return;
|
78
66
|
}
|
79
|
-
|
80
67
|
this._listeners = [];
|
81
68
|
this._disposed = true;
|
82
69
|
}
|
83
|
-
|
84
70
|
}
|
85
|
-
|
86
71
|
exports.default = ValueEmitter;
|
package/lib/errors.js
CHANGED
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.AlreadyDisposedError = void 0;
|
7
|
-
|
8
7
|
class AlreadyDisposedError extends Error {}
|
9
|
-
|
10
8
|
exports.AlreadyDisposedError = AlreadyDisposedError;
|
11
9
|
Object.defineProperty(AlreadyDisposedError.prototype, 'name', {
|
12
10
|
value: 'AlreadyDisposedError'
|
package/lib/index.js
CHANGED
@@ -21,11 +21,7 @@ Object.defineProperty(exports, "ValueEmitter", {
|
|
21
21
|
return _ValueEmitter.default;
|
22
22
|
}
|
23
23
|
});
|
24
|
-
|
25
24
|
var _Disposable = _interopRequireDefault(require("./Disposable"));
|
26
|
-
|
27
25
|
var _ValueEmitter = _interopRequireDefault(require("./ValueEmitter"));
|
28
|
-
|
29
26
|
var _errors = require("./errors");
|
30
|
-
|
31
27
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@parcel/events",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.9.1",
|
4
4
|
"license": "MIT",
|
5
5
|
"publishConfig": {
|
6
6
|
"access": "public"
|
@@ -18,5 +18,5 @@
|
|
18
18
|
"engines": {
|
19
19
|
"node": ">= 12.0.0"
|
20
20
|
},
|
21
|
-
"gitHead": "
|
21
|
+
"gitHead": "5c5dc302b559f0b0d8c57b4d638aac521c879b70"
|
22
22
|
}
|