diff --git a/src/Keyborg.ts b/src/Keyborg.ts index cc60b89f6..e986d34ae 100644 --- a/src/Keyborg.ts +++ b/src/Keyborg.ts @@ -279,8 +279,8 @@ export class Keyborg { } private constructor(win: WindowWithKeyborg, props?: KeyborgProps) { - this._id = "k" + ++_lastId; this._win = win; + this._id = this.createId(win); const current = win.__keyborg; @@ -296,6 +296,25 @@ export class Keyborg { } } + /** + * creates an id that will be truly global core instance + * @returns global id for the keyborg instance + */ + private createId(win: WindowWithKeyborg) { + const current = win.__keyborg; + let id = "k" + ++_lastId; + if (!current) { + return id; + } + + // other bundles might have created keyborg instances before this one + while (current.refs[id]) { + id = "k" + ++_lastId; + } + + return id; + } + private dispose(): void { const current = this._win?.__keyborg;