From 3a3e03bf2e47b1096a7b4c553419b8f2cac56b97 Mon Sep 17 00:00:00 2001 From: Alin Eugen Deac Date: Sat, 13 Apr 2024 12:58:03 +0200 Subject: [PATCH] Add interface for Service Registrar --- .../src/support/services/Registrar.ts | 101 ++++++++++++++++++ .../contracts/src/support/services/index.ts | 2 + 2 files changed, 103 insertions(+) create mode 100644 packages/contracts/src/support/services/Registrar.ts diff --git a/packages/contracts/src/support/services/Registrar.ts b/packages/contracts/src/support/services/Registrar.ts new file mode 100644 index 00000000..2b8342c2 --- /dev/null +++ b/packages/contracts/src/support/services/Registrar.ts @@ -0,0 +1,101 @@ +import ServiceProvider from "./ServiceProvider"; +import ServiceProviderConstructor from "./ServiceProviderConstructor"; + + +/** + * Service Registrar + * + * Able to register and boot service providers. + */ +export default interface Registrar +{ + /** + * Register a service provider + * + * @param {ServiceProvider | ServiceProviderConstructor} provider + * @param {boolean} [boot=true] + * + * @returns {Promise} False if already registered + * + * @async + */ + register(provider: ServiceProvider | ServiceProviderConstructor, boot?: boolean): Promise; + + /** + * Register multiple service providers + * + * @param {(ServiceProvider | ServiceProviderConstructor)[]} providers + * @param {boolean} [boot=true] + * @param {boolean} [safe=true] If true, given providers are only booted after all providers have been registered. + * + * @returns {Promise} + */ + registerMultiple(providers: (ServiceProvider | ServiceProviderConstructor)[], boot?: boolean, safe?: boolean): Promise; + + /** + * Determine if service provider has already been registered + * + * @param {ServiceProvider | ServiceProviderConstructor} provider + * + * @returns {boolean} + */ + isRegistered(provider: ServiceProvider | ServiceProviderConstructor): boolean + + /** + * Get the service providers that are registered + * + * @returns {ServiceProvider[]} + */ + get registered(): ServiceProvider[]; + + /** + * Boot given service provider + * + * @param {ServiceProvider} provider + * + * @returns {Promise} False if provider has already been booted + * + * @async + */ + boot(provider: ServiceProvider): Promise; + + /** + * Boots all given service providers + * + * **Note**: _Method skips providers that have already been booted!_ + * + * @param {ServiceProvider[]} providers + * + * @returns {Promise} + * + * @async + */ + bootMultiple(providers: ServiceProvider[]): Promise; + + /** + * Determine if service provider has already been booted + * + * @param {ServiceProvider} provider + * + * @returns {boolean} + */ + hasBooted(provider: ServiceProvider): boolean; + + /** + * Get the service providers that have been booted + * + * @returns {ServiceProvider[]} + */ + get booted(): ServiceProvider[]; + + /** + * Resolves service provider + * + * @param {ServiceProvider | ServiceProviderConstructor} provider + * + * @returns {ServiceProvider} Resolved service provider instance + * + * @throws {RegistrationException} + */ + resolveProvider(provider: ServiceProvider | ServiceProviderConstructor): ServiceProvider; +} \ No newline at end of file diff --git a/packages/contracts/src/support/services/index.ts b/packages/contracts/src/support/services/index.ts index 2e354da3..45e056f6 100644 --- a/packages/contracts/src/support/services/index.ts +++ b/packages/contracts/src/support/services/index.ts @@ -5,9 +5,11 @@ */ export const SUPPORT_SERVICES: unique symbol = Symbol('@aedart/contracts/support/services'); +import Registrar from "./Registrar"; import ServiceProvider from "./ServiceProvider"; import ServiceProviderConstructor from "./ServiceProviderConstructor"; export { + type Registrar, type ServiceProvider, type ServiceProviderConstructor, }