index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. const events_1 = require("events");
  3. const async_hooks_1 = require("async_hooks");
  4. const kEventEmitter = Symbol('kEventEmitter');
  5. const kAsyncResource = Symbol('kAsyncResource');
  6. class EventEmitterReferencingAsyncResource extends async_hooks_1.AsyncResource {
  7. constructor(ee, type, options) {
  8. super(type, options);
  9. this[kEventEmitter] = ee;
  10. }
  11. get eventEmitter() {
  12. return this[kEventEmitter];
  13. }
  14. }
  15. class EventEmitterAsyncResource extends events_1.EventEmitter {
  16. constructor(options) {
  17. let name;
  18. if (typeof options === 'string') {
  19. name = options;
  20. options = undefined;
  21. }
  22. else {
  23. name = (options === null || options === void 0 ? void 0 : options.name) || new.target.name;
  24. }
  25. super(options);
  26. this[kAsyncResource] =
  27. new EventEmitterReferencingAsyncResource(this, name, options);
  28. }
  29. emit(event, ...args) {
  30. return this.asyncResource.runInAsyncScope(super.emit, this, event, ...args);
  31. }
  32. emitDestroy() {
  33. this.asyncResource.emitDestroy();
  34. }
  35. asyncId() {
  36. return this.asyncResource.asyncId();
  37. }
  38. triggerAsyncId() {
  39. return this.asyncResource.triggerAsyncId();
  40. }
  41. get asyncResource() {
  42. return this[kAsyncResource];
  43. }
  44. static get EventEmitterAsyncResource() { return EventEmitterAsyncResource; }
  45. }
  46. module.exports = EventEmitterAsyncResource;
  47. //# sourceMappingURL=index.js.map