@isograph/disposable-types 0.0.0-main-79e2a3c0 → 0.0.0-main-b31e3ffc

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.
@@ -1,11 +1,11 @@
1
1
  ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.20.0","pnpm":"10.15.0"})
2
2
 
3
- > @isograph/disposable-types@0.0.0-main-79e2a3c0 compile-libs /home/runner/work/isograph/isograph/libs/isograph-disposable-types
3
+ > @isograph/disposable-types@0.0.0-main-b31e3ffc compile-libs /home/runner/work/isograph/isograph/libs/isograph-disposable-types
4
4
  > tsdown
5
5
 
6
6
  ℹ tsdown v0.20.1 powered by rolldown v1.0.0-rc.1
7
7
  ℹ config file: /home/runner/work/isograph/isograph/tsdown.config.ts
8
- (node:2441) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts?no-cache=30ac702b-12ce-449f-91ed-e242cb2824a1 is not specified and it doesn't parse as CommonJS.
8
+ (node:2646) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts?no-cache=1b15db1e-92af-4651-98b7-1c38909335c0 is not specified and it doesn't parse as CommonJS.
9
9
  Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
10
10
  To eliminate this warning, add "type": "module" to /home/runner/work/isograph/isograph/package.json.
11
11
  (Use `node --trace-warnings ...` to show where the warning was created)
@@ -1,5 +1,5 @@
1
1
  ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.20.0","pnpm":"10.15.0"})
2
2
 
3
- > @isograph/disposable-types@0.0.0-main-79e2a3c0 tsc /home/runner/work/isograph/isograph/libs/isograph-disposable-types
3
+ > @isograph/disposable-types@0.0.0-main-b31e3ffc tsc /home/runner/work/isograph/isograph/libs/isograph-disposable-types
4
4
  > tsc
5
5
 
package/dist/index.cjs CHANGED
@@ -0,0 +1,20 @@
1
+
2
+ //#region src/index.ts
3
+ function joinCleanups(...pairs) {
4
+ const items = pairs.map(([item]) => item);
5
+ const dispose = () => {
6
+ let error;
7
+ pairs.reduceRight((_, [, cleanup]) => {
8
+ try {
9
+ cleanup();
10
+ } catch (e) {
11
+ error ??= e;
12
+ }
13
+ }, void 0);
14
+ if (error !== void 0) throw error;
15
+ };
16
+ return [items, dispose];
17
+ }
18
+
19
+ //#endregion
20
+ exports.joinCleanups = joinCleanups;
package/dist/index.d.cts CHANGED
@@ -2,6 +2,8 @@
2
2
  type CleanupFn = () => void;
3
3
  type ItemCleanupPair<T> = [T, CleanupFn];
4
4
  type Factory<T> = () => ItemCleanupPair<T>;
5
+ type ItemsOf<T extends readonly ItemCleanupPair<unknown>[]> = { [I in keyof T]: T[I] extends ItemCleanupPair<infer V> ? V : never };
6
+ declare function joinCleanups<const T extends readonly ItemCleanupPair<unknown>[]>(...pairs: T): ItemCleanupPair<ItemsOf<T>>;
5
7
  //#endregion
6
- export { CleanupFn, Factory, ItemCleanupPair };
8
+ export { CleanupFn, Factory, ItemCleanupPair, joinCleanups };
7
9
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";KAAY,SAAA;AAAA,KACA,eAAA,OAAsB,CAAA,EAAG,SAAA;AAAA,KACzB,OAAA,YAAmB,eAAA,CAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";KAAY,SAAA;AAAA,KACA,eAAA,OAAsB,CAAA,EAAG,SAAA;AAAA,KACzB,OAAA,YAAmB,eAAA,CAAgB,CAAA;AAAA,KAE1C,OAAA,oBAA2B,eAAA,6BAClB,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,eAAA,YAA2B,CAAA;AAAA,iBAG1C,YAAA,0BACW,eAAA,YAAA,CAAA,GACtB,KAAA,EAAO,CAAA,GAAI,eAAA,CAAgB,OAAA,CAAQ,CAAA"}
package/dist/index.d.mts CHANGED
@@ -2,6 +2,8 @@
2
2
  type CleanupFn = () => void;
