getSnowflakeID method
Implementation
String getSnowflakeID() {
int timestamp = DateTime.now().millisecondsSinceEpoch - EPOCH;
if (timestamp < lastTimestamp) {
throw Exception('Clock moved backwards. Rejecting request.');
}
if (timestamp == lastTimestamp) {
sequence = (sequence + 1) & MAX_SEQUENCE;
if (sequence == 0) {
timestamp = waitNextMillis(lastTimestamp);
}
} else {
sequence = 0;
}
lastTimestamp = timestamp;
BigInt id = (BigInt.from(timestamp) << (MACHINE_ID_BITS + SEQUENCE_BITS)) |
(BigInt.from(machineId) << SEQUENCE_BITS) |
BigInt.from(sequence);
return id.toString();
}