@node-ts/bus-core
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -12,3 +12,3 @@ import { ClassConstructor } from '../util'; | ||
*/ | ||
get<T>(type: ClassConstructor<T>): T; | ||
get<T>(type: ClassConstructor<T>): T | Promise<T>; | ||
} |
@@ -95,3 +95,3 @@ import { Message } from '@node-ts/bus-messages'; | ||
withContainer(container: { | ||
get<T>(type: ClassConstructor<T>): T; | ||
get<T>(type: ClassConstructor<T>): T | Promise<T>; | ||
}): this; | ||
@@ -98,0 +98,0 @@ /** |
@@ -234,3 +234,9 @@ "use strict"; | ||
try { | ||
handlerInstance = this.coreDependencies.container.get(classHandler); | ||
const handlerInstanceFromContainer = this.coreDependencies.container.get(classHandler); | ||
if (handlerInstanceFromContainer instanceof Promise) { | ||
handlerInstance = await handlerInstanceFromContainer; | ||
} | ||
else { | ||
handlerInstance = handlerInstanceFromContainer; | ||
} | ||
if (!handlerInstance) { | ||
@@ -237,0 +243,0 @@ throw new Error('Container failed to resolve an instance.'); |
@@ -66,5 +66,15 @@ "use strict"; | ||
this.logger.debug('Initializing workflow', { workflow: WorkflowCtor.prototype.constructor.name }); | ||
const workflowInstance = container | ||
? container.get(WorkflowCtor) | ||
: new WorkflowCtor(); | ||
let workflowInstance; | ||
if (container) { | ||
const workflowInstanceFromContainer = container.get(WorkflowCtor); | ||
if (workflowInstanceFromContainer instanceof Promise) { | ||
workflowInstance = await workflowInstanceFromContainer; | ||
} | ||
else { | ||
workflowInstance = workflowInstanceFromContainer; | ||
} | ||
} | ||
else { | ||
workflowInstance = new WorkflowCtor(); | ||
} | ||
const mapper = new workflow_1.WorkflowMapper(WorkflowCtor); | ||
@@ -114,5 +124,15 @@ workflowInstance.configureWorkflow(mapper); | ||
try { | ||
const workflow = container | ||
? container.get(options.workflowCtor) | ||
: new options.workflowCtor(); | ||
let workflow; | ||
if (container) { | ||
const workflowFromContainer = container.get(options.workflowCtor); | ||
if (workflowFromContainer instanceof Promise) { | ||
workflow = await workflowFromContainer; | ||
} | ||
else { | ||
workflow = workflowFromContainer; | ||
} | ||
} | ||
else { | ||
workflow = new options.workflowCtor(); | ||
} | ||
const handler = workflow[options.workflowHandler]; | ||
@@ -189,5 +209,15 @@ const result = await handler.bind(workflow)(message, immutableWorkflowState, messageAttributes); | ||
this.logger.debug('Dispatching message to workflow', { msg: message, workflow: workflowCtor }); | ||
const workflow = container | ||
? container.get(workflowCtor) | ||
: new workflowCtor(); | ||
let workflow; | ||
if (container) { | ||
const workflowFromContainer = container.get(workflowCtor); | ||
if (workflowFromContainer instanceof Promise) { | ||
workflow = await workflowFromContainer; | ||
} | ||
else { | ||
workflow = workflowFromContainer; | ||
} | ||
} | ||
else { | ||
workflow = new workflowCtor(); | ||
} | ||
const immutableWorkflowState = Object.freeze(Object.assign({}, workflowState)); | ||
@@ -194,0 +224,0 @@ const handler = workflow[workflowHandler]; |
{ | ||
"name": "@node-ts/bus-core", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "A service bus for message-based, distributed node applications", | ||
@@ -48,3 +48,3 @@ "main": "./dist/index.js", | ||
], | ||
"gitHead": "3dd8c2b7f1508023d7a4bd28aa31fb3255e2d16d" | ||
"gitHead": "044757b4b55725acf4f7ec44f683feccf710be5b" | ||
} |
@@ -89,3 +89,44 @@ import { Bus, BusInstance } from '../service-bus' | ||
}) | ||
describe('when an async adapter is installed', () => { | ||
beforeEach(async () => { | ||
bus = await Bus | ||
.configure() | ||
.withContainer({ | ||
get <T>(type: ClassConstructor<T>) { | ||
return Promise.resolve(container[type.name] as T); | ||
} | ||
}) | ||
.withHandler(TestEventClassHandler) | ||
.withHandler(UnregisteredClassHandler) | ||
.initialize() | ||
await bus.start() | ||
}) | ||
afterEach(async () => { | ||
await bus.dispose() | ||
}) | ||
describe('and a handler is registered', () => { | ||
it('should route the message to the class based handler', async () => { | ||
await bus.publish(event) | ||
await sleep(0) | ||
messageLogger.verify(m => m.log(event), Times.once()) | ||
}) | ||
}) | ||
describe('and a handler is not registered', () => { | ||
it('should throw a ClassHandlerNotResolved error', async () => { | ||
const onError = waitForError(bus, error => { | ||
expect(error).toBeInstanceOf(HandlerDispatchRejected) | ||
const baseError = error as HandlerDispatchRejected | ||
expect(baseError.rejections[0]).toBeInstanceOf(ClassHandlerNotResolved) | ||
const classHandlerNotResolved = baseError.rejections[0] as ClassHandlerNotResolved | ||
expect(classHandlerNotResolved.reason).toEqual('Container failed to resolve an instance.') | ||
}) | ||
await bus.publish(new TestEvent2()) | ||
await onError | ||
}) | ||
}) | ||
}) | ||
describe('when no adapter is installed', () => { | ||
@@ -92,0 +133,0 @@ describe('and no class handlers are registered', () => { |
@@ -13,3 +13,3 @@ import { ClassConstructor } from '../util' | ||
*/ | ||
get <T>(type: ClassConstructor<T>): T | ||
get <T>(type: ClassConstructor<T>): T| Promise<T> | ||
} |
@@ -254,3 +254,3 @@ import { Message } from '@node-ts/bus-messages' | ||
*/ | ||
withContainer (container: { get <T>(type: ClassConstructor<T>): T }): this { | ||
withContainer (container: { get <T>(type: ClassConstructor<T>): T | Promise<T> }): this { | ||
this.container = container | ||
@@ -257,0 +257,0 @@ return this |
@@ -299,3 +299,8 @@ import { Transport, TransportMessage } from '../transport' | ||
try { | ||
handlerInstance = this.coreDependencies.container!.get(classHandler) | ||
const handlerInstanceFromContainer = this.coreDependencies.container!.get(classHandler); | ||
if(handlerInstanceFromContainer instanceof Promise) { | ||
handlerInstance = await handlerInstanceFromContainer; | ||
} else{ | ||
handlerInstance = handlerInstanceFromContainer; | ||
} | ||
if (!handlerInstance) { | ||
@@ -302,0 +307,0 @@ throw new Error('Container failed to resolve an instance.') |
@@ -48,3 +48,19 @@ import { CoreDependencies } from '../../util' | ||
}) | ||
describe('with an async container', () => { | ||
let container: IMock<ContainerAdapter> | ||
beforeEach(() => { | ||
container = Mock.ofType<ContainerAdapter>() | ||
container | ||
.setup(c => c.get(TestWorkflow)) | ||
.returns(() => Promise.resolve(new TestWorkflow(Mock.ofType<BusInstance>().object))) | ||
.verifiable(Times.once()) | ||
}) | ||
it('should fetch workflows from the container', async () => { | ||
await sut.initialize(new DefaultHandlerRegistry(), container.object) | ||
container.verifyAll() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -87,6 +87,13 @@ import { WorkflowState, WorkflowStatus } from '../workflow-state' | ||
const workflowInstance = container | ||
? container.get(WorkflowCtor) | ||
: new WorkflowCtor() | ||
let workflowInstance; | ||
if(container){ | ||
const workflowInstanceFromContainer = container.get(WorkflowCtor); | ||
if(workflowInstanceFromContainer instanceof Promise){ | ||
workflowInstance = await workflowInstanceFromContainer; | ||
}else{ | ||
workflowInstance = workflowInstanceFromContainer; | ||
} | ||
}else{ | ||
workflowInstance = new WorkflowCtor(); | ||
} | ||
const mapper = new WorkflowMapper(WorkflowCtor) | ||
@@ -159,5 +166,13 @@ workflowInstance.configureWorkflow(mapper) | ||
try { | ||
const workflow = container | ||
? container.get(options.workflowCtor) | ||
: new options.workflowCtor() | ||
let workflow: Workflow<WorkflowState>; | ||
if(container){ | ||
const workflowFromContainer = container.get(options.workflowCtor); | ||
if(workflowFromContainer instanceof Promise){ | ||
workflow = await workflowFromContainer; | ||
}else{ | ||
workflow = workflowFromContainer; | ||
} | ||
}else{ | ||
workflow = new options.workflowCtor(); | ||
} | ||
const handler = workflow[options.workflowHandler as keyof Workflow<WorkflowState>] as Function | ||
@@ -284,5 +299,13 @@ const result = await handler.bind(workflow)(message, immutableWorkflowState, messageAttributes) | ||
this.logger.debug('Dispatching message to workflow', { msg: message, workflow: workflowCtor }) | ||
const workflow = container | ||
? container.get(workflowCtor) | ||
: new workflowCtor() | ||
let workflow: Workflow<WorkflowState>; | ||
if(container){ | ||
const workflowFromContainer = container.get(workflowCtor); | ||
if(workflowFromContainer instanceof Promise){ | ||
workflow = await workflowFromContainer; | ||
} else { | ||
workflow = workflowFromContainer; | ||
} | ||
}else{ | ||
workflow = new workflowCtor(); | ||
} | ||
@@ -289,0 +312,0 @@ const immutableWorkflowState = Object.freeze({...workflowState}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
376969
7400