particles setting
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { LocalStorageService } from './local-storage.service';
|
||||
|
||||
describe('LocalStorageService', () => {
|
||||
let service: LocalStorageService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(LocalStorageService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user