backend-app-api: rename BackendRegistrable to BackendFeature

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-08-12 13:40:59 +02:00
parent 5e3761d2e7
commit e3d514507a
7 changed files with 24 additions and 30 deletions
@@ -15,7 +15,7 @@
*/
import {
BackendRegistrable,
BackendFeature,
ExtensionPoint,
ServiceRef,
} from '@backstage/backend-plugin-api';
@@ -25,7 +25,7 @@ type ServiceOrExtensionPoint = ExtensionPoint<unknown> | ServiceRef<unknown>;
export class BackendInitializer {
#started = false;
#extensions = new Map<BackendRegistrable, unknown>();
#features = new Map<BackendFeature, unknown>();
#registerInits = new Array<BackendRegisterInit>();
#extensionPoints = new Map<ServiceOrExtensionPoint, unknown>();
#serviceHolder: ServiceHolder;
@@ -51,13 +51,11 @@ export class BackendInitializer {
);
}
add<TOptions>(extension: BackendRegistrable, options?: TOptions) {
add<TOptions>(feature: BackendFeature, options?: TOptions) {
if (this.#started) {
throw new Error(
'extension can not be added after the backend has started',
);
throw new Error('feature can not be added after the backend has started');
}
this.#extensions.set(extension, options);
this.#features.set(feature, options);
}
async start(): Promise<void> {
@@ -67,13 +65,13 @@ export class BackendInitializer {
}
this.#started = true;
for (const [extension] of this.#extensions) {
for (const [feature] of this.#features) {
const provides = new Set<ServiceRef<unknown>>();
let registerInit: BackendRegisterInit | undefined = undefined;
console.log('Registering', extension.id);
extension.register({
console.log('Registering', feature.id);
feature.register({
registerExtensionPoint: (extensionPointRef, impl) => {
if (registerInit) {
throw new Error('registerExtensionPoint called after registerInit');
@@ -89,7 +87,7 @@ export class BackendInitializer {
throw new Error('registerInit must only be called once');
}
registerInit = {
id: extension.id,
id: feature.id,
provides,
consumes: new Set(Object.values(registerOptions.deps)),
deps: registerOptions.deps,
@@ -100,7 +98,7 @@ export class BackendInitializer {
if (!registerInit) {
throw new Error(
`registerInit was not called by register in ${extension.id}`,
`registerInit was not called by register in ${feature.id}`,
);
}
@@ -16,7 +16,7 @@
import {
AnyServiceFactory,
BackendRegistrable,
BackendFeature,
} from '@backstage/backend-plugin-api';
import { BackendInitializer } from './BackendInitializer';
import { ServiceRegistry } from './ServiceRegistry';
@@ -31,8 +31,8 @@ export class BackstageBackend implements Backend {
this.#initializer = new BackendInitializer(this.#services);
}
add(extension: BackendRegistrable): void {
this.#initializer.add(extension);
add(feature: BackendFeature): void {
this.#initializer.add(feature);
}
async start(): Promise<void> {
+2 -2
View File
@@ -16,7 +16,7 @@
import {
AnyServiceFactory,
BackendRegistrable,
BackendFeature,
FactoryFunc,
ServiceRef,
} from '@backstage/backend-plugin-api';
@@ -26,7 +26,7 @@ import { BackstageBackend } from './BackstageBackend';
* @public
*/
export interface Backend {
add(extension: BackendRegistrable): void;
add(feature: BackendFeature): void;
start(): Promise<void>;
}
@@ -14,11 +14,7 @@
* limitations under the License.
*/
import {
BackendInitRegistry,
BackendRegistrable,
ExtensionPoint,
} from './types';
import { BackendInitRegistry, BackendFeature, ExtensionPoint } from './types';
/** @public */
export function createExtensionPoint<T>(options: {
@@ -46,8 +42,8 @@ export interface BackendPluginConfig<TOptions> {
export function createBackendPlugin<TOptions>(
config: BackendPluginConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendRegistrable
: (options: TOptions) => BackendRegistrable {
? (options?: TOptions) => BackendFeature
: (options: TOptions) => BackendFeature {
return (options?: TOptions) => ({
id: config.id,
register(register: BackendInitRegistry) {
@@ -70,8 +66,8 @@ export interface BackendModuleConfig<TOptions> {
export function createBackendModule<TOptions>(
config: BackendModuleConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendRegistrable
: (options: TOptions) => BackendRegistrable {
? (options?: TOptions) => BackendFeature
: (options: TOptions) => BackendFeature {
return (options?: TOptions) => ({
id: `${config.pluginId}.${config.moduleId}`,
register(register: BackendInitRegistry) {
@@ -22,6 +22,6 @@ export {
} from './factories';
export type {
BackendInitRegistry,
BackendRegistrable,
BackendFeature,
ExtensionPoint,
} from './types';
@@ -48,7 +48,7 @@ export interface BackendInitRegistry {
}
/** @public */
export interface BackendRegistrable {
export interface BackendFeature {
id: string;
register(reg: BackendInitRegistry): void;
}
@@ -19,7 +19,7 @@ import {
AnyServiceFactory,
ServiceRef,
createServiceFactory,
BackendRegistrable,
BackendFeature,
} from '@backstage/backend-plugin-api';
/** @alpha */
@@ -53,7 +53,7 @@ export function createTestBackend<TServices extends any[]>(
/** @alpha */
export async function startTestBackend<TServices extends any[]>(
options: TestBackendOptions<TServices> & {
registrables?: BackendRegistrable[];
registrables?: BackendFeature[];
},
): Promise<void> {
const { registrables = [], ...otherOptions } = options;