Skip to content

Commit

Permalink
Add registerFactory for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthiee committed Dec 22, 2023
1 parent f8fd11c commit 7d39d6f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/src/request/handler/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import 'dart:async';
import 'package:dart_mediator/src/request/request.dart';
import 'package:meta/meta.dart';

/// Factory to create a [RequestHandler].
typedef RequestHandlerFactory<TResponse, TRequest extends Request<TResponse>>
= RequestHandler<TResponse, TRequest> Function();

/// Handler for [TRequest].
@internal
abstract interface class RequestHandler<TResponse,
Expand Down
27 changes: 25 additions & 2 deletions lib/src/request/handler/request_handler_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'package:dart_mediator/src/request/handler/request_handler.dart';

class RequestHandlerStore {
final _handlers = <Type, RequestHandler>{};
final _handlerFactories = <Type, RequestHandlerFactory>{};

/// Registers the [handler] to a given [TEvent].
/// Registers the [handler] to a given [TRequest].
void register<TResponse, TRequest extends Request<TResponse>>(
RequestHandler<TResponse, TRequest> handler,
) {
Expand All @@ -13,9 +14,31 @@ class RequestHandlerStore {
'register<$TResponse, $TRequest> was called with an already registered handler',
);

assert(
!_handlerFactories.containsKey(TRequest),
'register<$TRequest> was called with an already registered factory',
);

_handlers[TRequest] = handler;
}

/// Registers the [factory] to a given [TRequest].
void registerFactory<TResponse, TRequest extends Request<TResponse>>(
RequestHandlerFactory<TResponse, TRequest> factory,
) {
assert(
!_handlers.containsKey(TRequest),
'registerFactory<$TResponse, $TRequest> was called with an already registered handler',
);

assert(
!_handlerFactories.containsKey(TRequest),
'registerFactory<$TRequest> was called with an already registered factory',
);

_handlerFactories[TRequest] = factory;
}

/// Unregisters the given [handler].
void unregister<TResponse, TRequest extends Request<TResponse>>(
RequestHandler<TResponse, TRequest> handler,
Expand All @@ -31,7 +54,7 @@ class RequestHandlerStore {
/// Returns the registered [RequestHandler]'s for [TRequest].
RequestHandler<TResponse, TRequest>
getHandlerFor<TResponse, TRequest extends Request<TResponse>>() {
final handler = _handlers[TRequest];
final handler = _handlers[TRequest] ?? _handlerFactories[TRequest]?.call();

assert(
handler != null,
Expand Down
7 changes: 7 additions & 0 deletions lib/src/request/request_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class RequestManager {
_requestHandlerStore.register(handler);
}

/// Registers the request [factory] for the given [TRequest].
void registerFactory<TResponse, TRequest extends Request<TResponse>>(
RequestHandlerFactory<TResponse, TRequest> factory,
) {
_requestHandlerStore.registerFactory(factory);
}

/// Sends a [request] to a single [RequestHandler].
///
/// Make sure the [RequestHandler] is [register]ed before calling this method.
Expand Down
23 changes: 23 additions & 0 deletions test/unit/request/request_handler/request_handler_store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ void main() {
});
});

group('registerFactory', () {
test('it registers the handler', () {
expect(
() => requestHandlerStore.registerFactory<int, MockRequest<int>>(
() => MockRequestHandler<int, MockRequest<int>>(),
),
returnsNormally,
);
});
});

group('unregister', () {
final mockRequestHandler = MockRequestHandler<int, MockRequest<int>>();

Expand Down Expand Up @@ -83,6 +94,18 @@ void main() {
correctHandler,
);
});

test('it returns the request handler factory', () {
final mockHandler = MockRequestHandler<int, MockRequest<int>>();
correctHandlerFactory() => mockHandler;

requestHandlerStore.registerFactory(correctHandlerFactory);

expect(
requestHandlerStore.getHandlerFor<int, MockRequest<int>>(),
mockHandler,
);
});
});
});
}
16 changes: 16 additions & 0 deletions test/unit/request/requests_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ void main() {
});
});

group('registerFactory', () {
test('it registers the handler', () {
mockRequestHandlerFactory() =>
MockRequestHandler<String, MockRequest<String>>();

requestsManager.registerFactory<String, MockRequest<String>>(
mockRequestHandlerFactory,
);

verify(
() => mockRequestHandlerStore
.registerFactory(mockRequestHandlerFactory),
);
});
});

group('registerFunction', () {
test('it registers the handler', () {
requestsManager.registerFunction<String, MockRequest<String>>(
Expand Down

0 comments on commit 7d39d6f

Please sign in to comment.