@dxos/context 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b
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/dist/lib/browser/index.mjs +42 -27
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +42 -27
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/resource.d.ts +17 -3
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/src/resource.ts +29 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.3c1ae3b",
|
|
4
4
|
"description": "Async utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
14
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
15
|
"node": {
|
|
15
16
|
"require": "./dist/lib/node/index.cjs",
|
|
16
17
|
"default": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
}
|
|
18
|
-
"types": "./dist/types/src/index.d.ts"
|
|
18
|
+
}
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -27,10 +27,11 @@
|
|
|
27
27
|
"src"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
30
|
+
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
31
|
+
"@dxos/debug": "0.8.4-main.3c1ae3b",
|
|
32
|
+
"@dxos/log": "0.8.4-main.3c1ae3b",
|
|
33
|
+
"@dxos/node-std": "0.8.4-main.3c1ae3b",
|
|
34
|
+
"@dxos/util": "0.8.4-main.3c1ae3b"
|
|
34
35
|
},
|
|
35
36
|
"publishConfig": {
|
|
36
37
|
"access": "public"
|
package/src/resource.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { throwUnhandledError } from '@dxos/util';
|
|
|
6
6
|
|
|
7
7
|
import { Context } from './context';
|
|
8
8
|
|
|
9
|
+
import '@hazae41/symbol-dispose-polyfill';
|
|
10
|
+
|
|
9
11
|
export enum LifecycleState {
|
|
10
12
|
CLOSED = 'CLOSED',
|
|
11
13
|
OPEN = 'OPEN',
|
|
@@ -41,6 +43,17 @@ export abstract class Resource implements Lifecycle {
|
|
|
41
43
|
*/
|
|
42
44
|
#parentCtx: Context = this.#createParentContext();
|
|
43
45
|
|
|
46
|
+
/**
|
|
47
|
+
* ```ts
|
|
48
|
+
* await using resource = new Resource();
|
|
49
|
+
* await resource.open();
|
|
50
|
+
* ```
|
|
51
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
52
|
+
*/
|
|
53
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
54
|
+
await this.close();
|
|
55
|
+
}
|
|
56
|
+
|
|
44
57
|
get #name() {
|
|
45
58
|
return Object.getPrototypeOf(this).constructor.name;
|
|
46
59
|
}
|
|
@@ -60,12 +73,12 @@ export abstract class Resource implements Lifecycle {
|
|
|
60
73
|
/**
|
|
61
74
|
* To be overridden by subclasses.
|
|
62
75
|
*/
|
|
63
|
-
protected async _open(
|
|
76
|
+
protected async _open(_ctx: Context): Promise<void> {}
|
|
64
77
|
|
|
65
78
|
/**
|
|
66
79
|
* To be overridden by subclasses.
|
|
67
80
|
*/
|
|
68
|
-
protected async _close(
|
|
81
|
+
protected async _close(_ctx: Context): Promise<void> {}
|
|
69
82
|
|
|
70
83
|
/**
|
|
71
84
|
* Error handler for errors that are caught by the context.
|
|
@@ -82,6 +95,20 @@ export abstract class Resource implements Lifecycle {
|
|
|
82
95
|
throw err;
|
|
83
96
|
}
|
|
84
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Calls the provided function, opening and closing the resource.
|
|
100
|
+
* NOTE: Consider using `using` instead.
|
|
101
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
102
|
+
*/
|
|
103
|
+
async use<T>(fn: (resource: this) => Promise<T>): Promise<T> {
|
|
104
|
+
try {
|
|
105
|
+
await this.open();
|
|
106
|
+
return await fn(this);
|
|
107
|
+
} finally {
|
|
108
|
+
await this.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
85
112
|
/**
|
|
86
113
|
* Opens the resource.
|
|
87
114
|
* If the resource is already open, it does nothing.
|
|
@@ -100,7 +127,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
100
127
|
|
|
101
128
|
await this.#closePromise;
|
|
102
129
|
await (this.#openPromise ??= this.#open(ctx));
|
|
103
|
-
|
|
104
130
|
return this;
|
|
105
131
|
}
|
|
106
132
|
|
|
@@ -114,7 +140,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
114
140
|
}
|
|
115
141
|
await this.#openPromise;
|
|
116
142
|
await (this.#closePromise ??= this.#close(ctx));
|
|
117
|
-
|
|
118
143
|
return this;
|
|
119
144
|
}
|
|
120
145
|
|
|
@@ -135,10 +160,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
135
160
|
await this.#openPromise;
|
|
136
161
|
}
|
|
137
162
|
|
|
138
|
-
async [Symbol.asyncDispose](): Promise<void> {
|
|
139
|
-
await this.close();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
163
|
async #open(ctx?: Context): Promise<void> {
|
|
143
164
|
this.#closePromise = null;
|
|
144
165
|
this.#parentCtx = ctx?.derive({ name: this.#name }) ?? this.#createParentContext();
|