|
1 | 1 | // Copyright (c) Dolittle. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | | -import { ReplaySubject } from 'rxjs'; |
5 | 4 | import { ScopeId } from '@dolittle/sdk.events'; |
| 5 | +import { EventHandlerDecoratedType, EventHandlerId, EventHandlerIdAlreadyInUse, EventHandlerOptions } from './index'; |
6 | 6 |
|
7 | | -import { EventHandlerDecoratedType, EventHandlerId } from './index'; |
8 | | - |
| 7 | +/** |
| 8 | + * Handles registering and mappings between @eventHandler decorated classes and their given id and options. |
| 9 | + */ |
9 | 10 | export class EventHandlerDecoratedTypes { |
10 | | - private static readonly _eventHandlers = new Map<Function, EventHandlerId>(); |
| 11 | + private static readonly _eventHandlers = new Map<Function, EventHandlerId>() ; |
11 | 12 | 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); |
14 | 25 | } |
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 | + } |
17 | 39 | } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates an array of EventhandlerDecoratedType's and calls the callback on each one of them. |
| 43 | + * @param callback |
| 44 | + */ |
18 | 45 | static forEach(callback: (eventHandlerDecoratedType: EventHandlerDecoratedType) => void) { |
19 | 46 | const eventHandlerDecoratedTypes: EventHandlerDecoratedType[] = []; |
20 | | - for (const [func, eventHandlerId] of this._eventHandlers) { |
| 47 | + for (const [func, id] of this._eventHandlers) { |
21 | 48 | 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)); |
23 | 51 | } |
24 | 52 |
|
25 | 53 | for (const eventHandlerDecoratedType of eventHandlerDecoratedTypes) { |
|
0 commit comments