@contrail/util 1.1.3 → 1.1.4

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.
@@ -2,6 +2,10 @@ export declare enum TimeUnit {
2
2
  MILLISECOND = "ms",
3
3
  SECOND = "s"
4
4
  }
5
+ interface DumpOptions {
6
+ unit?: TimeUnit;
7
+ format?: boolean;
8
+ }
5
9
  export declare class Timer {
6
10
  private startTime?;
7
11
  private endTime?;
@@ -13,4 +17,16 @@ export declare class Timer {
13
17
  getElapsedTime(timeUnit?: TimeUnit): number;
14
18
  private getCurrentTime;
15
19
  }
20
+ export declare class TimerManager {
21
+ private timers;
22
+ private formatDuration;
23
+ start(key: string): void;
24
+ stop(key: string): void;
25
+ reset(key: string): void;
26
+ getElapsedTime(key: string, unit?: TimeUnit): number;
27
+ has(key: string): boolean;
28
+ clearAll(): void;
29
+ dump(options?: DumpOptions): Record<string, number | string>;
30
+ }
16
31
  export declare function delay(ms: number): Promise<unknown>;
32
+ export {};
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.delay = exports.Timer = exports.TimeUnit = void 0;
12
+ exports.delay = exports.TimerManager = exports.Timer = exports.TimeUnit = void 0;
13
13
  var TimeUnit;
14
14
  (function (TimeUnit) {
15
15
  TimeUnit["MILLISECOND"] = "ms";
@@ -55,6 +55,60 @@ class Timer {
55
55
  }
56
56
  }
57
57
  exports.Timer = Timer;
58
+ class TimerManager {
59
+ constructor() {
60
+ this.timers = new Map();
61
+ }
62
+ formatDuration(duration) {
63
+ return duration > 1000 ? `${(duration / 1000).toFixed(2)}s` : `${duration}ms`;
64
+ }
65
+ start(key) {
66
+ if (this.timers.has(key)) {
67
+ throw new Error(`Timer with key "${key}" already started`);
68
+ }
69
+ const timer = new Timer();
70
+ timer.start();
71
+ this.timers.set(key, timer);
72
+ }
73
+ stop(key) {
74
+ const timer = this.timers.get(key);
75
+ if (!timer) {
76
+ throw new Error(`Timer with key "${key}" not found`);
77
+ }
78
+ timer.stop();
79
+ }
80
+ reset(key) {
81
+ this.timers.delete(key);
82
+ }
83
+ getElapsedTime(key, unit = TimeUnit.MILLISECOND) {
84
+ const timer = this.timers.get(key);
85
+ if (!timer) {
86
+ throw new Error(`Timer with key "${key}" not found`);
87
+ }
88
+ return timer.getElapsedTime(unit);
89
+ }
90
+ has(key) {
91
+ return this.timers.has(key);
92
+ }
93
+ clearAll() {
94
+ this.timers.clear();
95
+ }
96
+ dump(options = {}) {
97
+ const { unit = TimeUnit.MILLISECOND, format = false } = options;
98
+ const result = {};
99
+ for (const [key, timer] of this.timers.entries()) {
100
+ try {
101
+ const duration = timer.getElapsedTime(unit);
102
+ result[key] = format ? this.formatDuration(duration) : duration;
103
+ }
104
+ catch (_a) {
105
+ result[key] = NaN;
106
+ }
107
+ }
108
+ return result;
109
+ }
110
+ }
111
+ exports.TimerManager = TimerManager;
58
112
  function delay(ms) {
59
113
  return __awaiter(this, void 0, void 0, function* () {
60
114
  return new Promise((resolve) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",