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
118 lines (96 loc) · 3.59 KB
/
SettingsManager.ts
File metadata and controls
118 lines (96 loc) · 3.59 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { SettingsResponse } from '../submission/SettingsResponse';
import { Utils } from '../Utils';
import { Configuration } from './Configuration';
interface ISettingsWithVersion {
version: number;
settings: { [key: string]: string };
}
export class SettingsManager {
/**
* A list of handlers that will be fired when the settings change.
* @type {Array}
* @private
*/
private static _handlers: Array<(config: Configuration) => void> = [];
public static onChanged(handler: (config: Configuration) => void) {
!!handler && this._handlers.push(handler);
}
public static applySavedServerSettings(config: Configuration): void {
if (!config || !config.isValid) {
return;
}
const savedSettings = this.getSavedServerSettings(config);
config.log.info(`Applying saved settings: v${savedSettings.version}`);
config.settings = Utils.merge(config.settings, savedSettings.settings);
this.changed(config);
}
public static getVersion(config: Configuration): number {
if (!config || !config.isValid) {
return 0;
}
const savedSettings = this.getSavedServerSettings(config);
return savedSettings.version || 0;
}
public static checkVersion(version: number, config: Configuration): void {
const currentVersion: number = this.getVersion(config);
if (version <= currentVersion) {
return;
}
config.log.info(`Updating settings from v${currentVersion} to v${version}`);
this.updateSettings(config, currentVersion);
}
public static updateSettings(config: Configuration, version?: number): void {
if (!config || !config.enabled) {
return;
}
const unableToUpdateMessage = 'Unable to update settings';
if (!config.isValid) {
config.log.error(`${unableToUpdateMessage}: ApiKey is not set.`);
return;
}
if (!version || version < 0) {
version = this.getVersion(config);
}
config.log.info(`Checking for updated settings from: v${version}.`);
config.submissionClient.getSettings(config, version, (response: SettingsResponse) => {
if (!config || !response || !response.success || !response.settings) {
config.log.warn(`${unableToUpdateMessage}: ${response.message}`);
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.
const savedServerSettings = SettingsManager.getSavedServerSettings(config);
for (const key in savedServerSettings) {
if (response.settings[key]) {
continue;
}
delete config.settings[key];
}
const newSettings: ISettingsWithVersion = {
version: response.settingsVersion,
settings: response.settings
};
config.storage.settings.save(newSettings);
config.log.info(`Updated settings: v${newSettings.version}`);
this.changed(config);
});
}
private static changed(config: Configuration) {
const handlers = this._handlers; // optimization for minifier.
for (const handler of handlers) {
try {
handler(config);
} catch (ex) {
config.log.error(`Error calling onChanged handler: ${ex}`);
}
}
}
private static getSavedServerSettings(config: Configuration): ISettingsWithVersion {
const item = config.storage.settings.get()[0];
if (item && item.value && item.value.version && item.value.settings) {
return item.value;
}
return { version: 0, settings: {} };
}
}