Code coverage report for core/src/time/primitives.ts

Statements: 100% (28 / 28)      Branches: 50% (2 / 4)      Functions: 100% (14 / 14)      Lines: 100% (25 / 25)      Ignored: none     

All files » core/src/time/ » primitives.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123  1                   1   49         3         56         12         18             1     5                             1     4         2             1       16         21 12   21         1         19                       1   3 3         1   3 3        
import {Milliseconds, Nanoseconds} from './constants';
import {millisAsNanos, nanosAsJsDate, nanosAsMillis} from './util';
 
 
export interface TimeMeasurement {
  asMilliseconds(truncate: boolean): Milliseconds;
 
  asNanoseconds(): bigint;
}
 
 
export class NanoTimeMeasurement implements TimeMeasurement {
 
  constructor(private readonly _nanos: Nanoseconds) {
  }
 
 
  asMilliseconds(truncate = false): Milliseconds {
    return nanosAsMillis(this._nanos, truncate);
  }
 
 
  asNanoseconds(): Nanoseconds {
    return this._nanos;
  }
 
 
  static fromNanoseconds(nanos: Nanoseconds): TimeMeasurement {
    return new NanoTimeMeasurement(nanos);
  }
 
 
  static fromMillis(millis: number): TimeMeasurement {
    return new NanoTimeMeasurement(millisAsNanos(millis));
  }
 
 
}
 
 
export class Instant extends NanoTimeMeasurement {
 
  asJsDate(): Date {
    return nanosAsJsDate(this.asNanoseconds());
  }
 
}
 
 
export interface Duration {
  readonly elapsed: TimeMeasurement;
 
  asMilliseconds(truncate: boolean): Milliseconds;
 
  asNanoseconds(): bigint;
}
 
 
export class EagerDuration extends NanoTimeMeasurement implements Duration {
 
  constructor(start: Nanoseconds, end: Nanoseconds) {
    super(end - start);
  }
 
 
  get elapsed(): TimeMeasurement {
    return this;
  }
 
 
}
 
 
export class LazyDuration implements Duration {
  private _elapsed: TimeMeasurement | undefined;
 
 
  constructor(readonly start: Nanoseconds, readonly end: Nanoseconds) {
  }
 
 
  get elapsed(): TimeMeasurement {
    if (!this._elapsed) {
      this._elapsed = NanoTimeMeasurement.fromNanoseconds(this.end - this.start);
    }
    return this._elapsed;
  }
 
 
  asMilliseconds(truncate = false): Milliseconds {
    return this.elapsed.asMilliseconds(truncate);
  }
 
 
  asNanoseconds(): bigint {
    return this.elapsed.asNanoseconds();
  }
 
}
 
 
export interface Period extends Duration {
  readonly from: Instant;
  readonly to: Instant;
}
 
 
export class EagerPeriod extends EagerDuration implements Period {
 
  constructor(readonly from: Instant, readonly to: Instant) {
    super(from.asNanoseconds(), to.asNanoseconds());
  }
}
 
 
export class LazyPeriod extends LazyDuration implements Period {
 
  constructor(readonly from: Instant, readonly to: Instant) {
    super(from.asNanoseconds(), to.asNanoseconds());
  }
}