@x-oasis/disposable 0.1.36

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/README.md ADDED
@@ -0,0 +1,231 @@
1
+ # @x-oasis/disposable
2
+
3
+ A TypeScript library for managing disposable resources. Provides utilities for creating, managing, and disposing of resources that need cleanup.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ $ npm i @x-oasis/disposable
9
+ ```
10
+
11
+ ## API
12
+
13
+ ### `IDisposable`
14
+
15
+ Interface for objects that can be disposed.
16
+
17
+ ```typescript
18
+ interface IDisposable {
19
+ dispose(): void;
20
+ }
21
+ ```
22
+
23
+ ### `Disposable`
24
+
25
+ A class that implements `IDisposable` and can register other disposables to be disposed together.
26
+
27
+ ```typescript
28
+ class Disposable implements IDisposable {
29
+ static readonly None: IDisposable; // A no-op disposable
30
+ dispose(): void;
31
+ registerDisposable<T extends IDisposable>(disposable: T): T;
32
+ }
33
+ ```
34
+
35
+ ### `DisposableStore`
36
+
37
+ A store that manages multiple disposables and can dispose them all at once.
38
+
39
+ ```typescript
40
+ class DisposableStore implements IDisposable {
41
+ dispose(): void;
42
+ get isDisposed(): boolean;
43
+ clear(): void;
44
+ add<T extends IDisposable>(thing: T): T;
45
+ delete<T extends IDisposable>(thing: T): void;
46
+ }
47
+ ```
48
+
49
+ ### `dispose()`
50
+
51
+ A utility function to dispose of a single disposable, an array of disposables, or an iterable of disposables.
52
+
53
+ ```typescript
54
+ function dispose<T extends IDisposable>(disposable: T): T;
55
+ function dispose<T extends IDisposable>(disposable: T | undefined): T | undefined;
56
+ function dispose<T extends IDisposable>(arg: T | Iterable<T> | undefined): any;
57
+ ```
58
+
59
+ ### `isDisposable()`
60
+
61
+ Type guard to check if an object implements `IDisposable`.
62
+
63
+ ```typescript
64
+ function isDisposable<T extends object>(thing: T): thing is T & IDisposable;
65
+ ```
66
+
67
+ ### `toDisposable()`
68
+
69
+ Converts a function to an `IDisposable` that will call the function when disposed.
70
+
71
+ ```typescript
72
+ function toDisposable(fn: Function): IDisposable;
73
+ ```
74
+
75
+ ## Usage
76
+
77
+ ### Basic Usage
78
+
79
+ ```typescript
80
+ import { Disposable, DisposableStore, dispose, toDisposable, isDisposable } from '@x-oasis/disposable';
81
+
82
+ // Create a simple disposable
83
+ const disposable = toDisposable(() => {
84
+ console.log('Cleaned up!');
85
+ });
86
+
87
+ // Dispose it
88
+ disposable.dispose(); // Logs: "Cleaned up!"
89
+
90
+ // Use Disposable class
91
+ const disposableObj = new Disposable();
92
+ disposableObj.registerDisposable(toDisposable(() => {
93
+ console.log('Resource 1 cleaned');
94
+ }));
95
+ disposableObj.registerDisposable(toDisposable(() => {
96
+ console.log('Resource 2 cleaned');
97
+ }));
98
+
99
+ disposableObj.dispose(); // Both resources are cleaned
100
+ ```
101
+
102
+ ### Using DisposableStore
103
+
104
+ ```typescript
105
+ import { DisposableStore, toDisposable } from '@x-oasis/disposable';
106
+
107
+ const store = new DisposableStore();
108
+
109
+ // Add disposables to the store
110
+ const cleanup1 = toDisposable(() => console.log('Cleanup 1'));
111
+ const cleanup2 = toDisposable(() => console.log('Cleanup 2'));
112
+
113
+ store.add(cleanup1);
114
+ store.add(cleanup2);
115
+
116
+ // Check if disposed
117
+ console.log(store.isDisposed); // false
118
+
119
+ // Dispose all at once
120
+ store.dispose(); // Logs: "Cleanup 1", "Cleanup 2"
121
+ console.log(store.isDisposed); // true
122
+
123
+ // Clear without disposing
124
+ store.clear(); // Removes all disposables without calling dispose()
125
+ ```
126
+
127
+ ### Disposing Multiple Resources
128
+
129
+ ```typescript
130
+ import { dispose, toDisposable } from '@x-oasis/disposable';
131
+
132
+ // Dispose an array
133
+ const disposables = [
134
+ toDisposable(() => console.log('1')),
135
+ toDisposable(() => console.log('2')),
136
+ toDisposable(() => console.log('3')),
137
+ ];
138
+
139
+ dispose(disposables); // All three are disposed
140
+
141
+ // Dispose a Set
142
+ const disposableSet = new Set([
143
+ toDisposable(() => console.log('A')),
144
+ toDisposable(() => console.log('B')),
145
+ ]);
146
+
147
+ dispose(disposableSet); // Both are disposed
148
+
149
+ // Dispose a single disposable
150
+ const single = toDisposable(() => console.log('Single'));
151
+ dispose(single); // Disposed
152
+ ```
153
+
154
+ ### Type Guard
155
+
156
+ ```typescript
157
+ import { isDisposable } from '@x-oasis/disposable';
158
+
159
+ const obj = {
160
+ dispose() {
161
+ console.log('Disposed');
162
+ },
163
+ };
164
+
165
+ if (isDisposable(obj)) {
166
+ // TypeScript now knows obj is IDisposable
167
+ obj.dispose();
168
+ }
169
+ ```
170
+
171
+ ### Disposable.None
172
+
173
+ ```typescript
174
+ import { Disposable } from '@x-oasis/disposable';
175
+
176
+ // A no-op disposable that does nothing when disposed
177
+ const none = Disposable.None;
178
+ none.dispose(); // Does nothing, no error
179
+ ```
180
+
181
+ ## Examples
182
+
183
+ ### Managing Event Listeners
184
+
185
+ ```typescript
186
+ import { DisposableStore, toDisposable } from '@x-oasis/disposable';
187
+
188
+ class Component {
189
+ private store = new DisposableStore();
190
+
191
+ setup() {
192
+ const button = document.querySelector('#myButton');
193
+
194
+ const handler = () => console.log('Clicked');
195
+ button?.addEventListener('click', handler);
196
+
197
+ // Register cleanup
198
+ this.store.add(toDisposable(() => {
199
+ button?.removeEventListener('click', handler);
200
+ }));
201
+ }
202
+
203
+ destroy() {
204
+ this.store.dispose(); // All event listeners are removed
205
+ }
206
+ }
207
+ ```
208
+
209
+ ### Managing Subscriptions
210
+
211
+ ```typescript
212
+ import { Disposable, toDisposable } from '@x-oasis/disposable';
213
+
214
+ class Service {
215
+ private disposable = new Disposable();
216
+
217
+ subscribe(callback: () => void) {
218
+ const subscription = createSubscription(callback);
219
+
220
+ this.disposable.registerDisposable(toDisposable(() => {
221
+ subscription.unsubscribe();
222
+ }));
223
+
224
+ return subscription;
225
+ }
226
+
227
+ cleanup() {
228
+ this.disposable.dispose(); // All subscriptions are unsubscribed
229
+ }
230
+ }
231
+ ```
@@ -0,0 +1,9 @@
1
+ import { IDisposable } from './types/disposable';
2
+ export declare function isDisposable<T extends object>(thing: T): thing is T & IDisposable;
3
+ export declare function toDisposable(fn: Function): IDisposable;
4
+ export declare class Disposable implements IDisposable {
5
+ static readonly None: Readonly<IDisposable>;
6
+ private readonly _store;
7
+ dispose(): void;
8
+ registerDisposable<T extends IDisposable>(disposable: T): void;
9
+ }
@@ -0,0 +1,13 @@
1
+ import { IDisposable } from './types/disposable';
2
+ export declare function dispose<T extends IDisposable>(disposable: T): T;
3
+ export declare function dispose<T extends IDisposable>(disposable: T | undefined): T | undefined;
4
+ export declare function dispose<T extends IDisposable>(arg: T | Iterable<T> | undefined): any;
5
+ export default class DisposableStore implements IDisposable {
6
+ private readonly _toDispose;
7
+ private _isDisposed;
8
+ dispose(): void;
9
+ get isDisposed(): boolean;
10
+ clear(): void;
11
+ add<T extends IDisposable>(thing: T): T;
12
+ delete<T extends IDisposable>(thing: T): void;
13
+ }
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _defineProperties(target, props) {
6
+ for (var i = 0; i < props.length; i++) {
7
+ var descriptor = props[i];
8
+ descriptor.enumerable = descriptor.enumerable || false;
9
+ descriptor.configurable = true;
10
+ if ("value" in descriptor) descriptor.writable = true;
11
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
12
+ }
13
+ }
14
+ function _createClass(Constructor, protoProps, staticProps) {
15
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
16
+ if (staticProps) _defineProperties(Constructor, staticProps);
17
+ Object.defineProperty(Constructor, "prototype", {
18
+ writable: false
19
+ });
20
+ return Constructor;
21
+ }
22
+ function _unsupportedIterableToArray(o, minLen) {
23
+ if (!o) return;
24
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
25
+ var n = Object.prototype.toString.call(o).slice(8, -1);
26
+ if (n === "Object" && o.constructor) n = o.constructor.name;
27
+ if (n === "Map" || n === "Set") return Array.from(o);
28
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
29
+ }
30
+ function _arrayLikeToArray(arr, len) {
31
+ if (len == null || len > arr.length) len = arr.length;
32
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
33
+ return arr2;
34
+ }
35
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
36
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
37
+ if (it) return (it = it.call(o)).next.bind(it);
38
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
39
+ if (it) o = it;
40
+ var i = 0;
41
+ return function () {
42
+ if (i >= o.length) return {
43
+ done: true
44
+ };
45
+ return {
46
+ done: false,
47
+ value: o[i++]
48
+ };
49
+ };
50
+ }
51
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
52
+ }
53
+ function _toPrimitive(input, hint) {
54
+ if (typeof input !== "object" || input === null) return input;
55
+ var prim = input[Symbol.toPrimitive];
56
+ if (prim !== undefined) {
57
+ var res = prim.call(input, hint || "default");
58
+ if (typeof res !== "object") return res;
59
+ throw new TypeError("@@toPrimitive must return a primitive value.");
60
+ }
61
+ return (hint === "string" ? String : Number)(input);
62
+ }
63
+ function _toPropertyKey(arg) {
64
+ var key = _toPrimitive(arg, "string");
65
+ return typeof key === "symbol" ? key : String(key);
66
+ }
67
+
68
+ function isObject(thing) {
69
+ return typeof thing === 'object' && thing !== null;
70
+ }
71
+ function isFunction(thing) {
72
+ return typeof thing === 'function';
73
+ }
74
+ function isArray(thing) {
75
+ return Array.isArray(thing);
76
+ }
77
+ function isIterable(thing) {
78
+ return thing && typeof thing === 'object' && typeof thing[Symbol.iterator] === 'function';
79
+ }
80
+
81
+ function dispose(disposables) {
82
+ if (isArray(disposables)) {
83
+ disposables.forEach(function (disposable) {
84
+ try {
85
+ disposable == null ? void 0 : disposable.dispose();
86
+ } catch (err) {
87
+ }
88
+ });
89
+ return [];
90
+ }
91
+ if (isIterable(disposables) && !isArray(disposables)) {
92
+ for (var _iterator = _createForOfIteratorHelperLoose(disposables), _step; !(_step = _iterator()).done;) {
93
+ var disposable = _step.value;
94
+ try {
95
+ disposable == null ? void 0 : disposable.dispose();
96
+ } catch (err) {
97
+ }
98
+ }
99
+ return undefined;
100
+ }
101
+ if (disposables && !isArray(disposables) && !isIterable(disposables) && isDisposable(disposables)) {
102
+ try {
103
+ disposables.dispose();
104
+ } catch (err) {
105
+ }
106
+ return disposables;
107
+ }
108
+ return undefined;
109
+ }
110
+ var DisposableStore = /*#__PURE__*/function () {
111
+ function DisposableStore() {
112
+ this._toDispose = new Set();
113
+ this._isDisposed = false;
114
+ }
115
+ var _proto = DisposableStore.prototype;
116
+ _proto.dispose = function dispose() {
117
+ if (this._isDisposed) {
118
+ return;
119
+ }
120
+ this._isDisposed = true;
121
+ this.clear();
122
+ };
123
+ _proto.clear = function clear() {
124
+ if (this._toDispose.size === 0) {
125
+ return;
126
+ }
127
+ dispose(this._toDispose);
128
+ this._toDispose.clear();
129
+ };
130
+ _proto.add = function add(thing) {
131
+ if (!thing) return thing;
132
+ if (thing === this) {
133
+ throw new Error('Cannot register a disposable on itself!');
134
+ }
135
+ if (this._isDisposed) ; else {
136
+ this._toDispose.add(thing);
137
+ }
138
+ return thing;
139
+ };
140
+ _proto["delete"] = function _delete(thing) {
141
+ if (!thing) {
142
+ return;
143
+ }
144
+ if (thing === this) {
145
+ throw new Error('Cannot dispose a disposable on itself!');
146
+ }
147
+ this._toDispose["delete"](thing);
148
+ thing.dispose();
149
+ };
150
+ _createClass(DisposableStore, [{
151
+ key: "isDisposed",
152
+ get: function get() {
153
+ return this._isDisposed;
154
+ }
155
+ }]);
156
+ return DisposableStore;
157
+ }();
158
+
159
+ function isDisposable(thing) {
160
+ return isObject(thing) && isFunction(thing.dispose);
161
+ }
162
+ function toDisposable(fn) {
163
+ return {
164
+ dispose: function dispose() {
165
+ return fn();
166
+ }
167
+ };
168
+ }
169
+ var Disposable = /*#__PURE__*/function () {
170
+ function Disposable() {
171
+ this._store = new DisposableStore();
172
+ }
173
+ var _proto = Disposable.prototype;
174
+ _proto.dispose = function dispose() {
175
+ this._store.dispose();
176
+ };
177
+ _proto.registerDisposable = function registerDisposable(disposable) {
178
+ if (disposable === this) {
179
+ throw new Error('Can not register itself');
180
+ }
181
+ this._store.add(disposable);
182
+ };
183
+ return Disposable;
184
+ }();
185
+ Disposable.None = /*#__PURE__*/Object.freeze({
186
+ dispose: function dispose() {}
187
+ });
188
+
189
+ exports.Disposable = Disposable;
190
+ exports.DisposableStore = DisposableStore;
191
+ exports.dispose = dispose;
192
+ exports.isDisposable = isDisposable;
193
+ exports.toDisposable = toDisposable;
194
+ //# sourceMappingURL=disposable.cjs.development.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disposable.cjs.development.js","sources":["../src/utils.ts","../src/DisposableStore.ts","../src/Disposable.ts"],"sourcesContent":["export function isObject(thing: any): thing is Object {\n return typeof thing === 'object' && thing !== null;\n}\n\nexport function isFunction(thing: any): thing is Function {\n return typeof thing === 'function';\n}\n\nexport function isArray(thing: any): thing is any[] {\n return Array.isArray(thing);\n}\n\nexport function isIterable<T = any>(thing: any): thing is Iterable<T> {\n return (\n thing &&\n typeof thing === 'object' &&\n typeof thing[Symbol.iterator] === 'function'\n );\n}\n","import { isDisposable } from './Disposable';\nimport { IDisposable } from './types/disposable';\nimport { isArray, isIterable } from './utils';\n\nexport function dispose<T extends IDisposable>(disposable: T): T;\nexport function dispose<T extends IDisposable>(\n disposable: T | undefined\n): T | undefined;\nexport function dispose<T extends IDisposable>(\n arg: T | Iterable<T> | undefined\n): any;\nexport function dispose<T extends IDisposable>(\n disposables: T | T[] | Iterable<T> | undefined\n): T | T[] | undefined {\n const errors = [];\n if (isArray(disposables)) {\n disposables.forEach((disposable) => {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n });\n return [];\n }\n if (isIterable<IDisposable>(disposables) && !isArray(disposables)) {\n for (const disposable of disposables as Iterable<IDisposable>) {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n }\n return undefined;\n }\n if (\n disposables &&\n !isArray(disposables) &&\n !isIterable(disposables) &&\n isDisposable<IDisposable>(disposables)\n ) {\n try {\n (disposables as IDisposable).dispose();\n } catch (err) {\n errors.push(err);\n }\n return disposables as T;\n }\n return undefined;\n}\n\nexport default class DisposableStore implements IDisposable {\n private readonly _toDispose = new Set<IDisposable>();\n\n private _isDisposed = false;\n\n public dispose(): void {\n if (this._isDisposed) {\n return;\n }\n\n this._isDisposed = true;\n this.clear();\n }\n\n public get isDisposed(): boolean {\n return this._isDisposed;\n }\n\n public clear(): void {\n if (this._toDispose.size === 0) {\n return;\n }\n\n dispose(this._toDispose);\n\n this._toDispose.clear();\n }\n\n public add<T extends IDisposable>(thing: T): T {\n if (!thing) return thing;\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot register a disposable on itself!');\n }\n\n if (this._isDisposed) {\n // ...\n } else {\n this._toDispose.add(thing);\n }\n\n return thing;\n }\n\n public delete<T extends IDisposable>(thing: T): void {\n if (!thing) {\n return;\n }\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot dispose a disposable on itself!');\n }\n this._toDispose.delete(thing);\n thing.dispose();\n }\n}\n","import DisposableStore from './DisposableStore';\nimport { IDisposable } from './types/disposable';\nimport { isFunction, isObject } from './utils';\n\nexport function isDisposable<T extends object>(\n thing: T\n): thing is T & IDisposable {\n return isObject(thing) && isFunction((thing as IDisposable).dispose);\n}\n\nexport function toDisposable(fn: Function): IDisposable {\n return {\n dispose: () => fn(),\n };\n}\n\nexport class Disposable implements IDisposable {\n static readonly None = Object.freeze<IDisposable>({ dispose() {} });\n\n private readonly _store = new DisposableStore();\n\n public dispose(): void {\n this._store.dispose();\n }\n\n registerDisposable<T extends IDisposable>(disposable: T) {\n if ((disposable as any) === this) {\n throw new Error('Can not register itself');\n }\n this._store.add(disposable);\n }\n}\n"],"names":["isObject","thing","isFunction","isArray","Array","isIterable","Symbol","iterator","dispose","disposables","forEach","disposable","err","_iterator","_createForOfIteratorHelperLoose","_step","done","value","undefined","isDisposable","DisposableStore","Set","_proto","prototype","_isDisposed","clear","_toDispose","size","add","Error","_delete","_createClass","key","get","toDisposable","fn","Disposable","_store","registerDisposable","Object","freeze"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAAgBA,QAAQA,CAACC,KAAU;EACjC,OAAO,OAAOA,KAAK,KAAK,QAAQ,IAAIA,KAAK,KAAK,IAAI;AACpD;SAEgBC,UAAUA,CAACD,KAAU;EACnC,OAAO,OAAOA,KAAK,KAAK,UAAU;AACpC;SAEgBE,OAAOA,CAACF,KAAU;EAChC,OAAOG,KAAK,CAACD,OAAO,CAACF,KAAK,CAAC;AAC7B;SAEgBI,UAAUA,CAAUJ,KAAU;EAC5C,OACEA,KAAK,IACL,OAAOA,KAAK,KAAK,QAAQ,IACzB,OAAOA,KAAK,CAACK,MAAM,CAACC,QAAQ,CAAC,KAAK,UAAU;AAEhD;;SCPgBC,OAAOA,CACrBC,WAA8C;EAG9C,IAAIN,OAAO,CAACM,WAAW,CAAC,EAAE;IACxBA,WAAW,CAACC,OAAO,CAAC,UAACC,UAAU;MAC7B,IAAI;QACFA,UAAU,oBAAVA,UAAU,CAAEH,OAAO,EAAE;OACtB,CAAC,OAAOI,GAAG,EAAE;;KAGf,CAAC;IACF,OAAO,EAAE;;EAEX,IAAIP,UAAU,CAAcI,WAAW,CAAC,IAAI,CAACN,OAAO,CAACM,WAAW,CAAC,EAAE;IACjE,SAAAI,SAAA,GAAAC,+BAAA,CAAyBL,WAAoC,GAAAM,KAAA,IAAAA,KAAA,GAAAF,SAAA,IAAAG,IAAA,GAAE;MAAA,IAApDL,UAAU,GA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=t[r];return o}function e(t){return Array.isArray(t)}function r(t){return t&&"object"==typeof t&&"function"==typeof t[Symbol.iterator]}function o(o){if(e(o))return o.forEach((function(t){try{null==t||t.dispose()}catch(t){}})),[];if(!r(o)||e(o)){if(o&&!e(o)&&!r(o)&&n(o)){try{o.dispose()}catch(t){}return o}}else for(var i,s=function(e,r){var o="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(o)return(o=o.call(e)).next.bind(o);if(Array.isArray(e)||(o=function(e,r){if(e){if("string"==typeof e)return t(e,void 0);var o=Object.prototype.toString.call(e).slice(8,-1);return"Object"===o&&e.constructor&&(o=e.constructor.name),"Map"===o||"Set"===o?Array.from(e):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?t(e,void 0):void 0}}(e))){o&&(e=o);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(o);!(i=s()).done;){var a=i.value;try{null==a||a.dispose()}catch(t){}}}Object.defineProperty(exports,"__esModule",{value:!0});var i=function(){function t(){this._toDispose=new Set,this._isDisposed=!1}var e,r,i=t.prototype;return i.dispose=function(){this._isDisposed||(this._isDisposed=!0,this.clear())},i.clear=function(){0!==this._toDispose.size&&(o(this._toDispose),this._toDispose.clear())},i.add=function(t){if(!t)return t;if(t===this)throw new Error("Cannot register a disposable on itself!");return this._isDisposed||this._toDispose.add(t),t},i.delete=function(t){if(t){if(t===this)throw new Error("Cannot dispose a disposable on itself!");this._toDispose.delete(t),t.dispose()}},e=t,(r=[{key:"isDisposed",get:function(){return this._isDisposed}}])&&function(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,"symbol"==typeof(i=function(t,e){if("object"!=typeof t||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,"string");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(o.key))?i:String(i),o)}var i}(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),t}();function n(t){return function(t){return"object"==typeof t&&null!==t}(t)&&function(t){return"function"==typeof t}(t.dispose)}var s=function(){function t(){this._store=new i}var e=t.prototype;return e.dispose=function(){this._store.dispose()},e.registerDisposable=function(t){if(t===this)throw new Error("Can not register itself");this._store.add(t)},t}();s.None=Object.freeze({dispose:function(){}}),exports.Disposable=s,exports.DisposableStore=i,exports.dispose=o,exports.isDisposable=n,exports.toDisposable=function(t){return{dispose:function(){return t()}}};
2
+ //# sourceMappingURL=disposable.cjs.production.min.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disposable.cjs.production.min.js","sources":["../src/utils.ts","../src/DisposableStore.ts","../src/Disposable.ts"],"sourcesContent":["export function isObject(thing: any): thing is Object {\n return typeof thing === 'object' && thing !== null;\n}\n\nexport function isFunction(thing: any): thing is Function {\n return typeof thing === 'function';\n}\n\nexport function isArray(thing: any): thing is any[] {\n return Array.isArray(thing);\n}\n\nexport function isIterable<T = any>(thing: any): thing is Iterable<T> {\n return (\n thing &&\n typeof thing === 'object' &&\n typeof thing[Symbol.iterator] === 'function'\n );\n}\n","import { isDisposable } from './Disposable';\nimport { IDisposable } from './types/disposable';\nimport { isArray, isIterable } from './utils';\n\nexport function dispose<T extends IDisposable>(disposable: T): T;\nexport function dispose<T extends IDisposable>(\n disposable: T | undefined\n): T | undefined;\nexport function dispose<T extends IDisposable>(\n arg: T | Iterable<T> | undefined\n): any;\nexport function dispose<T extends IDisposable>(\n disposables: T | T[] | Iterable<T> | undefined\n): T | T[] | undefined {\n const errors = [];\n if (isArray(disposables)) {\n disposables.forEach((disposable) => {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n });\n return [];\n }\n if (isIterable<IDisposable>(disposables) && !isArray(disposables)) {\n for (const disposable of disposables as Iterable<IDisposable>) {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n }\n return undefined;\n }\n if (\n disposables &&\n !isArray(disposables) &&\n !isIterable(disposables) &&\n isDisposable<IDisposable>(disposables)\n ) {\n try {\n (disposables as IDisposable).dispose();\n } catch (err) {\n errors.push(err);\n }\n return disposables as T;\n }\n return undefined;\n}\n\nexport default class DisposableStore implements IDisposable {\n private readonly _toDispose = new Set<IDisposable>();\n\n private _isDisposed = false;\n\n public dispose(): void {\n if (this._isDisposed) {\n return;\n }\n\n this._isDisposed = true;\n this.clear();\n }\n\n public get isDisposed(): boolean {\n return this._isDisposed;\n }\n\n public clear(): void {\n if (this._toDispose.size === 0) {\n return;\n }\n\n dispose(this._toDispose);\n\n this._toDispose.clear();\n }\n\n public add<T extends IDisposable>(thing: T): T {\n if (!thing) return thing;\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot register a disposable on itself!');\n }\n\n if (this._isDisposed) {\n // ...\n } else {\n this._toDispose.add(thing);\n }\n\n return thing;\n }\n\n public delete<T extends IDisposable>(thing: T): void {\n if (!thing) {\n return;\n }\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot dispose a disposable on itself!');\n }\n this._toDispose.delete(thing);\n thing.dispose();\n }\n}\n","import DisposableStore from './DisposableStore';\nimport { IDisposable } from './types/disposable';\nimport { isFunction, isObject } from './utils';\n\nexport function isDisposable<T extends object>(\n thing: T\n): thing is T & IDisposable {\n return isObject(thing) && isFunction((thing as IDisposable).dispose);\n}\n\nexport function toDisposable(fn: Function): IDisposable {\n return {\n dispose: () => fn(),\n };\n}\n\nexport class Disposable implements IDisposable {\n static readonly None = Object.freeze<IDisposable>({ dispose() {} });\n\n private readonly _store = new DisposableStore();\n\n public dispose(): void {\n this._store.dispose();\n }\n\n registerDisposable<T extends IDisposable>(disposable: T) {\n if ((disposable as any) === this) {\n throw new Error('Can not register itself');\n }\n this._store.add(disposable);\n }\n}\n"],"names":["isArray","thing","Array","isIterable","Symbol","iterator","dispose","disposables","forEach","disposable","err","isDisposable","_step","_iterator","_createForOfIteratorHelperLoose","done","value","DisposableStore","this","Set","_proto","prototype","_isDisposed","clear","_toDispose","size","add","Error","key","get","isObject","isFunction","Disposable","_store","registerDisposable","Object","freeze","fn"],"mappings":"gIAQgBA,EAAQC,GACtB,OAAOC,MAAMF,QAAQC,YAGPE,EAAoBF,GAClC,OACEA,GACiB,iBAAVA,GAC2B,mBAA3BA,EAAMG,OAAOC,mBCLRC,EACdC,GAGA,GAAIP,EAAQO,GAQV,OAPAA,EAAYC,SAAQ,SAACC,GACnB,UACEA,GAAAA,EAAYH,UACZ,MAAOI,QAIJ,GAET,IAAIP,EAAwBI,IAAiBP,EAAQO,IAUrD,GACEA,IACCP,EAAQO,KACRJ,EAAWI,IACZI,EAA0BJ,GAC1B,CACA,IACGA,EAA4BD,UAC7B,MAAOI,IAGT,OAAOH,QApBP,QAA6DK,EAA7DC,qrBAAAC,CAAyBP,KAAoCK,EAAAC,KAAAE,MAAE,CAAA,IAApDN,EAAUG,EAAAI,MACnB,UACEP,GAAAA,EAAYH,UACZ,MAAOI,6DAoBd,IAEoBO,aAArB,SAAAA,IACmBC,gBAAa,IAAIC,IAE1BD,kBAAc,EAkDvB,QAAAE,EAAAH,EAAAI,UArCE,OAqCFD,EAhDQd,QAAA,WACDY,KAAKI,cAITJ,KAAKI,aAAc,EACnBJ,KAAKK,UACNH,EAMMG,MAAA,WACwB,IAAzBL,KAAKM,WAAWC,OAIpBnB,EAAQY,KAAKM,YAEbN,KAAKM,WAAWD,UACjBH,EAEMM,IAAA,SAA2BzB,GAChC,IAAKA,EAAO,OAAOA,EACnB,GAAKA,IAAyCiB,KAC5C,MAAM,IAAIS,MAAM,2CASlB,OANIT,KAAKI,aAGPJ,KAAKM,WAAWE,IAAIzB,GAGfA,GACRmB,SAEM,SAA8BnB,GACnC,GAAKA,EAAL,CAGA,GAAKA,IAAyCiB,KAC5C,MAAM,IAAIS,MAAM,0CAElBT,KAAKM,kBAAkBvB,GACvBA,EAAMK,cACPW,OAAAW,iBAAAC,IAtCD,WACE,OAAOX,KAAKI,8gBACbL,cC/DaN,EACdV,GAEA,gBFPuBA,GACvB,MAAwB,iBAAVA,GAAgC,OAAVA,EEM7B6B,CAAS7B,aFHSA,GACzB,MAAwB,mBAAVA,EEEY8B,CAAY9B,EAAsBK,SAS9D,IAAa0B,aAAb,SAAAA,IAGmBd,YAAS,IAAID,EAY/B,IAAAG,EAAAY,EAAAX,UADE,OACFD,EAVQd,QAAA,WACLY,KAAKe,OAAO3B,WACbc,EAEDc,mBAAA,SAA0CzB,GACxC,GAAKA,IAAuBS,KAC1B,MAAM,IAAIS,MAAM,2BAElBT,KAAKe,OAAOP,IAAIjB,IACjBuB,KAbeA,OAAOG,OAAOC,OAAoB,CAAE9B,6IAPzB+B,GAC3B,MAAO,CACL/B,QAAS,WAAA,OAAM+B"}
@@ -0,0 +1,186 @@
1
+ function _defineProperties(target, props) {
2
+ for (var i = 0; i < props.length; i++) {
3
+ var descriptor = props[i];
4
+ descriptor.enumerable = descriptor.enumerable || false;
5
+ descriptor.configurable = true;
6
+ if ("value" in descriptor) descriptor.writable = true;
7
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
8
+ }
9
+ }
10
+ function _createClass(Constructor, protoProps, staticProps) {
11
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
12
+ if (staticProps) _defineProperties(Constructor, staticProps);
13
+ Object.defineProperty(Constructor, "prototype", {
14
+ writable: false
15
+ });
16
+ return Constructor;
17
+ }
18
+ function _unsupportedIterableToArray(o, minLen) {
19
+ if (!o) return;
20
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
21
+ var n = Object.prototype.toString.call(o).slice(8, -1);
22
+ if (n === "Object" && o.constructor) n = o.constructor.name;
23
+ if (n === "Map" || n === "Set") return Array.from(o);
24
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
25
+ }
26
+ function _arrayLikeToArray(arr, len) {
27
+ if (len == null || len > arr.length) len = arr.length;
28
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
29
+ return arr2;
30
+ }
31
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
32
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
33
+ if (it) return (it = it.call(o)).next.bind(it);
34
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
35
+ if (it) o = it;
36
+ var i = 0;
37
+ return function () {
38
+ if (i >= o.length) return {
39
+ done: true
40
+ };
41
+ return {
42
+ done: false,
43
+ value: o[i++]
44
+ };
45
+ };
46
+ }
47
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
48
+ }
49
+ function _toPrimitive(input, hint) {
50
+ if (typeof input !== "object" || input === null) return input;
51
+ var prim = input[Symbol.toPrimitive];
52
+ if (prim !== undefined) {
53
+ var res = prim.call(input, hint || "default");
54
+ if (typeof res !== "object") return res;
55
+ throw new TypeError("@@toPrimitive must return a primitive value.");
56
+ }
57
+ return (hint === "string" ? String : Number)(input);
58
+ }
59
+ function _toPropertyKey(arg) {
60
+ var key = _toPrimitive(arg, "string");
61
+ return typeof key === "symbol" ? key : String(key);
62
+ }
63
+
64
+ function isObject(thing) {
65
+ return typeof thing === 'object' && thing !== null;
66
+ }
67
+ function isFunction(thing) {
68
+ return typeof thing === 'function';
69
+ }
70
+ function isArray(thing) {
71
+ return Array.isArray(thing);
72
+ }
73
+ function isIterable(thing) {
74
+ return thing && typeof thing === 'object' && typeof thing[Symbol.iterator] === 'function';
75
+ }
76
+
77
+ function dispose(disposables) {
78
+ if (isArray(disposables)) {
79
+ disposables.forEach(function (disposable) {
80
+ try {
81
+ disposable == null ? void 0 : disposable.dispose();
82
+ } catch (err) {
83
+ }
84
+ });
85
+ return [];
86
+ }
87
+ if (isIterable(disposables) && !isArray(disposables)) {
88
+ for (var _iterator = _createForOfIteratorHelperLoose(disposables), _step; !(_step = _iterator()).done;) {
89
+ var disposable = _step.value;
90
+ try {
91
+ disposable == null ? void 0 : disposable.dispose();
92
+ } catch (err) {
93
+ }
94
+ }
95
+ return undefined;
96
+ }
97
+ if (disposables && !isArray(disposables) && !isIterable(disposables) && isDisposable(disposables)) {
98
+ try {
99
+ disposables.dispose();
100
+ } catch (err) {
101
+ }
102
+ return disposables;
103
+ }
104
+ return undefined;
105
+ }
106
+ var DisposableStore = /*#__PURE__*/function () {
107
+ function DisposableStore() {
108
+ this._toDispose = new Set();
109
+ this._isDisposed = false;
110
+ }
111
+ var _proto = DisposableStore.prototype;
112
+ _proto.dispose = function dispose() {
113
+ if (this._isDisposed) {
114
+ return;
115
+ }
116
+ this._isDisposed = true;
117
+ this.clear();
118
+ };
119
+ _proto.clear = function clear() {
120
+ if (this._toDispose.size === 0) {
121
+ return;
122
+ }
123
+ dispose(this._toDispose);
124
+ this._toDispose.clear();
125
+ };
126
+ _proto.add = function add(thing) {
127
+ if (!thing) return thing;
128
+ if (thing === this) {
129
+ throw new Error('Cannot register a disposable on itself!');
130
+ }
131
+ if (this._isDisposed) ; else {
132
+ this._toDispose.add(thing);
133
+ }
134
+ return thing;
135
+ };
136
+ _proto["delete"] = function _delete(thing) {
137
+ if (!thing) {
138
+ return;
139
+ }
140
+ if (thing === this) {
141
+ throw new Error('Cannot dispose a disposable on itself!');
142
+ }
143
+ this._toDispose["delete"](thing);
144
+ thing.dispose();
145
+ };
146
+ _createClass(DisposableStore, [{
147
+ key: "isDisposed",
148
+ get: function get() {
149
+ return this._isDisposed;
150
+ }
151
+ }]);
152
+ return DisposableStore;
153
+ }();
154
+
155
+ function isDisposable(thing) {
156
+ return isObject(thing) && isFunction(thing.dispose);
157
+ }
158
+ function toDisposable(fn) {
159
+ return {
160
+ dispose: function dispose() {
161
+ return fn();
162
+ }
163
+ };
164
+ }
165
+ var Disposable = /*#__PURE__*/function () {
166
+ function Disposable() {
167
+ this._store = new DisposableStore();
168
+ }
169
+ var _proto = Disposable.prototype;
170
+ _proto.dispose = function dispose() {
171
+ this._store.dispose();
172
+ };
173
+ _proto.registerDisposable = function registerDisposable(disposable) {
174
+ if (disposable === this) {
175
+ throw new Error('Can not register itself');
176
+ }
177
+ this._store.add(disposable);
178
+ };
179
+ return Disposable;
180
+ }();
181
+ Disposable.None = /*#__PURE__*/Object.freeze({
182
+ dispose: function dispose() {}
183
+ });
184
+
185
+ export { Disposable, DisposableStore, dispose, isDisposable, toDisposable };
186
+ //# sourceMappingURL=disposable.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disposable.esm.js","sources":["../src/utils.ts","../src/DisposableStore.ts","../src/Disposable.ts"],"sourcesContent":["export function isObject(thing: any): thing is Object {\n return typeof thing === 'object' && thing !== null;\n}\n\nexport function isFunction(thing: any): thing is Function {\n return typeof thing === 'function';\n}\n\nexport function isArray(thing: any): thing is any[] {\n return Array.isArray(thing);\n}\n\nexport function isIterable<T = any>(thing: any): thing is Iterable<T> {\n return (\n thing &&\n typeof thing === 'object' &&\n typeof thing[Symbol.iterator] === 'function'\n );\n}\n","import { isDisposable } from './Disposable';\nimport { IDisposable } from './types/disposable';\nimport { isArray, isIterable } from './utils';\n\nexport function dispose<T extends IDisposable>(disposable: T): T;\nexport function dispose<T extends IDisposable>(\n disposable: T | undefined\n): T | undefined;\nexport function dispose<T extends IDisposable>(\n arg: T | Iterable<T> | undefined\n): any;\nexport function dispose<T extends IDisposable>(\n disposables: T | T[] | Iterable<T> | undefined\n): T | T[] | undefined {\n const errors = [];\n if (isArray(disposables)) {\n disposables.forEach((disposable) => {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n });\n return [];\n }\n if (isIterable<IDisposable>(disposables) && !isArray(disposables)) {\n for (const disposable of disposables as Iterable<IDisposable>) {\n try {\n disposable?.dispose();\n } catch (err) {\n errors.push(err);\n }\n }\n return undefined;\n }\n if (\n disposables &&\n !isArray(disposables) &&\n !isIterable(disposables) &&\n isDisposable<IDisposable>(disposables)\n ) {\n try {\n (disposables as IDisposable).dispose();\n } catch (err) {\n errors.push(err);\n }\n return disposables as T;\n }\n return undefined;\n}\n\nexport default class DisposableStore implements IDisposable {\n private readonly _toDispose = new Set<IDisposable>();\n\n private _isDisposed = false;\n\n public dispose(): void {\n if (this._isDisposed) {\n return;\n }\n\n this._isDisposed = true;\n this.clear();\n }\n\n public get isDisposed(): boolean {\n return this._isDisposed;\n }\n\n public clear(): void {\n if (this._toDispose.size === 0) {\n return;\n }\n\n dispose(this._toDispose);\n\n this._toDispose.clear();\n }\n\n public add<T extends IDisposable>(thing: T): T {\n if (!thing) return thing;\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot register a disposable on itself!');\n }\n\n if (this._isDisposed) {\n // ...\n } else {\n this._toDispose.add(thing);\n }\n\n return thing;\n }\n\n public delete<T extends IDisposable>(thing: T): void {\n if (!thing) {\n return;\n }\n if ((thing as unknown as DisposableStore) === this) {\n throw new Error('Cannot dispose a disposable on itself!');\n }\n this._toDispose.delete(thing);\n thing.dispose();\n }\n}\n","import DisposableStore from './DisposableStore';\nimport { IDisposable } from './types/disposable';\nimport { isFunction, isObject } from './utils';\n\nexport function isDisposable<T extends object>(\n thing: T\n): thing is T & IDisposable {\n return isObject(thing) && isFunction((thing as IDisposable).dispose);\n}\n\nexport function toDisposable(fn: Function): IDisposable {\n return {\n dispose: () => fn(),\n };\n}\n\nexport class Disposable implements IDisposable {\n static readonly None = Object.freeze<IDisposable>({ dispose() {} });\n\n private readonly _store = new DisposableStore();\n\n public dispose(): void {\n this._store.dispose();\n }\n\n registerDisposable<T extends IDisposable>(disposable: T) {\n if ((disposable as any) === this) {\n throw new Error('Can not register itself');\n }\n this._store.add(disposable);\n }\n}\n"],"names":["isObject","thing","isFunction","isArray","Array","isIterable","Symbol","iterator","dispose","disposables","forEach","disposable","err","_iterator","_createForOfIteratorHelperLoose","_step","done","value","undefined","isDisposable","DisposableStore","Set","_proto","prototype","_isDisposed","clear","_toDispose","size","add","Error","_delete","_createClass","key","get","toDisposable","fn","Disposable","_store","registerDisposable","Object","freeze"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAAgBA,QAAQA,CAACC,KAAU;EACjC,OAAO,OAAOA,KAAK,KAAK,QAAQ,IAAIA,KAAK,KAAK,IAAI;AACpD;SAEgBC,UAAUA,CAACD,KAAU;EACnC,OAAO,OAAOA,KAAK,KAAK,UAAU;AACpC;SAEgBE,OAAOA,CAACF,KAAU;EAChC,OAAOG,KAAK,CAACD,OAAO,CAACF,KAAK,CAAC;AAC7B;SAEgBI,UAAUA,CAAUJ,KAAU;EAC5C,OACEA,KAAK,IACL,OAAOA,KAAK,KAAK,QAAQ,IACzB,OAAOA,KAAK,CAACK,MAAM,CAACC,QAAQ,CAAC,KAAK,UAAU;AAEhD;;SCPgBC,OAAOA,CACrBC,WAA8C;EAG9C,IAAIN,OAAO,CAACM,WAAW,CAAC,EAAE;IACxBA,WAAW,CAACC,OAAO,CAAC,UAACC,UAAU;MAC7B,IAAI;QACFA,UAAU,oBAAVA,UAAU,CAAEH,OAAO,EAAE;OACtB,CAAC,OAAOI,GAAG,EAAE;;KAGf,CAAC;IACF,OAAO,EAAE;;EAEX,IAAIP,UAAU,CAAcI,WAAW,CAAC,IAAI,CAACN,OAAO,CAACM,WAAW,CAAC,EAAE;IACjE,SAAAI,SAAA,GAAAC,+BAAA,CAAyBL,WAAoC,GAAAM,KAAA,IAAAA,KAAA,GAAAF,SAAA,IAAAG,IAAA,GAAE;MAAA,IAApDL,UAAU,GAAAI,KAAA,CAAAE,KAAA;MACnB,IAAI;QACFN,UAAU,oBAAVA,UAAU,CAAEH,OAAO,EAAE;OACtB,CAAC,OAAOI,GAAG,EAAE;;;IAIhB,OAAOM,SAAS;;EAElB,IACET,WAA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,IAAI,CAACH,UAAU,UAAO,CAACzB,KAAK,CAAC;IAC7BA,KAAK,CAACO,OAAO,EAAE;GAChB;EAAAuB,YAAA,CAAAX,eAAA;IAAAY,GAAA;IAAAC,GAAA,EAtCD,SAAAA;MACE,OAAO,IAAI,CAACT,WAAW;;;EACxB,OAAAJ,eAAA;AAAA;;SC/DaD,YAAYA,CAC1BlB,KAAQ;EAER,OAAOD,QAAQ,CAACC,KAAK,CAAC,IAAIC,UAAU,CAAED,KAAqB,CAACO,OAAO,CAAC;AACtE;AAEA,SAAgB0B,YAAYA,CAACC,EAAY;EACvC,OAAO;IACL3B,OAAO,EAAE,SAAAA;MAAA,OAAM2B,EAAE,EAAE;;GACpB;AACH;AAEA,IAAaC,UAAU;EAAvB,SAAAA;IAGmB,WAAM,GAAG,IAAIhB,eAAe,EAAE;;EAYhD,IAAAE,MAAA,GAAAc,UAAA,CAAAb,SAAA;EAAAD,MAAA,CAVQd,OAAO,GAAP,SAAAA;IACL,IAAI,CAAC6B,MAAM,CAAC7B,OAAO,EAAE;GACtB;EAAAc,MAAA,CAEDgB,kBAAkB,GAAlB,SAAAA,mBAA0C3B,UAAa;IACrD,IAAKA,UAAkB,KAAK,IAAI,EAAE;MAChC,MAAM,IAAIkB,KAAK,CAAC,yBAAyB,CAAC;;IAE5C,IAAI,CAACQ,MAAM,CAACT,GAAG,CAACjB,UAAU,CAAC;GAC5B;EAAA,OAAAyB,UAAA;AAAA;AAbeA,eAAI,gBAAGG,MAAM,CAACC,MAAM,CAAc;EAAEhC,OAAO,WAAAA;CAAO,CAAC;;;;"}
@@ -0,0 +1,3 @@
1
+ export { Disposable, toDisposable, isDisposable } from './Disposable';
2
+ export { default as DisposableStore, dispose } from './DisposableStore';
3
+ export * from './types';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+
2
+ 'use strict'
3
+
4
+ if (process.env.NODE_ENV === 'production') {
5
+ module.exports = require('./disposable.cjs.production.min.js')
6
+ } else {
7
+ module.exports = require('./disposable.cjs.development.js')
8
+ }
@@ -0,0 +1,3 @@
1
+ export interface IDisposable {
2
+ dispose(): void;
3
+ }
@@ -0,0 +1 @@
1
+ export * from './disposable';
@@ -0,0 +1,4 @@
1
+ export declare function isObject(thing: any): thing is Object;
2
+ export declare function isFunction(thing: any): thing is Function;
3
+ export declare function isArray(thing: any): thing is any[];
4
+ export declare function isIterable<T = any>(thing: any): thing is Iterable<T>;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@x-oasis/disposable",
3
+ "version": "0.1.36",
4
+ "description": "disposable function",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "module": "dist/disposable.esm.js",
8
+ "files": [
9
+ "dist",
10
+ "index.ts",
11
+ "src"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "author": "",
17
+ "license": "ISC",
18
+ "devDependencies": {
19
+ "tsdx": "^0.14.1"
20
+ },
21
+ "dependencies": {
22
+ "@x-oasis/is-object": "0.1.36"
23
+ },
24
+ "scripts": {
25
+ "build": "tsdx build --tsconfig tsconfig.build.json",
26
+ "clean": "rimraf ./dist",
27
+ "test": "vitest",
28
+ "compile": "tsc -p tsconfig.build.json"
29
+ }
30
+ }
@@ -0,0 +1,32 @@
1
+ import DisposableStore from './DisposableStore';
2
+ import { IDisposable } from './types/disposable';
3
+ import { isFunction, isObject } from './utils';
4
+
5
+ export function isDisposable<T extends object>(
6
+ thing: T
7
+ ): thing is T & IDisposable {
8
+ return isObject(thing) && isFunction((thing as IDisposable).dispose);
9
+ }
10
+
11
+ export function toDisposable(fn: Function): IDisposable {
12
+ return {
13
+ dispose: () => fn(),
14
+ };
15
+ }
16
+
17
+ export class Disposable implements IDisposable {
18
+ static readonly None = Object.freeze<IDisposable>({ dispose() {} });
19
+
20
+ private readonly _store = new DisposableStore();
21
+
22
+ public dispose(): void {
23
+ this._store.dispose();
24
+ }
25
+
26
+ registerDisposable<T extends IDisposable>(disposable: T) {
27
+ if ((disposable as any) === this) {
28
+ throw new Error('Can not register itself');
29
+ }
30
+ this._store.add(disposable);
31
+ }
32
+ }
@@ -0,0 +1,105 @@
1
+ import { isDisposable } from './Disposable';
2
+ import { IDisposable } from './types/disposable';
3
+ import { isArray, isIterable } from './utils';
4
+
5
+ export function dispose<T extends IDisposable>(disposable: T): T;
6
+ export function dispose<T extends IDisposable>(
7
+ disposable: T | undefined
8
+ ): T | undefined;
9
+ export function dispose<T extends IDisposable>(
10
+ arg: T | Iterable<T> | undefined
11
+ ): any;
12
+ export function dispose<T extends IDisposable>(
13
+ disposables: T | T[] | Iterable<T> | undefined
14
+ ): T | T[] | undefined {
15
+ const errors = [];
16
+ if (isArray(disposables)) {
17
+ disposables.forEach((disposable) => {
18
+ try {
19
+ disposable?.dispose();
20
+ } catch (err) {
21
+ errors.push(err);
22
+ }
23
+ });
24
+ return [];
25
+ }
26
+ if (isIterable<IDisposable>(disposables) && !isArray(disposables)) {
27
+ for (const disposable of disposables as Iterable<IDisposable>) {
28
+ try {
29
+ disposable?.dispose();
30
+ } catch (err) {
31
+ errors.push(err);
32
+ }
33
+ }
34
+ return undefined;
35
+ }
36
+ if (
37
+ disposables &&
38
+ !isArray(disposables) &&
39
+ !isIterable(disposables) &&
40
+ isDisposable<IDisposable>(disposables)
41
+ ) {
42
+ try {
43
+ (disposables as IDisposable).dispose();
44
+ } catch (err) {
45
+ errors.push(err);
46
+ }
47
+ return disposables as T;
48
+ }
49
+ return undefined;
50
+ }
51
+
52
+ export default class DisposableStore implements IDisposable {
53
+ private readonly _toDispose = new Set<IDisposable>();
54
+
55
+ private _isDisposed = false;
56
+
57
+ public dispose(): void {
58
+ if (this._isDisposed) {
59
+ return;
60
+ }
61
+
62
+ this._isDisposed = true;
63
+ this.clear();
64
+ }
65
+
66
+ public get isDisposed(): boolean {
67
+ return this._isDisposed;
68
+ }
69
+
70
+ public clear(): void {
71
+ if (this._toDispose.size === 0) {
72
+ return;
73
+ }
74
+
75
+ dispose(this._toDispose);
76
+
77
+ this._toDispose.clear();
78
+ }
79
+
80
+ public add<T extends IDisposable>(thing: T): T {
81
+ if (!thing) return thing;
82
+ if ((thing as unknown as DisposableStore) === this) {
83
+ throw new Error('Cannot register a disposable on itself!');
84
+ }
85
+
86
+ if (this._isDisposed) {
87
+ // ...
88
+ } else {
89
+ this._toDispose.add(thing);
90
+ }
91
+
92
+ return thing;
93
+ }
94
+
95
+ public delete<T extends IDisposable>(thing: T): void {
96
+ if (!thing) {
97
+ return;
98
+ }
99
+ if ((thing as unknown as DisposableStore) === this) {
100
+ throw new Error('Cannot dispose a disposable on itself!');
101
+ }
102
+ this._toDispose.delete(thing);
103
+ thing.dispose();
104
+ }
105
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { Disposable, toDisposable, isDisposable } from './Disposable';
2
+ export { default as DisposableStore, dispose } from './DisposableStore';
3
+ export * from './types';
@@ -0,0 +1,3 @@
1
+ export interface IDisposable {
2
+ dispose(): void;
3
+ }
@@ -0,0 +1 @@
1
+ export * from './disposable';
package/src/utils.ts ADDED
@@ -0,0 +1,19 @@
1
+ export function isObject(thing: any): thing is Object {
2
+ return typeof thing === 'object' && thing !== null;
3
+ }
4
+
5
+ export function isFunction(thing: any): thing is Function {
6
+ return typeof thing === 'function';
7
+ }
8
+
9
+ export function isArray(thing: any): thing is any[] {
10
+ return Array.isArray(thing);
11
+ }
12
+
13
+ export function isIterable<T = any>(thing: any): thing is Iterable<T> {
14
+ return (
15
+ thing &&
16
+ typeof thing === 'object' &&
17
+ typeof thing[Symbol.iterator] === 'function'
18
+ );
19
+ }