@guanghechen/disposable 1.0.3 → 2.0.1
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/LICENSE +21 -0
- package/README.md +140 -0
- package/lib/cjs/index.cjs +0 -8
- package/lib/esm/index.mjs +0 -2
- package/lib/types/index.d.ts +1 -2
- package/package.json +18 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
<header>
|
|
2
|
+
<h1 align="center">
|
|
3
|
+
<a href="https://github.com/guanghechen/sora/tree/@guanghechen/disposable@2.0.0/packages/disposable#readme">@guanghechen/disposable</a>
|
|
4
|
+
</h1>
|
|
5
|
+
<div align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@guanghechen/disposable">
|
|
7
|
+
<img
|
|
8
|
+
alt="Npm Version"
|
|
9
|
+
src="https://img.shields.io/npm/v/@guanghechen/disposable.svg"
|
|
10
|
+
/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@guanghechen/disposable">
|
|
13
|
+
<img
|
|
14
|
+
alt="Npm Download"
|
|
15
|
+
src="https://img.shields.io/npm/dm/@guanghechen/disposable.svg"
|
|
16
|
+
/>
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://www.npmjs.com/package/@guanghechen/disposable">
|
|
19
|
+
<img
|
|
20
|
+
alt="Npm License"
|
|
21
|
+
src="https://img.shields.io/npm/l/@guanghechen/disposable.svg"
|
|
22
|
+
/>
|
|
23
|
+
</a>
|
|
24
|
+
<a href="#install">
|
|
25
|
+
<img
|
|
26
|
+
alt="Module Formats: cjs, esm"
|
|
27
|
+
src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
|
|
28
|
+
/>
|
|
29
|
+
</a>
|
|
30
|
+
<a href="https://github.com/nodejs/node">
|
|
31
|
+
<img
|
|
32
|
+
alt="Node.js Version"
|
|
33
|
+
src="https://img.shields.io/node/v/@guanghechen/disposable"
|
|
34
|
+
/>
|
|
35
|
+
</a>
|
|
36
|
+
<a href="https://github.com/facebook/jest">
|
|
37
|
+
<img
|
|
38
|
+
alt="Tested with Jest"
|
|
39
|
+
src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
|
|
40
|
+
/>
|
|
41
|
+
</a>
|
|
42
|
+
<a href="https://github.com/prettier/prettier">
|
|
43
|
+
<img
|
|
44
|
+
alt="Code Style: prettier"
|
|
45
|
+
src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
|
|
46
|
+
/>
|
|
47
|
+
</a>
|
|
48
|
+
</div>
|
|
49
|
+
</header>
|
|
50
|
+
<br/>
|
|
51
|
+
|
|
52
|
+
Disposable pattern implementation for resource cleanup and memory management.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
- npm
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install --save @guanghechen/disposable
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- yarn
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
yarn add @guanghechen/disposable
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Usage
|
|
69
|
+
|
|
70
|
+
| Name | Description |
|
|
71
|
+
| :----------------: | :-------------------------------------------------------: |
|
|
72
|
+
| `Disposable` | Single disposable resource with cleanup callback |
|
|
73
|
+
| `BatchDisposable` | Batch disposable for managing multiple resources |
|
|
74
|
+
| `disposeAll` | Utility to dispose all disposable items |
|
|
75
|
+
| `isDisposable` | Type guard to check if object is disposable |
|
|
76
|
+
|
|
77
|
+
## Example
|
|
78
|
+
|
|
79
|
+
- Basic disposable:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { Disposable } from '@guanghechen/disposable'
|
|
83
|
+
|
|
84
|
+
const resource = new Disposable(() => {
|
|
85
|
+
console.log('Resource cleaned up!')
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
console.log(resource.disposed) // false
|
|
89
|
+
resource.dispose()
|
|
90
|
+
console.log(resource.disposed) // true
|
|
91
|
+
// Output: "Resource cleaned up!"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- Batch disposable:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { BatchDisposable, Disposable } from '@guanghechen/disposable'
|
|
98
|
+
|
|
99
|
+
const batch = new BatchDisposable()
|
|
100
|
+
|
|
101
|
+
const resource1 = new Disposable(() => console.log('Resource 1 disposed'))
|
|
102
|
+
const resource2 = new Disposable(() => console.log('Resource 2 disposed'))
|
|
103
|
+
|
|
104
|
+
batch.registerDisposable(resource1)
|
|
105
|
+
batch.registerDisposable(resource2)
|
|
106
|
+
|
|
107
|
+
batch.dispose()
|
|
108
|
+
// Output: "Resource 1 disposed"
|
|
109
|
+
// Output: "Resource 2 disposed"
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Utility functions:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { Disposable, disposeAll, isDisposable } from '@guanghechen/disposable'
|
|
116
|
+
|
|
117
|
+
const items: unknown[] = [
|
|
118
|
+
new Disposable(() => console.log('Disposed 1')),
|
|
119
|
+
new Disposable(() => console.log('Disposed 2')),
|
|
120
|
+
'not disposable'
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
// Check if an item is disposable
|
|
124
|
+
items.forEach(item => {
|
|
125
|
+
if (isDisposable(item)) {
|
|
126
|
+
console.log('Item is disposable')
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
// Filter and dispose only disposable items
|
|
131
|
+
const disposables = items.filter(isDisposable)
|
|
132
|
+
disposeAll(disposables) // Disposes all disposable items
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Reference
|
|
136
|
+
|
|
137
|
+
- [homepage][homepage]
|
|
138
|
+
|
|
139
|
+
[homepage]:
|
|
140
|
+
https://github.com/guanghechen/sora/tree/@guanghechen/disposable@2.0.0/packages/disposable#readme
|
package/lib/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var disposable_types = require('@guanghechen/disposable.types');
|
|
4
|
-
|
|
5
3
|
class SafeBatchHandler {
|
|
6
4
|
_errors;
|
|
7
5
|
_summary;
|
|
@@ -103,9 +101,3 @@ exports.Disposable = Disposable;
|
|
|
103
101
|
exports.SafeBatchHandler = SafeBatchHandler;
|
|
104
102
|
exports.disposeAll = disposeAll;
|
|
105
103
|
exports.isDisposable = isDisposable;
|
|
106
|
-
Object.keys(disposable_types).forEach(function (k) {
|
|
107
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
108
|
-
enumerable: true,
|
|
109
|
-
get: function () { return disposable_types[k]; }
|
|
110
|
-
});
|
|
111
|
-
});
|
package/lib/esm/index.mjs
CHANGED
package/lib/types/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { IBatchDisposable, IDisposable } from '@guanghechen/
|
|
2
|
-
export * from '@guanghechen/disposable.types';
|
|
1
|
+
import { IBatchDisposable, IDisposable } from '@guanghechen/types';
|
|
3
2
|
|
|
4
3
|
declare class BatchDisposable implements IBatchDisposable {
|
|
5
4
|
protected _disposed: boolean;
|
package/package.json
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/disposable",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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@
|
|
10
|
+
"url": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@2.0.0",
|
|
11
11
|
"directory": "packages/disposable"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@
|
|
13
|
+
"homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@2.0.0/packages/disposable#readme",
|
|
14
14
|
"keywords": [
|
|
15
15
|
"disposable"
|
|
16
16
|
],
|
|
17
17
|
"type": "module",
|
|
18
18
|
"exports": {
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./lib/types/index.d.ts",
|
|
21
|
+
"source": "./src/index.ts",
|
|
22
|
+
"import": "./lib/esm/index.mjs",
|
|
23
|
+
"require": "./lib/cjs/index.cjs"
|
|
24
|
+
}
|
|
22
25
|
},
|
|
23
26
|
"types": "./lib/types/index.d.ts",
|
|
24
27
|
"main": "./lib/cjs/index.cjs",
|
|
@@ -34,7 +37,13 @@
|
|
|
34
37
|
"README.md"
|
|
35
38
|
],
|
|
36
39
|
"dependencies": {
|
|
37
|
-
"@guanghechen/
|
|
40
|
+
"@guanghechen/types": "^2.0.1"
|
|
38
41
|
},
|
|
39
|
-
"
|
|
40
|
-
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "rollup -c ../../rollup.config.mjs",
|
|
44
|
+
"clean": "rimraf lib",
|
|
45
|
+
"test": "vitest run --config ../../vitest.config.ts",
|
|
46
|
+
"test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
|
|
47
|
+
"test:update": "vitest run --config ../../vitest.config.ts -u"
|
|
48
|
+
}
|
|
49
|
+
}
|