UNPKG

@raona/sp

Version:

Raona utilities to work with Sharepoint using pnp/sp

59 lines (58 loc) 2.81 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var lscache = require("lscache"); var notifier_1 = require("./notifier"); var Bind_1 = require("../decorators/Bind"); // import { Bind } from '../decorators'; var PersistentStorage = /** @class */ (function () { function PersistentStorage(storageKey) { this.storageKey = storageKey; this.notifier = notifier_1.Notifier.getInstance(); } PersistentStorage.prototype.init = function (callback, expirationTime, notificationCallback) { var _this = this; var data = this.get(); var subscription = this.subscribe(function () { if (notificationCallback) notificationCallback(); }); if (typeof callback !== 'undefined' && data == null) { callback().then(function (val) { _this.set(val, expirationTime); }); } return subscription; }; PersistentStorage.prototype.get = function () { // if(this.dataPromise){ // return this.dataPromise.then(() => { // this.dataPromise = null; // return this.getFromStorage(); // }); // } return this.getFromStorage(); }; PersistentStorage.prototype.getFromStorage = function () { return lscache.get(this.storageKey) ? lscache.get(this.storageKey).value : null; }; PersistentStorage.prototype.set = function (data, expirationTime) { lscache.set(this.storageKey, { value: data }, expirationTime); this.notifier.notify(this.storageKey, data); }; PersistentStorage.prototype.subscribe = function (callback) { return this.notifier.subscribe(this.storageKey, callback); }; PersistentStorage.prototype.unsubscribe = function (_a) { var subscriptionName = _a.subscriptionName, subscriptionId = _a.subscriptionId; this.notifier.unsubscribe({ subscriptionName: subscriptionName, subscriptionId: subscriptionId }); }; __decorate([ Bind_1.Bind() ], PersistentStorage.prototype, "get", null); return PersistentStorage; }()); exports.PersistentStorage = PersistentStorage;