3
3
  type ItemCleanupPair<T> = [T, CleanupFn];
4
4
  type Factory<T> = () => ItemCleanupPair<T>;
5
+ type ItemsOf<T extends readonly ItemCleanupPair<unknown>[]> = { [I in keyof T]: T[I] extends ItemCleanupPair<infer V> ? V : never };
6
+ declare function joinCleanups<const T extends readonly ItemCleanupPair<unknown>[]>(...pairs: T): ItemCleanupPair<ItemsOf<T>>;
5
7
  //#endregion
6
- export { CleanupFn, Factory, ItemCleanupPair };
8
+ export { CleanupFn, Factory, ItemCleanupPair, joinCleanups };
7
9
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";KAAY,SAAA;AAAA,KACA,eAAA,OAAsB,CAAA,EAAG,SAAA;AAAA,KACzB,OAAA,YAAmB,eAAA,CAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";KAAY,SAAA;AAAA,KACA,eAAA,OAAsB,CAAA,EAAG,SAAA;AAAA,KACzB,OAAA,YAAmB,eAAA,CAAgB,CAAA;AAAA,KAE1C,OAAA,oBAA2B,eAAA,6BAClB,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,eAAA,YAA2B,CAAA;AAAA,iBAG1C,YAAA,0BACW,eAAA,YAAA,CAAA,GACtB,KAAA,EAAO,CAAA,GAAI,eAAA,CAAgB,OAAA,CAAQ,CAAA"}
package/dist/index.mjs CHANGED
@@ -1 +1,20 @@
1
- export { };
1
+ //#region src/index.ts
2
+ function joinCleanups(...pairs) {
3
+ const items = pairs.map(([item]) => item);
4
+ const dispose = () => {
5
+ let error;
6
+ pairs.reduceRight((_, [, cleanup]) => {
7
+ try {
8
+ cleanup();
9
+ } catch (e) {
10
+ error ??= e;
11
+ }
12
+ }, void 0);
13
+ if (error !== void 0) throw error;
14
+ };
15
+ return [items, dispose];
16
+ }
17
+
18
+ //#endregion
19
+ export { joinCleanups };
20
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["export type CleanupFn = () => void;\nexport type ItemCleanupPair<T> = [T, CleanupFn];\nexport type Factory<T> = () => ItemCleanupPair<T>;\n\ntype ItemsOf<T extends readonly ItemCleanupPair<unknown>[]> = {\n [I in keyof T]: T[I] extends ItemCleanupPair<infer V> ? V : never;\n};\n\nexport function joinCleanups<\n const T extends readonly ItemCleanupPair<unknown>[],\n>(...pairs: T): ItemCleanupPair<ItemsOf<T>> {\n const items = pairs.map(([item]) => item) as ItemsOf<T>;\n const dispose: CleanupFn = () => {\n let error: unknown;\n pairs.reduceRight((_, [, cleanup]) => {\n try {\n cleanup();\n } catch (e) {\n error ??= e;\n }\n return undefined;\n }, undefined);\n if (error !== undefined) {\n throw error;\n }\n };\n return [items, dispose];\n}\n"],"mappings":";AAQA,SAAgB,aAEd,GAAG,OAAuC;CAC1C,MAAM,QAAQ,MAAM,KAAK,CAAC,UAAU,KAAK;CACzC,MAAM,gBAA2B;EAC/B,IAAI;AACJ,QAAM,aAAa,GAAG,GAAG,aAAa;AACpC,OAAI;AACF,aAAS;YACF,GAAG;AACV,cAAU;;KAGX,OAAU;AACb,MAAI,UAAU,OACZ,OAAM;;AAGV,QAAO,CAAC,OAAO,QAAQ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/disposable-types",
3
- "version": "0.0.0-main-79e2a3c0",
3
+ "version": "0.0.0-main-b31e3ffc",
4
4
  "description": "Common types for disposable items",
5
5
  "homepage": "https://isograph.dev",
6
6
  "main": "./dist/index.cjs",
