@furystack/utils 7.0.2 → 8.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.
Files changed (103) hide show
  1. package/README.md +3 -3
  2. package/esm/debounce.d.ts.map +1 -1
  3. package/esm/deep-merge.d.ts.map +1 -1
  4. package/esm/deep-merge.spec.js +1 -1
  5. package/esm/deep-merge.spec.js.map +1 -1
  6. package/esm/event-hub.d.ts +1 -2
  7. package/esm/event-hub.d.ts.map +1 -1
  8. package/esm/event-hub.js +2 -2
  9. package/esm/event-hub.js.map +1 -1
  10. package/esm/event-hub.spec.js +4 -4
  11. package/esm/event-hub.spec.js.map +1 -1
  12. package/esm/index.d.ts +6 -4
  13. package/esm/index.d.ts.map +1 -1
  14. package/esm/index.js +6 -4
  15. package/esm/index.js.map +1 -1
  16. package/esm/is-async-disposable.d.ts +7 -0
  17. package/esm/is-async-disposable.d.ts.map +1 -0
  18. package/esm/is-async-disposable.js +9 -0
  19. package/esm/is-async-disposable.js.map +1 -0
  20. package/esm/is-async-disposable.spec.d.ts +2 -0
  21. package/esm/is-async-disposable.spec.d.ts.map +1 -0
  22. package/esm/is-async-disposable.spec.js +21 -0
  23. package/esm/is-async-disposable.spec.js.map +1 -0
  24. package/esm/is-disposable.d.ts +7 -0
  25. package/esm/is-disposable.d.ts.map +1 -0
  26. package/esm/is-disposable.js +9 -0
  27. package/esm/is-disposable.js.map +1 -0
  28. package/esm/is-disposable.spec.d.ts +2 -0
  29. package/esm/is-disposable.spec.d.ts.map +1 -0
  30. package/esm/is-disposable.spec.js +21 -0
  31. package/esm/is-disposable.spec.js.map +1 -0
  32. package/esm/observable-value.d.ts +4 -5
  33. package/esm/observable-value.d.ts.map +1 -1
  34. package/esm/observable-value.js +3 -3
  35. package/esm/observable-value.js.map +1 -1
  36. package/esm/observable-value.spec.d.ts.map +1 -1
  37. package/esm/observable-value.spec.js +10 -9
  38. package/esm/observable-value.spec.js.map +1 -1
  39. package/esm/path-helper.js +1 -1
  40. package/esm/path-helper.js.map +1 -1
  41. package/esm/path-helper.spec.d.ts.map +1 -1
  42. package/esm/path-helper.spec.js +9 -1
  43. package/esm/path-helper.spec.js.map +1 -1
  44. package/esm/sort-by.d.ts +1 -1
  45. package/esm/sort-by.d.ts.map +1 -1
  46. package/esm/tuple.d.ts.map +1 -1
  47. package/esm/using-async.d.ts +8 -0
  48. package/esm/using-async.d.ts.map +1 -0
  49. package/esm/using-async.js +22 -0
  50. package/esm/using-async.js.map +1 -0
  51. package/esm/using-async.spec.d.ts +17 -0
  52. package/esm/using-async.spec.d.ts.map +1 -0
  53. package/esm/using-async.spec.js +84 -0
  54. package/esm/using-async.spec.js.map +1 -0
  55. package/esm/using.d.ts +8 -0
  56. package/esm/using.d.ts.map +1 -0
  57. package/esm/using.js +15 -0
  58. package/esm/using.js.map +1 -0
  59. package/esm/{disposable.spec.d.ts → using.spec.d.ts} +2 -7
  60. package/esm/using.spec.d.ts.map +1 -0
  61. package/esm/using.spec.js +62 -0
  62. package/esm/using.spec.js.map +1 -0
  63. package/esm/value-observer.d.ts +3 -4
  64. package/esm/value-observer.d.ts.map +1 -1
  65. package/esm/value-observer.js +3 -3
  66. package/esm/value-observer.js.map +1 -1
  67. package/package.json +3 -3
  68. package/src/deep-merge.spec.ts +2 -2
  69. package/src/event-hub.spec.ts +4 -4
  70. package/src/event-hub.ts +2 -4
  71. package/src/index.ts +6 -4
  72. package/src/is-async-disposable.spec.ts +23 -0
  73. package/src/is-async-disposable.ts +8 -0
  74. package/src/is-disposable.spec.ts +23 -0
  75. package/src/is-disposable.ts +8 -0
  76. package/src/observable-value.spec.ts +11 -9
  77. package/src/observable-value.ts +3 -4
  78. package/src/path-helper.spec.ts +10 -1
  79. package/src/path-helper.ts +1 -1
  80. package/src/using-async.spec.ts +93 -0
  81. package/src/using-async.ts +24 -0
  82. package/src/using.spec.ts +67 -0
  83. package/src/using.ts +13 -0
  84. package/src/value-observer.ts +3 -4
  85. package/esm/disposable.d.ts +0 -49
  86. package/esm/disposable.d.ts.map +0 -1
  87. package/esm/disposable.js +0 -56
  88. package/esm/disposable.js.map +0 -1
  89. package/esm/disposable.spec.d.ts.map +0 -1
  90. package/esm/disposable.spec.js +0 -118
  91. package/esm/disposable.spec.js.map +0 -1
  92. package/esm/trace.d.ts +0 -119
  93. package/esm/trace.d.ts.map +0 -1
  94. package/esm/trace.js +0 -139
  95. package/esm/trace.js.map +0 -1
  96. package/esm/trace.spec.d.ts +0 -2
  97. package/esm/trace.spec.d.ts.map +0 -1
  98. package/esm/trace.spec.js +0 -184
  99. package/esm/trace.spec.js.map +0 -1
  100. package/src/disposable.spec.ts +0 -130
  101. package/src/disposable.ts +0 -69
  102. package/src/trace.spec.ts +0 -200
  103. package/src/trace.ts +0 -265
