From 4d093a13899bee0501bc5e87dcc27d1d0531c0dc Mon Sep 17 00:00:00 2001 From: Daniel Scheidle Date: Wed, 9 Nov 2022 20:15:59 +0100 Subject: [PATCH] Fixed tests. --- src/app/services/storage.service.ts | 39 ++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/app/services/storage.service.ts b/src/app/services/storage.service.ts index 43aa23c..0ce22b5 100644 --- a/src/app/services/storage.service.ts +++ b/src/app/services/storage.service.ts @@ -6,30 +6,51 @@ import {BehaviorSubject} from "rxjs"; }) export class StorageService implements OnDestroy { - private storage: string[] = []; + private readonly storageKey = 'symbols' private _storage$ = new BehaviorSubject([]); 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 { - this.storage.push(symbol); - this._storage$.next(this.storage) + const symbols = this.getSymbols(); + symbols.push(symbol); + this.storeSymbols(symbols); + this._storage$.next(symbols); } 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) { - this.storage.splice(index, 1); + symbols.splice(index, 1); } - - console.log(this.storage) - this._storage$.next(this.storage) + this.storeSymbols(symbols); + this._storage$.next(symbols) } get(): BehaviorSubject { + if (this._storage$.value.length === 0) { + this._storage$.next(this.getSymbols()); + } return this._storage$; }