@monstermann/dsp 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Michael Ostermann
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,9 @@
1
+ <div align="center">
2
+
3
+ <h1>dsp</h1>
4
+
5
+ **Small & fast disposables.**
6
+
7
+ [Documentation](https://MichaelOstermann.github.io/dsp)
8
+
9
+ </div>
@@ -0,0 +1,64 @@
1
+ //#region src/Dsp/symbol.d.ts
2
+ declare const symbol: unique symbol;
3
+ //#endregion
4
+ //#region src/Dsp/types.d.ts
5
+ interface Dsp$1 {
6
+ [symbol]: boolean;
7
+ dsps: DspLink$1 | undefined;
8
+ vals: DspLink$1 | undefined;
9
+ }
10
+ interface DspLink$1 {
11
+ disposer: Dsp$1;
12
+ nextDsp: DspLink$1 | undefined;
13
+ nextVal: DspLink$1 | undefined;
14
+ prevDsp: DspLink$1 | undefined;
15
+ prevVal: DspLink$1 | undefined;
16
+ val: (() => void) | Dsp$1;
17
+ }
18
+ //#endregion
19
+ //#region src/Dsp/add.d.ts
20
+ declare function add(target: Dsp$1, value: (() => void) | Dsp$1): DspLink$1 | undefined;
21
+ //#endregion
22
+ //#region src/Dsp/create.d.ts
23
+ declare function create(): Dsp$1;
24
+ //#endregion
25
+ //#region src/Dsp/dispose.d.ts
26
+ declare function dispose(target: Dsp$1): void;
27
+ //#endregion
28
+ //#region src/Dsp/find.d.ts
29
+ declare function find(target: Dsp$1, value: (() => void) | Dsp$1): DspLink$1 | undefined;
30
+ //#endregion
31
+ //#region src/Dsp/includes.d.ts
32
+ declare function includes(target: Dsp$1, value: (() => void) | Dsp$1): boolean;
33
+ //#endregion
34
+ //#region src/Dsp/isDisposed.d.ts
35
+ declare function isDisposed(target: Dsp$1): boolean;
36
+ //#endregion
37
+ //#region src/Dsp/isDsp.d.ts
38
+ declare function isDsp(value: unknown): value is Dsp$1;
39
+ //#endregion
40
+ //#region src/Dsp/remove.d.ts
41
+ declare function remove(disposer: Dsp$1, value: (() => void) | Dsp$1): void;
42
+ //#endregion
43
+ //#region src/Dsp/unlink.d.ts
44
+ declare function unlink(link: DspLink$1 | undefined): void;
45
+ //#endregion
46
+ //#region src/Dsp/index.d.ts
47
+ interface Dsp {
48
+ [symbol]: boolean;
49
+ dsps: DspLink | undefined;
50
+ vals: DspLink | undefined;
51
+ }
52
+ interface DspLink {
53
+ disposer: Dsp;
54
+ nextDsp: DspLink | undefined;
55
+ nextVal: DspLink | undefined;
56
+ prevDsp: DspLink | undefined;
57
+ prevVal: DspLink | undefined;
58
+ val: (() => void) | Dsp;
59
+ }
60
+ declare namespace Dsp {
61
+ export { add, create, dispose, find, includes, isDisposed, isDsp, remove, symbol, unlink };
62
+ }
63
+ //#endregion
64
+ export { Dsp, DspLink };
package/dist/index.js ADDED
@@ -0,0 +1,165 @@
1
+ //#region src/Dsp/symbol.ts
2
+ const symbol = Symbol("Dsp");
3
+
4
+ //#endregion
5
+ //#region src/Dsp/internals.ts
6
+ function createLink(disposer, val) {
7
+ return {
8
+ disposer,
9
+ nextDsp: void 0,
10
+ nextVal: void 0,
11
+ prevDsp: void 0,
12
+ prevVal: void 0,
13
+ val
14
+ };
15
+ }
16
+ function linkVal(disposer, link) {
17
+ link.prevVal = disposer.vals;
18
+ if (disposer.vals) disposer.vals.nextVal = link;
19
+ disposer.vals = link;
20
+ return link;
21
+ }
22
+ function linkDsp(disposer, link) {
23
+ link.prevDsp = disposer.dsps;
24
+ if (disposer.dsps) disposer.dsps.nextDsp = link;
25
+ disposer.dsps = link;
26
+ return link;
27
+ }
28
+ function unlinkVal(link) {
29
+ if (link.prevVal) link.prevVal.nextVal = link.nextVal;
30
+ if (link.nextVal) link.nextVal.prevVal = link.prevVal;
31
+ if (link.disposer.vals === link) link.disposer.vals = link.prevVal;
32
+ link.nextVal = link.prevVal = void 0;
33
+ }
34
+ function unlinkDsp(link) {
35
+ if (symbol in link.val) {
36
+ if (link.prevDsp) link.prevDsp.nextDsp = link.nextDsp;
37
+ if (link.nextDsp) link.nextDsp.prevDsp = link.prevDsp;
38
+ if (link.val.dsps === link) link.val.dsps = link.prevDsp;
39
+ link.nextDsp = link.prevDsp = void 0;
40
+ }
41
+ }
42
+
43
+ //#endregion
44
+ //#region src/Dsp/dispose.ts
45
+ function dispose(target) {
46
+ let errors;
47
+ let stack = {
48
+ dsp: target,
49
+ prev: void 0
50
+ };
51
+ let dsp = target;
52
+ while (dsp) {
53
+ dsp[symbol] = true;
54
+ while (dsp.dsps) {
55
+ unlinkVal(dsp.dsps);
56
+ dsp.dsps = dsp.dsps.prevDsp;
57
+ }
58
+ while (dsp.vals) {
59
+ const val = dsp.vals.val;
60
+ dsp.vals = dsp.vals.prevVal;
61
+ if (symbol in val) {
62
+ stack = {
63
+ dsp: val,
64
+ prev: stack
65
+ };
66
+ dsp = val;
67
+ } else try {
68
+ val();
69
+ } catch (err) {
70
+ errors ??= [];
71
+ errors.push(err);
72
+ }
73
+ }
74
+ stack = stack?.prev;
75
+ dsp = stack?.dsp;
76
+ }
77
+ if (!errors) return;
78
+ if (errors.length === 1) throw errors[0];
79
+ throw new AggregateError(errors, "DisposeError");
80
+ }
81
+
82
+ //#endregion
83
+ //#region src/Dsp/add.ts
84
+ function add(a, b) {
85
+ if (symbol in b) {
86
+ if (a === b) return;
87
+ if (b[symbol]) return;
88
+ if (a[symbol]) return dispose(b);
89
+ return linkDsp(b, linkVal(a, createLink(a, b)));
90
+ }
91
+ if (a[symbol]) return b();
92
+ return linkVal(a, createLink(a, b));
93
+ }
94
+
95
+ //#endregion
96
+ //#region src/Dsp/create.ts
97
+ function create() {
98
+ return {
99
+ dsps: void 0,
100
+ [symbol]: false,
101
+ vals: void 0
102
+ };
103
+ }
104
+
105
+ //#endregion
106
+ //#region src/Dsp/find.ts
107
+ function find(target, value) {
108
+ let link = target.vals;
109
+ while (link) {
110
+ if (link.val === value) return link;
111
+ link = link.prevVal;
112
+ }
113
+ return link;
114
+ }
115
+
116
+ //#endregion
117
+ //#region src/Dsp/includes.ts
118
+ function includes(target, value) {
119
+ return !!find(target, value);
120
+ }
121
+
122
+ //#endregion
123
+ //#region src/Dsp/isDisposed.ts
124
+ function isDisposed(target) {
125
+ return target[symbol];
126
+ }
127
+
128
+ //#endregion
129
+ //#region src/Dsp/isDsp.ts
130
+ function isDsp(value) {
131
+ return value != null && typeof value === "object" && symbol in value;
132
+ }
133
+
134
+ //#endregion
135
+ //#region src/Dsp/unlink.ts
136
+ function unlink(link) {
137
+ if (!link) return;
138
+ unlinkVal(link);
139
+ unlinkDsp(link);
140
+ }
141
+
142
+ //#endregion
143
+ //#region src/Dsp/remove.ts
144
+ function remove(disposer, value) {
145
+ const link = find(disposer, value);
146
+ if (link) unlink(link);
147
+ }
148
+
149
+ //#endregion
150
+ //#region src/Dsp/index.js
151
+ const Dsp = {
152
+ add,
153
+ create,
154
+ dispose,
155
+ find,
156
+ includes,
157
+ isDisposed,
158
+ isDsp,
159
+ remove,
160
+ symbol,
161
+ unlink
162
+ };
163
+
164
+ //#endregion
165
+ export { Dsp };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@monstermann/dsp",
3
+ "type": "module",
4
+ "version": "0.2.0",
5
+ "description": "Small & fast disposables.",
6
+ "author": "Michael Ostermann <michaelostermann@me.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://MichaelOstermann.github.io/dsp",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/MichaelOstermann/dsp.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/MichaelOstermann/dsp/issues"
15
+ },
16
+ "keywords": [],
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ },
23
+ "./*": "./dist/*"
24
+ },
25
+ "main": "./dist/index.js",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "bundle": "tsdown",
33
+ "release": "bun publish --access public"
34
+ },
35
+ "dependencies": {}
36
+ }