Skip to content

Commit 962926a

Browse files
xflordHejdaJakub
authored andcommitted
feat: merge configs on initialization
* values from configs (default, instance, brandings) are now merged when loading instead of fetching them on each 'getProperty()' call
1 parent 0e8406a commit 962926a

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

libs/config/src/lib/app-config.service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,20 @@ export class AppConfigService {
128128
.get('/assets/config/instanceConfig.json', {
129129
headers: this.getNoCacheHeaders(),
130130
})
131-
.subscribe(
132-
(config: PerunConfig) => {
133-
this.storeService.setInstanceConfig(config);
131+
.subscribe({
132+
next: (config: PerunConfig) => {
133+
this.storeService.mergeConfig(config);
134134
const branding = document.location.hostname;
135135
if (config?.['brandings']?.[branding]) {
136-
this.storeService.setBanding(branding);
136+
this.storeService.mergeConfig(config?.['brandings']?.[branding]);
137137
}
138138
resolve();
139139
},
140-
() => {
140+
error: () => {
141141
// console.log('instance config not detected');
142142
resolve();
143-
}
144-
);
143+
},
144+
});
145145
});
146146
}
147147

libs/perun/services/src/lib/store.service.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ import { PerunConfig } from '@perun-web-apps/perun/models';
1010
providedIn: 'root',
1111
})
1212
export class StoreService {
13-
private instanceConfig: PerunConfig;
14-
private defaultConfig: PerunConfig;
13+
private config: PerunConfig;
1514
private appsConfig: PerunAppsConfig;
1615
private principal: PerunPrincipal;
1716
private initialPageId: number;
18-
private branding = '';
19-
20-
setInstanceConfig(instanceConfig: PerunConfig): void {
21-
this.instanceConfig = instanceConfig;
22-
}
2317

2418
setDefaultConfig(defaultConfig: PerunConfig): void {
25-
this.defaultConfig = defaultConfig;
19+
this.config = defaultConfig;
2620
}
2721

2822
getAppsConfig(): PerunAppsConfig {
@@ -57,35 +51,35 @@ export class StoreService {
5751
return this.getProperty('member_profile_attributes_friendly_names');
5852
}
5953

60-
setBanding(branding: string): void {
61-
this.branding = branding;
62-
}
63-
6454
getProperty<T extends keyof PerunConfig>(key: T): PerunConfig[T] {
65-
if (!this.instanceConfig || !this.defaultConfig) {
55+
if (!this.config) {
6656
return null;
6757
}
58+
return this.config[key];
59+
}
6860

69-
const configs: PerunConfig[] = [
70-
this.instanceConfig?.brandings?.[this.branding],
71-
this.instanceConfig,
72-
];
73-
74-
const defaultValue: PerunConfig[T] = this.defaultConfig[key];
75-
let currentValue: PerunConfig[T] = null;
76-
for (const config of configs) {
77-
if (config && (currentValue === null || currentValue === undefined)) {
78-
currentValue = config[key];
79-
}
61+
/*
62+
Merges a config into the existing config, substituting values in the existing with values in the configToMerge.
63+
Objects are merged per property, not as a whole (see `addMissingValuesToProperty`)
64+
Be careful of order of merging (make sure default config is set before merging another config)
65+
*/
66+
mergeConfig<T extends object, K extends keyof T>(configToMerge: PerunConfig): void {
67+
for (const key of Object.keys(configToMerge)) {
68+
if (key === 'brandings') continue;
69+
this.config[key] = this.addMissingValuesToProperty(
70+
configToMerge[key] as T[K],
71+
this.config[key] as T[K]
72+
);
8073
}
81-
if (currentValue === null) {
82-
return defaultValue;
83-
}
84-
85-
return this.addMissingValuesToProperty(currentValue, defaultValue);
8674
}
8775

88-
addMissingValuesToProperty<T extends object, K extends keyof T>(
76+
/*
77+
For an object property, merge properties from the `defaultValue` with properties from `value`.
78+
Properties defined in `value` are overwritten with their value, remaining properties are kept from `defaultValue`
79+
Returns `value` if a simple type is passed.
80+
Works recursively
81+
*/
82+
private addMissingValuesToProperty<T extends object, K extends keyof T>(
8983
value: T[K],
9084
defaultValue: T[K]
9185
): T[K] {

0 commit comments

Comments
 (0)