OpenSlides/client/src/app/core/core-services/storage.service.ts

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-10-26 10:23:14 +02:00
import { Injectable } from '@angular/core';
2018-10-26 10:23:14 +02:00
import { LocalStorage } from '@ngx-pwa/local-storage';
2019-02-15 12:39:14 +01:00
import { Observable } from 'rxjs';
import { OpenSlidesStatusService } from './openslides-status.service';
2018-10-26 10:23:14 +02:00
/**
* Provides an async API to an key-value store using ngx-pwa which is internally
* using IndexedDB or localStorage as a fallback.
*/
@Injectable({
providedIn: 'root'
})
export class StorageService {
private noClearKeys: string[] = [];
2018-10-26 10:23:14 +02:00
/**
* Constructor to create the StorageService. Needs the localStorage service.
* @param localStorage
*/
public constructor(private localStorage: LocalStorage, private OSStatus: OpenSlidesStatusService) {}
2018-10-26 10:23:14 +02:00
public addNoClearKey(key: string): void {
this.noClearKeys.push(key);
}
2018-10-26 10:23:14 +02:00
/**
* Sets the item into the store asynchronously.
* @param key
* @param item
*/
public async set(key: string, item: any): Promise<void> {
2019-02-15 12:39:14 +01:00
this.assertNotHistoryMode();
2018-10-26 10:23:14 +02:00
if (item === null || item === undefined) {
await this.remove(key); // You cannot do a setItem with null or undefined...
} else {
if (!(await this.localStorage.setItem(key, item).toPromise())) {
throw new Error('Could not set the item.');
}
}
}
/**
* get a value from the store. You need to subscribe to the request to retrieve the value.
2018-11-14 14:09:43 +01:00
*
2018-10-26 10:23:14 +02:00
* @param key The key to get the value from
* @returns The requested value to the key
*/
public async get<T>(key: string): Promise<T> {
2019-02-15 12:39:14 +01:00
return ((await this.localStorage.getItem<T>(key)) as Observable<T>).toPromise();
2018-10-26 10:23:14 +02:00
}
/**
* Remove the key from the store.
* @param key The key to remove the value from
*/
public async remove(key: string): Promise<void> {
2019-02-15 12:39:14 +01:00
this.assertNotHistoryMode();
2018-10-26 10:23:14 +02:00
if (!(await this.localStorage.removeItem(key).toPromise())) {
throw new Error('Could not delete the item.');
}
}
/**
* Clear the whole cache except for keys given in `addNoClearKey`.
2018-10-26 10:23:14 +02:00
*/
public async clear(): Promise<void> {
2019-02-15 12:39:14 +01:00
this.assertNotHistoryMode();
const savedData: { [key: string]: any } = {};
for (const key of this.noClearKeys) {
savedData[key] = await this.get(key);
}
2018-10-26 10:23:14 +02:00
if (!(await this.localStorage.clear().toPromise())) {
throw new Error('Could not clear the storage.');
}
for (const key of this.noClearKeys) {
await this.set(key, savedData[key]);
}
2018-10-26 10:23:14 +02:00
}
/**
* Throws an error, if we are in history mode.
*/
2019-02-15 12:39:14 +01:00
private assertNotHistoryMode(): void {
if (this.OSStatus.isInHistoryMode) {
throw new Error('You cannot use the storageService in histroy mode.');
}
}
2018-10-26 10:23:14 +02:00
}