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
83 lines (69 loc) · 2.75 KB
/
SettingsManager.ts
File metadata and controls
83 lines (69 loc) · 2.75 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
73
74
75
76
77
78
79
80
81
82
83
import { Configuration } from 'Configuration';
import { SettingsResponse } from '../submission/SettingsResponse';
import { Utils } from '../Utils';
export class SettingsManager {
/**
* The configuration settings path.
* @type {string}
* @private
*/
private static _configPath:string = 'ex-server-settings.json';
/**
* A list of handlers that will be fired when the settings change.
* @type {Array}
* @private
*/
private static _handlers:{ (config:Configuration):void }[] = [];
private static changed(config:Configuration) {
var handlers = this._handlers; // optimization for minifier.
for (var index = 0; index < handlers.length; index++) {
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) || {};
}
public static checkVersion(version:number, config:Configuration):void {
if (version) {
var savedConfigVersion = parseInt(<string>config.storage.get(`${this._configPath}-version`));
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];
}
var path = SettingsManager._configPath; // optimization for minifier.
config.storage.save(`${path}-version`, response.settingsVersion);
config.storage.save(path, response.settings);
config.log.info('Updated settings');
this.changed(config);
});
}
}