@dxos/context 0.8.4-main.b97322e → 0.8.4-main.c4373fc
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 +287 -125
- package/dist/lib/browser/index.mjs.map +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +287 -125
- package/dist/lib/node-esm/index.mjs.map +2 -2
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
|
@@ -12,6 +12,59 @@ var ContextDisposedError = class extends Error {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
// src/context.ts
|
|
15
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
16
|
+
if (privateCollection.has(obj)) {
|
|
17
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
21
|
+
if (descriptor.get) {
|
|
22
|
+
return descriptor.get.call(receiver);
|
|
23
|
+
}
|
|
24
|
+
return descriptor.value;
|
|
25
|
+
}
|
|
26
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
27
|
+
if (descriptor.set) {
|
|
28
|
+
descriptor.set.call(receiver, value);
|
|
29
|
+
} else {
|
|
30
|
+
if (!descriptor.writable) {
|
|
31
|
+
throw new TypeError("attempted to set read only private field");
|
|
32
|
+
}
|
|
33
|
+
descriptor.value = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
37
|
+
if (!privateMap.has(receiver)) {
|
|
38
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
39
|
+
}
|
|
40
|
+
return privateMap.get(receiver);
|
|
41
|
+
}
|
|
42
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
43
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
44
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
45
|
+
}
|
|
46
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
47
|
+
_check_private_redeclaration(obj, privateMap);
|
|
48
|
+
privateMap.set(obj, value);
|
|
49
|
+
}
|
|
50
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
51
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
52
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
function _define_property(obj, key, value) {
|
|
56
|
+
if (key in obj) {
|
|
57
|
+
Object.defineProperty(obj, key, {
|
|
58
|
+
value,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
obj[key] = value;
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
15
68
|
function _ts_decorate(decorators, target, key, desc) {
|
|
16
69
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
17
70
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -30,49 +83,25 @@ var DEFAULT_ERROR_HANDLER = (error, ctx) => {
|
|
|
30
83
|
};
|
|
31
84
|
var CONTEXT_FLAG_IS_DISPOSED = 1 << 0;
|
|
32
85
|
var CONTEXT_FLAG_LEAK_DETECTED = 1 << 1;
|
|
33
|
-
var
|
|
86
|
+
var _disposeCallbacks = /* @__PURE__ */ new WeakMap();
|
|
87
|
+
var _name = /* @__PURE__ */ new WeakMap();
|
|
88
|
+
var _parent = /* @__PURE__ */ new WeakMap();
|
|
89
|
+
var _attributes = /* @__PURE__ */ new WeakMap();
|
|
90
|
+
var _onError = /* @__PURE__ */ new WeakMap();
|
|
91
|
+
var _flags = /* @__PURE__ */ new WeakMap();
|
|
92
|
+
var _disposePromise = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
var _isDisposed = /* @__PURE__ */ new WeakMap();
|
|
94
|
+
var _leakDetected = /* @__PURE__ */ new WeakMap();
|
|
95
|
+
var _inspect_custom = inspect.custom;
|
|
34
96
|
var Context = class _Context {
|
|
35
|
-
constructor(params = {}, callMeta) {
|
|
36
|
-
this.#disposeCallbacks = [];
|
|
37
|
-
this.#name = void 0;
|
|
38
|
-
this.#parent = void 0;
|
|
39
|
-
this.#flags = 0;
|
|
40
|
-
this.#disposePromise = void 0;
|
|
41
|
-
this.maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
|
|
42
|
-
this[_b] = "Context";
|
|
43
|
-
this[_a] = () => this.toString();
|
|
44
|
-
this.#name = getContextName(params, callMeta);
|
|
45
|
-
this.#parent = params.parent;
|
|
46
|
-
this.#attributes = params.attributes ?? {};
|
|
47
|
-
this.#onError = params.onError ?? DEFAULT_ERROR_HANDLER;
|
|
48
|
-
}
|
|
49
97
|
static default() {
|
|
50
98
|
return new _Context();
|
|
51
99
|
}
|
|
52
|
-
#disposeCallbacks;
|
|
53
|
-
#name;
|
|
54
|
-
#parent;
|
|
55
|
-
#attributes;
|
|
56
|
-
#onError;
|
|
57
|
-
#flags;
|
|
58
|
-
#disposePromise;
|
|
59
|
-
get #isDisposed() {
|
|
60
|
-
return !!(this.#flags & CONTEXT_FLAG_IS_DISPOSED);
|
|
61
|
-
}
|
|
62
|
-
set #isDisposed(value) {
|
|
63
|
-
this.#flags = value ? this.#flags | CONTEXT_FLAG_IS_DISPOSED : this.#flags & ~CONTEXT_FLAG_IS_DISPOSED;
|
|
64
|
-
}
|
|
65
|
-
get #leakDetected() {
|
|
66
|
-
return !!(this.#flags & CONTEXT_FLAG_LEAK_DETECTED);
|
|
67
|
-
}
|
|
68
|
-
set #leakDetected(value) {
|
|
69
|
-
this.#flags = value ? this.#flags | CONTEXT_FLAG_LEAK_DETECTED : this.#flags & ~CONTEXT_FLAG_LEAK_DETECTED;
|
|
70
|
-
}
|
|
71
100
|
get disposed() {
|
|
72
|
-
return this
|
|
101
|
+
return _class_private_field_get(this, _isDisposed);
|
|
73
102
|
}
|
|
74
103
|
get disposeCallbacksLength() {
|
|
75
|
-
return this
|
|
104
|
+
return _class_private_field_get(this, _disposeCallbacks).length;
|
|
76
105
|
}
|
|
77
106
|
/**
|
|
78
107
|
* Schedules a callback to run when the context is disposed.
|
|
@@ -84,13 +113,13 @@ var Context = class _Context {
|
|
|
84
113
|
* @returns A function that can be used to remove the callback from the dispose list.
|
|
85
114
|
*/
|
|
86
115
|
onDispose(callback) {
|
|
87
|
-
if (this
|
|
116
|
+
if (_class_private_field_get(this, _isDisposed)) {
|
|
88
117
|
void (async () => {
|
|
89
118
|
try {
|
|
90
119
|
await callback();
|
|
91
120
|
} catch (error) {
|
|
92
121
|
log.catch(error, {
|
|
93
|
-
context: this
|
|
122
|
+
context: _class_private_field_get(this, _name)
|
|
94
123
|
}, {
|
|
95
124
|
F: __dxlog_file,
|
|
96
125
|
L: 119,
|
|
@@ -100,14 +129,14 @@ var Context = class _Context {
|
|
|
100
129
|
}
|
|
101
130
|
})();
|
|
102
131
|
}
|
|
103
|
-
this
|
|
104
|
-
if (this
|
|
105
|
-
this
|
|
132
|
+
_class_private_field_get(this, _disposeCallbacks).push(callback);
|
|
133
|
+
if (_class_private_field_get(this, _disposeCallbacks).length > this.maxSafeDisposeCallbacks && !_class_private_field_get(this, _leakDetected)) {
|
|
134
|
+
_class_private_field_set(this, _leakDetected, true);
|
|
106
135
|
const callSite = new StackTrace().getStackArray(1)[0].trim();
|
|
107
136
|
log.warn("Context has a large number of dispose callbacks (this might be a memory leak).", {
|
|
108
|
-
context: this
|
|
137
|
+
context: _class_private_field_get(this, _name),
|
|
109
138
|
callSite,
|
|
110
|
-
count: this
|
|
139
|
+
count: _class_private_field_get(this, _disposeCallbacks).length
|
|
111
140
|
}, {
|
|
112
141
|
F: __dxlog_file,
|
|
113
142
|
L: 128,
|
|
@@ -116,9 +145,9 @@ var Context = class _Context {
|
|
|
116
145
|
});
|
|
117
146
|
}
|
|
118
147
|
return () => {
|
|
119
|
-
const index = this
|
|
148
|
+
const index = _class_private_field_get(this, _disposeCallbacks).indexOf(callback);
|
|
120
149
|
if (index !== -1) {
|
|
121
|
-
this
|
|
150
|
+
_class_private_field_get(this, _disposeCallbacks).splice(index, 1);
|
|
122
151
|
}
|
|
123
152
|
};
|
|
124
153
|
}
|
|
@@ -131,20 +160,20 @@ var Context = class _Context {
|
|
|
131
160
|
* @returns true if there were no errors during the dispose process.
|
|
132
161
|
*/
|
|
133
162
|
async dispose(throwOnError = false) {
|
|
134
|
-
if (this
|
|
135
|
-
return this
|
|
163
|
+
if (_class_private_field_get(this, _disposePromise)) {
|
|
164
|
+
return _class_private_field_get(this, _disposePromise);
|
|
136
165
|
}
|
|
137
|
-
this
|
|
166
|
+
_class_private_field_set(this, _isDisposed, true);
|
|
138
167
|
let resolveDispose;
|
|
139
168
|
const promise = new Promise((resolve) => {
|
|
140
169
|
resolveDispose = resolve;
|
|
141
170
|
});
|
|
142
|
-
this
|
|
143
|
-
const callbacks = Array.from(this
|
|
144
|
-
this
|
|
171
|
+
_class_private_field_set(this, _disposePromise, promise);
|
|
172
|
+
const callbacks = Array.from(_class_private_field_get(this, _disposeCallbacks)).reverse();
|
|
173
|
+
_class_private_field_get(this, _disposeCallbacks).length = 0;
|
|
145
174
|
if (DEBUG_LOG_DISPOSE) {
|
|
146
175
|
log("disposing", {
|
|
147
|
-
context: this
|
|
176
|
+
context: _class_private_field_get(this, _name),
|
|
148
177
|
count: callbacks.length
|
|
149
178
|
}, {
|
|
150
179
|
F: __dxlog_file,
|
|
@@ -166,7 +195,7 @@ var Context = class _Context {
|
|
|
166
195
|
errors.push(err);
|
|
167
196
|
} else {
|
|
168
197
|
log.catch(err, {
|
|
169
|
-
context: this
|
|
198
|
+
context: _class_private_field_get(this, _name),
|
|
170
199
|
callback: i,
|
|
171
200
|
count: callbacks.length
|
|
172
201
|
}, {
|
|
@@ -184,7 +213,7 @@ var Context = class _Context {
|
|
|
184
213
|
resolveDispose(clean);
|
|
185
214
|
if (DEBUG_LOG_DISPOSE) {
|
|
186
215
|
log("disposed", {
|
|
187
|
-
context: this
|
|
216
|
+
context: _class_private_field_get(this, _name)
|
|
188
217
|
}, {
|
|
189
218
|
F: __dxlog_file,
|
|
190
219
|
L: 199,
|
|
@@ -200,11 +229,11 @@ var Context = class _Context {
|
|
|
200
229
|
* IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
|
|
201
230
|
*/
|
|
202
231
|
raise(error) {
|
|
203
|
-
if (this
|
|
232
|
+
if (_class_private_field_get(this, _isDisposed)) {
|
|
204
233
|
return;
|
|
205
234
|
}
|
|
206
235
|
try {
|
|
207
|
-
this
|
|
236
|
+
_class_private_field_get(this, _onError).call(this, error, this);
|
|
208
237
|
} catch (err) {
|
|
209
238
|
void Promise.reject(err);
|
|
210
239
|
}
|
|
@@ -230,21 +259,78 @@ var Context = class _Context {
|
|
|
230
259
|
return newCtx;
|
|
231
260
|
}
|
|
232
261
|
getAttribute(key) {
|
|
233
|
-
if (key in this
|
|
234
|
-
return this
|
|
262
|
+
if (key in _class_private_field_get(this, _attributes)) {
|
|
263
|
+
return _class_private_field_get(this, _attributes)[key];
|
|
235
264
|
}
|
|
236
|
-
if (this
|
|
237
|
-
return this
|
|
265
|
+
if (_class_private_field_get(this, _parent)) {
|
|
266
|
+
return _class_private_field_get(this, _parent).getAttribute(key);
|
|
238
267
|
}
|
|
239
268
|
return void 0;
|
|
240
269
|
}
|
|
241
270
|
toString() {
|
|
242
|
-
return `Context(${this
|
|
271
|
+
return `Context(${_class_private_field_get(this, _isDisposed) ? "disposed" : "active"})`;
|
|
243
272
|
}
|
|
244
|
-
async [
|
|
273
|
+
async [Symbol.asyncDispose]() {
|
|
245
274
|
await this.dispose();
|
|
246
275
|
}
|
|
276
|
+
constructor(params = {}, callMeta) {
|
|
277
|
+
_class_private_field_init(this, _isDisposed, {
|
|
278
|
+
get: get_isDisposed,
|
|
279
|
+
set: set_isDisposed
|
|
280
|
+
});
|
|
281
|
+
_class_private_field_init(this, _leakDetected, {
|
|
282
|
+
get: get_leakDetected,
|
|
283
|
+
set: set_leakDetected
|
|
284
|
+
});
|
|
285
|
+
_class_private_field_init(this, _disposeCallbacks, {
|
|
286
|
+
writable: true,
|
|
287
|
+
value: []
|
|
288
|
+
});
|
|
289
|
+
_class_private_field_init(this, _name, {
|
|
290
|
+
writable: true,
|
|
291
|
+
value: void 0
|
|
292
|
+
});
|
|
293
|
+
_class_private_field_init(this, _parent, {
|
|
294
|
+
writable: true,
|
|
295
|
+
value: void 0
|
|
296
|
+
});
|
|
297
|
+
_class_private_field_init(this, _attributes, {
|
|
298
|
+
writable: true,
|
|
299
|
+
value: void 0
|
|
300
|
+
});
|
|
301
|
+
_class_private_field_init(this, _onError, {
|
|
302
|
+
writable: true,
|
|
303
|
+
value: void 0
|
|
304
|
+
});
|
|
305
|
+
_class_private_field_init(this, _flags, {
|
|
306
|
+
writable: true,
|
|
307
|
+
value: 0
|
|
308
|
+
});
|
|
309
|
+
_class_private_field_init(this, _disposePromise, {
|
|
310
|
+
writable: true,
|
|
311
|
+
value: void 0
|
|
312
|
+
});
|
|
313
|
+
_define_property(this, "maxSafeDisposeCallbacks", MAX_SAFE_DISPOSE_CALLBACKS);
|
|
314
|
+
_define_property(this, Symbol.toStringTag, "Context");
|
|
315
|
+
_define_property(this, _inspect_custom, () => this.toString());
|
|
316
|
+
_class_private_field_set(this, _name, getContextName(params, callMeta));
|
|
317
|
+
_class_private_field_set(this, _parent, params.parent);
|
|
318
|
+
_class_private_field_set(this, _attributes, params.attributes ?? {});
|
|
319
|
+
_class_private_field_set(this, _onError, params.onError ?? DEFAULT_ERROR_HANDLER);
|
|
320
|
+
}
|
|
247
321
|
};
|
|
322
|
+
function get_isDisposed() {
|
|
323
|
+
return !!(_class_private_field_get(this, _flags) & CONTEXT_FLAG_IS_DISPOSED);
|
|
324
|
+
}
|
|
325
|
+
function set_isDisposed(value) {
|
|
326
|
+
_class_private_field_set(this, _flags, value ? _class_private_field_get(this, _flags) | CONTEXT_FLAG_IS_DISPOSED : _class_private_field_get(this, _flags) & ~CONTEXT_FLAG_IS_DISPOSED);
|
|
327
|
+
}
|
|
328
|
+
function get_leakDetected() {
|
|
329
|
+
return !!(_class_private_field_get(this, _flags) & CONTEXT_FLAG_LEAK_DETECTED);
|
|
330
|
+
}
|
|
331
|
+
function set_leakDetected(value) {
|
|
332
|
+
_class_private_field_set(this, _flags, value ? _class_private_field_get(this, _flags) | CONTEXT_FLAG_LEAK_DETECTED : _class_private_field_get(this, _flags) & ~CONTEXT_FLAG_LEAK_DETECTED);
|
|
333
|
+
}
|
|
248
334
|
Context = _ts_decorate([
|
|
249
335
|
safeInstanceof("Context")
|
|
250
336
|
], Context);
|
|
@@ -275,39 +361,82 @@ var cancelWithContext = (ctx, promise) => {
|
|
|
275
361
|
|
|
276
362
|
// src/resource.ts
|
|
277
363
|
import { throwUnhandledError } from "@dxos/util";
|
|
278
|
-
|
|
364
|
+
function _check_private_redeclaration2(obj, privateCollection) {
|
|
365
|
+
if (privateCollection.has(obj)) {
|
|
366
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function _class_apply_descriptor_get2(receiver, descriptor) {
|
|
370
|
+
if (descriptor.get) {
|
|
371
|
+
return descriptor.get.call(receiver);
|
|
372
|
+
}
|
|
373
|
+
return descriptor.value;
|
|
374
|
+
}
|
|
375
|
+
function _class_apply_descriptor_set2(receiver, descriptor, value) {
|
|
376
|
+
if (descriptor.set) {
|
|
377
|
+
descriptor.set.call(receiver, value);
|
|
378
|
+
} else {
|
|
379
|
+
if (!descriptor.writable) {
|
|
380
|
+
throw new TypeError("attempted to set read only private field");
|
|
381
|
+
}
|
|
382
|
+
descriptor.value = value;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
function _class_extract_field_descriptor2(receiver, privateMap, action) {
|
|
386
|
+
if (!privateMap.has(receiver)) {
|
|
387
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
388
|
+
}
|
|
389
|
+
return privateMap.get(receiver);
|
|
390
|
+
}
|
|
391
|
+
function _class_private_field_get2(receiver, privateMap) {
|
|
392
|
+
var descriptor = _class_extract_field_descriptor2(receiver, privateMap, "get");
|
|
393
|
+
return _class_apply_descriptor_get2(receiver, descriptor);
|
|
394
|
+
}
|
|
395
|
+
function _class_private_field_init2(obj, privateMap, value) {
|
|
396
|
+
_check_private_redeclaration2(obj, privateMap);
|
|
397
|
+
privateMap.set(obj, value);
|
|
398
|
+
}
|
|
399
|
+
function _class_private_field_set2(receiver, privateMap, value) {
|
|
400
|
+
var descriptor = _class_extract_field_descriptor2(receiver, privateMap, "set");
|
|
401
|
+
_class_apply_descriptor_set2(receiver, descriptor, value);
|
|
402
|
+
return value;
|
|
403
|
+
}
|
|
404
|
+
function _class_private_method_get(receiver, privateSet, fn) {
|
|
405
|
+
if (!privateSet.has(receiver)) {
|
|
406
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
407
|
+
}
|
|
408
|
+
return fn;
|
|
409
|
+
}
|
|
410
|
+
function _class_private_method_init(obj, privateSet) {
|
|
411
|
+
_check_private_redeclaration2(obj, privateSet);
|
|
412
|
+
privateSet.add(obj);
|
|
413
|
+
}
|
|
414
|
+
var LifecycleState = /* @__PURE__ */ (function(LifecycleState2) {
|
|
279
415
|
LifecycleState2["CLOSED"] = "CLOSED";
|
|
280
416
|
LifecycleState2["OPEN"] = "OPEN";
|
|
281
417
|
LifecycleState2["ERROR"] = "ERROR";
|
|
282
418
|
return LifecycleState2;
|
|
283
|
-
}({});
|
|
419
|
+
})({});
|
|
284
420
|
var CLOSE_RESOURCE_ON_UNHANDLED_ERROR = false;
|
|
421
|
+
var _lifecycleState = /* @__PURE__ */ new WeakMap();
|
|
422
|
+
var _openPromise = /* @__PURE__ */ new WeakMap();
|
|
423
|
+
var _closePromise = /* @__PURE__ */ new WeakMap();
|
|
424
|
+
var _internalCtx = /* @__PURE__ */ new WeakMap();
|
|
425
|
+
var _parentCtx = /* @__PURE__ */ new WeakMap();
|
|
426
|
+
var _name2 = /* @__PURE__ */ new WeakMap();
|
|
427
|
+
var _open = /* @__PURE__ */ new WeakSet();
|
|
428
|
+
var _close = /* @__PURE__ */ new WeakSet();
|
|
429
|
+
var _createContext = /* @__PURE__ */ new WeakSet();
|
|
430
|
+
var _createParentContext = /* @__PURE__ */ new WeakSet();
|
|
285
431
|
var Resource = class {
|
|
286
|
-
#lifecycleState = "CLOSED";
|
|
287
|
-
#openPromise = null;
|
|
288
|
-
#closePromise = null;
|
|
289
|
-
/**
|
|
290
|
-
* Managed internally by the resource.
|
|
291
|
-
* Recreated on close.
|
|
292
|
-
* Errors are propagated to the `_catch` method and the parent context.
|
|
293
|
-
*/
|
|
294
|
-
#internalCtx = this.#createContext();
|
|
295
|
-
/**
|
|
296
|
-
* Context that is used to bubble up errors that are not handled by the resource.
|
|
297
|
-
* Provided in the open method.
|
|
298
|
-
*/
|
|
299
|
-
#parentCtx = this.#createParentContext();
|
|
300
|
-
get #name() {
|
|
301
|
-
return Object.getPrototypeOf(this).constructor.name;
|
|
302
|
-
}
|
|
303
432
|
get isOpen() {
|
|
304
|
-
return this
|
|
433
|
+
return _class_private_field_get2(this, _lifecycleState) === "OPEN" && _class_private_field_get2(this, _closePromise) == null;
|
|
305
434
|
}
|
|
306
435
|
get _lifecycleState() {
|
|
307
|
-
return this
|
|
436
|
+
return _class_private_field_get2(this, _lifecycleState);
|
|
308
437
|
}
|
|
309
438
|
get _ctx() {
|
|
310
|
-
return this
|
|
439
|
+
return _class_private_field_get2(this, _internalCtx);
|
|
311
440
|
}
|
|
312
441
|
/**
|
|
313
442
|
* To be overridden by subclasses.
|
|
@@ -341,15 +470,15 @@ var Resource = class {
|
|
|
341
470
|
* @param ctx - Context to use for opening the resource. This context will receive errors that are not handled in `_catch`.
|
|
342
471
|
*/
|
|
343
472
|
async open(ctx) {
|
|
344
|
-
switch (this
|
|
473
|
+
switch (_class_private_field_get2(this, _lifecycleState)) {
|
|
345
474
|
case "OPEN":
|
|
346
475
|
return this;
|
|
347
476
|
case "ERROR":
|
|
348
|
-
throw new Error(`Invalid state: ${this
|
|
477
|
+
throw new Error(`Invalid state: ${_class_private_field_get2(this, _lifecycleState)}`);
|
|
349
478
|
default:
|
|
350
479
|
}
|
|
351
|
-
await this
|
|
352
|
-
await (this
|
|
480
|
+
await _class_private_field_get2(this, _closePromise);
|
|
481
|
+
await _class_private_field_set2(this, _openPromise, _class_private_field_get2(this, _openPromise) ?? _class_private_method_get(this, _open, open).call(this, ctx));
|
|
353
482
|
return this;
|
|
354
483
|
}
|
|
355
484
|
/**
|
|
@@ -357,65 +486,98 @@ var Resource = class {
|
|
|
357
486
|
* If the resource is already closed, it does nothing.
|
|
358
487
|
*/
|
|
359
488
|
async close(ctx) {
|
|
360
|
-
if (this
|
|
489
|
+
if (_class_private_field_get2(this, _lifecycleState) === "CLOSED") {
|
|
361
490
|
return this;
|
|
362
491
|
}
|
|
363
|
-
await this
|
|
364
|
-
await (this
|
|
492
|
+
await _class_private_field_get2(this, _openPromise);
|
|
493
|
+
await _class_private_field_set2(this, _closePromise, _class_private_field_get2(this, _closePromise) ?? _class_private_method_get(this, _close, close).call(this, ctx));
|
|
365
494
|
return this;
|
|
366
495
|
}
|
|
367
496
|
/**
|
|
368
497
|
* Waits until the resource is open.
|
|
369
498
|
*/
|
|
370
499
|
async waitUntilOpen() {
|
|
371
|
-
switch (this
|
|
500
|
+
switch (_class_private_field_get2(this, _lifecycleState)) {
|
|
372
501
|
case "OPEN":
|
|
373
502
|
return;
|
|
374
503
|
case "ERROR":
|
|
375
|
-
throw new Error(`Invalid state: ${this
|
|
504
|
+
throw new Error(`Invalid state: ${_class_private_field_get2(this, _lifecycleState)}`);
|
|
376
505
|
}
|
|
377
|
-
if (!this
|
|
506
|
+
if (!_class_private_field_get2(this, _openPromise)) {
|
|
378
507
|
throw new Error("Resource is not being opened");
|
|
379
508
|
}
|
|
380
|
-
await this
|
|
509
|
+
await _class_private_field_get2(this, _openPromise);
|
|
381
510
|
}
|
|
382
511
|
async [Symbol.asyncDispose]() {
|
|
383
512
|
await this.close();
|
|
384
513
|
}
|
|
385
|
-
|
|
386
|
-
this
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}) ?? this.#createParentContext();
|
|
390
|
-
await this._open(this.#parentCtx);
|
|
391
|
-
this.#lifecycleState = "OPEN";
|
|
392
|
-
}
|
|
393
|
-
async #close(ctx = Context.default()) {
|
|
394
|
-
this.#openPromise = null;
|
|
395
|
-
await this.#internalCtx.dispose();
|
|
396
|
-
await this._close(ctx);
|
|
397
|
-
this.#internalCtx = this.#createContext();
|
|
398
|
-
this.#lifecycleState = "CLOSED";
|
|
399
|
-
}
|
|
400
|
-
#createContext() {
|
|
401
|
-
return new Context({
|
|
402
|
-
name: this.#name,
|
|
403
|
-
onError: (error) => queueMicrotask(async () => {
|
|
404
|
-
try {
|
|
405
|
-
await this._catch(error);
|
|
406
|
-
} catch (err) {
|
|
407
|
-
this.#lifecycleState = "ERROR";
|
|
408
|
-
this.#parentCtx.raise(err);
|
|
409
|
-
}
|
|
410
|
-
})
|
|
514
|
+
constructor() {
|
|
515
|
+
_class_private_field_init2(this, _name2, {
|
|
516
|
+
get: get_name,
|
|
517
|
+
set: void 0
|
|
411
518
|
});
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
519
|
+
_class_private_method_init(this, _open);
|
|
520
|
+
_class_private_method_init(this, _close);
|
|
521
|
+
_class_private_method_init(this, _createContext);
|
|
522
|
+
_class_private_method_init(this, _createParentContext);
|
|
523
|
+
_class_private_field_init2(this, _lifecycleState, {
|
|
524
|
+
writable: true,
|
|
525
|
+
value: "CLOSED"
|
|
526
|
+
});
|
|
527
|
+
_class_private_field_init2(this, _openPromise, {
|
|
528
|
+
writable: true,
|
|
529
|
+
value: null
|
|
530
|
+
});
|
|
531
|
+
_class_private_field_init2(this, _closePromise, {
|
|
532
|
+
writable: true,
|
|
533
|
+
value: null
|
|
534
|
+
});
|
|
535
|
+
_class_private_field_init2(this, _internalCtx, {
|
|
536
|
+
writable: true,
|
|
537
|
+
value: _class_private_method_get(this, _createContext, createContext).call(this)
|
|
538
|
+
});
|
|
539
|
+
_class_private_field_init2(this, _parentCtx, {
|
|
540
|
+
writable: true,
|
|
541
|
+
value: _class_private_method_get(this, _createParentContext, createParentContext).call(this)
|
|
416
542
|
});
|
|
417
543
|
}
|
|
418
544
|
};
|
|
545
|
+
function get_name() {
|
|
546
|
+
return Object.getPrototypeOf(this).constructor.name;
|
|
547
|
+
}
|
|
548
|
+
async function open(ctx) {
|
|
549
|
+
_class_private_field_set2(this, _closePromise, null);
|
|
550
|
+
_class_private_field_set2(this, _parentCtx, ctx?.derive({
|
|
551
|
+
name: _class_private_field_get2(this, _name2)
|
|
552
|
+
}) ?? _class_private_method_get(this, _createParentContext, createParentContext).call(this));
|
|
553
|
+
await this._open(_class_private_field_get2(this, _parentCtx));
|
|
554
|
+
_class_private_field_set2(this, _lifecycleState, "OPEN");
|
|
555
|
+
}
|
|
556
|
+
async function close(ctx = Context.default()) {
|
|
557
|
+
_class_private_field_set2(this, _openPromise, null);
|
|
558
|
+
await _class_private_field_get2(this, _internalCtx).dispose();
|
|
559
|
+
await this._close(ctx);
|
|
560
|
+
_class_private_field_set2(this, _internalCtx, _class_private_method_get(this, _createContext, createContext).call(this));
|
|
561
|
+
_class_private_field_set2(this, _lifecycleState, "CLOSED");
|
|
562
|
+
}
|
|
563
|
+
function createContext() {
|
|
564
|
+
return new Context({
|
|
565
|
+
name: _class_private_field_get2(this, _name2),
|
|
566
|
+
onError: (error) => queueMicrotask(async () => {
|
|
567
|
+
try {
|
|
568
|
+
await this._catch(error);
|
|
569
|
+
} catch (err) {
|
|
570
|
+
_class_private_field_set2(this, _lifecycleState, "ERROR");
|
|
571
|
+
_class_private_field_get2(this, _parentCtx).raise(err);
|
|
572
|
+
}
|
|
573
|
+
})
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
function createParentContext() {
|
|
577
|
+
return new Context({
|
|
578
|
+
name: _class_private_field_get2(this, _name2)
|
|
579
|
+
});
|
|
580
|
+
}
|
|
419
581
|
var openInContext = async (ctx, resource) => {
|
|
420
582
|
await resource.open?.(ctx);
|
|
421
583
|
ctx.onDispose(() => resource.close?.());
|