PadManager: use a set instead of an array in padlist

Avoid looping on the array, especially useful if you have many pads.

--HG--
branch : padlist-use-set
This commit is contained in:
Chocobozzz 2020-03-24 11:51:15 +01:00 committed by muxator
parent 94ff21e25c
commit 963d12e614

View file

@ -49,8 +49,8 @@ var globalPads = {
* Updated without db access as new pads are created/old ones removed.
*/
let padList = {
list: [],
sorted : false,
list: new Set(),
cachedList: undefined,
initiated: false,
init: async function() {
let dbData = await db.findKeys("pad:*", "*:*:*");
@ -78,29 +78,26 @@ let padList = {
getPads: async function() {
await this.load();
if (!this.sorted) {
this.list.sort();
this.sorted = true;
if (!this.cachedList) {
this.cachedList = Array.from(this.list).sort();
}
return this.list;
return this.cachedList;
},
addPad: function(name) {
if (!this.initiated) return;
if (this.list.indexOf(name) == -1) {
this.list.push(name);
this.sorted = false;
if (!this.list.has(name)) {
this.list.add(name);
this.cachedList = undefined;
}
},
removePad: function(name) {
if (!this.initiated) return;
var index = this.list.indexOf(name);
if (index > -1) {
this.list.splice(index, 1);
this.sorted = false;
if (this.list.has(name)) {
this.list.delete(name);
this.cachedList = undefined;
}
}
};