fake-iamport-server
Version:
Fake iamport server for testing
67 lines • 2.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VolatileMap = void 0;
var tstl_1 = require("tstl");
var VolatileMap = /** @class */ (function () {
/* -----------------------------------------------------------
CONSTRUCTORS
----------------------------------------------------------- */
function VolatileMap(expiration, hasher, pred) {
if (hasher === void 0) { hasher = tstl_1.hash; }
if (pred === void 0) { pred = tstl_1.equal_to; }
this.expiration = expiration;
this.dict_ = new tstl_1.HashMap(hasher, pred);
this.timepoints_ = new tstl_1.TreeMap();
}
VolatileMap.prototype.clear = function () {
this.dict_.clear();
this.timepoints_.clear();
};
/* -----------------------------------------------------------
ACCESSORS
----------------------------------------------------------- */
VolatileMap.prototype.size = function () {
return this.dict_.size();
};
VolatileMap.prototype.get = function (key) {
return this.dict_.get(key);
};
VolatileMap.prototype.has = function (key) {
return this.dict_.has(key);
};
VolatileMap.prototype.back = function () {
if (this.size() === 0)
throw new tstl_1.OutOfRange("No element exists.");
return this.dict_.rbegin().second;
};
/* -----------------------------------------------------------
ELEMENTS I/O
----------------------------------------------------------- */
VolatileMap.prototype.set = function (key, value) {
this._Clean_up();
this.dict_.set(key, value);
this.timepoints_.set(Date.now(), key);
};
VolatileMap.prototype._Clean_up = function () {
var bound = Date.now() - this.expiration.time;
var last = this.timepoints_.upper_bound(bound);
for (var it = this.timepoints_.begin(); it.equals(last) === false;) {
this.dict_.erase(it.second);
it = this.timepoints_.erase(it);
}
if (this.timepoints_.size() < this.expiration.capacity)
return;
var left = this.timepoints_.size() - this.expiration.capacity;
while (left-- === 0) {
var it = this.timepoints_.begin();
this.dict_.erase(it.second);
this.timepoints_.erase(it);
}
};
VolatileMap.prototype.erase = function (key) {
return this.dict_.erase(key);
};
return VolatileMap;
}());
exports.VolatileMap = VolatileMap;
//# sourceMappingURL=VolatileMap.js.map