Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discuss] Adopt common Service interface #48455

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core/server/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Type } from '@kbn/config-schema';

import { ConfigPath, EnvironmentMode, PackageInfo } from '../config';
import { LoggerFactory } from '../logging';
import { Service } from '../../types';
import { CoreSetup, CoreStart } from '..';

export type PluginConfigSchema = Type<unknown> | null;
Expand Down Expand Up @@ -137,20 +138,19 @@ export interface DiscoveredPluginInternal extends DiscoveredPlugin {
}

/**
* The interface that should be returned by a `PluginInitializer`.
* The type that should be returned by a `PluginInitializer`.
*
* @public
*/
export interface Plugin<
export type Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
setup(core: CoreSetup, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
start(core: CoreStart, plugins: TPluginsStart): TStart | Promise<TStart>;
stop?(): void;
}
> = Service<
(core: CoreSetup, plugins: TPluginsSetup) => TSetup | Promise<TSetup>,
(core: CoreStart, plugins: TPluginsStart) => TStart | Promise<TStart>
>;

/**
* Context that's available to plugins during initialization stage.
Expand Down
6 changes: 5 additions & 1 deletion src/core/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export { SavedObjectsSerializer, RawDoc as SavedObjectsRawDoc } from './serializ

export { SavedObjectsMigrationLogger } from './migrations/core/migration_logger';

export { SavedObjectsService, SavedObjectsServiceStart } from './saved_objects_service';
export {
ISavedObjectsService,
SavedObjectsService,
SavedObjectsServiceStart,
} from './saved_objects_service';

export { config } from './saved_objects_config';
7 changes: 4 additions & 3 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreService } from 'src/core/types';
import { Service, ServiceType } from 'src/core/types';
import { first } from 'rxjs/operators';
import {
SavedObjectsClient,
Expand Down Expand Up @@ -63,8 +63,9 @@ export interface SavedObjectsSetupDeps {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SavedObjectsStartDeps {}

export class SavedObjectsService
implements CoreService<SavedObjectsServiceSetup, SavedObjectsServiceStart> {
export type ISavedObjectsService = ServiceType<SavedObjectsService>;

export class SavedObjectsService implements Service {
private migrator: KibanaMigrator | undefined;
private logger: Logger;
private clientProvider: ISavedObjectsClientProvider<KibanaRequest> | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { LegacyService } from './legacy';
import { Logger, LoggerFactory } from './logging';
import { UiSettingsService } from './ui_settings';
import { PluginsService, config as pluginsConfig } from './plugins';
import { SavedObjectsService } from '../server/saved_objects';
import { SavedObjectsService, ISavedObjectsService } from '../server/saved_objects';

import { config as elasticsearchConfig } from './elasticsearch';
import { config as httpConfig } from './http';
Expand All @@ -51,7 +51,7 @@ export class Server {
private readonly legacy: LegacyService;
private readonly log: Logger;
private readonly plugins: PluginsService;
private readonly savedObjects: SavedObjectsService;
private readonly savedObjects: ISavedObjectsService;
private readonly uiSettings: UiSettingsService;

constructor(
Expand Down
42 changes: 37 additions & 5 deletions src/core/types/core_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,41 @@
* under the License.
*/

/** @internal */
export interface CoreService<TSetup = void, TStart = void> {
setup(...params: any[]): Promise<TSetup>;
start(...params: any[]): Promise<TStart>;
stop(): Promise<void>;
/** @public */
export type AnyFunction = (...args: any[]) => any;

/** @public */
export interface Service<
TSetup extends AnyFunction = AnyFunction,
TStart extends AnyFunction = AnyFunction
> {
setup: TSetup;
start: TStart;
stop(): void;
}

/**
* Utility type for inferring the concrete type of a {@link Service}.
*
* @remarks
* TSDoc comments will be preserved and accessible in editors when using this utility type.
*
* @example
* ```ts
* // Implement the `Service` interface without having to specify the generics type arguments.
* export class MyService implements Service {
* setup(x: number) { return 3 * x }
* start() {}
* stop() {}
* }
*
* // Use `ServiceType` to export an interface that automatically infers the generic type arguments.
* export type IMyService = ServiceType<MyService>;
* // ⇒ Service<(x: number) => number, () => void>
* ```
*
* @public
*/
export type ServiceType<T extends Service<any, any>> = T extends Service<infer TSetup, infer TStart>
? Service<TSetup, TStart>
: never;