add mcId
This commit is contained in:
parent
e023e5bc44
commit
ee659d9e13
2 changed files with 85 additions and 16 deletions
38
ui/lib/mcid.ts
Normal file
38
ui/lib/mcid.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
// utils/mcid.ts
|
||||
export class Mcid {
|
||||
private lastTimestamp = -1;
|
||||
private sequence = 0;
|
||||
|
||||
constructor(private readonly machineId: number = 0) {
|
||||
if (machineId < 0 || machineId > 255) {
|
||||
throw new Error('Machine ID must be between 0 and 255');
|
||||
}
|
||||
}
|
||||
|
||||
generate(): number {
|
||||
let now = Date.now();
|
||||
|
||||
if (now < this.lastTimestamp) {
|
||||
throw new Error('Clock moved backwards');
|
||||
}
|
||||
|
||||
if (now === this.lastTimestamp) {
|
||||
this.sequence = (this.sequence + 1) & 0xf;
|
||||
if (this.sequence === 0) {
|
||||
while (now <= this.lastTimestamp) {
|
||||
now = Date.now();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.sequence = 0;
|
||||
}
|
||||
|
||||
this.lastTimestamp = now;
|
||||
|
||||
return (
|
||||
(now * 0x1000) + // 时间戳左移 12 位
|
||||
(this.machineId * 16) + // 机器 ID 左移 4 位
|
||||
this.sequence // 序列号
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue