Fix _lastId variable collision when multiple keyborg bundles are loaded #100
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.