-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSettingsManager.ts
More file actions
217 lines (189 loc) · 7.73 KB
/
SettingsManager.ts
File metadata and controls
217 lines (189 loc) · 7.73 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { diff } from 'deep-object-diff';
import { DeepReadonly } from 'ts-essentials';
import { LspWorkspace } from '../protocol/LspWorkspace';
import { LoggerFactory } from '../telemetry/LoggerFactory';
import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
import { Measure, Telemetry } from '../telemetry/TelemetryDecorator';
import { ReadinessContributor, ReadinessStatus } from '../utils/ReadinessContributor';
import { AwsRegion } from '../utils/Region';
import { toString } from '../utils/String';
import { PartialDataObserver, SubscriptionManager } from '../utils/SubscriptionManager';
import { parseWithPrettyError } from '../utils/ZodErrorWrapper';
import { ISettingsSubscriber, SettingsPathKey } from './ISettingsSubscriber';
import { DefaultSettings, Settings, SettingsState } from './Settings';
import { parseSettings } from './SettingsParser';
const logger = LoggerFactory.getLogger('SettingsManager');
export class SettingsManager implements ISettingsSubscriber, ReadinessContributor {
@Telemetry() private readonly telemetry!: ScopedTelemetry;
private readonly settingsState = new SettingsState();
private readonly subscriptionManager = new SubscriptionManager<Settings>();
private settingsReady = false;
constructor(
private readonly workspace: LspWorkspace,
private readonly initSettings?: DeepReadonly<Partial<Settings>>,
) {
this.registerSettingsGauges();
}
/**
* Get current settings synchronously
*/
getCurrentSettings(): Settings {
return this.settingsState.toSettings();
}
isReady(): ReadinessStatus {
return { ready: this.settingsReady };
}
reset() {
this.validateAndUpdate(DefaultSettings);
}
/**
* Subscribe to settings changes
* Overloaded to support both full and partial subscriptions
*/
subscribe<K extends SettingsPathKey>(path: K, observer: PartialDataObserver<Settings[K]>) {
const currentSettings = this.getCurrentSettings();
return this.subscriptionManager.addPartialSubscription(path, observer, currentSettings);
}
/**
* Sync configuration from LSP workspace
* Maintains existing behavior while adding notification support
*/
@Measure({ name: 'syncConfiguration', captureErrorAttributes: true })
async syncConfiguration(): Promise<void> {
try {
// Get CloudFormation-specific settings
const cfnConfig: unknown = await this.workspace.getConfiguration('aws.cloudformation');
// Get editor settings
const editorConfig: unknown = await this.workspace.getConfiguration('editor');
// Some editors return null for unconfigured sections.
// Initialization settings serve as a base layer, workspace config overrides them.
/* eslint-disable unicorn/no-useless-fallback-in-spread */
const mergedConfig = {
...(this.initSettings ?? {}),
...(cfnConfig ?? {}),
editor: {
...(this.initSettings?.editor ?? {}),
...(editorConfig ?? {}),
},
};
/* eslint-enable unicorn/no-useless-fallback-in-spread */
const settings = parseWithPrettyError(parseSettings, mergedConfig, this.getCurrentSettings());
this.validateAndUpdate(settings);
this.settingsReady = true;
} catch (error) {
logger.error(error, `Failed to sync configuration, keeping previous settings`);
}
}
updateRegion(region: AwsRegion): void {
try {
const currentSettings = this.getCurrentSettings();
this.validateAndUpdate({
...currentSettings,
profile: {
...currentSettings.profile,
region,
},
});
} catch (error) {
logger.error(error, `Failed to update region configuration, keeping previous settings`);
}
}
updateProfileSettings(profile: string, region: AwsRegion): void {
try {
const currentSettings = this.getCurrentSettings();
this.validateAndUpdate({
...currentSettings,
profile: {
profile,
region,
},
});
} catch (error) {
logger.error(error, `Failed to update profile configuration, keeping previous settings`);
}
}
/**
* Validate and update settings with notification support
* Maintains all existing validation logic from SettingsManager
*/
@Measure({ name: 'settingsUpdate', captureErrorAttributes: true })
private validateAndUpdate(newSettings: Settings): void {
this.settingsReady = false;
const oldSettings = this.settingsState.toSettings();
newSettings.diagnostics.cfnLint.initialization.maxDelayMs = clipNumber(
newSettings.diagnostics.cfnLint.initialization.maxDelayMs,
oldSettings.diagnostics.cfnLint.initialization.maxDelayMs,
{
greaterThan: 0,
},
);
newSettings.diagnostics.cfnLint.initialization.initialDelayMs = clipNumber(
newSettings.diagnostics.cfnLint.initialization.initialDelayMs,
oldSettings.diagnostics.cfnLint.initialization.initialDelayMs,
{
greaterThan: 0,
},
);
newSettings.diagnostics.cfnLint.initialization.maxDelayMs = Math.max(
newSettings.diagnostics.cfnLint.initialization.maxDelayMs,
newSettings.diagnostics.cfnLint.initialization.initialDelayMs,
);
// Validate Guard settings
newSettings.diagnostics.cfnGuard.delayMs = clipNumber(
newSettings.diagnostics.cfnGuard.delayMs,
oldSettings.diagnostics.cfnGuard.delayMs,
{
greaterThan: 0,
},
);
newSettings.diagnostics.cfnGuard.timeout = clipNumber(
newSettings.diagnostics.cfnGuard.timeout,
oldSettings.diagnostics.cfnGuard.timeout,
{
greaterThan: 0,
},
);
const difference = diff(oldSettings, newSettings);
const hasChanged = Object.keys(difference).length > 0;
if (hasChanged) {
try {
this.settingsState.update(newSettings);
logger.info(`Settings updated: ${toString(difference)}`);
this.subscriptionManager.notify(newSettings, oldSettings);
} catch (error) {
logger.error(error, 'Failed to update settings');
}
}
this.settingsReady = true;
}
private registerSettingsGauges(): void {
const settings = this.getCurrentSettings();
this.telemetry.registerGaugeProvider('settings.hover.enabled', () => (settings.hover.enabled ? 1 : 0));
this.telemetry.registerGaugeProvider('settings.completion.enabled', () =>
settings.completion.enabled ? 1 : 0,
);
this.telemetry.registerGaugeProvider('settings.diagnostics.cfnLint.enabled', () =>
settings.diagnostics.cfnLint.enabled ? 1 : 0,
);
this.telemetry.registerGaugeProvider('settings.diagnostics.cfnGuard.enabled', () =>
settings.diagnostics.cfnGuard.enabled ? 1 : 0,
);
}
}
function clipNumber(
value: number,
defaultValue: number,
conditions: {
greaterThan?: number;
lessThan?: number;
},
): number {
const { greaterThan = Number.NEGATIVE_INFINITY, lessThan = Number.POSITIVE_INFINITY } = conditions;
if (value <= greaterThan) {
return defaultValue;
}
if (value >= lessThan) {
return defaultValue;
}
return value;
}