@@ -0,0 +1,22 @@
1
+ import { isAsyncDisposable } from './is-async-disposable.js';
2
+ import { isDisposable } from './is-disposable.js';
3
+ /**
4
+ * Method that accepts an IDisposable resource that will be disposed after the callback
5
+ * @param resource The resource that is used in the callback and will be disposed afterwards
6
+ * @param callback The callback that will be executed asynchrounously before the resource will be disposed
7
+ * @returns A promise that will be resolved with a return value after the resource is disposed
8
+ */
9
+ export const usingAsync = async (resource, callback) => {
10
+ try {
11
+ return await callback(resource);
12
+ }
13
+ finally {
14
+ if (isAsyncDisposable(resource)) {
15
+ await resource[Symbol.asyncDispose]();
16
+ }
17
+ if (isDisposable(resource)) {
18
+ resource[Symbol.dispose]();
19
+ }
20
+ }
21
+ };
22
+ //# sourceMappingURL=using-async.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using-async.js","sourceRoot":"","sources":["../src/using-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,QAAW,EACX,QAAqC,EACrC,EAAE;IACF,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAA;IACjC,CAAC;YAAS,CAAC;QACT,IAAI,iBAAiB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAChC,MAAM,QAAQ,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAA;QACvC,CAAC;QACD,IAAI,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3B,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAA;QAC5B,CAAC;IACH,CAAC;AACH,CAAC,CAAA"}
@@ -0,0 +1,17 @@
1
+ export declare class MockAsyncDisposable implements AsyncDisposable {
2
+ private disposed;
3
+ isDisposed: () => boolean;
4
+ /**
5
+ * Disposes the MockDisposable instance, calls the dispose callback
6
+ */
7
+ [Symbol.asyncDispose](): Promise<void>;
8
+ /**
9
+ * Mock to throw an error
10
+ */
11
+ whooops(): void;
12
+ /**
13
+ * Defines the callback that will be called on dispose
14
+ */
15
+ disposeCallback: () => void;
16
+ }
17
+ //# sourceMappingURL=using-async.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using-async.spec.d.ts","sourceRoot":"","sources":["../src/using-async.spec.ts"],"names":[],"mappings":"AAIA,qBAAa,mBAAoB,YAAW,eAAe;IACzD,OAAO,CAAC,QAAQ,CAAQ;IACjB,UAAU,gBAAsB;IACvC;;OAEG;IACU,CAAC,MAAM,CAAC,YAAY,CAAC;IAKlC;;OAEG;IACI,OAAO;IAId;;OAEG;IACI,eAAe,EAAG,MAAM,IAAI,CAAA;CACpC"}
@@ -0,0 +1,84 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { usingAsync } from './using-async.js';
3
+ import { MockDisposable } from './using.spec.js';
4
+ export class MockAsyncDisposable {
5
+ disposed = false;
6
+ isDisposed = () => this.disposed;
7
+ /**
8
+ * Disposes the MockDisposable instance, calls the dispose callback
9
+ */
10
+ async [Symbol.asyncDispose]() {
11
+ this.disposed = true;
12
+ this.disposeCallback && this.disposeCallback();
13
+ }
14
+ /**
15
+ * Mock to throw an error
16
+ */
17
+ whooops() {
18
+ throw Error('Whooops');
19
+ }
20
+ /**
21
+ * Defines the callback that will be called on dispose
22
+ */
23
+ disposeCallback;
24
+ }
25
+ /**
26
+ * Unit tests for disposables
27
+ */
28
+ describe('usingAsync()', () => {
29
+ it('dispose should be called with usingAsync()', async () => {
30
+ const callbackMethod = vi.fn();
31
+ await usingAsync(new MockAsyncDisposable(), async (d) => {
32
+ d.disposeCallback = () => {
33
+ callbackMethod();
34
+ };
35
+ return new Promise((resolve) => {
36
+ setTimeout(resolve, 1);
37
+ });
38
+ });
39
+ expect(callbackMethod).toBeCalled();
40
+ });
41
+ it('dispose should be called when async fails', async () => {
42
+ const callbackMethod = vi.fn();
43
+ try {
44
+ await usingAsync(new MockAsyncDisposable(), async (d) => {
45
+ d.disposeCallback = () => {
46
+ callbackMethod();
47
+ };
48
+ return new Promise((_resolve, reject) => {
49
+ setTimeout(reject, 1);
50
+ });
51
+ });
52
+ }
53
+ catch (error) {
54
+ /** ignore */
55
+ }
56
+ expect(callbackMethod).toBeCalled();
57
+ });
58
+ it('should await dispose for asyncs with usingAsync()', async () => {
59
+ class AsyncDispose {
60
+ /** flag */
61
+ isDisposed = false;
62
+ /** set isDisposed with a timeout */
63
+ async [Symbol.asyncDispose]() {
64
+ await new Promise((resolve) => setTimeout(() => {
65
+ this.isDisposed = true;
66
+ resolve();
67
+ }, 10));
68
+ }
69
+ }
70
+ const asyncDispose = new AsyncDispose();
71
+ await usingAsync(asyncDispose, async () => {
72
+ /** */
73
+ });
74
+ expect(asyncDispose.isDisposed).toBe(true);
75
+ });
76
+ it('Should dispose a non-async disposable object as well', async () => {
77
+ const createdResource = await usingAsync(new MockDisposable(), async (mock) => {
78
+ expect(mock).toBeInstanceOf(MockDisposable);
79
+ return mock;
80
+ });
81
+ expect(createdResource.isDisposed()).toBe(true);
82
+ });
83
+ });
84
+ //# sourceM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,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;QACpE,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,IAAI,cAAc,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YAC5E,MAAM,CAAC,IAAI,CAAC,CAAC,cAAc,CAAC,cAAc,CAAC,CAAA;YAC3C,OAAO,IAAI,CAAA;QACb,CAAC,CAAC,CAAA;QAEF,MAAM,CAAC,eAAe,CAAC,UAAU,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACjD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/esm/using.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Method that accepts an IDisposable resource that will be disposed after the callback
3
+ * @param resource The resource that is used in the callback and will be disposed afterwards
4
+ * @param callback The callback that will be executed synchrounously before the resource will be disposed
5
+ * @returns the value that will be returned by the callback method
6
+ */
7
+ export declare const using: <T extends Disposable, TReturns>(resource: T, callback: (r: T) => TReturns) => TReturns;
8
+ //# sourceMappingURL=using.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using.d.ts","sourceRoot":"","sources":["../src/using.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,UAAU,EAAE,QAAQ,YAAY,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,KAAK,QAAQ,aAM9F,CAAA"}
package/esm/using.js ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Method that accepts an IDisposable resource that will be disposed after the callback
3
+ * @param resource The resource that is used in the callback and will be disposed afterwards
4
+ * @param callback The callback that will be executed synchrounously before the resource will be disposed
5
+ * @returns the value that will be returned by the callback method
6
+ */
7
+ export const using = (resource, callback) => {
8
+ try {
9
+ return callback(resource);
10
+ }
11
+ finally {
12
+ resource[Symbol.dispose]();
13
+ }
14
+ };
15
+ //# sourceMappingURL=using.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using.js","sourceRoot":"","sources":["../src/using.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAiC,QAAW,EAAE,QAA4B,EAAE,EAAE;IACjG,IAAI,CAAC;QACH,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC3B,CAAC;YAAS,CAAC;QACT,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAA;IAC5B,CAAC;AACH,CAAC,CAAA"}
@@ -1,11 +1,10 @@
1
- import type { Disposable } from './disposable.js';
2
1
  export declare class MockDisposable implements Disposable {
3
2
  private disposed;
4
3
  isDisposed: () => boolean;
5
4
  /**
6
5
  * Disposes the MockDisposable instance, calls the dispose callback
7
6
  */
8
- dispose: () => void;
7
+ [Symbol.dispose](): void;
9
8
  /**
10
9
  * Mock to throw an error
11
10
  */
@@ -15,8 +14,4 @@ export declare class MockDisposable implements Disposable {
15
14
  */
16
15
  disposeCallback: () => void;
17
16
  }
18
- /**
19
- * Unit tests for disposables
20
- */
21
- export declare const disposableTests: import("vitest").SuiteCollector<{}>;
22
- //# sourceMappingURL=disposable.spec.d.ts.map
17
+ //# sourceMappingURL=using.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using.spec.d.ts","sourceRoot":"","sources":["../src/using.spec.ts"],"names":[],"mappings":"AAGA,qBAAa,cAAe,YAAW,UAAU;IAC/C,OAAO,CAAC,QAAQ,CAAQ;IACjB,UAAU,gBAAsB;IACvC;;OAEG;IACI,CAAC,MAAM,CAAC,OAAO,CAAC;IAKvB;;OAEG;IACI,OAAO;IAId;;OAEG;IACI,eAAe,EAAG,MAAM,IAAI,CAAA;CACpC"}
@@ -0,0 +1,62 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { using } from './using.js';
3
+ export class MockDisposable {
4
+ disposed = false;
5
+ isDisposed = () => this.disposed;
6
+ /**
7
+ * Disposes the MockDisposable instance, calls the dispose callback
8
+ */
9
+ [Symbol.dispose]() {
10
+ this.disposed = true;
11
+ this.disposeCallback && this.disposeCallback();
12
+ }
13
+ /**
14
+ * Mock to throw an error
15
+ */
16
+ whooops() {
17
+ throw Error('Whooops');
18
+ }
19
+ /**
20
+ * Defines the callback that will be called on dispose
21
+ */
22
+ disposeCallback;
23
+ }
24
+ describe('Using', () => {
25
+ it('Can be constructed', () => {
26
+ using(new MockDisposable(), (d) => {
27
+ expect(d).toBeInstanceOf(MockDisposable);
28
+ });
29
+ });
30
+ it('Should return a value from a callback', () => {
31
+ const returned = using(new MockDisposable(), () => {
32
+ return 1;
33
+ });
34
+ expect(returned).toBe(1);
35
+ });
36
+ describe('isDisposed', () => {
37
+ it('should return a correct value before and after disposition', () => {
38
+ const d = new MockDisposable();
39
+ expect(d.isDisposed()).toBe(false);
40
+ d[Symbol.dispose]();
41
+ expect(d.isDisposed()).toBe(true);
42
+ });
43
+ });
44
+ describe('dispose()', () => {
45
+ it('should be called on error', () => {
46
+ const callbackMethod = vi.fn();
47
+ try {
48
+ using(new MockDisposable(), (d) => {
49
+ d.disposeCallback = () => {
50
+ callbackMethod();
51
+ };
52
+ d.whooops();
53
+ });
54
+ }
55
+ catch {
56
+ /** ignore */
57
+ }
58
+ expect(callbackMethod).toBeCalled();
59
+ });
60
+ });
61
+ });
62
+ //# sourceMappingURL=using.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"using.spec.js","sourceRoot":"","sources":["../src/using.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,MAAM,OAAO,cAAc;IACjB,QAAQ,GAAG,KAAK,CAAA;IACjB,UAAU,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAA;IACvC;;OAEG;IACI,CAAC,MA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B,EAAE,CAAC,2BAA2B,EAAE,GAAG,EAAE;YACnC,MAAM,cAAc,GAAG,EAAE,CAAC,EAAE,EAAE,CAAA;YAC9B,IAAI,CAAC;gBACH,KAAK,CAAC,IAAI,cAAc,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE;oBAChC,CAAC,CAAC,eAAe,GAAG,GAAG,EAAE;wBACvB,cAAc,EAAE,CAAA;oBAClB,CAAC,CAAA;oBACD,CAAC,CAAC,OAAO,EAAE,CAAA;gBACb,CAAC,CAAC,CAAA;YACJ,CAAC;YAAC,MAAM,CAAC;gBACP,aAAa;YACf,CAAC;YACD,MAAM,CAAC,cAAc,CAAC,CAAC,UAAU,EAAE,CAAA;QACrC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,4 +1,3 @@
1
- import type { Disposable } from './disposable.js';
2
1
  import type { ObservableValue, ValueChangeCallback } from './observable-value.js';
3
2
  export type ValueObserverOptions<T> = {
4
3
  filter?: (nextValue: T, lastValue: T) => boolean;
@@ -19,9 +18,9 @@ export type ValueObserverOptions<T> = {
19
18
  * // To update the value
20
19
  * observableValue.setValue(Math.random());
21
20
  * // if you want to dispose a single observer
22
- * observer.dispose();
21
+ * observer[Symbol.dispose]();
23
22
  * // if you want to dispose the whole observableValue with all of its observers:
24
- * observableValue.dispose();
23
+ * observableValue[Symbol.dispose]();
25
24
  * ```
26
25
  * @param T This type parameter is the value type to observe
27
26
  */
@@ -32,7 +31,7 @@ export declare class ValueObserver<T> implements Disposable {
32
31
  /**
33
32
  * Disposes the ValueObserver instance. Unsubscribes from the observable
34
33
  */
35
- dispose(): void;
34
+ [Symbol.dispose](): void;
36
35
  /**
37
36
  * @constructs ValueObserver<T> the ValueObserver instance
38
37
  * @param observable The related Observable object
@@ -1 +1 @@
1
- {"version":3,"file":"value-observer.d.ts","sourceRoot":"","sources":["../src/value-observer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UA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C,EACvC,QAAQ,EAAE,mBAAmB,CAAC,CAAC,CAAC,EACvB,OAAO,CAAC,EAAE,oBAAoB,CAAC,CAAC,CAAC,YAAA;CAEpD"}
@@ -14,9 +14,9 @@
14
14
  * // To update the value
15
15
  * observableValue.setValue(Math.random());
16
16
  * // if you want to dispose a single observer
17
- * observer.dispose();
17
+ * observer[Symbol.dispose]();
18
18
  * // if you want to dispose the whole observableValue with all of its observers:
19
- * observableValue.dispose();
19
+ * observableValue[Symbol.dispose]();
20
20
  * ```
21
21
  * @param T This type parameter is the value type to observe
22
22
  */
@@ -27,7 +27,7 @@ export class ValueObserver {
27
27
  /**
28
28
  * Disposes the ValueObserver instance. Unsubscribes from the observable
29
29
  */
30
- dispose() {
30
+ [Symbol.dispose]() {
31
31
  this.observable.unsubscribe(this);
32
32
  }
33
33
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"value-observer.js","sourceRoot":"","sources":["../src/value-observer.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,aAAa;IAeN;IACT;IACS;IAhBlB;;OAEG;IACI,OAAO;QACZ,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC;IAED;;;;;OAKG;IACH,YACkB,UAA8B,EACvC,QAAgC,EACvB,OAAiC;QAFjC,eAAU,GAAV,UAAU,CAAoB;QACvC,aAAQ,GAAR,QAAQ,CAAwB;QACvB,YAAO,GAAP,OAAO,CAA0B;IAChD,CAAC;CACL"}
1
+ {"version":3,"file":"value-observer.js","sourceRoot":"","sources":["../src/value-observer.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,aAAa;IAeN;IACT;IACS;IAhBlB;;OAEG;IACI,CAAC,MAAM,CAAC,OAAO,CAAC;QACrB,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC;IAED;;;;;OAKG;IACH,YACkB,UAA8B,EACvC,QAAgC,EACvB,OAAiC;QAFjC,eAAU,GAAV,UAAU,CAAoB;QACvC,aAAQ,GAAR,QAAQ,CAAwB;QACvB,YAAO,GAAP,OAAO,CAA0B;IAChD,CAAC;CACL"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@furystack/utils",
3
- "version": "7.0.2",
3
+ "version": "8.0.1",
4
4
  "description": "General utilities",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "homepage": "https://github.com/furystack/furystack",
36
36
  "devDependencies": {
37
- "typescript": "^5.4.5",
38
- "vitest": "^1.6.0"
37
+ "typescript": "^5.5.3",
38
+ "vitest": "^2.0.3"
39
39
  }
40
40
  }
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from 'vitest'
1
+ import { describe, expect, it } from 'vitest'
2
2
  import { deepMerge } from './deep-merge.js'
3
3
 
4
4
  describe('DeepMerge tests', () => {
@@ -12,7 +12,7 @@ describe('DeepMerge tests', () => {
12
12
  })
13
13
 
14
14
  it('Should skip falsy sources', () => {
15
- expect(deepMerge({ a: 1, b: 0, c: 0 }, null as any, { b: 2 }, { c: 3 })).toEqual({ a: 1, b: 2, c: 3 })
15
+ expect(deepMerge({ a: 1, b: 0, c: 0 }, null as any as {}, { b: 2 }, { c: 3 })).toEqual({ a: 1, b: 2, c: 3 })
16
16
  })
17
17
 
18
18
  it('Should override arrays', () => {
@@ -1,5 +1,5 @@
1
- import { EventHub } from './event-hub.js'
2
1
  import { describe, expect, it, vi } from 'vitest'
2
+ import { EventHub } from './event-hub.js'
3
3
 
4
4
  describe('EventHub', () => {
5
5
  it('Should fail on type errors', () => {
@@ -74,7 +74,7 @@ describe('EventHub', () => {
74
74
  expect(objectListener2).toBeCalledTimes(1)
75
75
  })
76
76
 
77
- it('Should add and remove a listener with subscription', () => {
77
+ it('Should add and remove a listener with subscription', async () => {
78
78
  const eventHub = new EventHub<{ ExampleNumberEvent: number }>()
79
79
  const numberListener = vi.fn((_val: number) => {})
80
80
 
@@ -82,7 +82,7 @@ describe('EventHub', () => {
82
82
  eventHub.emit('ExampleNumberEvent', 1)
83
83
 
84
84
  expect(numberListener).toBeCalledWith(1)
85
- subscription.dispose()
85
+ subscription[Symbol.dispose]()
86
86
  eventHub.emit('ExampleNumberEvent', 2)
87
87
  expect(numberListener).toBeCalledTimes(1)
88
88
  })
@@ -93,7 +93,7 @@ describe('EventHub', () => {
93
93
  const listener = vi.fn((_val: string) => {})
94
94
 
95
95
  hub.addListener('test', listener)
96
- hub.dispose()
96
+ hub[Symbol.dispose]()
97
97
  hub.emit('test', 'test')
98
98
  expect(listener).not.toBeCalled()
99
99
  })
package/src/event-hub.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { Disposable } from './disposable.js'
2
-
3
1
  type ListenerFunction<EventTypeMap extends Object, T extends keyof EventTypeMap> = (arg: EventTypeMap[T]) => void
4
2
 
5
3
  export class EventHub<EventTypeMap extends Object> implements Disposable {
@@ -29,7 +27,7 @@ export class EventHub<EventTypeMap extends Object> implements Disposable {
29
27
  listener: ListenerFunction<EventTypeMap, TEvent>,
30
28
  ): Disposable {
31
29
  this.addListener(event, listener)
32
- return { dispose: () => this.removeListener(event, listener) }
30
+ return { [Symbol.dispose]: () => this.removeListener(event, listener) }
33
31
  }
34
32
 
35
33
  public emit<TEvent extends keyof EventTypeMap>(event: TEvent, arg: EventTypeMap[TEvent]) {
@@ -38,7 +36,7 @@ export class EventHub<EventTypeMap extends Object> implements Disposable {
38
36
  }
39
37
  }
40
38
 
41
- public dispose() {
39
+ public [Symbol.dispose]() {
42
40
  this.listeners.clear()
43
41
  }
44
42
  }
package/src/index.ts CHANGED
@@ -1,11 +1,13 @@
1
- export * from './disposable.js'
2
- export * from './deep-merge.js'
3
1
  export * from './debounce.js'
2
+ export * from './deep-merge.js'
4
3
  export * from './event-hub.js'
4
+ export * from './is-async-disposable.js'
5
+ export * from './is-disposable.js'
5
6
  export * from './observable-value.js'
6
- export * from './value-observer.js'
7
7
  export * from './path-helper.js'
8
8
  export * from './sleep-async.js'
9
9
  export * from './sort-by.js'
10
- export * from './trace.js'
11
10
  export * from './tuple.js'
11
+ export * from './using-async.js'
12
+ export * from './using.js'
13
+ export * from './value-observer.js'
@@ -0,0 +1,23 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { isAsyncDisposable } from './is-async-disposable.js'
3
+
4
+ describe('isAsyndDisposable', () => {
5
+ it('should return true if the value is an instance of an async disposable object', () => {
6
+ const asyncDisposable = {
7
+ [Symbol.asyncDispose]: () => {},
8
+ }
9
+ expect(isAsyncDisposable(asyncDisposable)).toBe(true)
10
+ })
11
+
12
+ it('Should return false is the object has Symbol.asyncDispose but it is not a function', () => {
13
+ const asyncDisposable = {
14
+ [Symbol.asyncDispose]: 'not a function',
15
+ }
16
+ expect(isAsyncDisposable(asyncDisposable)).toBe(false)
17
+ })
18
+
19
+ it('Should return false is the object does not have Symbol.asyncDispose', () => {
20
+ const asyncDisposable = {}
21
+ expect(isAsyncDisposable(asyncDisposable)).toBe(false)
22
+ })
23
+ })
@@ -0,0 +1,8 @@
1
+ /**
2
+ *
3
+ * @param value The value to check
4
+ * @returns if the value is an instance of an async disposable object
5
+ */
6
+ export const isAsyncDisposable = (value: unknown): value is AsyncDisposable => {
7
+ return (value as AsyncDisposable)?.[Symbol.asyncDispose] instanceof Function
8
+ }
@@ -0,0 +1,23 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { isDisposable } from './is-disposable.js'
3
+
4
+ describe('isDisposable', () => {
5
+ it('should return true if the value is an instance of a disposable object', () => {
6
+ const disposable = {
7
+ [Symbol.dispose]: () => {},
8
+ }
9
+ expect(isDisposable(disposable)).toBe(true)
10
+ })
11
+
12
+ it('Should return false is the object has Symbol.dispose but it is not a function', () => {
13
+ const disposable = {
14
+ [Symbol.dispose]: 'not a function',
15
+ }
16
+ expect(isDisposable(disposable)).toBe(false)
17
+ })
18
+
19
+ it('Should return false is the object does not have Symbol.dispose', () => {
20
+ const disposable = {}
21
+ expect(isDisposable(disposable)).toBe(false)
22
+ })
23
+ })
@@ -0,0 +1,8 @@
1
+ /**
2
+ *
3
+ * @param value The value to check
4
+ * @returns if the value is an instance of a disposable object
5
+ */
6
+ export const isDisposable = (value: unknown): value is Disposable => {
7
+ return (value as Disposable)?.[Symbol.dispose] instanceof Function
8
+ }
@@ -1,4 +1,4 @@
1
- import { describe, it, expect, vi } from 'vitest'
1
+ import { describe, expect, it, vi } from 'vitest'
2
2
  import { ObservableValue } from './observable-value.js'
3
3
 
4
4
  /**
@@ -74,8 +74,10 @@ export const observableTests = describe('Observable', () => {
74
74
  v.subscribe(callback1)
75
75
  v.subscribe(callback2)
76
76
  expect(v.getObservers().length).toBe(2)
77
- v.dispose()
77
+ v[Symbol.dispose]()
78
78
  expect(v.getObservers().length).toBe(0)
79
+
80
+ expect(v.isDisposed).toBe(true)
79
81
  })
80
82
 
81
83
  it('should remove the subscription on Observer dispose', () => {
@@ -85,25 +87,25 @@ export const observableTests = describe('Observable', () => {
85
87
  const v = new ObservableValue(1)
86
88
  const observer = v.subscribe(callback1)
87
89
  expect(v.getObservers().length).toBe(1)
88
- observer.dispose()
90
+ observer[Symbol.dispose]()
89
91
  expect(v.getObservers().length).toBe(0)
90
92
  })
91
93
 
92
94
  it('should throw an error for setValue() when the observer has been disposed', () => {
93
95
  const v = new ObservableValue(1)
94
- v.dispose()
96
+ v[Symbol.dispose]()
95
97
  expect(() => v.setValue(3)).toThrowError('Observable already disposed')
96
98
  })
97
99
 
98
100
  it('should throw an error for getValue() when the observer has been disposed', () => {
99
101
  const v = new ObservableValue(1)
100
- v.dispose()
102
+ v[Symbol.dispose]()
101
103
  expect(() => v.getValue()).toThrowError('Observable already disposed')
102
104
  })
103
105
 
104
106
  it('should throw an error for subscribe() when the observer has been disposed', () => {
105
107
  const v = new ObservableValue(1)
106
- v.dispose()
108
+ v[Symbol.dispose]()
107
109
  expect(() =>
108
110
  v.subscribe(() => {
109
111
  /** */
@@ -120,10 +122,10 @@ export const observableTests = describe('Observable', () => {
120
122
  }
121
123
  }
122
124
  const v = new ObservableValue(1)
123
- const observer = v.subscribe(new Alma().Callback)
124
- v.subscribe(new Alma().Callback)
125
+ const observer = v.subscribe(() => new Alma().Callback())
126
+ v.subscribe(() => new Alma().Callback())
125
127
  expect(v.getObservers().length).toBe(2)
126
- observer.dispose()
128
+ observer[Symbol.dispose]()
127
129
  expect(v.getObservers().length).toBe(1)
128
130
  v.setValue(3)
129
131
 
@@ -1,4 +1,3 @@
1
- import type { Disposable } from './disposable.js'
2
1
  import type { ValueObserverOptions } from './value-observer.js'
3
2
  import { ValueObserver } from './value-observer.js'
4
3
 
@@ -42,9 +41,9 @@ const defaultComparer = <T>(a: T, b: T) => a !== b
42
41
  * // To update the value
43
42
  * observableValue.setValue(Math.random());
44
43
  * // if you want to dispose a single observer
45
- * observer.dispose();
44
+ * observer[Symbol.dispose]();
46
45
  * // if you want to dispose the whole observableValue with all of its observers:
47
- * observableValue.dispose();
46
+ * observableValue[Symbol.dispose]();
48
47
  * ```
49
48
  * @param T Generic argument to indicate the value type
50
49
  */
@@ -58,7 +57,7 @@ export class ObservableValue<T> implements Disposable {
58
57
  /**
59
58
  * Disposes the ObservableValue object, removes all observers
60
59
  */
61
- public dispose() {
60
+ public [Symbol.dispose]() {
62
61
  this.observers.clear()
63
62
  this._isDisposed = true
64
63
  // @ts-expect-error getting currentValue after disposing is not allowed
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from 'vitest'
1
+ import { describe, expect, it } from 'vitest'
2
2
  import { PathHelper } from './path-helper.js'
3
3
 
4
4
  /**
@@ -69,4 +69,13 @@ export const pathHelperTests = describe('PathHelper', () => {
69
69
  expect(PathHelper.getParentPath('Root')).toBe('Root')
70
70
  })
71
71
  })
72
+
73
+ describe('#normalize()', () => {
74
+ it('Should normalize the path', () => {
75
+ expect(PathHelper.normalize('Root/Example/Content')).toBe('Root/Example/Content')
76
+ expect(PathHelper.normalize('/Root/Example/Content')).toBe('Root/Example/Content')
77
+ expect(PathHelper.normalize('Root/Example/Content/')).toBe('Root/Example/Content')
78
+ expect(PathHelper.normalize('/Root/Example/Content/')).toBe('Root/Example/Content')
79
+ })
80
+ })
72
81
  })
@@ -35,7 +35,7 @@ export class PathHelper {
35
35
  * @returns the joined path string
36
36
  */
37
37
  public static joinPaths(...args: string[]) {
38
- return args.map(this.trimSlashes).join('/')
38
+ return args.map((path) => this.trimSlashes(path)).join('/')
39
39
  }
40
40
 
41
41
  /**