-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Keyborg ids should be unique for multiple bundles #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Currently if a keyborg instance is created from different bundles they will have the same id since the counter is on the file scope. This PR updates the id generation so that it is unique for a global core instance
📊 Bundle size reportUnchanged fixtures
|
| } | ||
|
|
||
| // other bundles might have created keyborg instances before this one | ||
| while (current.refs[id]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implies that the other __keyborg has refs. And loops through it too.
Let's probably have something like this for createId():
private createId() {
const rnd = new Uint32Array(4);
crypto.getRandomValues(rnd);
return rnd.join('') + '|' + Date.now() + '|' + ++_lastId;
}
It has a pretty good chance to never collide...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's only one js thread, right? so they wouldn't loop through the global at the same time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not about thread safety, it's about accessing black box internals assuming it's not a black box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a different prefix for the new version that is collision save:
k-num
Probably the same should be done for the core id just to get rid of the file scoped thing, and make it version agnostic

Currently if a keyborg instance is created from different bundles they will have the same id since the counter is on the file scope.
This PR updates the id generation so that it is unique for a global core instance