-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric
committed
May 25, 2023
1 parent
28117fd
commit e615d04
Showing
6 changed files
with
62 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { Snowflake } from "./snowflake"; | ||
|
||
export * from "./snowflake"; | ||
|
||
export const sleep = async (ms: number): Promise<void> => | ||
await new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
export const random = (min: number, max: number): number => | ||
Math.floor(Math.random() * (max - min) + min); | ||
|
||
const snowflake = new Snowflake(random(0, 1023)); | ||
export const nextNonce = (): string => snowflake.nextId().toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export class Snowflake { | ||
private static readonly EPOCH = 1609459200000; | ||
private static readonly SEQUENCE_BITS = 12; | ||
private static readonly WORKER_ID_BITS = 10; | ||
private static readonly MAX_SEQUENCE = (1 << Snowflake.SEQUENCE_BITS) - 1; | ||
private static readonly MAX_WORKER_ID = (1 << Snowflake.WORKER_ID_BITS) - 1; | ||
|
||
private sequence: number; | ||
private lastTimestamp: number; | ||
|
||
constructor(private readonly workerId: number) { | ||
if (workerId > Snowflake.MAX_WORKER_ID || workerId < 0) { | ||
throw new Error( | ||
`worker id must be between 0 and ${Snowflake.MAX_WORKER_ID}` | ||
); | ||
} | ||
this.sequence = 0; | ||
this.lastTimestamp = -1; | ||
} | ||
|
||
public nextId(): number { | ||
let timestamp = Date.now(); | ||
if (timestamp < this.lastTimestamp) { | ||
throw new Error("Invalid System Clock"); | ||
} | ||
if (this.lastTimestamp == timestamp) { | ||
this.sequence++; | ||
if (this.sequence > Snowflake.MAX_SEQUENCE) { | ||
while (Date.now() <= timestamp) { | ||
// Wait for next millisecond | ||
} | ||
timestamp = Date.now(); | ||
this.sequence = 0; | ||
} | ||
} else { | ||
this.sequence = 0; | ||
} | ||
this.lastTimestamp = timestamp; | ||
const id = | ||
((timestamp - Snowflake.EPOCH) << | ||
(Snowflake.SEQUENCE_BITS + Snowflake.WORKER_ID_BITS)) | | ||
(this.workerId << Snowflake.SEQUENCE_BITS) | | ||
this.sequence; | ||
return id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.