UNPKG

react-simple-api

Version:

Create and cache API requests and responses

35 lines 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiCache = void 0; var ApiCache = /** @class */ (function () { function ApiCache() { this.apiResponses = new Map(); } // Ensure that there is only one instance of the cache in the application ApiCache.getInstance = function () { if (this.instance) { return this.instance; } this.instance = new ApiCache(); return this.instance; }; ApiCache.prototype.getCachedResponse = function (id) { return this.apiResponses.get(id); }; ApiCache.prototype.setCachedResponse = function (id, data, cacheExpiry) { var _this = this; this.apiResponses.set(id, data); if (cacheExpiry && cacheExpiry > 0) { setTimeout(function () { _this.deleteCachedResponse(id); }, cacheExpiry); } return data; }; ApiCache.prototype.deleteCachedResponse = function (id) { this.apiResponses.delete(id); }; return ApiCache; }()); exports.ApiCache = ApiCache; //# sourceMappingURL=cache.js.map