@guanghechen/disposable 1.0.0-alpha.2 → 1.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file. See
4
4
  [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-alpha.3](https://github.com/guanghechen/sora/compare/@guanghechen/disposable@1.0.0-alpha.2...@guanghechen/disposable@1.0.0-alpha.3) (2024-03-09)
7
+
8
+ ### Performance Improvements
9
+
10
+ - :art: refactor disposable
11
+ ([3e3d676](https://github.com/guanghechen/sora/commit/3e3d676ade205b6b7325276b3116c32fe194f33f))
12
+
13
+ # Change Log
14
+
15
+ All notable changes to this project will be documented in this file. See
16
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
17
+
6
18
  # [1.0.0-alpha.2](https://github.com/guanghechen/sora/compare/@guanghechen/disposable@1.0.0-alpha.1...@guanghechen/disposable@1.0.0-alpha.2) (2024-02-03)
7
19
 
8
20
  ### Performance Improvements
package/lib/cjs/index.cjs CHANGED
@@ -2,23 +2,45 @@
2
2
 
3
3
  var disposable_types = require('@guanghechen/disposable.types');
4
4
 
5
- function disposeAll(disposables) {
6
- const errors = [];
7
- for (const disposable of disposables) {
5
+ class SafeBatchHandler {
6
+ _errors;
7
+ _summary;
8
+ constructor() {
9
+ this._errors = [];
10
+ this._summary = undefined;
11
+ }
12
+ cleanup() {
13
+ this._errors.length = 0;
14
+ this._summary = undefined;
15
+ }
16
+ run(action) {
8
17
  try {
9
- disposable.dispose();
18
+ action();
10
19
  }
11
- catch (e) {
12
- errors.push(e);
20
+ catch (error) {
21
+ this._errors.push(error);
22
+ this._summary = undefined;
13
23
  }
14
24
  }
15
- if (errors.length === 1) {
16
- throw errors[0];
17
- }
18
- if (errors.length > 1) {
19
- throw new AggregateError(errors, 'Encountered errors while disposing');
25
+ summary() {
26
+ if (this._summary === undefined) {
27
+ if (this._errors.length === 1)
28
+ throw (this._summary = this._errors[0]);
29
+ if (this._errors.length > 1) {
30
+ this._summary = new AggregateError(this._errors, 'Encountered errors while disposing');
31
+ }
32
+ }
33
+ if (this._summary !== undefined)
34
+ throw this._summary;
20
35
  }
21
36
  }
37
+ function disposeAll(disposables) {
38
+ const handler = new SafeBatchHandler();
39
+ for (const disposable of disposables)
40
+ handler.run(() => disposable.dispose());
41
+ handler.summary();
42
+ handler.cleanup();
43
+ }
22
44
 
23
45
  class BatchDisposable {
24
46
  _disposed;
@@ -58,12 +80,6 @@ class Disposable {
58
80
  this._onDispose = onDispose;
59
81
  this._disposed = false;
60
82
  }
61
- static fromCallback(onDispose) {
62
- return new Disposable(onDispose);
63
- }
64
- static fromUnsubscribable(unsubscribable) {
65
- return new Disposable(() => unsubscribable.unsubscribe());
66
- }
67
83
  get disposed() {
68
84
  return this._disposed;
69
85
  }
@@ -84,6 +100,7 @@ function isDisposable(obj) {
84
100
 
85
101
  exports.BatchDisposable = BatchDisposable;
86
102
  exports.Disposable = Disposable;
103
+ exports.SafeBatchHandler = SafeBatchHandler;
87
104
  exports.disposeAll = disposeAll;
88
105
  exports.isDisposable = isDisposable;
89
106
  Object.keys(disposable_types).forEach(function (k) {
package/lib/esm/index.mjs CHANGED
@@ -1,22 +1,44 @@
1
1
  export * from '@guanghechen/disposable.types';
2
2
 
3
- function disposeAll(disposables) {
4
- const errors = [];
5
- for (const disposable of disposables) {
3
+ class SafeBatchHandler {
4
+ _errors;
5
+ _summary;
6
+ constructor() {
7
+ this._errors = [];
8
+ this._summary = undefined;
9
+ }
10
+ cleanup() {
11
+ this._errors.length = 0;
12
+ this._summary = undefined;
13
+ }
14
+ run(action) {
6
15
  try {
7
- disposable.dispose();
16
+ action();
8
17
  }
9
- catch (e) {
10
- errors.push(e);
18
+ catch (error) {
19
+ this._errors.push(error);
20
+ this._summary = undefined;
11
21
  }
12
22
  }
13
- if (errors.length === 1) {
14
- throw errors[0];
15
- }
16
- if (errors.length > 1) {
17
- throw new AggregateError(errors, 'Encountered errors while disposing');
23
+ summary() {
24
+ if (this._summary === undefined) {
25
+ if (this._errors.length === 1)
26
+ throw (this._summary = this._errors[0]);
27
+ if (this._errors.length > 1) {
28
+ this._summary = new AggregateError(this._errors, 'Encountered errors while disposing');
29
+ }
30
+ }
31
+ if (this._summary !== undefined)
32
+ throw this._summary;
18
33
  }
19
34
  }
35
+ function disposeAll(disposables) {
36
+ const handler = new SafeBatchHandler();
37
+ for (const disposable of disposables)
38
+ handler.run(() => disposable.dispose());
39
+ handler.summary();
40
+ handler.cleanup();
41
+ }
20
42
 
21
43
  class BatchDisposable {
22
44
  _disposed;
@@ -56,12 +78,6 @@ class Disposable {
56
78
  this._onDispose = onDispose;
57
79
  this._disposed = false;
58
80
  }
59
- static fromCallback(onDispose) {
60
- return new Disposable(onDispose);
61
- }
62
- static fromUnsubscribable(unsubscribable) {
63
- return new Disposable(() => unsubscribable.unsubscribe());
64
- }
65
81
  get disposed() {
66
82
  return this._disposed;
67
83
  }
@@ -80,4 +96,4 @@ function isDisposable(obj) {
80
96
  typeof Reflect.get(obj, 'disposed') === 'boolean');
81
97
  }
82
98
 
83
- export { BatchDisposable, Disposable, disposeAll, isDisposable };
99
+ export { BatchDisposable, Disposable, SafeBatchHandler, disposeAll, isDisposable };
@@ -1,6 +1,5 @@
1
1
  import { IBatchDisposable, IDisposable } from '@guanghechen/disposable.types';
2
2
  export * from '@guanghechen/disposable.types';
3
- import { IUnsubscribable } from '@guanghechen/subscribe.types';
4
3
 
5
4
  declare class BatchDisposable implements IBatchDisposable {
6
5
  protected _disposed: boolean;
@@ -14,15 +13,21 @@ declare class BatchDisposable implements IBatchDisposable {
14
13
  declare class Disposable implements IDisposable {
15
14
  protected readonly _onDispose: () => void;
16
15
  protected _disposed: boolean;
17
- private constructor();
18
- static fromCallback(onDispose: () => void): IDisposable;
19
- static fromUnsubscribable(unsubscribable: IUnsubscribable): IDisposable;
16
+ constructor(onDispose: () => void);
20
17
  get disposed(): boolean;
21
18
  dispose(): void;
22
19
  }
23
20
 
21
+ declare class SafeBatchHandler {
22
+ private readonly _errors;
23
+ private _summary;
24
+ constructor();
25
+ cleanup(): void;
26
+ run(action: () => void): void;
27
+ summary(): void | never;
28
+ }
24
29
  declare function disposeAll(disposables: Iterable<IDisposable>): void | never;
25
30
 
26
31
  declare function isDisposable(obj: unknown): obj is IDisposable;
27
32
 
28
- export { BatchDisposable, Disposable, disposeAll, isDisposable };
33
+ export { BatchDisposable, Disposable, SafeBatchHandler, disposeAll, isDisposable };
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@guanghechen/disposable",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.3",
4
4
  "author": {
5
5
  "name": "guanghechen",
6
6
  "url": "https://github.com/guanghechen/"
7
7
  },
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.0-alpha.1",
10
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.0-alpha.2",
11
11
  "directory": "packages/disposable"
12
12
  },
13
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.0-alpha.1/packages/disposable#readme",
13
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.0-alpha.2/packages/disposable#readme",
14
14
  "keywords": [
15
15
  "disposable"
16
16
  ],
@@ -34,8 +34,7 @@
34
34
  "README.md"
35
35
  ],
36
36
  "dependencies": {
37
- "@guanghechen/disposable.types": "^1.0.0-alpha.2",
38
- "@guanghechen/subscribe.types": "^1.0.0-alpha.2"
37
+ "@guanghechen/disposable.types": "^1.0.0-alpha.2"
39
38
  },
40
- "gitHead": "9c051a66df3f4dbd8eddd709046af0a975fdc702"
39
+ "gitHead": "8f4595ed8e3b21ac509a16aeb52cbb686636d9f0"
41
40
  }