react-dadata
Version:
React-компонент для подсказок адресов, организаций и банков с помощью сервиса DaData.ru
106 lines (105 loc) • 3.41 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { HttpCache } from './abstract';
var minute = 60000;
var DefaultHttpCache = /** @class */ (function (_super) {
__extends(DefaultHttpCache, _super);
function DefaultHttpCache() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._map = new Map();
_this._ttl = 10 * minute;
return _this;
}
Object.defineProperty(DefaultHttpCache, "shared", {
/**
* Синглтон
* @example
* ```ts
* cache.shared.get('key');
* ```
*/
get: function () {
if (!DefaultHttpCache.sharedInstance) {
DefaultHttpCache.sharedInstance = new DefaultHttpCache();
}
return DefaultHttpCache.sharedInstance;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DefaultHttpCache.prototype, "ttl", {
/**
* Время жизни кеша в миллисекундах
* @example
* ```ts
* cache.ttl = 60000;
* cache.ttl = Infinity;
* cache.tll = 0;
*
* // негативные значения игнорируются
* cache.ttl = -1;
* cache.ttl = Number.NEGATIVE_INFINITY;
* ```
*/
get: function () {
return this._ttl;
},
set: function (ttl) {
if (typeof ttl === 'number' && ttl >= 0) {
this._ttl = ttl;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(DefaultHttpCache.prototype, "size", {
/**
* Количество элементов в кеше
*/
get: function () {
return this._map.size;
},
enumerable: false,
configurable: true
});
DefaultHttpCache.prototype.get = function (key) {
var data = this._map.get(key);
if (!data)
return null;
if (data.expires <= Date.now()) {
this.delete(key);
return null;
}
return data.data;
};
DefaultHttpCache.prototype.set = function (key, data) {
this._map.set(key, {
data: data,
expires: Date.now() + this.ttl,
});
return this;
};
DefaultHttpCache.prototype.delete = function (key) {
this._map.delete(key);
return this;
};
DefaultHttpCache.prototype.reset = function () {
this._map.clear();
return this;
};
return DefaultHttpCache;
}(HttpCache));
export { DefaultHttpCache };