next method

String next()

Returns the next TID, strictly greater than every TID this generator has returned before.

Implementation

String next() {
  final now = _now();
  // Never go backwards, and never repeat: a coarse clock resolution or a
  // backwards NTP step must not reorder the keys already issued.
  _lastTimestamp = now > _lastTimestamp ? now : _lastTimestamp + 1;

  // `_lastTimestamp << _clockIdBits` would compose a value up to ~63 bits
  // wide. That is exact on the VM (64-bit `int`), but on dart2js `int` is a
  // JS double, exact only up to 2^53; a current-era microsecond timestamp
  // (~1.8e15) shifted left by 10 bits overflows that and silently drops
  // low bits, corrupting the TID. `BigInt` is arbitrary-precision on every
  // platform (as used for the same reason in `car_decoder.dart`), so
  // composing the 63-bit value with it keeps this exact on the web too.
  final packed = (BigInt.from(_lastTimestamp) << _clockIdBits) | _clockIdBig;

  return _encode(packed);
}