@dxos/context 0.8.4-main.a4bbb77 → 0.8.4-main.ae835ea

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.a4bbb77",
3
+ "version": "0.8.4-main.ae835ea",
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.a4bbb77",
31
- "@dxos/log": "0.8.4-main.a4bbb77",
32
- "@dxos/node-std": "0.8.4-main.a4bbb77",
33
- "@dxos/util": "0.8.4-main.a4bbb77"
30
+ "@dxos/debug": "0.8.4-main.ae835ea",
31
+ "@dxos/log": "0.8.4-main.ae835ea",
32
+ "@dxos/node-std": "0.8.4-main.ae835ea",
33
+ "@dxos/util": "0.8.4-main.ae835ea"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"
package/src/resource.ts CHANGED
@@ -60,12 +60,12 @@ export abstract class Resource implements Lifecycle {
60
60
  /**
61
61
  * To be overridden by subclasses.
62
62
  */
63
- protected async _open(ctx: Context): Promise<void> {}
63
+ protected async _open(_ctx: Context): Promise<void> {}
64
64
 
65
65
  /**
66
66
  * To be overridden by subclasses.
67
67
  */
68
- protected async _close(ctx: Context): Promise<void> {}
68
+ protected async _close(_ctx: Context): Promise<void> {}
69
69
 
70
70
  /**
71
71
  * Error handler for errors that are caught by the context.
@@ -82,6 +82,18 @@ export abstract class Resource implements Lifecycle {
82
82
  throw err;
83
83
  }
84
84
 
85
+ /**
86
+ * Calls the provided function, opening and closing the resource.
87
+ */
88
+ async use<T>(fn: (resource: this) => Promise<T>): Promise<T> {
89
+ try {
90
+ await this.open();
91
+ return await fn(this);
92
+ } finally {
93
+ await this.close();
94
+ }
95
+ }
96
+
85
97
  /**
86
98
  * Opens the resource.
87
99
  * If the resource is already open, it does nothing.
@@ -100,7 +112,6 @@ export abstract class Resource implements Lifecycle {
100
112
 
101
113
  await this.#closePromise;
102
114
  await (this.#openPromise ??= this.#open(ctx));
103
-
104
115
  return this;
105
116
  }
106
117
 
@@ -114,7 +125,6 @@ export abstract class Resource implements Lifecycle {
114
125
  }
115
126
  await this.#openPromise;
116
127
  await (this.#closePromise ??= this.#close(ctx));
117
-
118
128
  return this;
119
129
  }
120
130