@parcel/events 2.0.0-nightly.121 → 2.0.0-nightly.1212

Sign up to get free protection for your applications and to get access to all the features.
package/lib/Disposable.js CHANGED
@@ -5,17 +5,19 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _assert = _interopRequireDefault(require("assert"));
8
+ function _assert() {
9
+ const data = _interopRequireDefault(require("assert"));
9
10
 
10
- var _errors = require("./errors");
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ _assert = function () {
12
+ return data;
13
+ };
13
14
 
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
+ return data;
16
+ }
15
17
 
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; }
18
+ var _errors = require("./errors");
17
19
 
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; }
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
- // ?Set<DisposableLike>
27
- constructor(...disposables) {
28
- _defineProperty(this, "disposed", false);
28
+ disposed = false;
29
+ #disposables
30
+ /*: ?Set<DisposableLike> */
31
+ ;
29
32
 
30
- _disposables.set(this, {
31
- writable: true,
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)(_classPrivateFieldGet(this, _disposables) != null);
42
+ (0, _assert().default)(this.#disposables != null);
44
43
 
45
44
  for (let disposable of disposables) {
46
- _classPrivateFieldGet(this, _disposables).add(disposable);
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;
@@ -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
- constructor() {
18
- _defineProperty(this, "_listeners", []);
19
-
20
- _defineProperty(this, "_disposed", false);
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 listenerIndex = emitter._listeners.indexOf(listener);
45
+ let listeners = emitter._listeners;
46
+ let listenerIndex = listeners.indexOf(listener);
48
47
 
49
48
  if (listenerIndex > -1) {
50
- emitter._listeners.splice(listenerIndex, 1);
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, "Disposable", {
6
+ Object.defineProperty(exports, "AlreadyDisposedError", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _Disposable.default;
9
+ return _errors.AlreadyDisposedError;
10
10
  }
11
11
  });
12
- Object.defineProperty(exports, "ValueEmitter", {
12
+ Object.defineProperty(exports, "Disposable", {
13
13
  enumerable: true,
14
14
  get: function () {
15
- return _ValueEmitter.default;
15
+ return _Disposable.default;
16
16
  }
17
17
  });
18
- Object.defineProperty(exports, "AlreadyDisposedError", {
18
+ Object.defineProperty(exports, "ValueEmitter", {
19
19
  enumerable: true,
20
20
  get: function () {
21
- return _errors.AlreadyDisposedError;
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.121+8dc29d37",
3
+ "version": "2.0.0-nightly.1212+334e8a5ca",
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": ">= 10.0.0"
19
+ "node": ">= 12.0.0"
16
20
  },
17
- "gitHead": "8dc29d37664b24556ce0d2e91771b2ce30ae3f69"
21
+ "gitHead": "334e8a5ca57f6517cd6ab8dc783b530f811e846f"
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; // ?Set<DisposableLike>
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
- for (let disposable of this.#disposables) {
43
- if (typeof disposable === 'function') {
44
- disposable();
45
- } else {
46
- disposable.dispose();
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
  }
@@ -41,9 +41,10 @@ export default class ValueEmitter<TValue> implements IDisposable {
41
41
  return;
42
42
  }
43
43
 
44
- let listenerIndex = emitter._listeners.indexOf(listener);
44
+ let listeners = emitter._listeners;
45
+ let listenerIndex = listeners.indexOf(listener);
45
46
  if (listenerIndex > -1) {
46
- emitter._listeners.splice(listenerIndex, 1);
47
+ listeners.splice(listenerIndex, 1);
47
48
  }
48
49
 
49
50
  emitter = null;
package/src/types.js CHANGED
@@ -1,5 +1,8 @@
1
1
  // @flow strict-local
2
2
 
3
3
  export interface IDisposable {
4
- dispose(): void;
4
+ /** This can return a Promise, as dispose() of all inner disposables are
5
+ * awaited in Disposable#dispose()
6
+ */
7
+ dispose(): mixed;
5
8
  }