@@ -140,19 +140,30 @@ export class InstanceManager extends EventEmitter {
140140 * Should only be false if it has been validated at a previous point in time, e.g. loading after startup.
141141 * @return void if everything went fine and a string describing the issue if something went wrong.
142142 */
143- async updateInstanceConfig ( instanceName : string , config : unknown , validation = true ) : Promise < Result < void > > {
143+ updateInstanceConfig ( instanceName : string , config : unknown , validation = true ) : Promise < Result < void > > {
144144 // Check existence and get service instance.
145145 const inst = this . serviceInstances [ instanceName ] ;
146146 if ( inst === undefined ) {
147- return error ( "Service instance doesn't exist." ) ;
147+ return Promise . resolve ( error ( "Service instance doesn't exist." ) ) ;
148148 }
149149
150150 const service = this . services . getService ( inst . serviceType ) ;
151151 if ( service . failed ) {
152- return error ( "The service of this instance couldn't be found." ) ;
152+ return Promise . resolve ( error ( "The service of this instance couldn't be found." ) ) ;
153153 }
154154
155- if ( validation || ! service . result . requiresNoConfig ) {
155+ // If we don't need validation, because we are loading the configuration from disk, we can set it directly
156+ // so that after we return the promise from updateInstanceClient the PersistenceManager can be sure that the
157+ // config has been written.
158+ // Can also be used when there is no configuration needed so that we don't spawn another promise.
159+ if ( ! validation || service . result . requiresNoConfig ) {
160+ inst . config = config ;
161+ this . emit ( "change" ) ;
162+ return this . updateInstanceClient ( inst , instanceName , service . result ) ;
163+ }
164+
165+ // We need to do validation, spawn a Promise
166+ return ( async ( ) => {
156167 const schemaValid = this . ajv . validate ( service . result . schema , config ) ;
157168 if ( ! schemaValid ) {
158169 return error ( "Config invalid: " + this . ajv . errorsText ( ) ) ;
@@ -170,16 +181,16 @@ export class InstanceManager extends EventEmitter {
170181 ) ;
171182 return error ( "Config invalid: " + err ) ;
172183 }
173- }
174184
175- // All checks passed. Set config.
176- inst . config = config ;
185+ // All checks passed. Set config and save it.
186+ inst . config = config ;
187+ this . emit ( "change" ) ;
177188
178- // Update client of this instance using the new config.
179- const updateResult = await this . updateInstanceClient ( inst , instanceName , service . result ) ;
189+ // Update client of this instance using the new config.
190+ const updateResult = await this . updateInstanceClient ( inst , instanceName , service . result ) ;
180191
181- this . emit ( "change" ) ;
182- return updateResult ;
192+ return updateResult ;
193+ } ) ( ) ;
183194 }
184195
185196 /**
0 commit comments