Local storage as Observable Service

Jul 23, 2020 10:08 · 106 words · 1 minute read angular rxjs

Creating a observable service from browser s local storage

A quick and simple example of creating a stream for your entire local storage. We will use an Observable based service.

Our service holds _entireStorage$:BehaviorSubject<Map<string, string>> subject that we will subscribe all users of this service to in constructor. This will handle multicasting. Other than that we have 3 very simple methods - adding and removing of items from local storage (these are public and can be used in components / directives using this service) and _allStorage that basicaly gathers entire storage into a map. This map is the data we store in our subject.

Live Stackblitz demo of Observable local storage service.

Local storage service