Skip to content

Commit 7261943

Browse files
committed
Changed IStorage to non-generic
Content type is not enforced, and we use one common storage for heterogeneous data (events and configuration data).
1 parent c65fe95 commit 7261943

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

src/configuration/Configuration.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Configuration implements IConfigurationSettings {
7676
*/
7777
public settings: Object = {};
7878

79-
public storage: IStorage<Object>;
79+
public storage: IStorage;
8080

8181
public queue: IEventQueue;
8282

@@ -106,7 +106,7 @@ export class Configuration implements IConfigurationSettings {
106106
this.submissionBatchSize = inject(configSettings.submissionBatchSize) || 50;
107107
this.submissionAdapter = inject(configSettings.submissionAdapter);
108108
this.submissionClient = inject(configSettings.submissionClient) || new DefaultSubmissionClient();
109-
this.storage = inject(configSettings.storage) || new InMemoryStorage<any>();
109+
this.storage = inject(configSettings.storage) || new InMemoryStorage();
110110
this.queue = inject(configSettings.queue) || new DefaultEventQueue(this);
111111

112112
SettingsManager.applySavedServerSettings(this);

src/configuration/IConfigurationSettings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export interface IConfigurationSettings {
2121
submissionBatchSize?: number;
2222
submissionClient?: ISubmissionClient;
2323
submissionAdapter?: ISubmissionAdapter;
24-
storage?: IStorage<any>;
24+
storage?: IStorage;
2525
queue?: IEventQueue;
2626
}

src/storage/IStorage.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { IStorageItem } from './IStorageItem';
22

3-
export interface IStorage<T> {
4-
save(path: string, value: T): boolean;
5-
get(path: string): T;
6-
getList(searchPattern?: string, limit?: number): IStorageItem<T>[];
3+
export interface IStorage {
4+
save(path: string, value: any): boolean;
5+
get(path: string): any;
6+
getList(searchPattern?: string, limit?: number): IStorageItem[];
77
remove(path: string): void;
88
}

src/storage/IStorageItem.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export interface IStorageItem<T> {
1+
export interface IStorageItem {
22
created: number;
33
path: string;
4-
value: T;
4+
value: any;
55
}

src/storage/InMemoryStorage-spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expect } from 'chai';
55

66
describe('InMemoryStorage', () => {
77
it('should save events', () => {
8-
let storage = new InMemoryStorage<IEvent>();
8+
let storage = new InMemoryStorage();
99
let key = 'ex-q-';
1010
let event1: IEvent = { type: 'log', reference_id: key + '123454321' };
1111
let event2: IEvent = { type: 'log', reference_id: key + '098765432' };
@@ -20,15 +20,15 @@ describe('InMemoryStorage', () => {
2020
});
2121

2222
it('should save once', () => {
23-
let storage = new InMemoryStorage<number>();
23+
let storage = new InMemoryStorage();
2424
storage.save('one', 1);
2525
expect(storage.getList().length).to.equal(1);
2626
storage.save('one', 1);
2727
expect(storage.getList().length).to.equal(1);
2828
});
2929

3030
it('should get by key', () => {
31-
let storage = new InMemoryStorage<any>();
31+
let storage = new InMemoryStorage();
3232
storage.save('ex-server-settings.json-version', 1);
3333
storage.save('ex-server-settings.json', { exist: true });
3434
expect(storage.getList().length).to.equal(2);
@@ -37,7 +37,7 @@ describe('InMemoryStorage', () => {
3737
});
3838

3939
it('should get saved events', () => {
40-
let storage = new InMemoryStorage<IEvent>();
40+
let storage = new InMemoryStorage();
4141
let key = 'ex-q-';
4242
let event1: IEvent = { type: 'log', reference_id: key + '11' };
4343
let event2: IEvent = { type: 'log', reference_id: key + '12' };
@@ -81,7 +81,7 @@ describe('InMemoryStorage', () => {
8181
});
8282

8383
it('should clear all events', () => {
84-
let storage = new InMemoryStorage<IEvent>();
84+
let storage = new InMemoryStorage();
8585
let key = 'ex-q-';
8686
let event1: IEvent = { type: 'log', reference_id: key + '11' };
8787
let event2: IEvent = { type: 'log', reference_id: key + '12' };
@@ -111,7 +111,7 @@ describe('InMemoryStorage', () => {
111111
});
112112

113113
it('should get with limit', () => {
114-
let storage = new InMemoryStorage<IEvent>(250);
114+
let storage = new InMemoryStorage(250);
115115
for (let index: number = 0; index < 260; index++) {
116116
storage.save('ex-q-' + index, { type: 'log', reference_id: index.toString() });
117117
}
@@ -127,7 +127,7 @@ describe('InMemoryStorage', () => {
127127
}
128128

129129
const DATE: Date = new Date();
130-
let storage = new InMemoryStorage<IEvent>();
130+
let storage = new InMemoryStorage();
131131
for (let index: number = 0; index < 10; index++) {
132132
storage.save('ex-q-' + index, {
133133
date: getDate(DATE, index),
@@ -139,7 +139,7 @@ describe('InMemoryStorage', () => {
139139
}
140140

141141
let offset: number = 0;
142-
let events: IStorageItem<IEvent>[] = storage.getList('ex-q-', 2);
142+
let events: IStorageItem[] = storage.getList('ex-q-', 2);
143143
while (events && events.length > 0) {
144144
expect(2).to.equal(events.length);
145145
for (let ei = 0; ei < 2; ei++) {
@@ -152,12 +152,12 @@ describe('InMemoryStorage', () => {
152152
});
153153

154154
it('should respect max items limit', () => {
155-
let storage = new InMemoryStorage<IEvent>(5);
155+
let storage = new InMemoryStorage(5);
156156
for (let index: number = 0; index < 5; index++) {
157157
storage.save('ex-q-' + index, { type: 'log', reference_id: index.toString() });
158158
}
159159

160-
let events: IStorageItem<IEvent>[] = storage.getList();
160+
let events: IStorageItem[] = storage.getList();
161161
expect(events.length).to.equal(5);
162162
expect(events[0].path).to.equal('ex-q-0');
163163
storage.save('ex-q-6', { type: 'log', reference_id: '6' });

src/storage/InMemoryStorage.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { IStorage } from './IStorage';
22
import { IStorageItem } from './IStorageItem';
33

4-
export class InMemoryStorage<T> implements IStorage<T> {
5-
private _items: IStorageItem<T>[] = [];
4+
export class InMemoryStorage implements IStorage {
5+
private _items: IStorageItem[] = [];
66
private _maxItems: number;
77

88
constructor(maxItems?: number) {
99
this._maxItems = maxItems > 0 ? maxItems : 250;
1010
}
1111

12-
public save(path: string, value: T): boolean {
12+
public save(path: string, value: any): boolean {
1313
if (!path || !value) {
1414
return false;
1515
}
@@ -22,19 +22,19 @@ export class InMemoryStorage<T> implements IStorage<T> {
2222
return true;
2323
}
2424

25-
public get(path: string): T {
26-
let item: IStorageItem<T> = path ? this.getList(`^${path}$`, 1)[0] : null;
25+
public get(path: string): any {
26+
let item: IStorageItem = path ? this.getList(`^${path}$`, 1)[0] : null;
2727
return item ? item.value : null;
2828
}
2929

30-
public getList(searchPattern?: string, limit?: number): IStorageItem<T>[] {
30+
public getList(searchPattern?: string, limit?: number): IStorageItem[] {
3131
let items = this._items; // Optimization for minifier
3232
if (!searchPattern) {
3333
return items.slice(0, limit);
3434
}
3535

3636
let regex = new RegExp(searchPattern);
37-
let results: IStorageItem<T>[] = [];
37+
let results: IStorageItem[] = [];
3838
for (let index = 0; index < items.length; index++) {
3939
if (regex.test(items[index].path)) {
4040
results.push(items[index]);

0 commit comments

Comments
 (0)