newMessageId function

String newMessageId()

Time-ordered, collision-resistant message id: <microsSinceEpoch:16>_<counter:4>_<rand> — fixed-width components, so string order == arrival order (inbox file names sort correctly).

Implementation

String newMessageId() {
  final micros = DateTime.now().toUtc().microsecondsSinceEpoch;
  final counter = (_messageCounter++).toRadixString(36).padLeft(4, '0');
  // dart2js: shifts are 32-bit — `1 << 32` == 0, so nextInt(0) would throw
  // on web (issue #1074). Spell the bound literally.
  final rand = _messageRandom.nextInt(0xFFFFFFFF).toRadixString(36);
  return '${micros.toString().padLeft(16, '0')}_${counter}_$rand';
}