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