@dxos/util 0.3.11-next.e28df4f → 0.3.11-next.f4c1077

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-parse-json.d.ts","sourceRoot":"","sources":["../../../src/safe-parse-json.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,aAAa,EAAE;IAC1B,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC;IACxE,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC;CAQpE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/util",
3
- "version": "0.3.11-next.e28df4f",
3
+ "version": "0.3.11-next.f4c1077",
4
4
  "description": "Temporary bucket for misc functions, which should graduate into separate packages.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -16,13 +16,13 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "@dxos/debug": "0.3.11-next.e28df4f",
20
- "@dxos/invariant": "0.3.11-next.e28df4f",
21
- "@dxos/keys": "0.3.11-next.e28df4f",
22
- "@dxos/node-std": "0.3.11-next.e28df4f"
19
+ "@dxos/keys": "0.3.11-next.f4c1077",
20
+ "@dxos/invariant": "0.3.11-next.f4c1077",
21
+ "@dxos/debug": "0.3.11-next.f4c1077",
22
+ "@dxos/node-std": "0.3.11-next.f4c1077"
23
23
  },
24
24
  "devDependencies": {
25
- "@dxos/crypto": "0.3.11-next.e28df4f"
25
+ "@dxos/crypto": "0.3.11-next.f4c1077"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
@@ -0,0 +1,37 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { describe, test } from '@dxos/test';
8
+
9
+ import { defer, deferAsync } from './defer';
10
+
11
+ describe('defer', () => {
12
+ test('defer', () => {
13
+ const events: string[] = [];
14
+
15
+ {
16
+ using _ = defer(() => events.push('1'));
17
+ events.push('2');
18
+ }
19
+
20
+ expect(events).to.deep.eq(['2', '1']);
21
+ });
22
+
23
+ test('deferAsync', async () => {
24
+ const events: string[] = [];
25
+
26
+ {
27
+ await using _ = deferAsync(async () => {
28
+ await new Promise((resolve) => setTimeout(resolve, 5));
29
+ events.push('1');
30
+ });
31
+ events.push('2');
32
+ }
33
+ events.push('3');
34
+
35
+ expect(events).to.deep.eq(['2', '1', '3']);
36
+ });
37
+ });
package/src/defer.ts ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Run function on scope exit.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * {
7
+ * using _ = defer(() => console.log('exiting'));
8
+ *
9
+ * ...
10
+ * }
11
+ */
12
+ //
13
+ // Copyright 2024 DXOS.org
14
+ //
15
+
16
+ export const defer = (fn: () => void): Disposable => new DeferGuard(fn);
17
+
18
+ class DeferGuard {
19
+ /**
20
+ * @internal
21
+ */
22
+ constructor(private readonly _fn: () => void) {}
23
+
24
+ [Symbol.dispose]() {
25
+ const result = this._fn();
26
+ if ((result as any) instanceof Promise) {
27
+ throw new Error('Async functions in defer are not supported. Use deferAsync instead.');
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Run async function on scope exit.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * {
38
+ * await using _ = deferAsync(async () => console.log('exiting'));
39
+ *
40
+ * ...
41
+ * }
42
+ */
43
+ export const deferAsync = (fn: () => Promise<void>): AsyncDisposable => new DeferAsyncGuard(fn);
44
+
45
+ class DeferAsyncGuard implements AsyncDisposable {
46
+ /**
47
+ * @internal
48
+ */
49
+ constructor(private readonly _fn: () => Promise<void>) {}
50
+
51
+ async [Symbol.asyncDispose]() {
52
+ await this._fn();
53
+ }
54
+ }
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export * from './random';
18
18
  export * from './range';
19
19
  export * from './reducers';
20
20
  export * from './safe-instanceof';
21
+ export * from './safe-parse-json';
21
22
  export * from './sort';
22
23
  export * from './tracer';
23
24
  export * from './types';
@@ -27,3 +28,4 @@ export * from './sum';
27
28
  export * from './for-each-async';
28
29
  export * from './weak';
29
30
  export * from './map-values';
31
+ export * from './defer';
@@ -0,0 +1,15 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export const safeParseJson: {
6
+ <T extends object>(data: string | undefined | null, defaultValue: T): T;
7
+ <T extends object>(data: string | undefined | null): T | undefined;
8
+ } = <T extends object>(data: string | undefined | null, defaultValue?: T) => {
9
+ if (data) {
10
+ try {
11
+ return JSON.parse(data);
12
+ } catch (err) {}
13
+ }
14
+ return defaultValue;
15
+ };