@nent/core
Version:
33 lines (32 loc) • 989 B
JavaScript
/*!
* NENT 2022
*/
import { DATA_EVENTS, } from '../../../services/data/interfaces';
import { getCookie, setCookie } from './utils';
/* It's a data provider that uses cookies to store data */
export class CookieService {
/**
* A constructor function.
* @param {Document} document - The document object.
* @param {IEventEmitter} eventBus - IEventEmitter - This is the event bus that the component will
* use to communicate with other components.
* @param {string} name - The name of the component.
*/
constructor(document, eventBus, name) {
this.document = document;
this.eventBus = eventBus;
this.name = name;
}
async get(key) {
return getCookie(this.document, key) || null;
}
async set(key, value) {
const existing = await this.get(key);
if (existing == value)
return;
setCookie(this.document, key, value, { sameSite: 'strict' });
this.eventBus.emit(DATA_EVENTS.DataChanged, {
provider: this.name,
});
}
}