forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserStorage.ts
More file actions
48 lines (38 loc) · 1.09 KB
/
BrowserStorage.ts
File metadata and controls
48 lines (38 loc) · 1.09 KB
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
42
43
44
45
46
47
48
import { KeyValueStorageBase } from './KeyValueStorageBase';
export class BrowserStorage extends KeyValueStorageBase {
private prefix: string;
public static isAvailable(): boolean {
try {
const storage = window.localStorage;
const x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return false;
}
}
constructor(namespace: string, prefix: string = 'com.exceptionless.', maxItems: number = 20) {
super(maxItems);
this.prefix = prefix + namespace + '-';
}
public write(key: string, value: string) {
window.localStorage.setItem(key, value);
}
public read(key: string) {
return window.localStorage.getItem(key);
}
public readAllKeys() {
return Object.keys(window.localStorage)
.filter((key) => key.indexOf(this.prefix) === 0);
}
public delete(key: string) {
window.localStorage.removeItem(key);
}
public getKey(timestamp) {
return this.prefix + timestamp;
}
public getTimestamp(key) {
return parseInt(key.substr(this.prefix.length), 10);
}
}