UNPKG

@schoolbelle/common

Version:

273 lines (268 loc) 7.55 kB
import { Injectable, defineInjectable } from '@angular/core'; import { Subject, fromEvent } from 'rxjs'; import { filter } from 'rxjs/operators'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const expiry_data_key = 'storage-with-expiration'; class CustomStorageEvent { } class LocalStorageService { constructor() { this.changeEvent = new Subject(); this.session = window.sessionStorage; if (!this.isAvailable()) console.error(`localstorage is not supported or disabled.`); try { this.local = window.localStorage; } catch (e) { // ie11 protected mode denies localStorage access... // should redirect to the related support page. console.error(`ie11 protected mode is suspected.`); console.error(e); this.local = this.session; } fromEvent(window, 'storage') .pipe(filter((/** * @return {?} */ () => document.hasFocus() === false))) .subscribe((/** * @param {?} e * @return {?} */ (e) => { this.changeEvent.next({ key: e.key, to: e.newValue, from: e.oldValue, fromThisTab: false }); })); // expire old data everytime page is loaded this.expireAll(); } /** * @return {?} */ get onChange() { return this.changeEvent.asObservable(); } /** * @return {?} */ expireAll() { return Promise.all([ this.expire('local'), this.expire('session'), ]); } /** * @param {?} key * @param {?} value * @param {?=} type * @return {?} */ set(key, value, type = 'local') { return this.get(key, type).then((/** * @param {?} from * @return {?} */ from => { return new Promise((/** * @param {?} resolve * @return {?} */ resolve => { if (type === 'local') this.local.setItem(key, value); else this.session.setItem(key, value); resolve(true); this.changeEvent.next({ key, from, to: value, fromThisTab: true }); })); })); } /** * @param {?} key * @param {?=} type * @return {?} */ get(key, type = 'local') { return new Promise((/** * @param {?} resolve * @return {?} */ resolve => { if (type === 'local') { resolve(this.local.getItem(key)); } else { resolve(this.session.getItem(key)); } })); } /** * @param {?} key * @param {?=} type * @return {?} */ remove(key, type = 'local') { return this.get(key, type).then((/** * @param {?} from * @return {?} */ from => { return new Promise((/** * @param {?} resolve * @return {?} */ resolve => { if (type === 'local') this.local.removeItem(key); else this.session.removeItem(key); resolve(true); this.changeEvent.next({ key, from, to: undefined, fromThisTab: true }); })); })); } /** * @param {?} key * @param {?} value * @param {?} expires_at * @param {?=} type * @return {?} */ setEx(key, value, expires_at, type = 'local') { return this.get(expiry_data_key, type) .then((/** * @param {?} json * @return {?} */ json => { return typeof json === 'string' ? JSON.parse(json) : {}; })) .then((/** * @param {?} ob * @return {?} */ (ob) => { ob[key] = { value, expires_at }; return this.set(expiry_data_key, JSON.stringify(ob), type); })); } /** * @param {?} key * @param {?=} type * @return {?} */ getEx(key, type = 'local') { return this.get(expiry_data_key, type) .then((/** * @param {?} json * @return {?} */ json => typeof json === 'string' ? JSON.parse(json) : {})) .then((/** * @param {?} ob * @return {?} */ ob => { /** @type {?} */ const pair = ob[key]; return pair && pair.expires_at >= Date.now() ? pair.value : null; })); } /** * @private * @param {?=} type * @return {?} */ expire(type = 'local') { return this.get(expiry_data_key, type) .then((/** * @param {?} json * @return {?} */ json => { return typeof json === 'string' ? JSON.parse(json) : {}; })) .then((/** * @param {?} ob * @return {?} */ (ob) => { for (let key in ob) { if (!ob[key].expires_at || ob[key].expires_at < Date.now()) delete ob[key]; } if (Object.keys(ob).length) return this.set(expiry_data_key, JSON.stringify(ob), type); else return this.remove(expiry_data_key, type); })) .catch((/** * @param {?} e * @return {?} */ e => { console.error(e); return this.remove(expiry_data_key, type); })); } /** * @param {?} key * @param {?=} type * @return {?} */ removeEx(key, type = 'local') { return this.get(expiry_data_key, type) .then((/** * @param {?} json * @return {?} */ json => { return typeof json === 'string' ? JSON.parse(json) : {}; })) .then((/** * @param {?} ob * @return {?} */ (ob) => { delete ob[key]; return this.set(expiry_data_key, JSON.stringify(ob), type); })); } /** * @return {?} */ isAvailable() { /** @type {?} */ const test = 'test'; try { localStorage.setItem(test, test); localStorage.removeItem(test); return true; } catch (e) { return false; } } } LocalStorageService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ LocalStorageService.ctorParameters = () => []; /** @nocollapse */ LocalStorageService.ngInjectableDef = defineInjectable({ factory: function LocalStorageService_Factory() { return new LocalStorageService(); }, token: LocalStorageService, providedIn: "root" }); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { expiry_data_key, CustomStorageEvent, LocalStorageService }; //# sourceMappingURL=schoolbelle-common-local-storage.js.map