@manojadams/session-store
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@manojadams/session-store", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Simple state management with session storage", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -0,1 +1,2 @@ | ||
import { IStore } from "./constants"; | ||
@@ -6,7 +7,15 @@ /** | ||
*/ | ||
abstract class SessionStore<T> { | ||
abstract class SessionStore<T extends IStore> { | ||
protected __sessionData: T; | ||
protected _name: string; | ||
protected _createdAt: Date; | ||
protected _updatedAt: Date; | ||
isReady: boolean; | ||
constructor() { | ||
constructor(_name?: string) { | ||
this._name = _name || this.constructor.name; | ||
this.__sessionData = <T>{}; | ||
this._createdAt = new Date(); | ||
this._updatedAt = new Date(); | ||
this.isReady = false; | ||
} | ||
@@ -16,9 +25,5 @@ | ||
const storeName = this.constructor.name; | ||
try { | ||
const sessionData = sessionStorage.getItem(storeName); | ||
if (sessionData) { | ||
this.__sessionData = JSON.parse(sessionData); | ||
} | ||
} catch { | ||
console.error("store_init_error\t:", storeName); | ||
const sessionData = sessionStorage.getItem(storeName); | ||
if (sessionData) { | ||
this.__sessionData = JSON.parse(sessionData); | ||
} | ||
@@ -33,10 +38,15 @@ } | ||
this.__sessionData[dataKey] = value; | ||
this._updatedAt = new Date(); | ||
} | ||
destroy() { | ||
const storeName = this.constructor.name; | ||
sessionStorage.setItem(storeName, JSON.stringify(this.__sessionData)); | ||
const createdAt = this.getData("_createdAt") || this._createdAt.toISOString(); | ||
const updatedAt = this._updatedAt.toISOString(); | ||
this.setData("_createdAt", createdAt); | ||
this.setData("_updatedAt", updatedAt); | ||
sessionStorage.setItem(this._name, JSON.stringify(this.__sessionData)); | ||
} | ||
} | ||
export default SessionStore; | ||
export default SessionStore; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3217
5
45