forked from ngxs-labs/async-storage-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.service.ts
41 lines (33 loc) · 961 Bytes
/
storage.service.ts
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
38
39
40
41
import { Injectable } from '@angular/core';
import { Observable, from } from 'rxjs';
import { Storage } from '@ionic/storage';
import { AsyncStorageEngine } from '../../../src/lib/symbols';
@Injectable({
providedIn: 'root',
})
export class StorageService implements AsyncStorageEngine {
constructor(private storage: Storage) {
this.init();
}
length(): Observable<number> {
return from(this.storage.length());
}
getItem(key: any): Observable<any> {
return from(this.storage.get(key));
}
setItem(key: any, val: any): void {
this.storage.set(key, val);
}
removeItem(key: any): void {
this.storage.remove(key);
}
clear(): void {
this.storage.clear();
}
key(val: number): Observable<string> {
return from(this.storage.keys().then((keys) => keys[val]));
}
private async init(): Promise<void> {
await this.storage.create();
}
}