Files
public-pool-ui/src/app/services/local-storage.service.ts
T
2023-07-24 22:20:55 -04:00

45 lines
1.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, shareReplay } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LocalStorageService {
private PARTICLES = 'PARTICLES';
private _particles$: BehaviorSubject<boolean>;
public particles$: Observable<boolean>;
constructor() {
this._particles$ = new BehaviorSubject<boolean>(this.getParticles());
this.particles$ = this._particles$.asObservable().pipe(shareReplay({ refCount: true, bufferSize: 1 }));
}
private get(key: string): string | null {
return localStorage.getItem(key);
}
private set(key: string, value: string) {
localStorage.setItem(key, value);
}
private remove(key: string): void {
localStorage.removeItem(key);
}
public getParticles(): boolean {
const result = this.get(this.PARTICLES);
return result == null || JSON.parse(result)?.particles === true;
}
public setParticles(particles: boolean) {
this.set(this.PARTICLES, JSON.stringify({ particles }));
this._particles$.next(particles);
}
}