@mlightcad/common
Version:
[](https://opensource.org/licenses/MIT) [](https://www.npmjs.com/package/@mlightcad/common)
119 lines • 4.23 kB
JavaScript
/**
* @fileoverview Performance monitoring and collection system for the AutoCAD Common library.
*
* This module provides a singleton-based performance collector that can store,
* retrieve, and format performance metrics for debugging and optimization purposes.
*
* @module AcCmPerformanceCollector
* @version 1.0.0
*/
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
/**
* A singleton class for collecting and managing performance data.
* All entries must have a unique name. Entries are stored in a Map.
*/
var AcCmPerformanceCollector = /** @class */ (function () {
/**
* Private constructor to enforce singleton pattern.
*/
function AcCmPerformanceCollector() {
/** Map of performance entries keyed by their unique name. */
this.entries = new Map();
}
/**
* Retrieves the singleton instance of the AcCmPerformanceCollector.
* @returns The shared AcCmPerformanceCollector instance.
*/
AcCmPerformanceCollector.getInstance = function () {
if (!AcCmPerformanceCollector.instance) {
AcCmPerformanceCollector.instance = new AcCmPerformanceCollector();
}
return AcCmPerformanceCollector.instance;
};
/**
* Adds or replaces a performance entry by name.
* @template T The type of the performance data.
* @param entry A performance entry object with name, data, and format method.
*/
AcCmPerformanceCollector.prototype.collect = function (entry) {
this.entries.set(entry.name, entry);
};
/**
* Logs all performance entries to the console using their format method.
*/
AcCmPerformanceCollector.prototype.printAll = function () {
var e_1, _a;
try {
for (var _b = __values(this.entries), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), name_1 = _d[0], entry = _d[1];
console.log("".concat(name_1, ":"));
console.log(entry.format());
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Clears all collected performance entries.
*/
AcCmPerformanceCollector.prototype.clear = function () {
this.entries.clear();
};
/**
* Retrieves all entries as an array.
* @returns A copy of all performance entries.
*/
AcCmPerformanceCollector.prototype.getAll = function () {
return Array.from(this.entries.values());
};
/**
* Gets a single entry by name.
* @param name The unique name of the entry.
* @returns The matching entry or undefined.
*/
AcCmPerformanceCollector.prototype.getEntry = function (name) {
return this.entries.get(name);
};
/**
* Removes an entry by name.
* @param name The name of the entry to remove.
* @returns True if the entry was removed; false if not found.
*/
AcCmPerformanceCollector.prototype.remove = function (name) {
return this.entries.delete(name);
};
return AcCmPerformanceCollector;
}());
export { AcCmPerformanceCollector };
//# sourceMappingURL=AcCmPerformanceCollector.js.map