]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/wrappers/storage.service.ts
Refactor video miniature
[github/Chocobozzz/PeerTube.git] / client / src / app / core / wrappers / storage.service.ts
1 import { Observable, Subject } from 'rxjs'
2 import { filter } from 'rxjs/operators'
3 import { Injectable } from '@angular/core'
4 import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage'
5
6 abstract class StorageService {
7 protected instance: Storage
8 static storageSub = new Subject<string>()
9
10 watch (keys?: string[]): Observable<string> {
11 return StorageService.storageSub.asObservable().pipe(filter(val => keys ? keys.includes(val) : true))
12 }
13
14 getItem <T extends string> (key: string) {
15 return this.instance.getItem(key) as T
16 }
17
18 setItem (key: string, data: any, notifyOfUpdate = true) {
19 this.instance.setItem(key, data)
20 if (notifyOfUpdate) StorageService.storageSub.next(key)
21 }
22
23 removeItem (key: string, notifyOfUpdate = true) {
24 this.instance.removeItem(key)
25 if (notifyOfUpdate) StorageService.storageSub.next(key)
26 }
27 }
28
29 @Injectable()
30 export class LocalStorageService extends StorageService {
31 protected instance: Storage = peertubeLocalStorage
32 }
33
34 @Injectable()
35 export class SessionStorageService extends StorageService {
36 protected instance: Storage = peertubeSessionStorage
37 }