frontend/.pnpm-store/v3/files/7e/a690b1c91cf4bf94b0c205f839d5885c0d9fd306391a8835313a9493963fd801334f35ec13a24da310d5ba68614be493642e93d4e71904069335047f0e2214

49 lines
1.1 KiB
Plaintext

'use strict';
var toPosInt = require('es5-ext/number/to-pos-integer')
, create = Object.create, hasOwnProperty = Object.prototype.hasOwnProperty;
module.exports = function (limit) {
var size = 0, base = 1, queue = create(null), map = create(null), index = 0, del;
limit = toPosInt(limit);
return {
hit: function (id) {
var oldIndex = map[id], nuIndex = ++index;
queue[nuIndex] = id;
map[id] = nuIndex;
if (!oldIndex) {
++size;
if (size <= limit) return;
id = queue[base];
del(id);
return id;
}
delete queue[oldIndex];
if (base !== oldIndex) return;
while (!hasOwnProperty.call(queue, ++base)) continue; //jslint: skip
},
delete: del = function (id) {
var oldIndex = map[id];
if (!oldIndex) return;
delete queue[oldIndex];
delete map[id];
--size;
if (base !== oldIndex) return;
if (!size) {
index = 0;
base = 1;
return;
}
while (!hasOwnProperty.call(queue, ++base)) continue; //jslint: skip
},
clear: function () {
size = 0;
base = 1;
queue = create(null);
map = create(null);
index = 0;
}
};
};