1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.channel = exports.makePatchingRequire = void 0;
|
6 | var patchRequire_1 = require("./patchRequire");
|
7 | var patchRequire_2 = require("./patchRequire");
|
8 | Object.defineProperty(exports, "makePatchingRequire", { enumerable: true, get: function () { return patchRequire_2.makePatchingRequire; } });
|
9 | var trueFilter = function (publishing) { return true; };
|
10 | var ContextPreservingEventEmitter = (function () {
|
11 | function ContextPreservingEventEmitter() {
|
12 | this.version = require("./../../package.json").version;
|
13 | this.subscribers = {};
|
14 | this.contextPreservationFunction = function (cb) { return cb; };
|
15 | this.knownPatches = {};
|
16 | this.currentlyPublishing = false;
|
17 | }
|
18 | ContextPreservingEventEmitter.prototype.shouldPublish = function (name) {
|
19 | var listeners = this.subscribers[name];
|
20 | if (listeners) {
|
21 | return listeners.some(function (_a) {
|
22 | var filter = _a.filter;
|
23 | return !filter || filter(false);
|
24 | });
|
25 | }
|
26 | return false;
|
27 | };
|
28 | ContextPreservingEventEmitter.prototype.publish = function (name, event) {
|
29 | if (this.currentlyPublishing) {
|
30 | return;
|
31 | }
|
32 | var listeners = this.subscribers[name];
|
33 |
|
34 | if (listeners) {
|
35 | var standardEvent_1 = {
|
36 | timestamp: Date.now(),
|
37 | data: event,
|
38 | };
|
39 | this.currentlyPublishing = true;
|
40 | listeners.forEach(function (_a) {
|
41 | var listener = _a.listener, filter = _a.filter;
|
42 | try {
|
43 | if (filter && filter(true)) {
|
44 | listener(standardEvent_1);
|
45 | }
|
46 | }
|
47 | catch (e) {
|
48 |
|
49 | }
|
50 | });
|
51 | this.currentlyPublishing = false;
|
52 | }
|
53 | };
|
54 | ContextPreservingEventEmitter.prototype.subscribe = function (name, listener, filter) {
|
55 | if (filter === void 0) { filter = trueFilter; }
|
56 | if (!this.subscribers[name]) {
|
57 | this.subscribers[name] = [];
|
58 | }
|
59 | this.subscribers[name].push({ listener: listener, filter: filter });
|
60 | };
|
61 | ContextPreservingEventEmitter.prototype.unsubscribe = function (name, listener, filter) {
|
62 | if (filter === void 0) { filter = trueFilter; }
|
63 | var listeners = this.subscribers[name];
|
64 | if (listeners) {
|
65 | for (var index = 0; index < listeners.length; ++index) {
|
66 | if (listeners[index].listener === listener && listeners[index].filter === filter) {
|
67 | listeners.splice(index, 1);
|
68 | return true;
|
69 | }
|
70 | }
|
71 | }
|
72 | return false;
|
73 | };
|
74 |
|
75 | ContextPreservingEventEmitter.prototype.reset = function () {
|
76 | var _this = this;
|
77 | this.subscribers = {};
|
78 | this.contextPreservationFunction = function (cb) { return cb; };
|
79 |
|
80 | Object.getOwnPropertyNames(this.knownPatches).forEach(function (prop) { return delete _this.knownPatches[prop]; });
|
81 | };
|
82 | ContextPreservingEventEmitter.prototype.bindToContext = function (cb) {
|
83 | return this.contextPreservationFunction(cb);
|
84 | };
|
85 | ContextPreservingEventEmitter.prototype.addContextPreservation = function (preserver) {
|
86 | var previousPreservationStack = this.contextPreservationFunction;
|
87 | this.contextPreservationFunction = (function (cb) { return preserver(previousPreservationStack(cb)); });
|
88 | };
|
89 | ContextPreservingEventEmitter.prototype.registerMonkeyPatch = function (packageName, patcher) {
|
90 | if (!this.knownPatches[packageName]) {
|
91 | this.knownPatches[packageName] = [];
|
92 | }
|
93 | this.knownPatches[packageName].push(patcher);
|
94 | };
|
95 | ContextPreservingEventEmitter.prototype.getPatchesObject = function () {
|
96 | return this.knownPatches;
|
97 | };
|
98 | return ContextPreservingEventEmitter;
|
99 | }());
|
100 | if (!global.diagnosticsSource) {
|
101 | global.diagnosticsSource = new ContextPreservingEventEmitter();
|
102 |
|
103 |
|
104 | var moduleModule = require("module");
|
105 |
|
106 |
|
107 | moduleModule.prototype.require = patchRequire_1.makePatchingRequire(global.diagnosticsSource.getPatchesObject());
|
108 | }
|
109 | exports.channel = global.diagnosticsSource;
|
110 |
|
\ | No newline at end of file |