Skip to content

Commit 4be4a1a

Browse files
committed
feat(frontend/cache): Implement Cache class for session storage management
- Added `Cache` class in `app.js` to manage session storage. - Implemented methods to load, save, and proxy cache data. - Ensured cache data is stored in session storage and automatically saved on updates. - Integrated the `Cache` class with the main `App` class for seamless session management.
1 parent 5e6ee7d commit 4be4a1a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

frontend/public/js/app.js

+63
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,68 @@ class Data {
6565
}
6666
}
6767

68+
/**
69+
* @class Cache
70+
* @description Handles the cache of the application.
71+
*/
72+
class Cache {
73+
/**
74+
* @constructor - Initializes the Cache class.
75+
* @param {Object} app - The App class instance.
76+
*/
77+
constructor(app) {
78+
this.app = app;
79+
this.key = 'appCache';
80+
this._data = this.loadCache();
81+
82+
return this.createProxy(this._data);
83+
}
84+
85+
/**
86+
* @method createProxy - Creates a proxy for the cache object.
87+
* @param {Object} data - The cache object to proxy.
88+
* @returns {Object} - The proxied cache object.
89+
*/
90+
createProxy(data) {
91+
const handler = {
92+
set: (target, prop, value) => {
93+
if (typeof value === 'object' && value !== null) {
94+
value = this.createProxy(value);
95+
}
96+
target[prop] = value;
97+
this.saveCache(); // Save cache when any property is set
98+
return true;
99+
},
100+
get: (target, prop) => {
101+
if (prop in target) {
102+
if (typeof target[prop] === 'object' && target[prop] !== null) {
103+
return this.createProxy(target[prop]);
104+
}
105+
return target[prop];
106+
}
107+
return undefined;
108+
},
109+
};
110+
111+
return new Proxy(data, handler);
112+
}
113+
114+
/**
115+
* @method loadCache - Loads cache from the session-storage.
116+
*/
117+
loadCache() {
118+
const storedCache = sessionStorage.getItem(this.key);
119+
return storedCache ? JSON.parse(storedCache) : {};
120+
}
121+
122+
/**
123+
* @method saveCache - Saves cache to the session-storage.
124+
*/
125+
saveCache() {
126+
sessionStorage.setItem(this.key, JSON.stringify(this._data));
127+
}
128+
}
129+
68130
/**
69131
* @class Auth
70132
* @description Handles the authentication of the application.
@@ -563,6 +625,7 @@ class App {
563625
this.apiURL = apiURL;
564626

565627
this.data = new Data(this);
628+
this.cache = new Cache(this);
566629
this.auth = new Auth(this);
567630
this.location = new Location(this);
568631
this.ui = new UI(this);

0 commit comments

Comments
 (0)