@dxos/context 0.8.4-main.67995b8 → 0.8.4-main.72ec0f3
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 +41 -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 +41 -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 +16 -3
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/resource.ts +27 -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.72ec0f3",
|
|
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,10 @@
|
|
|
27
27
|
"src"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@dxos/debug": "0.8.4-main.
|
|
31
|
-
"@dxos/log": "0.8.4-main.
|
|
32
|
-
"@dxos/node-std": "0.8.4-main.
|
|
33
|
-
"@dxos/util": "0.8.4-main.
|
|
30
|
+
"@dxos/debug": "0.8.4-main.72ec0f3",
|
|
31
|
+
"@dxos/log": "0.8.4-main.72ec0f3",
|
|
32
|
+
"@dxos/node-std": "0.8.4-main.72ec0f3",
|
|
33
|
+
"@dxos/util": "0.8.4-main.72ec0f3"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
package/src/resource.ts
CHANGED
|
@@ -41,6 +41,17 @@ export abstract class Resource implements Lifecycle {
|
|
|
41
41
|
*/
|
|
42
42
|
#parentCtx: Context = this.#createParentContext();
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* ```ts
|
|
46
|
+
* await using resource = new Resource();
|
|
47
|
+
* await resource.open();
|
|
48
|
+
* ```
|
|
49
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
50
|
+
*/
|
|
51
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
52
|
+
await this.close();
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
get #name() {
|
|
45
56
|
return Object.getPrototypeOf(this).constructor.name;
|
|
46
57
|
}
|
|
@@ -60,12 +71,12 @@ export abstract class Resource implements Lifecycle {
|
|
|
60
71
|
/**
|
|
61
72
|
* To be overridden by subclasses.
|
|
62
73
|
*/
|
|
63
|
-
protected async _open(
|
|
74
|
+
protected async _open(_ctx: Context): Promise<void> {}
|
|
64
75
|
|
|
65
76
|
/**
|
|
66
77
|
* To be overridden by subclasses.
|
|
67
78
|
*/
|
|
68
|
-
protected async _close(
|
|
79
|
+
protected async _close(_ctx: Context): Promise<void> {}
|
|
69
80
|
|
|
70
81
|
/**
|
|
71
82
|
* Error handler for errors that are caught by the context.
|
|
@@ -82,6 +93,20 @@ export abstract class Resource implements Lifecycle {
|
|
|
82
93
|
throw err;
|
|
83
94
|
}
|
|
84
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Calls the provided function, opening and closing the resource.
|
|
98
|
+
* NOTE: Consider using `using` instead.
|
|
99
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
100
|
+
*/
|
|
101
|
+
async use<T>(fn: (resource: this) => Promise<T>): Promise<T> {
|
|
102
|
+
try {
|
|
103
|
+
await this.open();
|
|
104
|
+
return await fn(this);
|
|
105
|
+
} finally {
|
|
106
|
+
await this.close();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
85
110
|
/**
|
|
86
111
|
* Opens the resource.
|
|
87
112
|
* If the resource is already open, it does nothing.
|
|
@@ -100,7 +125,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
100
125
|
|
|
101
126
|
await this.#closePromise;
|
|
102
127
|
await (this.#openPromise ??= this.#open(ctx));
|
|
103
|
-
|
|
104
128
|
return this;
|
|
105
129
|
}
|
|
106
130
|
|
|
@@ -114,7 +138,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
114
138
|
}
|
|
115
139
|
await this.#openPromise;
|
|
116
140
|
await (this.#closePromise ??= this.#close(ctx));
|
|
117
|
-
|
|
118
141
|
return this;
|
|
119
142
|
}
|
|
120
143
|
|
|
@@ -135,10 +158,6 @@ export abstract class Resource implements Lifecycle {
|
|
|
135
158
|
await this.#openPromise;
|
|
136
159
|
}
|
|
137
160
|
|
|
138
|
-
async [Symbol.asyncDispose](): Promise<void> {
|
|
139
|
-
await this.close();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
161
|
async #open(ctx?: Context): Promise<void> {
|
|
143
162
|
this.#closePromise = null;
|
|
144
163
|
this.#parentCtx = ctx?.derive({ name: this.#name }) ?? this.#createParentContext();
|