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