diff options
| author | Dax Raad <[email protected]> | 2025-05-26 14:36:45 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-26 14:36:45 -0400 |
| commit | 802389a90eaa9a173a98305003b9e58b95584cd1 (patch) | |
| tree | 9c6c3e0d345c1db4ad16800a09fe3e12f8c9e242 | |
| parent | 4880b08b8accf80f5f5b2dc8bc88ffdcaa893754 (diff) | |
| download | opencode-802389a90eaa9a173a98305003b9e58b95584cd1.tar.gz opencode-802389a90eaa9a173a98305003b9e58b95584cd1.zip | |
fixed id generation
| -rw-r--r-- | js/src/id/id.ts | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/js/src/id/id.ts b/js/src/id/id.ts index 96ea870a1..0cb1afe9d 100644 --- a/js/src/id/id.ts +++ b/js/src/id/id.ts @@ -13,6 +13,10 @@ export namespace Identifier { const LENGTH = 26; + // State for monotonic ID generation + let lastTimestamp = 0; + let counter = 0; + export function ascending(prefix: keyof typeof prefixes, given?: string) { return generateID(prefix, false, given); } @@ -31,7 +35,15 @@ export namespace Identifier { throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`); } - let now = BigInt(Date.now()); + const currentTimestamp = Date.now(); + + if (currentTimestamp !== lastTimestamp) { + lastTimestamp = currentTimestamp; + counter = 0; + } + counter++; + + let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter); if (descending) { now = ~now; |
