420733b93a
* add Patch CSP option * show style version, size, and update age in manager * add scope selector to style search in manager * keep scroll position and selections in tab's session * directly install usercss from raw github links * ditch localStorage, use on-demand SessionStore proxy * simplify localization * allow <code> tag in i18n-html * keep nodes in HTML templates * API.getAllStyles is actually faster with code untouched * fix fitToContent when applies-to is taller than window * dedupe linter.enableForEditor calls * prioritize visible CMs in refreshOnViewListener * don't scroll to last style on editing a new one * delay colorview for invisible CMs * eslint comma-dangle error + autofix files * styleViaXhr: also toggle for disableAll pref * styleViaXhr: allow cookies for sandbox CSP * simplify notes in options * simplify getStylesViaXhr * oldUI fixups: * remove separator before 1st applies-to * center name bubbles * fix updateToc focus on a newly added section * fix fitToContent when cloning section * remove CSS `contain` as it makes no difference * replace overrides with declarative CSS + code cosmetics * simplify adjustWidth and make it work in FF
72 lines
1.4 KiB
JavaScript
72 lines
1.4 KiB
JavaScript
/* exported createCache */
|
|
'use strict';
|
|
|
|
// create a FIFO limit-size map.
|
|
function createCache({size = 1000, onDeleted} = {}) {
|
|
const map = new Map();
|
|
const buffer = Array(size);
|
|
let index = 0;
|
|
let lastIndex = 0;
|
|
return {
|
|
get,
|
|
set,
|
|
delete: delete_,
|
|
clear,
|
|
has: id => map.has(id),
|
|
entries: function *() {
|
|
for (const [id, item] of map) {
|
|
yield [id, item.data];
|
|
}
|
|
},
|
|
values: function *() {
|
|
for (const item of map.values()) {
|
|
yield item.data;
|
|
}
|
|
},
|
|
get size() {
|
|
return map.size;
|
|
},
|
|
};
|
|
|
|
function get(id) {
|
|
const item = map.get(id);
|
|
return item && item.data;
|
|
}
|
|
|
|
function set(id, data) {
|
|
if (map.size === size) {
|
|
// full
|
|
map.delete(buffer[lastIndex].id);
|
|
if (onDeleted) {
|
|
onDeleted(buffer[lastIndex].id, buffer[lastIndex].data);
|
|
}
|
|
lastIndex = (lastIndex + 1) % size;
|
|
}
|
|
const item = {id, data, index};
|
|
map.set(id, item);
|
|
buffer[index] = item;
|
|
index = (index + 1) % size;
|
|
}
|
|
|
|
function delete_(id) {
|
|
const item = map.get(id);
|
|
if (!item) {
|
|
return false;
|
|
}
|
|
map.delete(item.id);
|
|
const lastItem = buffer[lastIndex];
|
|
lastItem.index = item.index;
|
|
buffer[item.index] = lastItem;
|
|
lastIndex = (lastIndex + 1) % size;
|
|
if (onDeleted) {
|
|
onDeleted(item.id, item.data);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function clear() {
|
|
map.clear();
|
|
index = lastIndex = 0;
|
|
}
|
|
}
|