Skip to content

Commit

Permalink
breaking change: make getInstance async
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianwessel committed Feb 24, 2024
1 parent 4a45b83 commit 9c821c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/ServiceBuilder/ServiceBuilder.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class ServiceBuilder<
* @param options - additional config like logger, stores and opentelemetry span processor
* @returns The instance of the service class
*/
getInstance(
async getInstance(
eventBridge: EventBridge,
options: {
logLevel?: LogLevelName
Expand Down Expand Up @@ -172,13 +172,18 @@ export class ServiceBuilder<
logger,
})

const [commandDefinitionList, subscriptionDefinitionList] = await Promise.all([
Promise.all(this.commandDefinitionList),
Promise.all(this.subscriptionDefinitionList),
])

const C = this.getCustomClass()
this.instance = new C({
logger,
eventBridge,
info: this.info,
commandDefinitionList: this.commandDefinitionList,
subscriptionDefinitionList: this.subscriptionDefinitionList,
commandDefinitionList,
subscriptionDefinitionList,
config,
spanProcessor: options.spanProcessor,
secretStore,
Expand Down
43 changes: 6 additions & 37 deletions packages/core/src/core/Service/Service.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type { StateDeleteFunction, StateGetterFunction, StateSetterFunction } fr
import type {
Command,
CommandDefinition,
CommandDefinitionList,
CommandDefinitionListResolved,
CommandFunctionContext,
ContextBase,
Expand All @@ -39,7 +38,6 @@ import type {
ServiceConstructorInput,
Subscription,
SubscriptionDefinition,
SubscriptionDefinitionList,
SubscriptionDefinitionListResolved,
SubscriptionFunctionContext,
TenantId,
Expand Down Expand Up @@ -84,11 +82,8 @@ export class Service<ConfigType = unknown | undefined> extends ServiceBaseClass
protected subscriptions = new Map<string, SubscriptionDefinition>()
protected commands = new Map<string, CommandDefinition>()

public commandDefinitionList: CommandDefinitionList<any>
public subscriptionDefinitionList: SubscriptionDefinitionList<any>

public commandDefinitionListResolved: CommandDefinitionListResolved<any> = []
public subscriptionDefinitionListResolved: SubscriptionDefinitionListResolved<any> = []
public commandDefinitionList: CommandDefinitionListResolved<any>
public subscriptionDefinitionList: SubscriptionDefinitionListResolved<any>
public config: ConfigType

public isStarted: boolean = false
Expand Down Expand Up @@ -118,6 +113,9 @@ export class Service<ConfigType = unknown | undefined> extends ServiceBaseClass
* It connects to the event bridge and subscribes to the topics that are in the subscription list.
*/
async start() {
if (this.isStarted) {
throw new UnhandledError(StatusCode.InternalServerError, 'Service already started')
}
return this.startActiveSpan('purista.start', {}, undefined, async (span) => {
try {
if (this.configSchema) {
Expand All @@ -133,13 +131,7 @@ export class Service<ConfigType = unknown | undefined> extends ServiceBaseClass
}
}

this.commandDefinitionListResolved = await Promise.all(this.commandDefinitionList)
this.subscriptionDefinitionListResolved = await Promise.all(this.subscriptionDefinitionList)

await this.initializeEventbridgeConnect(
this.commandDefinitionListResolved,
this.subscriptionDefinitionListResolved,
)
await this.initializeEventbridgeConnect(this.commandDefinitionList, this.subscriptionDefinitionList)
await this.sendServiceInfo(EBMessageType.InfoServiceReady)
this.logger.info(
{ ...span.spanContext(), puristaVersion },
Expand All @@ -156,29 +148,6 @@ export class Service<ConfigType = unknown | undefined> extends ServiceBaseClass
})
}

/**
* The Definition lists provided by builders
* for commands and subscriptions are Promises
* This functions returns teh resolved values.
*
* The service needs to be started.
* Otherwise, this function will throw
*
* @returns
*/
getDefinitionsResolved() {
if (!this.isStarted) {
throw new Error(
`service ${this.serviceInfo.serviceName} version ${this.serviceInfo.serviceVersion} needs to be started before using getDefinitionsResolved`,
)
}

return {
commands: this.commandDefinitionListResolved,
subscriptions: this.subscriptionDefinitionListResolved,
}
}

/**
* Connect service to event bridge to receive commands and command responses
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/core/types/ServiceConstructorInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { ConfigStore } from '../ConfigStore/index.js'
import type { EventBridge } from '../EventBridge/index.js'
import type { SecretStore } from '../SecretStore/index.js'
import type { StateStore } from '../StateStore/index.js'
import type { CommandDefinitionList } from './commandType/index.js'
import type { CommandDefinitionListResolved } from './commandType/index.js'
import type { ServiceInfoType } from './infoType/index.js'
import type { Logger } from './Logger.js'
import type { SubscriptionDefinitionList } from './subscription/index.js'
import type { SubscriptionDefinitionListResolved } from './subscription/index.js'

/**
* @group Service
Expand All @@ -21,9 +21,9 @@ export type ServiceConstructorInput<ConfigType> = {
/** The eventBridge instance */
eventBridge: EventBridge
/** The list of command definitions for this service */
commandDefinitionList: CommandDefinitionList<any>
commandDefinitionList: CommandDefinitionListResolved<any>
/** The list of subscription definitions for this service */
subscriptionDefinitionList: SubscriptionDefinitionList<any>
subscriptionDefinitionList: SubscriptionDefinitionListResolved<any>
/** The service specific config */
config: ConfigType
/** The secret store instance */
Expand Down

0 comments on commit 9c821c5

Please sign in to comment.