@rsdk/common 3.2.0-next.2 → 3.4.2-next.2

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.4.2-next.2](https://github.com/R-Vision/rsdk/compare/v3.4.2-next.1...v3.4.2-next.2) (2023-09-07)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **common:** added dedicated stopwatch exception ([e81a8f1](https://github.com/R-Vision/rsdk/commit/e81a8f10898473751fa81d473503e488719d9750))
11
+ * **common:** fix stopwatch and migrate to bigint ([08a14ba](https://github.com/R-Vision/rsdk/commit/08a14bacd0342ee7e0d3d9224dc93f102fb309be))
12
+ * **common:** stopwatch return this on methods, dont new instance ([61c8be2](https://github.com/R-Vision/rsdk/commit/61c8be238e0c4aea582981196e65bc73823872ff))
13
+
14
+ ## [3.2.0](https://github.com/R-Vision/rsdk/compare/v3.2.0-next.2...v3.2.0) (2023-08-09)
15
+
16
+ **Note:** Version bump only for package @rsdk/common
17
+
6
18
  ## [3.2.0-next.2](https://github.com/R-Vision/rsdk/compare/v3.2.0-next.1...v3.2.0-next.2) (2023-08-09)
7
19
 
8
20
  **Note:** Version bump only for package @rsdk/common
@@ -1,4 +1,12 @@
1
- export type HrTime = [number, number];
1
+ export declare abstract class StopwatchException extends Error {
2
+ name: string;
3
+ }
4
+ export declare class StopwatchAlreadyStarted extends StopwatchException {
5
+ constructor();
6
+ }
7
+ export declare class StopwatchNotStarted extends StopwatchException {
8
+ constructor();
9
+ }
2
10
  /**
3
11
  * Timer used to mesure execution time.
4
12
  *
@@ -7,8 +15,7 @@ export type HrTime = [number, number];
7
15
  */
8
16
  export declare class Stopwatch {
9
17
  private _start;
10
- private _current;
11
- private _isRunning;
18
+ private accumulateDuration;
12
19
  /**
13
20
  * Number of milliseconds passed from stopwatch started.
14
21
  *
@@ -21,10 +28,11 @@ export declare class Stopwatch {
21
28
  * Time in process.hrtime format passed from stopwatch started.
22
29
  *
23
30
  * @readonly
24
- * @type {HrTime}
31
+ * @type {BigInt}
25
32
  * @memberof Stopwatch
26
33
  */
27
- get elapsed(): HrTime;
34
+ get elapsed(): bigint;
35
+ get isRunning(): boolean;
28
36
  /**
29
37
  * Stops the stopwatch and clears time.
30
38
  * Returns this.
@@ -32,7 +40,7 @@ export declare class Stopwatch {
32
40
  * @returns {Stopwatch}
33
41
  * @memberof Stopwatch
34
42
  */
35
- reset(): Stopwatch;
43
+ reset(): this;
36
44
  /**
37
45
  * Clears time and restarts the stopwatch.
38
46
  * Returns this.
@@ -40,7 +48,7 @@ export declare class Stopwatch {
40
48
  * @returns {Stopwatch}
41
49
  * @memberof Stopwatch
42
50
  */
43
- restart(): Stopwatch;
51
+ restart(): this;
44
52
  /**
45
53
  * Starts stopwatch.
46
54
  * Returns this.
@@ -48,7 +56,7 @@ export declare class Stopwatch {
48
56
  * @returns {Stopwatch}
49
57
  * @memberof Stopwatch
50
58
  */
51
- start(): Stopwatch;
59
+ start(): this;
52
60
  /**
53
61
  * Pauses stopwatch. Can be resumed with .start()
54
62
  * Returns this.
@@ -56,6 +64,6 @@ export declare class Stopwatch {
56
64
  * @returns {Stopwatch}
57
65
  * @memberof Stopwatch
58
66
  */
59
- pause(): Stopwatch;
67
+ pause(): this;
60
68
  private convertToMilliseconds;
61
69
  }
@@ -1,6 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Stopwatch = void 0;
3
+ exports.Stopwatch = exports.StopwatchNotStarted = exports.StopwatchAlreadyStarted = exports.StopwatchException = void 0;
4
+ class StopwatchException extends Error {
5
+ name = this.constructor.name;
6
+ }
7
+ exports.StopwatchException = StopwatchException;
8
+ class StopwatchAlreadyStarted extends StopwatchException {
9
+ constructor() {
10
+ super('Stopwatch already started');
11
+ }
12
+ }
13
+ exports.StopwatchAlreadyStarted = StopwatchAlreadyStarted;
14
+ class StopwatchNotStarted extends StopwatchException {
15
+ constructor() {
16
+ super('Stopwatch not started');
17
+ }
18
+ }
19
+ exports.StopwatchNotStarted = StopwatchNotStarted;
4
20
  /**
5
21
  * Timer used to mesure execution time.
6
22
  *
@@ -9,8 +25,7 @@ exports.Stopwatch = void 0;
9
25
  */
10
26
  class Stopwatch {
11
27
  _start = null;
12
- _current = null;
13
- _isRunning = false;
28
+ accumulateDuration = 0n;
14
29
  /**
15
30
  * Number of milliseconds passed from stopwatch started.
16
31
  *
@@ -25,14 +40,20 @@ class Stopwatch {
25
40
  * Time in process.hrtime format passed from stopwatch started.
26
41
  *
27
42
  * @readonly
28
- * @type {HrTime}
43
+ * @type {BigInt}
29
44
  * @memberof Stopwatch
30
45
  */
31
46
  get elapsed() {
32
- if (!this._start || !this._current) {
33
- return [0, 0];
47
+ if (!this._start && !this.accumulateDuration) {
48
+ throw new StopwatchNotStarted();
34
49
  }
35
- return this._isRunning ? process.hrtime(this._start) : this._current;
50
+ const currentDuration = this._start
51
+ ? process.hrtime.bigint() - this._start
52
+ : 0n;
53
+ return currentDuration + this.accumulateDuration;
54
+ }
55
+ get isRunning() {
56
+ return !this._start;
36
57
  }
37
58
  /**
38
59
  * Stops the stopwatch and clears time.
@@ -43,8 +64,7 @@ class Stopwatch {
43
64
  */
44
65
  reset() {
45
66
  this._start = null;
46
- this._current = null;
47
- this._isRunning = false;
67
+ this.accumulateDuration = 0n;
48
68
  return this;
49
69
  }
50
70
  /**
@@ -67,10 +87,10 @@ class Stopwatch {
67
87
  * @memberof Stopwatch
68
88
  */
69
89
  start() {
70
- if (!this._start) {
71
- this._start = process.hrtime();
90
+ if (this._start) {
91
+ throw new StopwatchAlreadyStarted();
72
92
  }
73
- this._isRunning = true;
93
+ this._start = process.hrtime.bigint();
74
94
  return this;
75
95
  }
76
96
  /**
@@ -81,16 +101,16 @@ class Stopwatch {
81
101
  * @memberof Stopwatch
82
102
  */
83
103
  pause() {
84
- if (this._start) {
85
- this._current = process.hrtime(this._start);
86
- this._isRunning = false;
104
+ if (!this._start) {
105
+ throw new StopwatchNotStarted();
87
106
  }
107
+ this.accumulateDuration += this.elapsed;
108
+ this._start = null;
88
109
  return this;
89
110
  }
90
111
  convertToMilliseconds(hrtime) {
91
- const nanoseconds = hrtime[0] * 1e9 + hrtime[1];
92
- const milliseconds = nanoseconds / 1e6;
93
- return milliseconds;
112
+ const milliseconds = hrtime / BigInt(1e6);
113
+ return Number(milliseconds);
94
114
  }
95
115
  }
96
116
  exports.Stopwatch = Stopwatch;
@@ -1 +1 @@
1
- {"version":3,"file":"stopwatch.class.js","sourceRoot":"","sources":["../../src/time/stopwatch.class.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AACH,MAAa,SAAS;IACZ,MAAM,GAAkB,IAAI,CAAC;IAC7B,QAAQ,GAAkB,IAAI,CAAC;IAC/B,UAAU,GAAG,KAAK,CAAC;IAE3B;;;;;;OAMG;IACH,IAAI,mBAAmB;QACrB,OAAO,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAClD,CAAC;IAED;;;;;;OAMG;IACH,IAAI,OAAO;QACT,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SACf;QAED,OAAO,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;IACvE,CAAC;IAED;;;;;;OAMG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QAExB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;O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ED,IAAI,CAAC,kBAAkB,IAAI,IAAI,CAAC,OAAO,CAAC;QACxC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,qBAAqB,CAAC,MAAc;QAC1C,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAE1C,OAAO,MAAM,CAAC,YAAY,CAAC,CAAC;IAC9B,CAAC;CACF;AAxGD,8BAwGC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdk/common",
3
- "version": "3.2.0-next.2",
3
+ "version": "3.4.2-next.2",
4
4
  "description": "Useful common classes, functions and types",
5
5
  "license": "Apache License 2.0",
6
6
  "publishConfig": {
@@ -13,5 +13,5 @@
13
13
  "scripts": {
14
14
  "prepublishOnly": "npm run build"
15
15
  },
16
- "gitHead": "a8b94e2e2c13c8b7077a0942cd64a19524b258c7"
16
+ "gitHead": "fc1f5623ceba7ab218bc9168afd29afe127437b6"
17
17
  }
@@ -1,4 +1,18 @@
1
- export type HrTime = [number, number];
1
+ export abstract class StopwatchException extends Error {
2
+ override name = this.constructor.name;
3
+ }
4
+
5
+ export class StopwatchAlreadyStarted extends StopwatchException {
6
+ constructor() {
7
+ super('Stopwatch already started');
8
+ }
9
+ }
10
+
11
+ export class StopwatchNotStarted extends StopwatchException {
12
+ constructor() {
13
+ super('Stopwatch not started');
14
+ }
15
+ }
2
16
 
3
17
  /**
4
18
  * Timer used to mesure execution time.
@@ -7,9 +21,8 @@ export type HrTime = [number, number];
7
21
  * @class Stopwatch
8
22
  */
9
23
  export class Stopwatch {
10
- private _start: HrTime | null = null;
11
- private _current: HrTime | null = null;
12
- private _isRunning = false;
24
+ private _start: bigint | null = null;
25
+ private accumulateDuration = 0n;
13
26
 
14
27
  /**
15
28
  * Number of milliseconds passed from stopwatch started.
@@ -26,15 +39,23 @@ export class Stopwatch {
26
39
  * Time in process.hrtime format passed from stopwatch started.
27
40
  *
28
41
  * @readonly
29
- * @type {HrTime}
42
+ * @type {BigInt}
30
43
  * @memberof Stopwatch
31
44
  */
32
- get elapsed(): HrTime {
33
- if (!this._start || !this._current) {
34
- return [0, 0];
45
+ get elapsed(): bigint {
46
+ if (!this._start && !this.accumulateDuration) {
47
+ throw new StopwatchNotStarted();
35
48
  }
36
49
 
37
- return this._isRunning ? process.hrtime(this._start) : this._current;
50
+ const currentDuration = this._start
51
+ ? process.hrtime.bigint() - this._start
52
+ : 0n;
53
+
54
+ return currentDuration + this.accumulateDuration;
55
+ }
56
+
57
+ get isRunning(): boolean {
58
+ return !this._start;
38
59
  }
39
60
 
40
61
  /**
@@ -44,10 +65,9 @@ export class Stopwatch {
44
65
  * @returns {Stopwatch}
45
66
  * @memberof Stopwatch
46
67
  */
47
- reset(): Stopwatch {
68
+ reset(): this {
48
69
  this._start = null;
49
- this._current = null;
50
- this._isRunning = false;
70
+ this.accumulateDuration = 0n;
51
71
 
52
72
  return this;
53
73
  }
@@ -59,7 +79,7 @@ export class Stopwatch {
59
79
  * @returns {Stopwatch}
60
80
  * @memberof Stopwatch
61
81
  */
62
- restart(): Stopwatch {
82
+ restart(): this {
63
83
  this.reset();
64
84
  this.start();
65
85
 
@@ -73,12 +93,11 @@ export class Stopwatch {
73
93
  * @returns {Stopwatch}
74
94
  * @memberof Stopwatch
75
95
  */
76
- start(): Stopwatch {
77
- if (!this._start) {
78
- this._start = process.hrtime();
96
+ start(): this {
97
+ if (this._start) {
98
+ throw new StopwatchAlreadyStarted();
79
99
  }
80
-
81
- this._isRunning = true;
100
+ this._start = process.hrtime.bigint();
82
101
 
83
102
  return this;
84
103
  }
@@ -90,19 +109,19 @@ export class Stopwatch {
90
109
  * @returns {Stopwatch}
91
110
  * @memberof Stopwatch
92
111
  */
93
- pause(): Stopwatch {
94
- if (this._start) {
95
- this._current = process.hrtime(this._start);
96
- this._isRunning = false;
112
+ pause(): this {
113
+ if (!this._start) {
114
+ throw new StopwatchNotStarted();
97
115
  }
98
116
 
117
+ this.accumulateDuration += this.elapsed;
118
+ this._start = null;
99
119
  return this;
100
120
  }
101
121
 
102
- private convertToMilliseconds(hrtime: HrTime): number {
103
- const nanoseconds = hrtime[0] * 1e9 + hrtime[1];
104
- const milliseconds = nanoseconds / 1e6;
122
+ private convertToMilliseconds(hrtime: bigint): number {
123
+ const milliseconds = hrtime / BigInt(1e6);
105
124
 
106
- return milliseconds;
125
+ return Number(milliseconds);
107
126
  }
108
127
  }