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