Skip to content

Conversation

Copy link

Copilot AI commented May 23, 2025

Problem

The Keyborg.ts file currently uses a file-scoped _lastId counter to generate unique IDs for Keyborg and KeyborgCore instances. This approach can lead to ID conflicts when multiple versions or instances of the library are loaded in the same environment, potentially causing unexpected behavior or collisions.

Solution

This PR centralizes and improves the ID generation logic by:

  1. Removing the file-scoped counter: Eliminates the _lastId variable that could cause conflicts across instances
  2. Introducing getUniqueId function: A new utility function that takes a prefix (k for Keyborg, c for KeyborgCore) and returns unique IDs using timestamp + random counter
  3. Updating all ID generation: Replaces all _lastId references with calls to the centralized function

Changes Made

  • Removed let _lastId = 0; file-scoped counter
  • Added getUniqueId(prefix: string): string function that generates IDs in format <prefix>-<timestamp>-<counter>
  • Updated KeyborgCore constructor: "c" + ++_lastIdgetUniqueId("c")
  • Updated Keyborg constructor: "k" + ++_lastIdgetUniqueId("k")

Benefits

  • Cross-instance uniqueness: IDs are now unique across multiple library versions/instances
  • Collision prevention: Timestamp + random counter approach virtually eliminates ID conflicts
  • Maintained functionality: All existing behavior is preserved, only ID format changes
  • Minimal code impact: Surgical changes with no breaking API modifications

ID Format Change

Before: k1, k2, c1, c2, etc.
After: k-1748029187912-4694, c-1748029187913-5031, etc.

The new format ensures uniqueness while maintaining the prefix-based identification system.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cdn.fwupd.org
    • Triggering command: /usr/bin/fwupdmgr refresh (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mathis-m <11584315+mathis-m@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace local counter with getUniqueId function Centralize ID generation to avoid conflicts across multiple library instances May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant