UNPKG

5.21 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved.
3// Licensed under the MIT license. See LICENSE file in the project root for details.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.channel = exports.makePatchingRequire = void 0;
6var patchRequire_1 = require("./patchRequire");
7var patchRequire_2 = require("./patchRequire");
8Object.defineProperty(exports, "makePatchingRequire", { enumerable: true, get: function () { return patchRequire_2.makePatchingRequire; } });
9var trueFilter = function (publishing) { return true; };
10var ContextPreservingEventEmitter = /** @class */ (function () {
11 function ContextPreservingEventEmitter() {
12 this.version = require("./../../package.json").version; // Allow for future versions to replace things?
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; // Avoid reentrancy
31 }
32 var listeners = this.subscribers[name];
33 // Note: Listeners called synchronously to preserve context
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 // Subscriber threw an error
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 // Used for tests
75 ContextPreservingEventEmitter.prototype.reset = function () {
76 var _this = this;
77 this.subscribers = {};
78 this.contextPreservationFunction = function (cb) { return cb; };
79 // Modify the knownPatches object rather than replace, since a reference will be used in the require patcher
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}());
100if (!global.diagnosticsSource) {
101 global.diagnosticsSource = new ContextPreservingEventEmitter();
102 // TODO: should this only patch require after at least one monkey patch is registered?
103 /* tslint:disable-next-line:no-var-requires */
104 var moduleModule = require("module");
105 // Note: We pass in the object now before any patches are registered, but the object is passed by reference
106 // so any updates made to the object will be visible in the patcher.
107 moduleModule.prototype.require = patchRequire_1.makePatchingRequire(global.diagnosticsSource.getPatchesObject());
108}
109exports.channel = global.diagnosticsSource;
110//# sourceMappingURL=channel.js.map
\No newline at end of file