]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { Observable, Subject } from 'rxjs' |
d3217560 | 2 | import { filter } from 'rxjs/operators' |
67ed6552 | 3 | import { Injectable } from '@angular/core' |
4504f09f | 4 | import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage' |
d3217560 RK |
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 (key: string) { | |
15 | return this.instance.getItem(key) | |
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 | } |