Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Keyborg.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ interface WindowWithKeyborg extends Window {
};
}

let _lastId = 0;
function generateId(): string {
const arr = new Uint8Array(16);
crypto.getRandomValues(arr);

let id = "";
for (let i = 0; i < arr.length; i++) {
id += arr[i].toString(16).padStart(2, "0");
}

return id;
}

export type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;

Expand Down Expand Up @@ -215,7 +225,7 @@ function createKeyborgCore(targetWindow: WindowWithKeyborg): KeyborgCoreHandle {

export function createKeyborg(win: Window): Keyborg {
const kwin = win as WindowWithKeyborg;
const id = "k" + ++_lastId;
const id = generateId();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am paranoid, I would use both incremented id + random number, not just one of them. random numbers can repeat (with unlikely, but non-zero probability). incremented part fully guarantees absence of the same instance collisions and strongly reduces slim chances of the multiple instance collision :).

let localWin: WindowWithKeyborg | undefined = kwin;
let core: KeyborgCoreHandle | undefined;
// `callbacks` is exposed as `instance._cb` to satisfy the slot wire
Expand Down