Skip to content

Commit 2c3bfd1

Browse files
committed
Merge branch 'in-scope' of github.com:dolittle/JavaScript.SDK into event-type
2 parents 3421a63 + adda5ac commit 2c3bfd1

10 files changed

Lines changed: 73 additions & 27 deletions

File tree

Samples/Basic/MyEventHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { eventHandler, handles } from '@dolittle/sdk.events.handling';
55
import { MyEvent } from './MyEvent';
66

77

8-
@eventHandler('a27074a7-5b01-43c9-b4f0-c1d59668d844')
8+
@eventHandler('a27074a7-5b01-43c9-b4f0-c1d59668d844', { inScope: '406d6473-7cc9-44a6-a55f-775c1021d957' })
99
export class MyEventHandler {
1010

1111
@handles(MyEvent)

Samples/Environment/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
- 27017:27017
1010

1111
runtime-basic:
12-
image: dolittle/runtime:5.0.1
12+
image: dolittle/runtime:5.0.2
1313
volumes:
1414
- ${PWD}/resources-basic.json:/app/.dolittle/resources.json
1515
- ${PWD}/tenants.json:/app/.dolittle/tenants.json
@@ -22,7 +22,7 @@ services:
2222
- 50053:50053
2323

2424
runtime-eventhorizon:
25-
image: dolittle/runtime:5.0.1
25+
image: dolittle/runtime:5.0.2
2626
volumes:
2727
- ${PWD}/resources-eventhorizon.json:/app/.dolittle/resources.json
2828
- ${PWD}/tenants.json:/app/.dolittle/tenants.json

Samples/EventHorizon/MyEventHandler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Dolittle. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
import { EventContext } from '@dolittle/sdk.events';
4-
import { eventHandler, handles, inScope } from '@dolittle/sdk.events.handling';
4+
import { eventHandler, handles } from '@dolittle/sdk.events.handling';
55
import { MyEvent } from './MyEvent';
66

77

8-
@eventHandler('62f7d968-9e70-47a6-9bb7-b21e1b3ed71c')
9-
@inScope('406d6473-7cc9-44a6-a55f-775c1021d957')
8+
@eventHandler('62f7d968-9e70-47a6-9bb7-b21e1b3ed71c', { inScope: '406d6473-7cc9-44a6-a55f-775c1021d957' })
109
export class MyEventHandler {
1110

1211
@handles(MyEvent)

Source/events.handling/EventHandler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Artifact, ArtifactMap } from '@dolittle/sdk.artifacts';
77
import { EventContext, ScopeId } from '@dolittle/sdk.events';
88

99
import { EventHandlerDecoratedTypes, IEventHandler, EventHandlerSignature, MissingEventHandlerForType, EventHandlerId } from './index';
10+
import { EventHandlerOptions } from './EventHandlerOptions';
1011

1112
/**
1213
* Represents an implementation of {@link IEventHandler}.
@@ -43,17 +44,16 @@ export class EventHandler implements IEventHandler {
4344
}
4445
}
4546

46-
export function eventHandler(eventHandlerId: Guid | string) {
47+
/**
48+
* Decorator to mark a class as an EventHandler.
49+
* @param {Guid | string} eventHandlerId EventHandler's given id
50+
* @param {EventHandlerOptions} [options={}] Options to give to the EventHandler
51+
*/
52+
export function eventHandler(eventHandlerId: Guid | string, options: EventHandlerOptions = {}) {
4753
return function (target: any) {
4854
EventHandlerDecoratedTypes.registerEventHandler(
4955
EventHandlerId.from(eventHandlerId),
5056
target);
51-
};
52-
}
53-
export function inScope(scopeId: Guid | string) {
54-
return function (target: any) {
55-
EventHandlerDecoratedTypes.registerScope(
56-
ScopeId.from(scopeId),
57-
target);
57+
EventHandlerDecoratedTypes.registerOptions(options, target);
5858
};
5959
}

Source/events.handling/EventHandlerDecoratedType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import { EventHandlerId } from './index';
99
* Represents an event handler created from the decorator
1010
*/
1111
export class EventHandlerDecoratedType {
12-
constructor(readonly eventHandlerId: EventHandlerId, readonly scopeId: ScopeId, readonly type: Function) {
12+
constructor(readonly eventHandlerId: EventHandlerId, readonly scopeId: ScopeId, readonly partitioned: boolean, readonly type: Function) {
1313
}
1414
}

Source/events.handling/EventHandlerDecoratedTypes.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
// Copyright (c) Dolittle. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
import { ReplaySubject } from 'rxjs';
54
import { ScopeId } from '@dolittle/sdk.events';
5+
import { EventHandlerDecoratedType, EventHandlerId, EventHandlerIdAlreadyInUse, EventHandlerOptions } from './index';
66

7-
import { EventHandlerDecoratedType, EventHandlerId } from './index';
8-
7+
/**
8+
* Handles registering and mappings between @eventHandler decorated classes and their given id and options.
9+
*/
910
export class EventHandlerDecoratedTypes {
10-
private static readonly _eventHandlers = new Map<Function, EventHandlerId>();
11+
private static readonly _eventHandlers = new Map<Function, EventHandlerId>() ;
1112
private static readonly _scopes = new Map<Function, ScopeId>();
12-
static registerEventHandler(eventHandlerId: EventHandlerId, type: Function) {
13-
this._eventHandlers.set(type, eventHandlerId);
13+
private static readonly _unpartitioned = new Map<Function, boolean>();
14+
15+
/**
16+
* Registers an @EventHandlerId to a specific type.
17+
* @param {EventHandlerId} eventHandlerId EventHandlerId to register the type with.
18+
* @param {Function} eventHandlerType Type of the event handler.
19+
*/
20+
static registerEventHandler(eventHandlerId: EventHandlerId, eventHandlerType: Function) {
21+
for (const [func, id] of this._eventHandlers) {
22+
if (id.equals(eventHandlerId)) throw new EventHandlerIdAlreadyInUse(eventHandlerId, eventHandlerType, func);
23+
}
24+
this._eventHandlers.set(eventHandlerType, eventHandlerId);
1425
}
15-
static registerScope(scopeId: ScopeId, type: Function) {
16-
this._scopes.set(type, scopeId);
26+
27+
/**
28+
* Registers @EventHandlerOptions to a specific type.
29+
* @param options EventHandlerOptions to register the type with.
30+
* @param eventHandlerType Type of the event handler.
31+
*/
32+
static registerOptions(options: EventHandlerOptions, eventHandlerType: Function) {
33+
if (options.inScope) {
34+
this._scopes.set(eventHandlerType, ScopeId.from(options.inScope));
35+
}
36+
if (options.unpartitioned) {
37+
this._unpartitioned.set(eventHandlerType, true);
38+
}
1739
}
40+
41+
/**
42+
* Creates an array of EventhandlerDecoratedType's and calls the callback on each one of them.
43+
* @param callback
44+
*/
1845
static forEach(callback: (eventHandlerDecoratedType: EventHandlerDecoratedType) => void) {
1946
const eventHandlerDecoratedTypes: EventHandlerDecoratedType[] = [];
20-
for (const [func, eventHandlerId] of this._eventHandlers) {
47+
for (const [func, id] of this._eventHandlers) {
2148
const scopeId = this._scopes.has(func) ? this._scopes.get(func)! : ScopeId.default;
22-
eventHandlerDecoratedTypes.push(new EventHandlerDecoratedType(eventHandlerId, scopeId, func));
49+
const partitioned = !this._unpartitioned.has(func);
50+
eventHandlerDecoratedTypes.push(new EventHandlerDecoratedType(id, scopeId, partitioned, func));
2351
}
2452

2553
for (const eventHandlerDecoratedType of eventHandlerDecoratedTypes) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Exception } from '@dolittle/rudiments';
5+
import { EventHandlerId } from './index';
6+
7+
export class EventHandlerIdAlreadyInUse extends Exception {
8+
constructor(eventHandlerId: EventHandlerId, type: Function, alreadyUsedType: Function) {
9+
super(`EventHandlerId '${eventHandlerId}' in EventHandler '${type.name}' is already in use in type '${alreadyUsedType.name}'`);
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Guid } from '@dolittle/rudiments';
5+
6+
export type EventHandlerOptions = { inScope?: Guid | string, unpartitioned?: boolean };

Source/events.handling/EventHandlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class EventHandlers implements IEventHandlers {
6969
}
7070
this._logger.debug(`Register EventHandler '${_.eventHandlerId}'`);
7171

72-
const eventHandler = new EventHandler(_.eventHandlerId, _.scopeId, true, this.getEventHandlerMethodsByArtifact(_.type));
72+
const eventHandler = new EventHandler(_.eventHandlerId, _.scopeId, _.partitioned, this.getEventHandlerMethodsByArtifact(_.type));
7373
this.register(eventHandler, this._cancellation);
7474
});
7575
}

Source/events.handling/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ export { HandlesDecoratedMethods } from './HandlesDecoratedMethods';
1010
export { EventHandlerDecoratedType } from './EventHandlerDecoratedType';
1111
export { EventHandlerDecoratedTypes } from './EventHandlerDecoratedTypes';
1212
export { IEventHandler } from './IEventHandler';
13-
export { EventHandler, eventHandler, inScope } from './EventHandler';
13+
export { EventHandler, eventHandler } from './EventHandler';
1414
export { EventHandlerBuilder, EventHandlerBuilderCallback } from './EventHandlerBuilder';
1515
export { IEventHandlers } from './IEventHandlers';
1616
export { EventHandlers } from './EventHandlers';
1717
export { EventHandlersBuilder, EventHandlersBuilderCallback } from './EventHandlersBuilder';
18+
export { EventHandlerIdAlreadyInUse } from './EventHandlerIdAlreadyInUse';
19+
export { EventHandlerOptions } from './EventHandlerOptions';
1820
export { handles } from './handles';
1921
export * as internal from './Internal';

0 commit comments

Comments
 (0)