@looker/embed-services
Version:
92 lines • 3.83 kB
JavaScript
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); }
import { all_themes, default_theme, delete_theme, theme, update_theme } from '@looker/sdk';
import { ItemList } from './ItemList';
import { getFactory } from './ServiceFactory';
class ThemeService extends 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(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(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(default_theme(_this4.sdk, ts));
}
return _this4.defaultTheme;
})();
}
delete(id) {
var _this5 = this;
return _asyncToGenerator(function* () {
_this5.clearIfExpired();
yield _this5.sdk.ok(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(all_themes(_this6.sdk, options === null || options === void 0 ? void 0 : options.fields));
_this6.index();
_this6.setExpiration();
return _this6;
})();
}
}
export var THEME_SERVICE_NAME = 'ThemeService';
export var themeServiceCreator = (sdk, timeToLive) => {
return new ThemeService(sdk, timeToLive);
};
export var registerThemeService = timeToLive => {
getFactory().register(THEME_SERVICE_NAME, themeServiceCreator, timeToLive);
};
export var getThemeService = () => getFactory().get(THEME_SERVICE_NAME);
//# sourceMappingURL=ThemeService.js.map