Page MenuHomePhabricator
Authored By
Nux
Dec 31 2022, 4:48 PM
Size
1 KB
Referenced Files
None
Subscribers
None

test_ls.js

/**
* Notifications by storage events (PoC).
*/
const notificationsKey = 'notifications';
const notificationCounter = document.querySelector('#storage_test span');
// event executed when storage values change
window.addEventListener('storage', () => {
let obj = JSON.parse(localStorage.getItem(notificationsKey));
if (obj != null && ('count' in obj)) {
receiveNotification(obj);
}
});
// setup state onload from cache
window.addEventListener('load', () => {
let obj = JSON.parse(localStorage.getItem(notificationsKey));
if (obj != null && ('count' in obj)) {
receiveNotification(obj);
}
});
/**
* Apply notification state.
* @param {Object} obj {count:number};
*/
function receiveNotification(obj) {
console.log('receiveNotification:', obj);
notificationCounter.textContent = obj.count;
}
/**
* Send notification to other tabs.
*
* @param {Number|undefined} count
*/
function emitNotification(count) {
if (typeof count !== 'number') {
count = 0;
}
let obj = {count:count};
localStorage.setItem(notificationsKey, JSON.stringify(obj));
// set for myself
receiveNotification(obj);
}

File Metadata

Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9998106
Default Alt Text
test_ls.js (1 KB)

Event Timeline