@@ -61,6 +61,9 @@ import { registerRoutes } from './routes';
6161 * the factory provided to `setClientFactory` and wrapped by all wrappers
6262 * registered through `addClientWrapper`.
6363 *
64+ * All the setup APIs will throw if called after the service has started, and therefor cannot be used
65+ * from legacy plugin code. Legacy plugins should use the legacy savedObject service until migrated.
66+ *
6467 * @example
6568 * ```ts
6669 * import { SavedObjectsClient, CoreSetup } from 'src/core/server';
@@ -275,6 +278,7 @@ export class SavedObjectsService
275278 private migrator$ = new Subject < KibanaMigrator > ( ) ;
276279 private typeRegistry = new SavedObjectTypeRegistry ( ) ;
277280 private validations : PropertyValidators = { } ;
281+ private started = false ;
278282
279283 constructor ( private readonly coreContext : CoreContext ) {
280284 this . logger = coreContext . logger . get ( 'savedobjects-service' ) ;
@@ -316,19 +320,28 @@ export class SavedObjectsService
316320
317321 return {
318322 setClientFactoryProvider : provider => {
323+ if ( this . started ) {
324+ throw new Error ( 'cannot call `setClientFactoryProvider` after service startup.' ) ;
325+ }
319326 if ( this . clientFactoryProvider ) {
320327 throw new Error ( 'custom client factory is already set, and can only be set once' ) ;
321328 }
322329 this . clientFactoryProvider = provider ;
323330 } ,
324331 addClientWrapper : ( priority , id , factory ) => {
332+ if ( this . started ) {
333+ throw new Error ( 'cannot call `addClientWrapper` after service startup.' ) ;
334+ }
325335 this . clientFactoryWrappers . push ( {
326336 priority,
327337 id,
328338 factory,
329339 } ) ;
330340 } ,
331341 registerType : type => {
342+ if ( this . started ) {
343+ throw new Error ( 'cannot call `registerType` after service startup.' ) ;
344+ }
332345 this . typeRegistry . registerType ( type ) ;
333346 } ,
334347 } ;
@@ -415,6 +428,8 @@ export class SavedObjectsService
415428 clientProvider . addClientWrapperFactory ( priority , id , factory ) ;
416429 } ) ;
417430
431+ this . started = true ;
432+
418433 return {
419434 migrator,
420435 clientProvider,
0 commit comments