forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserLocalStorage.ts
More file actions
48 lines (38 loc) · 1.17 KB
/
BrowserLocalStorage.ts
File metadata and controls
48 lines (38 loc) · 1.17 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 "@exceptionless/core";
export class BrowserLocalStorage 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 writeValue(key: string, value: string): void {
window.localStorage.setItem(key, value);
}
public readValue(key: string): string {
return window.localStorage.getItem(key);
}
public removeValue(key: string): void {
window.localStorage.removeItem(key);
}
public getAllKeys(): string[] {
return Object.keys(window.localStorage)
.filter((key) => key.indexOf(this.prefix) === 0);
}
public getKey(timestamp: number): string {
return this.prefix + timestamp;
}
public getTimestamp(key: string): number {
return parseInt(key.substr(this.prefix.length), 10);
}
}