UNPKG

@looker/embed-services

Version:
101 lines (100 loc) 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.themeServiceCreator = exports.registerThemeService = exports.getThemeService = exports.THEME_SERVICE_NAME = void 0; var _sdk = require("@looker/sdk"); var _ItemList = require("./ItemList"); var _ServiceFactory = require("./ServiceFactory"); function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class ThemeService extends _ItemList.ItemList { constructor() { super(...arguments); _defineProperty(this, "defaultTheme", void 0); } get(id, options) { var _this = this; return _asyncToGenerator(function* () { var cache = _this.getCacheDefault(options); _this.clearIfExpired(); var item = _this.indexedItems[id]; if (cache && item) { return item; } item = yield _this.sdk.ok((0, _sdk.theme)(_this.sdk, id, options === null || options === void 0 ? void 0 : options.fields)); if (item) { _this.items = [..._this.items, item]; _this.index(); _this.setExpiration(); } return item; })(); } getAll(options) { var _this2 = this; return _asyncToGenerator(function* () { if (_this2.getCacheDefault(options) && !_this2.expired()) { return _this2; } yield _this2.load(options); return _this2; })(); } set(id, item) { var _this3 = this; return _asyncToGenerator(function* () { _this3.clearIfExpired(); var theme = yield _this3.sdk.ok((0, _sdk.update_theme)(_this3.sdk, id, item)); if (theme) { _this3.items = [..._this3.items, theme]; _this3.index(); _this3.setExpiration(); } return theme; })(); } getDefaultTheme(ts) { var _this4 = this; return _asyncToGenerator(function* () { if (_this4.expired()) { _this4.defaultTheme = yield _this4.sdk.ok((0, _sdk.default_theme)(_this4.sdk, ts)); } return _this4.defaultTheme; })(); } delete(id) { var _this5 = this; return _asyncToGenerator(function* () { _this5.clearIfExpired(); yield _this5.sdk.ok((0, _sdk.delete_theme)(_this5.sdk, id)); _this5.items = _this5.items.filter(item => item.id !== id); _this5.index(); })(); } load(options) { var _this6 = this; return _asyncToGenerator(function* () { yield _this6.getDefaultTheme(); _this6.items = yield _this6.sdk.ok((0, _sdk.all_themes)(_this6.sdk, options === null || options === void 0 ? void 0 : options.fields)); _this6.index(); _this6.setExpiration(); return _this6; })(); } } var THEME_SERVICE_NAME = exports.THEME_SERVICE_NAME = 'ThemeService'; var themeServiceCreator = (sdk, timeToLive) => { return new ThemeService(sdk, timeToLive); }; exports.themeServiceCreator = themeServiceCreator; var registerThemeService = timeToLive => { (0, _ServiceFactory.getFactory)().register(THEME_SERVICE_NAME, themeServiceCreator, timeToLive); }; exports.registerThemeService = registerThemeService; var getThemeService = () => (0, _ServiceFactory.getFactory)().get(THEME_SERVICE_NAME); exports.getThemeService = getThemeService; //# sourceMappingURL=ThemeService.js.map