Skip to content

Conversation

@ling1726
Copy link
Contributor

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

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
@ling1726 ling1726 requested review from a team and mshoho as code owners February 28, 2024 18:43
@github-actions
Copy link

📊 Bundle size report

Package & Exports Baseline (minified/GZIP) PR Change
keyborg
All exports
6.413 kB
1.999 kB
6.519 kB
2.037 kB
106 B
38 B
keyborg
createKeyborg() & disposeKeyborg()
6.19 kB
1.951 kB
6.296 kB
1.99 kB
106 B
39 B
Unchanged fixtures
Package & Exports Size (minified/GZIP)
keyborg
KEYBORG_FOCUSIN constant
62 B
82 B
🤖 This report was generated against 22868096bedf20567c8c325bb6f34dc386b817c9

}

// other bundles might have created keyborg instances before this one
while (current.refs[id]) {
Copy link
Member

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...

Copy link
Contributor Author

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?

Copy link
Member

@mshoho mshoho Feb 28, 2024

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.

Copy link

@mathis-m mathis-m May 16, 2025

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

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.

4 participants