Maxzilla60 / observable-localstorage

Use the browser's LocalStorage as an RxJS Observable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Observable LocalStorage

A simple module that provides subscribing to changes in LocalStorage as an RxJS Observable.

Installing

npm install @maxzilla60/observable-localstorage --registry=https://npm.pkg.github.com/

Usage

The api is intentionally similar to LocalStorage's api, with a set('key', value) for updating and a get('key') for getting the observable instance. There is also a getLatestValue('key') which will return the current value of the key (not an observable). The implementation will always try to JSON.stringify & JSON.parse and will fail silently and just return the string value.

import oLocalStorage from '@maxzilla60/observable-localstorage';
import { distinctUntilChanged } from 'rxjs/operators';

oLocalStorage.set('stuff', ['Initial value']);

oLocalStorage.get('stuff')
	.pipe(distinctUntilChanged(arraysMatch)) // Only subscribe to distinct new values
	.subscribe(newValue => {
		// Do stuff with the stuff...
	});

About

Use the browser's LocalStorage as an RxJS Observable


Languages

Language:TypeScript 100.0%