Fix _lastId variable collision when multiple keyborg bundles are loaded#100
Draft
Copilot wants to merge 2 commits into
Draft
Fix _lastId variable collision when multiple keyborg bundles are loaded#100Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
…le bundle compatibility Co-authored-by: mathis-m <11584315+mathis-m@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix
Fix _lastId variable collision when multiple keyborg bundles are loaded
May 23, 2025
_lastId variable to ensure ID uniqueness across bundles
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When multiple bundles of the keyborg package are loaded on the same site, each bundle maintains its own file-scoped
_lastIdvariable. This leads to ID conflicts and incorrect behavior as different bundles can generate overlapping IDs for KeyborgCore and Keyborg instances.Solution
Move the
_lastIdcounter to the sharedwindow.__keyborgobject so all bundles use the same ID sequence, ensuring unique IDs across all instances regardless of which bundle created them.Changes Made
WindowWithKeyborginterface to include_lastId: numbergetNextId()helper function that manages the shared ID counter onwindow.__keyborg._lastId++_lastIdusages with calls togetNextId(win)_lastIdvariable_lastIdpropertyCompatibility Features
The fix ensures compatibility between fixed and unfixed bundles:
window.__keyborgdoesn't exist, initializes it with_lastId: 0window.__keyborgexists but lacks_lastId, adds it starting from 0_lastIdvalue when creating new__keyborgobjectsTesting
This change resolves the multiple bundle issue while maintaining full backward compatibility.
💡 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.