{"id":1202,"date":"2023-07-18T05:00:00","date_gmt":"2023-07-18T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1202"},"modified":"2023-07-17T15:51:40","modified_gmt":"2023-07-17T22:51:40","slug":"notifications-from-localstorage-with-signals","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/notifications-from-localstorage-with-signals\/","title":{"rendered":"Notifications from LocalStorage with Signals"},"content":{"rendered":"\n<p>Yesterday, we saw that <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/localstorage-and-sessionstorage\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>LocalStorage<\/code> can be used as a persistent cache<\/a> to store our data in the browser. Earlier, we covered that <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/using-services-to-cache-data\/\" target=\"_blank\" rel=\"noopener\" title=\"\">services are a cache of their own<\/a> but have one instance per app\/browser tab, which means that applications opened in multiple tabs can have an inconsistent state since they each have their own &#8220;singleton&#8221; services.<\/p>\n\n\n\n<p><code>LocalStorage<\/code> can be used to share data between multiple tabs that render the same application. Even better, there is a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/storage_event\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>storage<\/code> event<\/a> that can be listened to to know when another tab updated <code>LocalStorage<\/code>: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"163\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-14.png\" alt=\"\" class=\"wp-image-1203\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-14.png 660w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-14-300x74.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Using the Rxjs <code><a href=\"https:\/\/rxjs.dev\/api\/index\/function\/fromEvent\" target=\"_blank\" rel=\"noopener\" title=\"\">fromEvent<\/a><\/code> function, we can turn the above event listener into an <code>Observable<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"189\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-16.png\" alt=\"\" class=\"wp-image-1205\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-16.png 466w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-16-300x122.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And if we&#8217;re using Angular 16+, we can turn the above <code>Observable<\/code> into a <code>Signal<\/code> with one more function:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"211\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-17.png\" alt=\"\" class=\"wp-image-1206\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-17.png 576w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-17-300x110.png 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above <code>Signal<\/code> could be used in a service to synchronize data between tabs. Spying on that <code>Signal<\/code> to see what&#8217;s going on in it is as easy as registering a side-effect on it using the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-effect\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>effect<\/code> function<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"177\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-18.png\" alt=\"\" class=\"wp-image-1207\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-18.png 875w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-18-300x61.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-18-768x155.png 768w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can see that <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-sccyjb?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">code example on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we saw that LocalStorage can be used as a persistent cache to store our data in the browser. Earlier, we covered that services are a cache of their own but have one instance per app\/browser tab, which means that applications opened in multiple tabs can have an inconsistent state since they each have their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,17],"tags":[],"class_list":["post-1202","post","type-post","status-publish","format-standard","hentry","category-javascript","category-tools"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1202\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}