aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/wrappers/storage.service.ts
blob: ad3aca6b8c031cd67849883ff4c7dedf9281c69e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Observable, Subject } from 'rxjs'
import { filter } from 'rxjs/operators'
import { Injectable } from '@angular/core'
import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage'

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))
  }

  getItem (key: string) {
    return this.instance.getItem(key)
  }

  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
}