Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F35933306
test_ls.js
Nux (Maciej Jaros)
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
Nux
Dec 31 2022, 4:48 PM
2022-12-31 16:48:33 (UTC+0)
Size
1 KB
Referenced Files
None
Subscribers
None
test_ls.js
View Options
/**
* 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
Details
Attached
Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9998106
Default Alt Text
test_ls.js (1 KB)
Attached To
Mode
T326042: Synchronise state of notifications between tabs
Attached
Detach File
Event Timeline
Log In to Comment