@guanghechen/disposable 1.0.2 → 2.0.0
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 +133 -0
- package/lib/cjs/index.cjs +0 -1
- package/lib/esm/index.mjs +0 -1
- package/lib/types/index.d.ts +0 -1
- package/package.json +12 -5
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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 { disposeAll, isDisposable } from '@guanghechen/disposable'
|
|
116
|
+
|
|
117
|
+
const items = [
|
|
118
|
+
new Disposable(() => console.log('Disposed 1')),
|
|
119
|
+
new Disposable(() => console.log('Disposed 2')),
|
|
120
|
+
'not disposable'
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
items.forEach(item => {
|
|
124
|
+
if (isDisposable(item)) {
|
|
125
|
+
console.log('Item is disposable')
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
disposeAll(items) // Disposes all disposable items in the array
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
[homepage]:
|
|
133
|
+
https://github.com/guanghechen/sora/tree/@guanghechen/disposable@2.0.0/packages/disposable#readme
|
package/lib/cjs/index.cjs
CHANGED
package/lib/esm/index.mjs
CHANGED
package/lib/types/index.d.ts
CHANGED
|
@@ -31,4 +31,3 @@ declare function disposeAll(disposables: Iterable<IDisposable>): void | never;
|
|
|
31
31
|
declare function isDisposable(obj: unknown): obj is IDisposable;
|
|
32
32
|
|
|
33
33
|
export { BatchDisposable, Disposable, SafeBatchHandler, disposeAll, isDisposable };
|
|
34
|
-
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/disposable",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
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.
|
|
10
|
+
"url": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.3",
|
|
11
11
|
"directory": "packages/disposable"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.
|
|
13
|
+
"homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/disposable@1.0.3/packages/disposable#readme",
|
|
14
14
|
"keywords": [
|
|
15
15
|
"disposable"
|
|
16
16
|
],
|
|
@@ -25,6 +25,13 @@
|
|
|
25
25
|
"module": "./lib/esm/index.mjs",
|
|
26
26
|
"source": "./src/index.ts",
|
|
27
27
|
"license": "MIT",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "rollup -c ../../rollup.config.mjs",
|
|
30
|
+
"clean": "rimraf lib",
|
|
31
|
+
"test": "vitest run --config ../../vitest.config.ts",
|
|
32
|
+
"test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
|
|
33
|
+
"test:update": "vitest run --config ../../vitest.config.ts -u"
|
|
34
|
+
},
|
|
28
35
|
"files": [
|
|
29
36
|
"lib/",
|
|
30
37
|
"!lib/**/*.map",
|
|
@@ -34,7 +41,7 @@
|
|
|
34
41
|
"README.md"
|
|
35
42
|
],
|
|
36
43
|
"dependencies": {
|
|
37
|
-
"@guanghechen/disposable.types": "^
|
|
44
|
+
"@guanghechen/disposable.types": "^2.0.0"
|
|
38
45
|
},
|
|
39
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "12990a720b31d50d217e2e17a6191256dc94eda6"
|
|
40
47
|
}
|