2020-02-28 12:52:21 +00:00
|
|
|
import { Observable, Subject } from 'rxjs'
|
|
|
|
import { filter } from 'rxjs/operators'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { Injectable } from '@angular/core'
|
2020-08-03 16:06:49 +00:00
|
|
|
import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage'
|
2020-02-28 12:52:21 +00:00
|
|
|
|
|
|
|
abstract class StorageService {
|
|
|
|
protected instance: Storage
|
|
|
|
static storageSub = new Subject<string>()
|
|
|
|
|
|
|
|
watch (keys?: string[]): Observable<string> {
|
|
|
|
return StorageService.storageSub.asObservable().pipe(filter(val => keys ? keys.includes(val) : true))
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:24:30 +00:00
|
|
|
getItem <T extends string> (key: string) {
|
|
|
|
return this.instance.getItem(key) as T
|
2020-02-28 12:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setItem (key: string, data: any, notifyOfUpdate = true) {
|
|
|
|
this.instance.setItem(key, data)
|
|
|
|
if (notifyOfUpdate) StorageService.storageSub.next(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeItem (key: string, notifyOfUpdate = true) {
|
|
|
|
this.instance.removeItem(key)
|
|
|
|
if (notifyOfUpdate) StorageService.storageSub.next(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class LocalStorageService extends StorageService {
|
|
|
|
protected instance: Storage = peertubeLocalStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SessionStorageService extends StorageService {
|
|
|
|
protected instance: Storage = peertubeSessionStorage
|
|
|
|
}
|