@parcel/events 2.0.0-nightly.122 → 2.0.0-nightly.1221
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/Disposable.js +22 -34
- package/lib/ValueEmitter.js +8 -9
- package/lib/index.js +6 -6
- package/package.json +7 -3
- package/src/Disposable.js +9 -10
- package/src/ValueEmitter.js +3 -2
- package/src/types.js +4 -1
package/lib/Disposable.js
CHANGED
@@ -5,17 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
7
|
|
8
|
-
|
8
|
+
function _assert() {
|
9
|
+
const data = _interopRequireDefault(require("assert"));
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
_assert = function () {
|
12
|
+
return data;
|
13
|
+
};
|
13
14
|
|
14
|
-
|
15
|
+
return data;
|
16
|
+
}
|
15
17
|
|
16
|
-
|
18
|
+
var _errors = require("./errors");
|
17
19
|
|
18
|
-
function
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19
21
|
|
20
22
|
/*
|
21
23
|
* A general-purpose disposable class. It can normalize disposable-like values
|
@@ -23,16 +25,13 @@ function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = p
|
|
23
25
|
* disposable-like values to be disposed of at once.
|
24
26
|
*/
|
25
27
|
class Disposable {
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
disposed = false;
|
29
|
+
#disposables
|
30
|
+
/*: ?Set<DisposableLike> */
|
31
|
+
;
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
value: void 0
|
33
|
-
});
|
34
|
-
|
35
|
-
_classPrivateFieldSet(this, _disposables, new Set(disposables));
|
33
|
+
constructor(...disposables) {
|
34
|
+
this.#disposables = new Set(disposables);
|
36
35
|
}
|
37
36
|
|
38
37
|
add(...disposables) {
|
@@ -40,35 +39,24 @@ class Disposable {
|
|
40
39
|
throw new _errors.AlreadyDisposedError('Cannot add new disposables after disposable has been disposed');
|
41
40
|
}
|
42
41
|
|
43
|
-
(0, _assert.default)(
|
42
|
+
(0, _assert().default)(this.#disposables != null);
|
44
43
|
|
45
44
|
for (let disposable of disposables) {
|
46
|
-
|
45
|
+
this.#disposables.add(disposable);
|
47
46
|
}
|
48
47
|
}
|
49
48
|
|
50
|
-
dispose() {
|
49
|
+
async dispose() {
|
51
50
|
if (this.disposed) {
|
52
51
|
return;
|
53
52
|
}
|
54
53
|
|
55
|
-
(0, _assert.default)(_classPrivateFieldGet(this, _disposables) != null);
|
56
|
-
|
57
|
-
for (let disposable of _classPrivateFieldGet(this, _disposables)) {
|
58
|
-
if (typeof disposable === 'function') {
|
59
|
-
disposable();
|
60
|
-
} else {
|
61
|
-
disposable.dispose();
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
_classPrivateFieldSet(this, _disposables, null);
|
66
|
-
|
67
54
|
this.disposed = true;
|
55
|
+
(0, _assert().default)(this.#disposables != null);
|
56
|
+
await Promise.all([...this.#disposables].map(disposable => typeof disposable === 'function' ? disposable() : disposable.dispose()));
|
57
|
+
this.#disposables = null;
|
68
58
|
}
|
69
59
|
|
70
60
|
}
|
71
61
|
|
72
|
-
exports.default = Disposable;
|
73
|
-
|
74
|
-
var _disposables = new WeakMap();
|
62
|
+
exports.default = Disposable;
|
package/lib/ValueEmitter.js
CHANGED
@@ -7,18 +7,16 @@ exports.default = void 0;
|
|
7
7
|
|
8
8
|
var _errors = require("./errors");
|
9
9
|
|
10
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
11
|
-
|
12
10
|
// Like an EventEmitter, but for only a single "event". This provides type-safety
|
13
11
|
// for the values emitted. Rather than passing predetermined strings (which can
|
14
12
|
// be misspelled), create an instance of ValueEmitter for every logical "event"
|
15
13
|
// to be dispatched, and type it according to the type of value emitted.
|
16
14
|
class ValueEmitter {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
// An array of listeners. One might think a Set would be better for O(1) removal,
|
16
|
+
// but splicing a JS array gets pretty close, and copying the array (as is done
|
17
|
+
// in emit) is far faster than a Set copy: https://github.com/atom/event-kit/pull/39
|
18
|
+
_listeners = [];
|
19
|
+
_disposed = false;
|
22
20
|
|
23
21
|
addListener(listener) {
|
24
22
|
if (this._disposed) {
|
@@ -44,10 +42,11 @@ class ValueEmitter {
|
|
44
42
|
return;
|
45
43
|
}
|
46
44
|
|
47
|
-
let
|
45
|
+
let listeners = emitter._listeners;
|
46
|
+
let listenerIndex = listeners.indexOf(listener);
|
48
47
|
|
49
48
|
if (listenerIndex > -1) {
|
50
|
-
|
49
|
+
listeners.splice(listenerIndex, 1);
|
51
50
|
}
|
52
51
|
|
53
52
|
emitter = null;
|
package/lib/index.js
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
Object.defineProperty(exports, "
|
6
|
+
Object.defineProperty(exports, "AlreadyDisposedError", {
|
7
7
|
enumerable: true,
|
8
8
|
get: function () {
|
9
|
-
return
|
9
|
+
return _errors.AlreadyDisposedError;
|
10
10
|
}
|
11
11
|
});
|
12
|
-
Object.defineProperty(exports, "
|
12
|
+
Object.defineProperty(exports, "Disposable", {
|
13
13
|
enumerable: true,
|
14
14
|
get: function () {
|
15
|
-
return
|
15
|
+
return _Disposable.default;
|
16
16
|
}
|
17
17
|
});
|
18
|
-
Object.defineProperty(exports, "
|
18
|
+
Object.defineProperty(exports, "ValueEmitter", {
|
19
19
|
enumerable: true,
|
20
20
|
get: function () {
|
21
|
-
return
|
21
|
+
return _ValueEmitter.default;
|
22
22
|
}
|
23
23
|
});
|
24
24
|
|
package/package.json
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "@parcel/events",
|
3
|
-
"version": "2.0.0-nightly.
|
3
|
+
"version": "2.0.0-nightly.1221+3437f64e4",
|
4
4
|
"license": "MIT",
|
5
5
|
"publishConfig": {
|
6
6
|
"access": "public"
|
7
7
|
},
|
8
|
+
"funding": {
|
9
|
+
"type": "opencollective",
|
10
|
+
"url": "https://opencollective.com/parcel"
|
11
|
+
},
|
8
12
|
"repository": {
|
9
13
|
"type": "git",
|
10
14
|
"url": "https://github.com/parcel-bundler/parcel.git"
|
@@ -12,7 +16,7 @@
|
|
12
16
|
"main": "lib/index.js",
|
13
17
|
"source": "src/index.js",
|
14
18
|
"engines": {
|
15
|
-
"node": ">=
|
19
|
+
"node": ">= 12.0.0"
|
16
20
|
},
|
17
|
-
"gitHead": "
|
21
|
+
"gitHead": "3437f64e4c6f57d911614c480b742e229149f200"
|
18
22
|
}
|
package/src/Disposable.js
CHANGED
@@ -14,7 +14,7 @@ type DisposableLike = IDisposable | (() => mixed);
|
|
14
14
|
*/
|
15
15
|
export default class Disposable implements IDisposable {
|
16
16
|
disposed: boolean = false;
|
17
|
-
#disposables
|
17
|
+
#disposables /*: ?Set<DisposableLike> */;
|
18
18
|
|
19
19
|
constructor(...disposables: Array<DisposableLike>) {
|
20
20
|
this.#disposables = new Set(disposables);
|
@@ -33,21 +33,20 @@ export default class Disposable implements IDisposable {
|
|
33
33
|
}
|
34
34
|
}
|
35
35
|
|
36
|
-
dispose(): void {
|
36
|
+
async dispose(): Promise<void> {
|
37
37
|
if (this.disposed) {
|
38
38
|
return;
|
39
39
|
}
|
40
40
|
|
41
|
+
this.disposed = true;
|
42
|
+
|
41
43
|
invariant(this.#disposables != null);
|
42
|
-
|
43
|
-
|
44
|
-
disposable()
|
45
|
-
|
46
|
-
|
47
|
-
}
|
48
|
-
}
|
44
|
+
await Promise.all(
|
45
|
+
[...this.#disposables].map(disposable =>
|
46
|
+
typeof disposable === 'function' ? disposable() : disposable.dispose(),
|
47
|
+
),
|
48
|
+
);
|
49
49
|
|
50
50
|
this.#disposables = null;
|
51
|
-
this.disposed = true;
|
52
51
|
}
|
53
52
|
}
|
package/src/ValueEmitter.js
CHANGED
@@ -41,9 +41,10 @@ export default class ValueEmitter<TValue> implements IDisposable {
|
|
41
41
|
return;
|
42
42
|
}
|
43
43
|
|
44
|
-
let
|
44
|
+
let listeners = emitter._listeners;
|
45
|
+
let listenerIndex = listeners.indexOf(listener);
|
45
46
|
if (listenerIndex > -1) {
|
46
|
-
|
47
|
+
listeners.splice(listenerIndex, 1);
|
47
48
|
}
|
48
49
|
|
49
50
|
emitter = null;
|