@dxos/context 0.8.4-main.dedc0f3 → 0.8.4-main.e8ec1fe

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/context",
3
- "version": "0.8.4-main.dedc0f3",
3
+ "version": "0.8.4-main.e8ec1fe",
4
4
  "description": "Async utils.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -27,10 +27,10 @@
27
27
  "src"
28
28
  ],
29
29
  "dependencies": {
30
- "@dxos/debug": "0.8.4-main.dedc0f3",
31
- "@dxos/log": "0.8.4-main.dedc0f3",
32
- "@dxos/util": "0.8.4-main.dedc0f3",
33
- "@dxos/node-std": "0.8.4-main.dedc0f3"
30
+ "@dxos/debug": "0.8.4-main.e8ec1fe",
31
+ "@dxos/log": "0.8.4-main.e8ec1fe",
32
+ "@dxos/util": "0.8.4-main.e8ec1fe",
33
+ "@dxos/node-std": "0.8.4-main.e8ec1fe"
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(ctx: Context): Promise<void> {}
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(ctx: Context): Promise<void> {}
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();