fake-toss-payments-server
Version:
Fake toss-payments server for testing
47 lines • 1.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VolatileMap = void 0;
var tstl_1 = require("tstl");
var VolatileMap = /** @class */ (function () {
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.size = function () {
return this.dict_.size();
};
VolatileMap.prototype.get = function (key) {
return this.dict_.get(key);
};
VolatileMap.prototype.clear = function () {
this.dict_.clear();
this.timepoints_.clear();
};
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);
}
};
return VolatileMap;
}());
exports.VolatileMap = VolatileMap;
//# sourceMappingURL=VolatileMap.js.map