@dxos/context 0.8.4-main.dedc0f3 → 0.8.4-main.e00bdcdb52
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 +195 -318
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +195 -318
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/context-disposed-error.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +4 -3
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/promise-utils.d.ts.map +1 -1
- package/dist/types/src/resource.d.ts +17 -3
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/trace-context.d.ts +54 -0
- package/dist/types/src/trace-context.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -9
- package/src/context.ts +17 -4
- package/src/index.ts +1 -0
- package/src/resource.ts +33 -9
- package/src/trace-context.ts +72 -0
|
@@ -14,66 +14,12 @@ 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
|
-
}
|
|
70
17
|
function _ts_decorate(decorators, target, key, desc) {
|
|
71
18
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
72
19
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
73
20
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
74
21
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
75
22
|
}
|
|
76
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/common/context/src/context.ts";
|
|
77
23
|
var DEBUG_LOG_DISPOSE = false;
|
|
78
24
|
var MAX_SAFE_DISPOSE_CALLBACKS = 300;
|
|
79
25
|
var DEFAULT_ERROR_HANDLER = (error, ctx) => {
|
|
@@ -85,25 +31,51 @@ var DEFAULT_ERROR_HANDLER = (error, ctx) => {
|
|
|
85
31
|
};
|
|
86
32
|
var CONTEXT_FLAG_IS_DISPOSED = 1 << 0;
|
|
87
33
|
var CONTEXT_FLAG_LEAK_DETECTED = 1 << 1;
|
|
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;
|
|
98
34
|
var Context = class _Context {
|
|
99
35
|
static default() {
|
|
100
36
|
return new _Context();
|
|
101
37
|
}
|
|
38
|
+
#disposeCallbacks = [];
|
|
39
|
+
#name = void 0;
|
|
40
|
+
#parent = void 0;
|
|
41
|
+
#attributes;
|
|
42
|
+
#onError;
|
|
43
|
+
#flags = 0;
|
|
44
|
+
#disposePromise = void 0;
|
|
45
|
+
#signal = void 0;
|
|
46
|
+
maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
|
|
47
|
+
constructor(params = {}, callMeta) {
|
|
48
|
+
this.#name = getContextName(params, callMeta);
|
|
49
|
+
this.#parent = params.parent;
|
|
50
|
+
this.#attributes = params.attributes ?? {};
|
|
51
|
+
this.#onError = params.onError ?? DEFAULT_ERROR_HANDLER;
|
|
52
|
+
}
|
|
53
|
+
get #isDisposed() {
|
|
54
|
+
return !!(this.#flags & CONTEXT_FLAG_IS_DISPOSED);
|
|
55
|
+
}
|
|
56
|
+
set #isDisposed(value) {
|
|
57
|
+
this.#flags = value ? this.#flags | CONTEXT_FLAG_IS_DISPOSED : this.#flags & ~CONTEXT_FLAG_IS_DISPOSED;
|
|
58
|
+
}
|
|
59
|
+
get #leakDetected() {
|
|
60
|
+
return !!(this.#flags & CONTEXT_FLAG_LEAK_DETECTED);
|
|
61
|
+
}
|
|
62
|
+
set #leakDetected(value) {
|
|
63
|
+
this.#flags = value ? this.#flags | CONTEXT_FLAG_LEAK_DETECTED : this.#flags & ~CONTEXT_FLAG_LEAK_DETECTED;
|
|
64
|
+
}
|
|
102
65
|
get disposed() {
|
|
103
|
-
return
|
|
66
|
+
return this.#isDisposed;
|
|
104
67
|
}
|
|
105
68
|
get disposeCallbacksLength() {
|
|
106
|
-
return
|
|
69
|
+
return this.#disposeCallbacks.length;
|
|
70
|
+
}
|
|
71
|
+
get signal() {
|
|
72
|
+
if (this.#signal) {
|
|
73
|
+
return this.#signal;
|
|
74
|
+
}
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
this.#signal = controller.signal;
|
|
77
|
+
this.onDispose(() => controller.abort());
|
|
78
|
+
return this.#signal;
|
|
107
79
|
}
|
|
108
80
|
/**
|
|
109
81
|
* Schedules a callback to run when the context is disposed.
|
|
@@ -115,41 +87,31 @@ var Context = class _Context {
|
|
|
115
87
|
* @returns A function that can be used to remove the callback from the dispose list.
|
|
116
88
|
*/
|
|
117
89
|
onDispose(callback) {
|
|
118
|
-
if (
|
|
90
|
+
if (this.#isDisposed) {
|
|
119
91
|
void (async () => {
|
|
120
92
|
try {
|
|
121
93
|
await callback();
|
|
122
94
|
} catch (error) {
|
|
123
95
|
log.catch(error, {
|
|
124
|
-
context:
|
|
125
|
-
}, {
|
|
126
|
-
F: __dxlog_file,
|
|
127
|
-
L: 119,
|
|
128
|
-
S: this,
|
|
129
|
-
C: (f, a) => f(...a)
|
|
96
|
+
context: this.#name
|
|
130
97
|
});
|
|
131
98
|
}
|
|
132
99
|
})();
|
|
133
100
|
}
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
|
|
101
|
+
this.#disposeCallbacks.push(callback);
|
|
102
|
+
if (this.#disposeCallbacks.length > this.maxSafeDisposeCallbacks && !this.#leakDetected) {
|
|
103
|
+
this.#leakDetected = true;
|
|
137
104
|
const callSite = new StackTrace().getStackArray(1)[0].trim();
|
|
138
105
|
log.warn("Context has a large number of dispose callbacks (this might be a memory leak).", {
|
|
139
|
-
context:
|
|
106
|
+
context: this.#name,
|
|
140
107
|
callSite,
|
|
141
|
-
count:
|
|
142
|
-
}, {
|
|
143
|
-
F: __dxlog_file,
|
|
144
|
-
L: 128,
|
|
145
|
-
S: this,
|
|
146
|
-
C: (f, a) => f(...a)
|
|
108
|
+
count: this.#disposeCallbacks.length
|
|
147
109
|
});
|
|
148
110
|
}
|
|
149
111
|
return () => {
|
|
150
|
-
const index =
|
|
112
|
+
const index = this.#disposeCallbacks.indexOf(callback);
|
|
151
113
|
if (index !== -1) {
|
|
152
|
-
|
|
114
|
+
this.#disposeCallbacks.splice(index, 1);
|
|
153
115
|
}
|
|
154
116
|
};
|
|
155
117
|
}
|
|
@@ -162,26 +124,21 @@ var Context = class _Context {
|
|
|
162
124
|
* @returns true if there were no errors during the dispose process.
|
|
163
125
|
*/
|
|
164
126
|
async dispose(throwOnError = false) {
|
|
165
|
-
if (
|
|
166
|
-
return
|
|
127
|
+
if (this.#disposePromise) {
|
|
128
|
+
return this.#disposePromise;
|
|
167
129
|
}
|
|
168
|
-
|
|
130
|
+
this.#isDisposed = true;
|
|
169
131
|
let resolveDispose;
|
|
170
132
|
const promise = new Promise((resolve) => {
|
|
171
133
|
resolveDispose = resolve;
|
|
172
134
|
});
|
|
173
|
-
|
|
174
|
-
const callbacks = Array.from(
|
|
175
|
-
|
|
135
|
+
this.#disposePromise = promise;
|
|
136
|
+
const callbacks = Array.from(this.#disposeCallbacks).reverse();
|
|
137
|
+
this.#disposeCallbacks.length = 0;
|
|
176
138
|
if (DEBUG_LOG_DISPOSE) {
|
|
177
139
|
log("disposing", {
|
|
178
|
-
context:
|
|
140
|
+
context: this.#name,
|
|
179
141
|
count: callbacks.length
|
|
180
|
-
}, {
|
|
181
|
-
F: __dxlog_file,
|
|
182
|
-
L: 173,
|
|
183
|
-
S: this,
|
|
184
|
-
C: (f, a) => f(...a)
|
|
185
142
|
});
|
|
186
143
|
}
|
|
187
144
|
let i = 0;
|
|
@@ -197,14 +154,9 @@ var Context = class _Context {
|
|
|
197
154
|
errors.push(err);
|
|
198
155
|
} else {
|
|
199
156
|
log.catch(err, {
|
|
200
|
-
context:
|
|
157
|
+
context: this.#name,
|
|
201
158
|
callback: i,
|
|
202
159
|
count: callbacks.length
|
|
203
|
-
}, {
|
|
204
|
-
F: __dxlog_file,
|
|
205
|
-
L: 188,
|
|
206
|
-
S: this,
|
|
207
|
-
C: (f, a) => f(...a)
|
|
208
160
|
});
|
|
209
161
|
}
|
|
210
162
|
}
|
|
@@ -215,12 +167,7 @@ var Context = class _Context {
|
|
|
215
167
|
resolveDispose(clean);
|
|
216
168
|
if (DEBUG_LOG_DISPOSE) {
|
|
217
169
|
log("disposed", {
|
|
218
|
-
context:
|
|
219
|
-
}, {
|
|
220
|
-
F: __dxlog_file,
|
|
221
|
-
L: 199,
|
|
222
|
-
S: this,
|
|
223
|
-
C: (f, a) => f(...a)
|
|
170
|
+
context: this.#name
|
|
224
171
|
});
|
|
225
172
|
}
|
|
226
173
|
return clean;
|
|
@@ -231,17 +178,18 @@ var Context = class _Context {
|
|
|
231
178
|
* IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
|
|
232
179
|
*/
|
|
233
180
|
raise(error) {
|
|
234
|
-
if (
|
|
181
|
+
if (this.#isDisposed) {
|
|
235
182
|
return;
|
|
236
183
|
}
|
|
237
184
|
try {
|
|
238
|
-
|
|
185
|
+
this.#onError(error, this);
|
|
239
186
|
} catch (err) {
|
|
240
187
|
void Promise.reject(err);
|
|
241
188
|
}
|
|
242
189
|
}
|
|
243
190
|
derive({ onError, attributes } = {}) {
|
|
244
191
|
const newCtx = new _Context({
|
|
192
|
+
parent: this,
|
|
245
193
|
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
246
194
|
onError: async (error) => {
|
|
247
195
|
if (!onError) {
|
|
@@ -261,78 +209,23 @@ var Context = class _Context {
|
|
|
261
209
|
return newCtx;
|
|
262
210
|
}
|
|
263
211
|
getAttribute(key) {
|
|
264
|
-
if (key in
|
|
265
|
-
return
|
|
212
|
+
if (key in this.#attributes) {
|
|
213
|
+
return this.#attributes[key];
|
|
266
214
|
}
|
|
267
|
-
if (
|
|
268
|
-
return
|
|
215
|
+
if (this.#parent) {
|
|
216
|
+
return this.#parent.getAttribute(key);
|
|
269
217
|
}
|
|
270
218
|
return void 0;
|
|
271
219
|
}
|
|
220
|
+
[Symbol.toStringTag] = "Context";
|
|
221
|
+
[inspect.custom] = () => this.toString();
|
|
272
222
|
toString() {
|
|
273
|
-
return `Context(${
|
|
223
|
+
return `Context(${this.#isDisposed ? "disposed" : "active"})`;
|
|
274
224
|
}
|
|
275
225
|
async [Symbol.asyncDispose]() {
|
|
276
226
|
await this.dispose();
|
|
277
227
|
}
|
|
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
|
-
}
|
|
323
228
|
};
|
|
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
|
-
}
|
|
336
229
|
Context = _ts_decorate([
|
|
337
230
|
safeInstanceof("Context")
|
|
338
231
|
], Context);
|
|
@@ -362,93 +255,61 @@ var cancelWithContext = (ctx, promise) => {
|
|
|
362
255
|
};
|
|
363
256
|
|
|
364
257
|
// src/resource.ts
|
|
258
|
+
import "@hazae41/symbol-dispose-polyfill";
|
|
365
259
|
import { throwUnhandledError } from "@dxos/util";
|
|
366
|
-
|
|
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) {
|
|
260
|
+
var LifecycleState = /* @__PURE__ */ (function(LifecycleState2) {
|
|
417
261
|
LifecycleState2["CLOSED"] = "CLOSED";
|
|
418
262
|
LifecycleState2["OPEN"] = "OPEN";
|
|
419
263
|
LifecycleState2["ERROR"] = "ERROR";
|
|
420
264
|
return LifecycleState2;
|
|
421
|
-
}({});
|
|
265
|
+
})({});
|
|
422
266
|
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();
|
|
433
267
|
var Resource = class {
|
|
268
|
+
#lifecycleState = "CLOSED";
|
|
269
|
+
#openPromise = null;
|
|
270
|
+
#closePromise = null;
|
|
271
|
+
/**
|
|
272
|
+
* Managed internally by the resource.
|
|
273
|
+
* Recreated on close.
|
|
274
|
+
* Errors are propagated to the `_catch` method and the parent context.
|
|
275
|
+
*/
|
|
276
|
+
#internalCtx = this.#createContext();
|
|
277
|
+
/**
|
|
278
|
+
* Context that is used to bubble up errors that are not handled by the resource.
|
|
279
|
+
* Provided in the open method.
|
|
280
|
+
*/
|
|
281
|
+
#parentCtx = this.#createParentContext();
|
|
282
|
+
/**
|
|
283
|
+
* ```ts
|
|
284
|
+
* await using resource = new Resource();
|
|
285
|
+
* await resource.open();
|
|
286
|
+
* ```
|
|
287
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
288
|
+
*/
|
|
289
|
+
async [Symbol.asyncDispose]() {
|
|
290
|
+
await this.close();
|
|
291
|
+
}
|
|
292
|
+
get #name() {
|
|
293
|
+
return Object.getPrototypeOf(this).constructor.name;
|
|
294
|
+
}
|
|
434
295
|
get isOpen() {
|
|
435
|
-
return
|
|
296
|
+
return this.#lifecycleState === "OPEN" && this.#closePromise == null;
|
|
436
297
|
}
|
|
437
298
|
get _lifecycleState() {
|
|
438
|
-
return
|
|
299
|
+
return this.#lifecycleState;
|
|
439
300
|
}
|
|
440
301
|
get _ctx() {
|
|
441
|
-
return
|
|
302
|
+
return this.#internalCtx;
|
|
442
303
|
}
|
|
443
304
|
/**
|
|
444
305
|
* To be overridden by subclasses.
|
|
445
306
|
*/
|
|
446
|
-
async _open(
|
|
307
|
+
async _open(_ctx) {
|
|
447
308
|
}
|
|
448
309
|
/**
|
|
449
310
|
* To be overridden by subclasses.
|
|
450
311
|
*/
|
|
451
|
-
async _close(
|
|
312
|
+
async _close(_ctx) {
|
|
452
313
|
}
|
|
453
314
|
/**
|
|
454
315
|
* Error handler for errors that are caught by the context.
|
|
@@ -465,6 +326,19 @@ var Resource = class {
|
|
|
465
326
|
throw err;
|
|
466
327
|
}
|
|
467
328
|
/**
|
|
329
|
+
* Calls the provided function, opening and closing the resource.
|
|
330
|
+
* NOTE: Consider using `using` instead.
|
|
331
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using
|
|
332
|
+
*/
|
|
333
|
+
async use(fn) {
|
|
334
|
+
try {
|
|
335
|
+
await this.open();
|
|
336
|
+
return await fn(this);
|
|
337
|
+
} finally {
|
|
338
|
+
await this.close();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
468
342
|
* Opens the resource.
|
|
469
343
|
* If the resource is already open, it does nothing.
|
|
470
344
|
* If the resource is in an error state, it throws an error.
|
|
@@ -472,15 +346,15 @@ var Resource = class {
|
|
|
472
346
|
* @param ctx - Context to use for opening the resource. This context will receive errors that are not handled in `_catch`.
|
|
473
347
|
*/
|
|
474
348
|
async open(ctx) {
|
|
475
|
-
switch (
|
|
349
|
+
switch (this.#lifecycleState) {
|
|
476
350
|
case "OPEN":
|
|
477
351
|
return this;
|
|
478
352
|
case "ERROR":
|
|
479
|
-
throw new Error(`Invalid state: ${
|
|
353
|
+
throw new Error(`Invalid state: ${this.#lifecycleState}`);
|
|
480
354
|
default:
|
|
481
355
|
}
|
|
482
|
-
await
|
|
483
|
-
await
|
|
356
|
+
await this.#closePromise;
|
|
357
|
+
await (this.#openPromise ??= this.#open(ctx));
|
|
484
358
|
return this;
|
|
485
359
|
}
|
|
486
360
|
/**
|
|
@@ -488,108 +362,111 @@ var Resource = class {
|
|
|
488
362
|
* If the resource is already closed, it does nothing.
|
|
489
363
|
*/
|
|
490
364
|
async close(ctx) {
|
|
491
|
-
if (
|
|
365
|
+
if (this.#lifecycleState === "CLOSED") {
|
|
492
366
|
return this;
|
|
493
367
|
}
|
|
494
|
-
await
|
|
495
|
-
await
|
|
368
|
+
await this.#openPromise;
|
|
369
|
+
await (this.#closePromise ??= this.#close(ctx));
|
|
496
370
|
return this;
|
|
497
371
|
}
|
|
498
372
|
/**
|
|
499
373
|
* Waits until the resource is open.
|
|
500
374
|
*/
|
|
501
375
|
async waitUntilOpen() {
|
|
502
|
-
switch (
|
|
376
|
+
switch (this.#lifecycleState) {
|
|
503
377
|
case "OPEN":
|
|
504
378
|
return;
|
|
505
379
|
case "ERROR":
|
|
506
|
-
throw new Error(`Invalid state: ${
|
|
380
|
+
throw new Error(`Invalid state: ${this.#lifecycleState}`);
|
|
507
381
|
}
|
|
508
|
-
if (!
|
|
382
|
+
if (!this.#openPromise) {
|
|
509
383
|
throw new Error("Resource is not being opened");
|
|
510
384
|
}
|
|
511
|
-
await
|
|
512
|
-
}
|
|
513
|
-
async
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
385
|
+
await this.#openPromise;
|
|
386
|
+
}
|
|
387
|
+
async #open(ctx) {
|
|
388
|
+
this.#closePromise = null;
|
|
389
|
+
this.#parentCtx = ctx?.derive({
|
|
390
|
+
name: this.#name
|
|
391
|
+
}) ?? this.#createParentContext();
|
|
392
|
+
this.#internalCtx = this.#createContext(this.#parentCtx);
|
|
393
|
+
await this._open(this.#parentCtx);
|
|
394
|
+
this.#lifecycleState = "OPEN";
|
|
395
|
+
}
|
|
396
|
+
async #close(ctx = Context.default()) {
|
|
397
|
+
this.#openPromise = null;
|
|
398
|
+
await this.#internalCtx.dispose();
|
|
399
|
+
await this._close(ctx);
|
|
400
|
+
this.#internalCtx = this.#createContext();
|
|
401
|
+
this.#lifecycleState = "CLOSED";
|
|
402
|
+
}
|
|
403
|
+
#createContext(attributeParent) {
|
|
404
|
+
return new Context({
|
|
405
|
+
name: this.#name,
|
|
406
|
+
parent: attributeParent,
|
|
407
|
+
onError: (error) => queueMicrotask(async () => {
|
|
408
|
+
try {
|
|
409
|
+
await this._catch(error);
|
|
410
|
+
} catch (err) {
|
|
411
|
+
this.#lifecycleState = "ERROR";
|
|
412
|
+
this.#parentCtx.raise(err);
|
|
413
|
+
}
|
|
414
|
+
})
|
|
540
415
|
});
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
416
|
+
}
|
|
417
|
+
#createParentContext() {
|
|
418
|
+
return new Context({
|
|
419
|
+
name: this.#name
|
|
544
420
|
});
|
|
545
421
|
}
|
|
546
422
|
};
|
|
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
|
-
}
|
|
583
423
|
var openInContext = async (ctx, resource) => {
|
|
584
424
|
await resource.open?.(ctx);
|
|
585
425
|
ctx.onDispose(() => resource.close?.());
|
|
586
426
|
return resource;
|
|
587
427
|
};
|
|
428
|
+
|
|
429
|
+
// src/trace-context.ts
|
|
430
|
+
var TRACE_SPAN_ATTRIBUTE = "dxos.trace-span";
|
|
431
|
+
var ContextRpcCodec = class {
|
|
432
|
+
/**
|
|
433
|
+
* Read the W3C trace context from a DXOS `Context` for an outgoing RPC.
|
|
434
|
+
*
|
|
435
|
+
* @returns `TraceContextData` to attach to the wire message, or `undefined`
|
|
436
|
+
* if the context has no active trace.
|
|
437
|
+
*/
|
|
438
|
+
static encode(ctx) {
|
|
439
|
+
const traceCtx = ctx.getAttribute(TRACE_SPAN_ATTRIBUTE);
|
|
440
|
+
if (traceCtx == null || typeof traceCtx.traceparent !== "string") {
|
|
441
|
+
return void 0;
|
|
442
|
+
}
|
|
443
|
+
return traceCtx;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Reconstruct a DXOS `Context` from W3C trace context received in an
|
|
447
|
+
* incoming RPC request.
|
|
448
|
+
*
|
|
449
|
+
* @returns A `Context` carrying the trace context, or `Context.default()`
|
|
450
|
+
* if the data is missing/invalid.
|
|
451
|
+
*/
|
|
452
|
+
static decode(traceContext) {
|
|
453
|
+
if (typeof traceContext.traceparent !== "string" || traceContext.traceparent.length === 0) {
|
|
454
|
+
return Context.default();
|
|
455
|
+
}
|
|
456
|
+
return new Context({
|
|
457
|
+
attributes: {
|
|
458
|
+
[TRACE_SPAN_ATTRIBUTE]: traceContext
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
};
|
|
588
463
|
export {
|
|
589
464
|
Context,
|
|
590
465
|
ContextDisposedError,
|
|
466
|
+
ContextRpcCodec,
|
|
591
467
|
LifecycleState,
|
|
592
468
|
Resource,
|
|
469
|
+
TRACE_SPAN_ATTRIBUTE,
|
|
593
470
|
cancelWithContext,
|
|
594
471
|
openInContext,
|
|
595
472
|
rejectOnDispose
|