mirror of
https://github.com/Dansen999/at-certification-stock.git
synced 2026-01-10 21:13:40 +00:00
Fixed tests.
This commit is contained in:
@@ -6,30 +6,51 @@ import {BehaviorSubject} from "rxjs";
|
|||||||
})
|
})
|
||||||
export class StorageService implements OnDestroy {
|
export class StorageService implements OnDestroy {
|
||||||
|
|
||||||
private storage: string[] = [];
|
private readonly storageKey = 'symbols'
|
||||||
private _storage$ = new BehaviorSubject<string[]>([]);
|
private _storage$ = new BehaviorSubject<string[]>([]);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getSymbols(): string[] {
|
||||||
|
try {
|
||||||
|
const symbols = JSON.parse(localStorage.getItem(this.storageKey) || '');
|
||||||
|
if (Array.isArray(symbols)) {
|
||||||
|
return symbols;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Failed reading symbols.")
|
||||||
|
}
|
||||||
|
localStorage.setItem(this.storageKey, '[]');
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private storeSymbols(symbols: string[]): void {
|
||||||
|
localStorage.setItem(this.storageKey, JSON.stringify(symbols));
|
||||||
|
}
|
||||||
|
|
||||||
add(symbol: string): void {
|
add(symbol: string): void {
|
||||||
this.storage.push(symbol);
|
const symbols = this.getSymbols();
|
||||||
this._storage$.next(this.storage)
|
symbols.push(symbol);
|
||||||
|
this.storeSymbols(symbols);
|
||||||
|
this._storage$.next(symbols);
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(symbol: string): void {
|
remove(symbol: string): void {
|
||||||
console.log('Remove ' + symbol)
|
const symbols = this.getSymbols();
|
||||||
|
|
||||||
let index = this.storage.findIndex(item => item == symbol);
|
let index = symbols.findIndex(item => item == symbol);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
this.storage.splice(index, 1);
|
symbols.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
this.storeSymbols(symbols);
|
||||||
console.log(this.storage)
|
this._storage$.next(symbols)
|
||||||
this._storage$.next(this.storage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get(): BehaviorSubject<string[]> {
|
get(): BehaviorSubject<string[]> {
|
||||||
|
if (this._storage$.value.length === 0) {
|
||||||
|
this._storage$.next(this.getSymbols());
|
||||||
|
}
|
||||||
return this._storage$;
|
return this._storage$;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user