forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.ts
More file actions
72 lines (58 loc) · 2.5 KB
/
SettingsManager.ts
File metadata and controls
72 lines (58 loc) · 2.5 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Configuration } from 'Configuration';
import { SettingsResponse } from '../submission/SettingsResponse';
import { Utils } from '../Utils';
export class SettingsManager {
private static _configPath:string = 'ex-server-settings.json';
private static _handlers:{ (config:Configuration):void }[] = [];
private static changed(config:Configuration) {
for (var index = 0; index < this._handlers.length; index++) {
this._handlers[index](config);
}
}
public static onChanged(handler:(config:Configuration) => void) {
!!handler && this._handlers.push(handler);
}
public static applySavedServerSettings(config:Configuration):void {
config.log.info('Applying saved settings.');
config.settings = Utils.merge(config.settings, this.getSavedServerSettings(config));
this.changed(config);
}
private static getSavedServerSettings(config:Configuration):Object {
return config.storage.get(this._configPath, 1)[0] || {};
}
public static checkVersion(version:number, config:Configuration):void {
if (isNaN(version) || version <= 0) {
return;
}
var savedConfigVersion = parseInt(<string>config.storage.get(`${this._configPath}-version`, 1)[0]);
if (isNaN(savedConfigVersion) || version > savedConfigVersion) {
config.log.info(`Updating settings from v${(!isNaN(savedConfigVersion) ? savedConfigVersion : 0)} to v${version}`);
this.updateSettings(config);
}
}
public static updateSettings(config:Configuration):void {
if (!config.isValid) {
config.log.error('Unable to update settings: ApiKey is not set.');
return;
}
config.submissionClient.getSettings(config, (response:SettingsResponse) => {
if (!response || !response.success || !response.settings) {
return;
}
config.settings = Utils.merge(config.settings, response.settings);
// TODO: Store snapshot of settings after reading from config and attributes and use that to revert to defaults.
// Remove any existing server settings that are not in the new server settings.
var savedServerSettings = SettingsManager.getSavedServerSettings(config);
for (var key in savedServerSettings) {
if (response.settings[key]) {
continue;
}
delete config.settings[key];
}
config.storage.save(`${this._configPath}-version`, response.settingsVersion);
config.storage.save(this._configPath, response.settings);
config.log.info('Updated settings');
this.changed(config);
});
}
}