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
73 lines (60 loc) · 2.15 KB
/
SettingsManager.ts
File metadata and controls
73 lines (60 loc) · 2.15 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
import { Configuration } from "./Configuration.js";
export class ServerSettings {
constructor(
public settings: Record<string, string>,
public version: number
) { }
}
export class SettingsManager {
private static readonly SettingsKey: string = "settings";
private static _isUpdatingSettings = false;
public static async applySavedServerSettings(config: Configuration): Promise<void> {
if (!config?.isValid) {
return;
}
const savedSettings = await this.getSavedServerSettings(config);
if (savedSettings) {
config.applyServerSettings(savedSettings);
}
}
public static async updateSettings(config: Configuration): Promise<void> {
if (!config?.enabled || this._isUpdatingSettings) {
return;
}
this._isUpdatingSettings = true;
const { log } = config.services;
try {
const unableToUpdateMessage = "Unable to update settings";
if (!config.isValid) {
log.error(`${unableToUpdateMessage}: ApiKey is not set`);
return;
}
const version = config.settingsVersion;
log.trace(`Checking for updated settings from: v${version}`);
const response = await config.services.submissionClient.getSettings(version);
if (response.status === 304) {
log.trace("Settings are up-to-date");
return;
}
if (!response?.success || !response.data) {
log.warn(`${unableToUpdateMessage}: ${response.message}`);
return;
}
config.applyServerSettings(response.data);
await config.services.storage.setItem(SettingsManager.SettingsKey, JSON.stringify(response.data));
log.trace(`Updated settings: v${response.data.version}`);
} catch (ex) {
log.error(`Error updating settings: ${ex.message}`);
} finally {
this._isUpdatingSettings = false;
}
}
private static async getSavedServerSettings(config: Configuration): Promise<ServerSettings> {
try {
const settings = await config.services.storage.getItem(SettingsManager.SettingsKey);
return settings && JSON.parse(settings) as ServerSettings || new ServerSettings({}, 0);
} catch {
return new ServerSettings({}, 0);
}
}
}