Move generateId function

This commit is contained in:
toasted-nutbread 2020-02-16 11:59:17 -05:00
parent 912d59d3df
commit 42f1c2463c
2 changed files with 11 additions and 9 deletions

View File

@ -19,7 +19,7 @@
class FrontendApiSender { class FrontendApiSender {
constructor() { constructor() {
this.senderId = FrontendApiSender.generateId(16); this.senderId = yomichan.generateId(16);
this.ackTimeout = 3000; // 3 seconds this.ackTimeout = 3000; // 3 seconds
this.responseTimeout = 10000; // 10 seconds this.responseTimeout = 10000; // 10 seconds
this.callbacks = new Map(); this.callbacks = new Map();
@ -123,12 +123,4 @@ class FrontendApiSender {
info.timer = null; info.timer = null;
info.reject(new Error(reason)); info.reject(new Error(reason));
} }
static generateId(length) {
let id = '';
for (let i = 0; i < length; ++i) {
id += Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
}
return id;
}
} }

View File

@ -257,6 +257,16 @@ const yomichan = (() => {
// Public // Public
generateId(length) {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
let id = '';
for (const value of array) {
id += value.toString(16).padStart(2, '0');
}
return id;
}
triggerOrphaned(error) { triggerOrphaned(error) {
this.trigger('orphaned', {error}); this.trigger('orphaned', {error});
} }