Skip to content

Commit b3c9d67

Browse files
committed
Added the ability for plugins to specify default configuration settings.
1 parent 21c7cfd commit b3c9d67

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/Utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ module Exceptionless {
2525
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
2626
}
2727

28+
public static merge(defaultValues:any, values:any) {
29+
var result = {};
30+
31+
for (var key in defaultValues || {}) {
32+
if (!!defaultValues[key]) {
33+
result[key] = defaultValues[key];
34+
}
35+
}
36+
37+
for (var key in values || {}) {
38+
if (!!values[key]) {
39+
result[key] = values[key];
40+
}
41+
}
42+
43+
return result;
44+
}
45+
2846
public static parseVersion(source:string): string {
2947
if (!source) {
3048
return null;

src/configuration/Configuration.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
/// <reference path="../references.ts" />
22

33
module Exceptionless {
4-
export class Configuration {
4+
export class Configuration implements IConfigurationSettings{
55
private _apiKey:string;
66
private _enabled:boolean = false;
77
private _serverUrl:string = 'https://collector.exceptionless.io';
88
private _plugins:IEventPlugin[] = [];
99

1010
public lastReferenceIdManager:ILastReferenceIdManager = new InMemoryLastReferenceIdManager();
11-
public log:ILog = new NullLog();
12-
public submissionBatchSize = 50;
13-
public submissionClient:ISubmissionClient = new DefaultSubmissionClient();
14-
public storage:IStorage<any> = new InMemoryStorage<any>();
11+
public log:ILog;
12+
public submissionBatchSize;
13+
public submissionClient:ISubmissionClient;
14+
public storage:IStorage<any>;
1515
public queue:IEventQueue;
1616
public defaultTags:string[] = [];
1717
public defaultData:Object = {};
1818

19-
constructor(apiKey:string, serverUrl?:string) {
20-
this.apiKey = apiKey;
21-
this.serverUrl = serverUrl;
22-
this.queue = new DefaultEventQueue(this);
19+
constructor(settings?:IConfigurationSettings) {
20+
function inject(fn:any) {
21+
return typeof fn === 'function' ? fn(this) : fn;
22+
}
23+
24+
settings = Utils.merge(Configuration.defaults, settings);
25+
26+
this.apiKey = settings.apiKey;
27+
this.serverUrl = settings.serverUrl;
28+
this.lastReferenceIdManager = inject(settings.lastReferenceIdManager) || new InMemoryLastReferenceIdManager();
29+
this.log = inject(settings.log) || new NullLog();
30+
this.submissionBatchSize = inject(settings.submissionBatchSize) || 50;
31+
this.submissionClient = inject(settings.submissionClient) || new DefaultSubmissionClient();
32+
this.storage = inject(settings.storage) || new InMemoryStorage<any>();
33+
this.queue = inject(settings.queue) || new DefaultEventQueue(this);
2334

2435
EventPluginManager.addDefaultPlugins(this);
2536
}
@@ -29,7 +40,7 @@ module Exceptionless {
2940
}
3041

3142
public set apiKey(value:string) {
32-
this._apiKey = value;
43+
this._apiKey = value || null;
3344
this._enabled = !!value && value.length > 0;
3445
}
3546

@@ -103,5 +114,14 @@ module Exceptionless {
103114
public useReferenceIds(): void {
104115
this.addPlugin(new ReferenceIdPlugin());
105116
}
117+
118+
private static _defaultSettings:IConfigurationSettings = null;
119+
public static get defaults() {
120+
if(Configuration._defaultSettings === null) {
121+
Configuration._defaultSettings = {};
122+
}
123+
124+
return Configuration._defaultSettings;
125+
}
106126
}
107127
}

src/configuration/IConfigurationOptions.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)