forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryStorage.ts
More file actions
47 lines (38 loc) · 1.01 KB
/
InMemoryStorage.ts
File metadata and controls
47 lines (38 loc) · 1.01 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
import { IStorage } from './IStorage';
import { IStorageItem } from './IStorageItem';
export class InMemoryStorage implements IStorage {
private maxItems: number;
private items: IStorageItem[] = [];
private lastTimestamp: number = 0;
constructor(maxItems: number) {
this.maxItems = maxItems;
}
public save(value: any): number {
if (!value) {
return null;
}
const items = this.items;
const timestamp = Math.max(Date.now(), this.lastTimestamp + 1);
const item = { timestamp, value };
if (items.push(item) > this.maxItems) {
items.shift();
}
this.lastTimestamp = timestamp;
return item.timestamp;
}
public get(limit?: number): IStorageItem[] {
return this.items.slice(0, limit);
}
public remove(timestamp: number): void {
const items = this.items;
for (let i = 0; i < items.length; i++) {
if (items[i].timestamp === timestamp) {
items.splice(i, 1);
return;
}
}
}
public clear(): void {
this.items = [];
}
}