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