@@ -28,9 +28,9 @@
28
28
  "scripts": {
29
29
  "compile-libs": "tsdown",
30
30
  "watch-libs": "tsdown --watch",
31
- "test": "echo this package has no tests",
32
- "test-watch": "echo this package has no tests",
33
- "coverage": "echo this package has no coverage",
31
+ "test": "vitest run",
32
+ "test-watch": "vitest watch",
33
+ "coverage": "vitest run --coverage",
34
34
  "tsc": "tsc",
35
35
  "tsc-force": "tsc --build --clean && tsc --build --force"
36
36
  }
package/src/index.ts CHANGED
@@ -1,3 +1,28 @@
1
1
  export type CleanupFn = () => void;
2
2
  export type ItemCleanupPair<T> = [T, CleanupFn];
3
3
  export type Factory<T> = () => ItemCleanupPair<T>;
4
+
5
+ type ItemsOf<T extends readonly ItemCleanupPair<unknown>[]> = {
6
+ [I in keyof T]: T[I] extends ItemCleanupPair<infer V> ? V : never;
7
+ };
8
+
9
+ export function joinCleanups<
10
+ const T extends readonly ItemCleanupPair<unknown>[],
11
+ >(...pairs: T): ItemCleanupPair<ItemsOf<T>> {
12
+ const items = pairs.map(([item]) => item) as ItemsOf<T>;
13
+ const dispose: CleanupFn = () => {
14
+ let error: unknown;
15
+ pairs.reduceRight((_, [, cleanup]) => {
16
+ try {
17
+ cleanup();
18
+ } catch (e) {
19
+ error ??= e;
20
+ }
21
+ return undefined;
22
+ }, undefined);
23
+ if (error !== undefined) {
24
+ throw error;
25
+ }
26
+ };
27
+ return [items, dispose];
28
+ }
@@ -0,0 +1,97 @@
1
+ import { assert, describe, expect, test } from 'vitest';
2
+ import { joinCleanups } from './index';
3
+
4
+ describe('joinCleanups', () => {
5
+ test('Disposes in reverse argument order', () => {
6
+ const order: string[] = [];
7
+ const [items, dispose] = joinCleanups(
8
+ [
9
+ 'a',
10
+ () => {
11
+ order.push('a');
12
+ },
13
+ ],
14
+ [
15
+ 'b',
16
+ () => {
17
+ order.push('b');
18
+ },
19
+ ],
20
+ [
21
+ 'c',
22
+ () => {
23
+ order.push('c');
24
+ },
25
+ ],
26
+ );
27
+
28
+ expect(items).toEqual(['a', 'b', 'c']);
29
+ dispose();
30
+ expect(order).toEqual(['c', 'b', 'a']);
31
+ });
32
+
33
+ test('If a cleanup throws, remaining cleanups still run and the first error is rethrown', () => {
34
+ const order: string[] = [];
35
+ const errorB = new Error('b');
36
+ const [, dispose] = joinCleanups(
37
+ [
38
+ 'a',
39
+ () => {
40
+ order.push('a');
41
+ },
42
+ ],
43
+ [
44
+ 'b',
45
+ () => {
46
+ order.push('b');
47
+ throw errorB;
48
+ },
49
+ ],
50
+ [
51
+ 'c',
52
+ () => {
53
+ order.push('c');
54
+ },
55
+ ],
56
+ );
57
+
58
+ let thrown: unknown;
59
+ try {
60
+ dispose();
61
+ } catch (e) {
62
+ thrown = e;
63
+ }
64
+
65
+ assert(thrown === errorB);
66
+ expect(order).toEqual(['c', 'b', 'a']);
67
+ });
68
+
69
+ test('If two cleanups throw, the first error in dispose order is rethrown', () => {
70
+ const errorA = new Error('a');
71
+ const errorC = new Error('c');
72
+ const [, dispose] = joinCleanups(
73
+ [
74
+ 'a',
75
+ () => {
76
+ throw errorA;
77
+ },
78
+ ],
79
+ ['b', () => {}],
80
+ [
81
+ 'c',
82
+ () => {
83
+ throw errorC;
84
+ },
85
+ ],
86
+ );
87
+
88
+ let thrown: unknown;
89
+ try {
90
+ dispose();
91
+ } catch (e) {
92
+ thrown = e;
93
+ }
94
+
95
+ assert(thrown === errorC);
96
+ });
97
+ });