@nent/core
Version:
39 lines (38 loc) • 1.47 kB
JavaScript
/*!
* NENT 2022
*/
import { DATA_EVENTS, } from '../../../services/data/interfaces';
/* It's a data provider that uses the sessionStorage API to store data */
export class SessionService {
/**
* A constructor function that takes in a window object, an eventBus object, a name string, and a
* prefix string. The prefix string is set to an empty string by default. The constructor function
* then sets the sessionStorage property to the window's sessionStorage.
* @param {Window} win - Window - the window object
* @param {IEventEmitter} eventBus - IEventEmitter - This is the event bus that the class will use to
* emit events.
* @param {string} name - The name of the session storage key.
* @param {string} [prefix] - This is the prefix that will be used for all the keys in the session
* storage.
*/
constructor(win, eventBus, name, prefix = '') {
this.eventBus = eventBus;
this.name = name;
this.prefix = prefix;
this.sessionStorage = win.sessionStorage;
}
async get(key) {
var _a;
return (_a = this.sessionStorage) === null || _a === void 0 ? void 0 : _a.getItem(this.prefix + key);
}
async set(key, value) {
var _a;
const existing = await this.get(key);
if (existing == value)
return;
(_a = this.sessionStorage) === null || _a === void 0 ? void 0 : _a.setItem(this.prefix + key, value);
this.eventBus.emit(DATA_EVENTS.DataChanged, {
provider: this.name,
});
}
}