Move generateId function
This commit is contained in:
parent
912d59d3df
commit
42f1c2463c
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user