license import { BehaviorSubject } from 'rxjs' ; import { skip } from 'rxjs/operators' ; export function getStoredBooleanOrDefault ( key , defaultVal ) { const storedVal = localStorage . getItem ( key ) ; return storedVal != null ? ! ! + storedVal : defaultVal ; } export function getStoredNumberOrDefault ( key , defaultVal ) { const storedVal = localStorage . getItem ( key ) ; return storedVal != null ? + storedVal : defaultVal ; } export function createBooleanLocalStorageBehaviorSubject ( key , defaultVal ) { const initVal = getStoredBooleanOrDefault ( key , defaultVal ) ; const behaviorSubject = new BehaviorSubject < boolean > ( initVal ) ; behaviorSubject . pipe ( skip ( 1 ) ) . subscribe ( ( updatedVal ) => { localStorage . setItem ( key , updatedVal ? '1' : '0' ) ; } ) ; return behaviorSubject ; } export function createNumberLocalStorageBehaviorSubject ( key , defaultVal ) { const initVal = getStoredNumberOrDefault ( key , defaultVal ) ; const behaviorSubject = new BehaviorSubject < number > ( initVal ) ; behaviorSubject . pipe ( skip ( 1 ) ) . subscribe ( ( updatedVal ) => { localStorage . setItem ( key , `${ updatedVal }` ) ; } ) ; return behaviorSubject ; }