UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

61 lines (60 loc) 2.7 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { LRUMap } from 'lru_map'; var CachingClient = /*#__PURE__*/function () { function CachingClient(config) { _classCallCheck(this, CachingClient); var defaults = { cacheSize: 10, cacheMaxAge: 0 }; this.config = _objectSpread(_objectSpread({}, defaults), config); // never set cacheSize or cacheMaxAge to negative numbers this.config.cacheSize = Math.max(this.config.cacheSize || 0, 0); this.config.cacheMaxAge = Math.max(this.config.cacheMaxAge || 0, 0); // DIR-474: cap cache at 30 days. if (this.config.cacheMaxAge) { this.config.cacheMaxAge = Math.min(this.config.cacheMaxAge, 30 * 24 * 60 * 60 * 1000); } // Only set cache if maxCacheAge and cacheSize are set this.cache = !this.config.cacheMaxAge || !this.config.cacheSize ? null : new LRUMap(this.config.cacheSize); } return _createClass(CachingClient, [{ key: "setCachedProfile", value: function setCachedProfile(cacheIdentifier, profile) { this.cache && this.cache.set(cacheIdentifier, { expire: Date.now() + this.config.cacheMaxAge, profile: profile }); } }, { key: "getCachedProfile", value: function getCachedProfile(cacheIdentifier) { if (!this.cache) { return null; } var cached = this.cache.get(cacheIdentifier); if (!cached) { return null; } if (cached.expire < Date.now()) { this.cache.delete(cacheIdentifier); return null; } // Extend expiry "date" this.setCachedProfile(cacheIdentifier, cached.profile); return cached.profile; } }, { key: "flushCache", value: function flushCache() { if (this.cache) { this.cache.clear(); } } }]); }(); export { CachingClient as default };