@composed-di/core 0.5.0-alpha → 0.5.1-alpha
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/serviceEventListener.d.ts +43 -30
- package/dist/serviceEventListener.d.ts.map +1 -1
- package/dist/serviceModule.d.ts.map +1 -1
- package/dist/serviceModule.js +44 -17
- package/dist/serviceModule.js.map +1 -1
- package/package.json +1 -1
- package/src/serviceEventListener.ts +39 -30
- package/src/serviceModule.ts +50 -18
|
@@ -4,48 +4,54 @@ import type { ServiceKey } from './serviceKey';
|
|
|
4
4
|
* disposal, or method call), returned by a ServiceEventListener when the
|
|
5
5
|
* operation starts.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* `end` is invoked exactly once when the operation finishes, so
|
|
8
|
+
* implementations can close over per-call state (a start time, a span
|
|
9
|
+
* handle, a correlation id) without any bookkeeping to pair concurrent
|
|
10
|
+
* start/finish events.
|
|
11
11
|
*/
|
|
12
12
|
export interface EventSpan {
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* Optional wrapper around the operation itself. When present, the module
|
|
15
|
+
* invokes the operation as `run(() => operation())`, so the listener can
|
|
16
|
+
* establish ambient state that the operation body and its async
|
|
17
|
+
* continuations inherit — this is what lets spans of nested service
|
|
18
|
+
* calls form a parent-child hierarchy.
|
|
17
19
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
+
* Implementations must invoke `fn` exactly once, synchronously, and
|
|
21
|
+
* return its result unchanged (for async operations, the promise itself).
|
|
22
|
+
* `end` is still delivered separately when the operation finishes.
|
|
20
23
|
*
|
|
21
|
-
* @param
|
|
22
|
-
*
|
|
23
|
-
* instance), absent for dispose spans.
|
|
24
|
-
* @return void
|
|
24
|
+
* @param fn - A thunk that performs the operation.
|
|
25
|
+
* @return The value returned by `fn`.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
+
run?<T>(fn: () => T): T;
|
|
27
28
|
/**
|
|
28
|
-
* Invoked when the operation
|
|
29
|
-
*
|
|
30
|
-
*
|
|
29
|
+
* Invoked exactly once when the operation finishes, whether it succeeded
|
|
30
|
+
* or failed. For methods that return a promise, this fires when the
|
|
31
|
+
* promise settles, not when the method returns. On failure, the error is
|
|
32
|
+
* rethrown to the caller after this is invoked.
|
|
31
33
|
*
|
|
32
|
-
*
|
|
34
|
+
* Whether to retain or log the outcome is the implementation's choice;
|
|
35
|
+
* values are passed by reference, so implementations must not mutate them.
|
|
36
|
+
*
|
|
37
|
+
* @param outcome - How the operation finished, and its value or error.
|
|
33
38
|
* @return void
|
|
34
39
|
*/
|
|
35
|
-
|
|
40
|
+
end?(outcome: EventOutcome): void;
|
|
36
41
|
}
|
|
37
42
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
43
|
+
* How an operation finished, delivered to EventSpan.end: `success` carries
|
|
44
|
+
* the value produced by the operation (the return or resolved value for
|
|
45
|
+
* method calls, the service instance for initialize, undefined for
|
|
46
|
+
* dispose); `failure` carries the error that was thrown or rejected.
|
|
41
47
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
48
|
+
export type EventOutcome = {
|
|
49
|
+
type: 'success';
|
|
50
|
+
value: unknown;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'failure';
|
|
53
|
+
error: unknown;
|
|
54
|
+
};
|
|
49
55
|
/**
|
|
50
56
|
* Context of a service initialization, delivered to onInitialize.
|
|
51
57
|
* Future fields are added here rather than as extra parameters.
|
|
@@ -75,10 +81,17 @@ export interface MethodCallContext {
|
|
|
75
81
|
* The unique identifier of the service the method belongs to.
|
|
76
82
|
*/
|
|
77
83
|
key: ServiceKey<unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* The name of the class implementing the service (the instance's
|
|
86
|
+
* constructor name), which may differ from `key.name`. Undefined for
|
|
87
|
+
* services that are not instances of a named class, such as plain
|
|
88
|
+
* object literals.
|
|
89
|
+
*/
|
|
90
|
+
className?: string;
|
|
78
91
|
/**
|
|
79
92
|
* The name of the method that is being called.
|
|
80
93
|
*/
|
|
81
|
-
|
|
94
|
+
functionName: string;
|
|
82
95
|
/**
|
|
83
96
|
* The arguments the method was invoked with, passed by reference;
|
|
84
97
|
* implementations must not mutate them.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceEventListener.d.ts","sourceRoot":"","sources":["../src/serviceEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;GASG;AACH,MAAM,WAAW,SAAS;IACxB
|
|
1
|
+
{"version":3,"file":"serviceEventListener.d.ts","sourceRoot":"","sources":["../src/serviceEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;GASG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;;;;;;;;OAaG;IACH,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAExB;;;;;;;;;;;OAWG;IACH,GAAG,CAAC,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;CACnC;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GACtB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAE5E;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,GAAG,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC1B;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,GAAG,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC1B;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,GAAG,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IAEzB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,IAAI,EAAE,SAAS,OAAO,EAAE,CAAC;CAC1B;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,YAAY,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,SAAS,GAAG,IAAI,CAAC;IAE5D;;;;;OAKG;IACH,SAAS,CAAC,CAAC,OAAO,EAAE,cAAc,GAAG,SAAS,GAAG,IAAI,CAAC;IAEtD;;;;;;OAMG;IACH,YAAY,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceModule.d.ts","sourceRoot":"","sources":["../src/serviceModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAsB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"serviceModule.d.ts","sourceRoot":"","sources":["../src/serviceModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAsB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,KAAK,EAEV,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAEhC,KAAK,cAAc,GAAG,cAAc,CAAC,OAAO,EAAE,SAAS,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAG1E;;;;;;;GAOG;AACH,qBAAa,aAAa;IAMJ,QAAQ,CAAC,SAAS,EAAE,cAAc,EAAE;IALxD;;;;OAIG;IACH,OAAO;IAOP;;;;;;OAMG;IACU,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IA2BnD;;;;;OAKG;IACU,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAWhE;;;;;;;;;OASG;IACI,OAAO,CAAC,KAAK,CAAC,EAAE,YAAY;IAQnC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,CAAC,IAAI,CACT,OAAO,EAAE,CAAC,aAAa,GAAG,cAAc,CAAC,EAAE,EAC3C,QAAQ,CAAC,EAAE,oBAAoB,GAC9B,aAAa;CAsBjB"}
|
package/dist/serviceModule.js
CHANGED
|
@@ -230,9 +230,9 @@ function isSuitable(key, factory) {
|
|
|
230
230
|
* lifecycle events and method calls.
|
|
231
231
|
*
|
|
232
232
|
* For each of initialize, dispose, and method calls, the listener is invoked
|
|
233
|
-
* at the start of the operation and may return an EventSpan whose `end`
|
|
234
|
-
*
|
|
235
|
-
* being reported.
|
|
233
|
+
* at the start of the operation and may return an EventSpan whose `end` is
|
|
234
|
+
* called with the outcome when the operation finishes. Errors are rethrown
|
|
235
|
+
* after being reported.
|
|
236
236
|
*
|
|
237
237
|
* @param listener The listener notified of lifecycle and method call events.
|
|
238
238
|
* @param delegate The original service factory to be instrumented.
|
|
@@ -250,25 +250,25 @@ function makeObservable(listener, delegate) {
|
|
|
250
250
|
if (dispose) {
|
|
251
251
|
const span = (_a = listener.onDispose) === null || _a === void 0 ? void 0 : _a.call(listener, { key });
|
|
252
252
|
try {
|
|
253
|
-
dispose
|
|
253
|
+
invokeWithin(span, dispose);
|
|
254
254
|
}
|
|
255
255
|
catch (error) {
|
|
256
|
-
(_b = span === null || span === void 0 ? void 0 : span.
|
|
256
|
+
(_b = span === null || span === void 0 ? void 0 : span.end) === null || _b === void 0 ? void 0 : _b.call(span, { type: 'failure', error });
|
|
257
257
|
throw error;
|
|
258
258
|
}
|
|
259
|
-
(_c = span === null || span === void 0 ? void 0 : span.end) === null || _c === void 0 ? void 0 : _c.call(span);
|
|
259
|
+
(_c = span === null || span === void 0 ? void 0 : span.end) === null || _c === void 0 ? void 0 : _c.call(span, { type: 'success', value: undefined });
|
|
260
260
|
}
|
|
261
261
|
},
|
|
262
262
|
initialize: (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
263
263
|
var _a, _b, _c;
|
|
264
264
|
const span = (_a = listener.onInitialize) === null || _a === void 0 ? void 0 : _a.call(listener, { key });
|
|
265
265
|
try {
|
|
266
|
-
const instance = observeMethodCalls(yield delegate.initialize(...args), listener, key);
|
|
267
|
-
(_b = span === null || span === void 0 ? void 0 : span.end) === null || _b === void 0 ? void 0 : _b.call(span, {
|
|
266
|
+
const instance = observeMethodCalls(yield invokeWithin(span, () => delegate.initialize(...args)), listener, key);
|
|
267
|
+
(_b = span === null || span === void 0 ? void 0 : span.end) === null || _b === void 0 ? void 0 : _b.call(span, { type: 'success', value: instance });
|
|
268
268
|
return instance;
|
|
269
269
|
}
|
|
270
270
|
catch (error) {
|
|
271
|
-
(_c = span === null || span === void 0 ? void 0 : span.
|
|
271
|
+
(_c = span === null || span === void 0 ? void 0 : span.end) === null || _c === void 0 ? void 0 : _c.call(span, { type: 'failure', error });
|
|
272
272
|
throw error;
|
|
273
273
|
}
|
|
274
274
|
}),
|
|
@@ -277,8 +277,8 @@ function makeObservable(listener, delegate) {
|
|
|
277
277
|
/**
|
|
278
278
|
* Wraps an object with a Proxy to notify the listener of method calls.
|
|
279
279
|
*
|
|
280
|
-
* Methods returning a promise report
|
|
281
|
-
* not when the method returns.
|
|
280
|
+
* Methods returning a promise report their outcome when the promise
|
|
281
|
+
* settles, not when the method returns.
|
|
282
282
|
*
|
|
283
283
|
* @param thing The object whose method calls need to be observed.
|
|
284
284
|
* @param listener The listener notified of method call events.
|
|
@@ -289,6 +289,7 @@ function observeMethodCalls(thing, listener, key) {
|
|
|
289
289
|
if (typeof thing !== 'object' || thing === null) {
|
|
290
290
|
return thing;
|
|
291
291
|
}
|
|
292
|
+
const className = classNameOf(thing);
|
|
292
293
|
return new Proxy(thing, {
|
|
293
294
|
get(target, prop) {
|
|
294
295
|
const value = Reflect.get(target, prop);
|
|
@@ -297,27 +298,28 @@ function observeMethodCalls(thing, listener, key) {
|
|
|
297
298
|
var _a, _b, _c;
|
|
298
299
|
const span = (_a = listener.onMethodCall) === null || _a === void 0 ? void 0 : _a.call(listener, {
|
|
299
300
|
key,
|
|
300
|
-
|
|
301
|
+
className,
|
|
302
|
+
functionName: prop,
|
|
301
303
|
args,
|
|
302
304
|
});
|
|
303
305
|
try {
|
|
304
|
-
const result = value.apply(target, args);
|
|
306
|
+
const result = invokeWithin(span, () => value.apply(target, args));
|
|
305
307
|
if (result instanceof Promise) {
|
|
306
308
|
return result.then((resolved) => {
|
|
307
309
|
var _a;
|
|
308
|
-
(_a = span === null || span === void 0 ? void 0 : span.end) === null || _a === void 0 ? void 0 : _a.call(span, {
|
|
310
|
+
(_a = span === null || span === void 0 ? void 0 : span.end) === null || _a === void 0 ? void 0 : _a.call(span, { type: 'success', value: resolved });
|
|
309
311
|
return resolved;
|
|
310
312
|
}, (error) => {
|
|
311
313
|
var _a;
|
|
312
|
-
(_a = span === null || span === void 0 ? void 0 : span.
|
|
314
|
+
(_a = span === null || span === void 0 ? void 0 : span.end) === null || _a === void 0 ? void 0 : _a.call(span, { type: 'failure', error });
|
|
313
315
|
throw error;
|
|
314
316
|
});
|
|
315
317
|
}
|
|
316
|
-
(_b = span === null || span === void 0 ? void 0 : span.end) === null || _b === void 0 ? void 0 : _b.call(span, { result });
|
|
318
|
+
(_b = span === null || span === void 0 ? void 0 : span.end) === null || _b === void 0 ? void 0 : _b.call(span, { type: 'success', value: result });
|
|
317
319
|
return result;
|
|
318
320
|
}
|
|
319
321
|
catch (error) {
|
|
320
|
-
(_c = span === null || span === void 0 ? void 0 : span.
|
|
322
|
+
(_c = span === null || span === void 0 ? void 0 : span.end) === null || _c === void 0 ? void 0 : _c.call(span, { type: 'failure', error });
|
|
321
323
|
throw error;
|
|
322
324
|
}
|
|
323
325
|
};
|
|
@@ -326,4 +328,29 @@ function observeMethodCalls(thing, listener, key) {
|
|
|
326
328
|
},
|
|
327
329
|
});
|
|
328
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Invokes an operation through the EventSpan's `run` wrapper when the
|
|
333
|
+
* listener provided one, so it can establish ambient state (tracing
|
|
334
|
+
* context) around the operation; invokes the operation directly otherwise.
|
|
335
|
+
*
|
|
336
|
+
* @param span The EventSpan returned by the listener, if any.
|
|
337
|
+
* @param fn The thunk performing the operation.
|
|
338
|
+
* @returns The value returned by `fn`.
|
|
339
|
+
*/
|
|
340
|
+
function invokeWithin(span, fn) {
|
|
341
|
+
return (span === null || span === void 0 ? void 0 : span.run) ? span.run(fn) : fn();
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Resolves the class name of a service instance, or undefined for values
|
|
345
|
+
* that are not instances of a named class (plain object literals,
|
|
346
|
+
* null-prototype objects).
|
|
347
|
+
*
|
|
348
|
+
* @param thing The service instance to inspect.
|
|
349
|
+
* @returns The constructor name, or undefined when there is none to report.
|
|
350
|
+
*/
|
|
351
|
+
function classNameOf(thing) {
|
|
352
|
+
var _a;
|
|
353
|
+
const name = (_a = thing.constructor) === null || _a === void 0 ? void 0 : _a.name;
|
|
354
|
+
return name && name !== 'Object' ? name : undefined;
|
|
355
|
+
}
|
|
329
356
|
//# sourceMappingURL=serviceModule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceModule.js","sourceRoot":"","sources":["../src/serviceModule.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA8D;AAC9D,qDAAkD;AAElD,uDAAoD;AACpD,qCAA+E;
|
|
1
|
+
{"version":3,"file":"serviceModule.js","sourceRoot":"","sources":["../src/serviceModule.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAA8D;AAC9D,qDAAkD;AAElD,uDAAoD;AACpD,qCAA+E;AAS/E;;;;;;;GAOG;AACH,MAAa,aAAa;IACxB;;;;OAIG;IACH,YAA6B,SAA2B;QAA3B,cAAS,GAAT,SAAS,CAAkB;QACtD,yBAAyB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC1C,SAAS,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,wBAAwB,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACU,GAAG,CAAI,GAAkB;;YACpC,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAuB,EAAE,EAAE;gBAC9D,OAAO,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAClC,CAAC,CAAC,CAAC;YAEH,+DAA+D;YAC/D,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,MAAM,IAAI,oCAA2B,CACnC,yCAAyC,GAAG,CAAC,IAAI,EAAE,CACpD,CAAC;YACJ,CAAC;YAED,iCAAiC;YACjC,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,GAAG,CACpC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,aAAkC,EAAE,EAAE;gBAC3D,+EAA+E;gBAC/E,IAAI,aAAa,YAAY,+BAAkB,EAAE,CAAC;oBAChD,OAAO,IAAI,iCAAe,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;gBAClD,CAAC;gBACD,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,CAA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AAK,EAAE,EAAE;;gCACR,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,qDAAG,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;gCACxC,MAAM,KAAK,CAAC;4BACd,CAAC,CACF,CAAC;wBACJ,CAAC;wBACD,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,qDAAG,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;wBAChD,OAAO,MAAM,CAAC;oBAChB,CAAC;oBAAC,OAAO,KAAK,EAAE,CAAC;wBACf,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,qDAAG,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;wBACxC,MAAM,KAAK,CAAC;oBACd,CAAC;gBACH,CAAC,CAAC;YACJ,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,YAAY,CAAI,IAAsB,EAAE,EAAW;IAC1D,OAAO,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,EAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AACzC,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,WAAW,CAAC,KAAa;;IAChC,MAAM,IAAI,GAAG,MAAA,KAAK,CAAC,WAAW,0CAAE,IAAI,CAAC;IACrC,OAAO,IAAI,IAAI,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -5,50 +5,51 @@ import type { ServiceKey } from './serviceKey';
|
|
|
5
5
|
* disposal, or method call), returned by a ServiceEventListener when the
|
|
6
6
|
* operation starts.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* `end` is invoked exactly once when the operation finishes, so
|
|
9
|
+
* implementations can close over per-call state (a start time, a span
|
|
10
|
+
* handle, a correlation id) without any bookkeeping to pair concurrent
|
|
11
|
+
* start/finish events.
|
|
12
12
|
*/
|
|
13
13
|
export interface EventSpan {
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Optional wrapper around the operation itself. When present, the module
|
|
16
|
+
* invokes the operation as `run(() => operation())`, so the listener can
|
|
17
|
+
* establish ambient state that the operation body and its async
|
|
18
|
+
* continuations inherit — this is what lets spans of nested service
|
|
19
|
+
* calls form a parent-child hierarchy.
|
|
18
20
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
+
* Implementations must invoke `fn` exactly once, synchronously, and
|
|
22
|
+
* return its result unchanged (for async operations, the promise itself).
|
|
23
|
+
* `end` is still delivered separately when the operation finishes.
|
|
21
24
|
*
|
|
22
|
-
* @param
|
|
23
|
-
*
|
|
24
|
-
* instance), absent for dispose spans.
|
|
25
|
-
* @return void
|
|
25
|
+
* @param fn - A thunk that performs the operation.
|
|
26
|
+
* @return The value returned by `fn`.
|
|
26
27
|
*/
|
|
27
|
-
|
|
28
|
+
run?<T>(fn: () => T): T;
|
|
28
29
|
|
|
29
30
|
/**
|
|
30
|
-
* Invoked when the operation
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* Invoked exactly once when the operation finishes, whether it succeeded
|
|
32
|
+
* or failed. For methods that return a promise, this fires when the
|
|
33
|
+
* promise settles, not when the method returns. On failure, the error is
|
|
34
|
+
* rethrown to the caller after this is invoked.
|
|
33
35
|
*
|
|
34
|
-
*
|
|
36
|
+
* Whether to retain or log the outcome is the implementation's choice;
|
|
37
|
+
* values are passed by reference, so implementations must not mutate them.
|
|
38
|
+
*
|
|
39
|
+
* @param outcome - How the operation finished, and its value or error.
|
|
35
40
|
* @return void
|
|
36
41
|
*/
|
|
37
|
-
|
|
42
|
+
end?(outcome: EventOutcome): void;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
46
|
+
* How an operation finished, delivered to EventSpan.end: `success` carries
|
|
47
|
+
* the value produced by the operation (the return or resolved value for
|
|
48
|
+
* method calls, the service instance for initialize, undefined for
|
|
49
|
+
* dispose); `failure` carries the error that was thrown or rejected.
|
|
44
50
|
*/
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
* The value produced by the operation: the return value for synchronous
|
|
48
|
-
* methods, or the resolved value for methods returning a promise.
|
|
49
|
-
*/
|
|
50
|
-
result: unknown;
|
|
51
|
-
}
|
|
51
|
+
export type EventOutcome =
|
|
52
|
+
{ type: 'success'; value: unknown } | { type: 'failure'; error: unknown };
|
|
52
53
|
|
|
53
54
|
/**
|
|
54
55
|
* Context of a service initialization, delivered to onInitialize.
|
|
@@ -82,10 +83,18 @@ export interface MethodCallContext {
|
|
|
82
83
|
*/
|
|
83
84
|
key: ServiceKey<unknown>;
|
|
84
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The name of the class implementing the service (the instance's
|
|
88
|
+
* constructor name), which may differ from `key.name`. Undefined for
|
|
89
|
+
* services that are not instances of a named class, such as plain
|
|
90
|
+
* object literals.
|
|
91
|
+
*/
|
|
92
|
+
className?: string;
|
|
93
|
+
|
|
85
94
|
/**
|
|
86
95
|
* The name of the method that is being called.
|
|
87
96
|
*/
|
|
88
|
-
|
|
97
|
+
functionName: string;
|
|
89
98
|
|
|
90
99
|
/**
|
|
91
100
|
* The arguments the method was invoked with, passed by reference;
|
package/src/serviceModule.ts
CHANGED
|
@@ -3,7 +3,10 @@ import { ServiceFactory } from './serviceFactory';
|
|
|
3
3
|
import { ServiceScope } from './serviceScope';
|
|
4
4
|
import { ServiceSelector } from './serviceSelector';
|
|
5
5
|
import { ServiceFactoryNotFoundError, ServiceModuleInitError } from './errors';
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
EventSpan,
|
|
8
|
+
ServiceEventListener,
|
|
9
|
+
} from './serviceEventListener';
|
|
7
10
|
|
|
8
11
|
type GenericFactory = ServiceFactory<unknown, readonly ServiceKey<any>[]>;
|
|
9
12
|
type GenericKey = ServiceKey<any>;
|
|
@@ -265,9 +268,9 @@ function isSuitable<T, D extends readonly ServiceKey<any>[]>(
|
|
|
265
268
|
* lifecycle events and method calls.
|
|
266
269
|
*
|
|
267
270
|
* For each of initialize, dispose, and method calls, the listener is invoked
|
|
268
|
-
* at the start of the operation and may return an EventSpan whose `end`
|
|
269
|
-
*
|
|
270
|
-
* being reported.
|
|
271
|
+
* at the start of the operation and may return an EventSpan whose `end` is
|
|
272
|
+
* called with the outcome when the operation finishes. Errors are rethrown
|
|
273
|
+
* after being reported.
|
|
271
274
|
*
|
|
272
275
|
* @param listener The listener notified of lifecycle and method call events.
|
|
273
276
|
* @param delegate The original service factory to be instrumented.
|
|
@@ -288,26 +291,26 @@ function makeObservable<T, D extends readonly ServiceKey<any>[]>(
|
|
|
288
291
|
if (dispose) {
|
|
289
292
|
const span = listener.onDispose?.({ key });
|
|
290
293
|
try {
|
|
291
|
-
dispose
|
|
294
|
+
invokeWithin(span, dispose);
|
|
292
295
|
} catch (error) {
|
|
293
|
-
span?.
|
|
296
|
+
span?.end?.({ type: 'failure', error });
|
|
294
297
|
throw error;
|
|
295
298
|
}
|
|
296
|
-
span?.end?.();
|
|
299
|
+
span?.end?.({ type: 'success', value: undefined });
|
|
297
300
|
}
|
|
298
301
|
},
|
|
299
302
|
initialize: async (...args) => {
|
|
300
303
|
const span = listener.onInitialize?.({ key });
|
|
301
304
|
try {
|
|
302
305
|
const instance = observeMethodCalls(
|
|
303
|
-
await delegate.initialize(...args),
|
|
306
|
+
await invokeWithin(span, () => delegate.initialize(...args)),
|
|
304
307
|
listener,
|
|
305
308
|
key,
|
|
306
309
|
);
|
|
307
|
-
span?.end?.({
|
|
310
|
+
span?.end?.({ type: 'success', value: instance });
|
|
308
311
|
return instance;
|
|
309
312
|
} catch (error) {
|
|
310
|
-
span?.
|
|
313
|
+
span?.end?.({ type: 'failure', error });
|
|
311
314
|
throw error;
|
|
312
315
|
}
|
|
313
316
|
},
|
|
@@ -317,8 +320,8 @@ function makeObservable<T, D extends readonly ServiceKey<any>[]>(
|
|
|
317
320
|
/**
|
|
318
321
|
* Wraps an object with a Proxy to notify the listener of method calls.
|
|
319
322
|
*
|
|
320
|
-
* Methods returning a promise report
|
|
321
|
-
* not when the method returns.
|
|
323
|
+
* Methods returning a promise report their outcome when the promise
|
|
324
|
+
* settles, not when the method returns.
|
|
322
325
|
*
|
|
323
326
|
* @param thing The object whose method calls need to be observed.
|
|
324
327
|
* @param listener The listener notified of method call events.
|
|
@@ -334,6 +337,8 @@ function observeMethodCalls(
|
|
|
334
337
|
return thing;
|
|
335
338
|
}
|
|
336
339
|
|
|
340
|
+
const className = classNameOf(thing);
|
|
341
|
+
|
|
337
342
|
return new Proxy(thing, {
|
|
338
343
|
get(target, prop) {
|
|
339
344
|
const value = Reflect.get(target, prop);
|
|
@@ -341,27 +346,28 @@ function observeMethodCalls(
|
|
|
341
346
|
return (...args: unknown[]) => {
|
|
342
347
|
const span = listener.onMethodCall?.({
|
|
343
348
|
key,
|
|
344
|
-
|
|
349
|
+
className,
|
|
350
|
+
functionName: prop,
|
|
345
351
|
args,
|
|
346
352
|
});
|
|
347
353
|
try {
|
|
348
|
-
const result = value.apply(target, args);
|
|
354
|
+
const result = invokeWithin(span, () => value.apply(target, args));
|
|
349
355
|
if (result instanceof Promise) {
|
|
350
356
|
return result.then(
|
|
351
357
|
(resolved) => {
|
|
352
|
-
span?.end?.({
|
|
358
|
+
span?.end?.({ type: 'success', value: resolved });
|
|
353
359
|
return resolved;
|
|
354
360
|
},
|
|
355
361
|
(error) => {
|
|
356
|
-
span?.
|
|
362
|
+
span?.end?.({ type: 'failure', error });
|
|
357
363
|
throw error;
|
|
358
364
|
},
|
|
359
365
|
);
|
|
360
366
|
}
|
|
361
|
-
span?.end?.({ result });
|
|
367
|
+
span?.end?.({ type: 'success', value: result });
|
|
362
368
|
return result;
|
|
363
369
|
} catch (error) {
|
|
364
|
-
span?.
|
|
370
|
+
span?.end?.({ type: 'failure', error });
|
|
365
371
|
throw error;
|
|
366
372
|
}
|
|
367
373
|
};
|
|
@@ -370,3 +376,29 @@ function observeMethodCalls(
|
|
|
370
376
|
},
|
|
371
377
|
});
|
|
372
378
|
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Invokes an operation through the EventSpan's `run` wrapper when the
|
|
382
|
+
* listener provided one, so it can establish ambient state (tracing
|
|
383
|
+
* context) around the operation; invokes the operation directly otherwise.
|
|
384
|
+
*
|
|
385
|
+
* @param span The EventSpan returned by the listener, if any.
|
|
386
|
+
* @param fn The thunk performing the operation.
|
|
387
|
+
* @returns The value returned by `fn`.
|
|
388
|
+
*/
|
|
389
|
+
function invokeWithin<T>(span: EventSpan | void, fn: () => T): T {
|
|
390
|
+
return span?.run ? span.run(fn) : fn();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Resolves the class name of a service instance, or undefined for values
|
|
395
|
+
* that are not instances of a named class (plain object literals,
|
|
396
|
+
* null-prototype objects).
|
|
397
|
+
*
|
|
398
|
+
* @param thing The service instance to inspect.
|
|
399
|
+
* @returns The constructor name, or undefined when there is none to report.
|
|
400
|
+
*/
|
|
401
|
+
function classNameOf(thing: object): string | undefined {
|
|
402
|
+
const name = thing.constructor?.name;
|
|
403
|
+
return name && name !== 'Object' ? name : undefined;
|
|
404
|
+
}
|