-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework subscribe methods as extensions (#9)
* Add `EventHandler.factory` * Remove and replace factory based functions with extension method * Make the built-in handlers immutable * Update example with `subscribeFactory` usage * Add factory based request handler * Remove and replace factory based functions with extension method * Make test data immutable * Rework pipeline behavior to use extensions for registration * add more request integration tests * Add more event integration tests * Add more choregraphy integration tests * Update comment
- Loading branch information
Showing
24 changed files
with
730 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,72 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:meta/meta.dart'; | ||
|
||
/// Factory to create a [EventHandler]. | ||
typedef EventHandlerFactory<TEvent> = EventHandler<TEvent> Function(); | ||
|
||
/// Handler for [TEvent]. | ||
abstract interface class EventHandler<TEvent> { | ||
/// Function based event handler | ||
/// Function based [EventHandler]. | ||
/// | ||
/// Each event the underlying [handler] will be executed. | ||
const factory EventHandler.function( | ||
FutureOr<void> Function(TEvent event) handler, | ||
) = _FunctionEventHandler; | ||
|
||
/// Factory based [EventHandler] | ||
/// | ||
/// Each event the underlying [factory] will be instantiated and used | ||
/// to handle the [TEvent]. | ||
const factory EventHandler.factory( | ||
EventHandlerFactory<TEvent> factory, | ||
) = _FactoryEventHandler; | ||
|
||
/// Handles the given [event]. | ||
FutureOr<void> handle(TEvent event); | ||
} | ||
|
||
@immutable | ||
class _FunctionEventHandler<T> implements EventHandler<T> { | ||
final FutureOr<void> Function(T event) handler; | ||
|
||
const _FunctionEventHandler(this.handler); | ||
|
||
@override | ||
FutureOr<void> handle(T event) => handler(event); | ||
|
||
@override | ||
int get hashCode => Object.hash(runtimeType, handler); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return identical(this, other) || | ||
(other.runtimeType == runtimeType && | ||
other is _FunctionEventHandler<T> && | ||
other.handler == handler); | ||
} | ||
} | ||
|
||
@immutable | ||
class _FactoryEventHandler<T> implements EventHandler<T> { | ||
final EventHandlerFactory<T> factory; | ||
|
||
const _FactoryEventHandler(this.factory); | ||
|
||
@override | ||
FutureOr<void> handle(T event) { | ||
final handler = factory(); | ||
return handler.handle(event); | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hash(runtimeType, factory); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return identical(this, other) || | ||
(other.runtimeType == runtimeType && | ||
other is _FactoryEventHandler<T> && | ||
other.factory == factory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.