Skip to content

Commit

Permalink
feat: add module event handle registries attr
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Jun 25, 2022
1 parent 02555e0 commit ea7494c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/buzz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library buzz;

export 'buzz_impl.dart';
export 'infra/event_bus_holder.dart';
export 'infra/registries.dart';
export 'infra/typed_event_bus.dart';
export 'kinds/app_events.dart';
export 'kinds/commands.dart';
Expand Down
29 changes: 23 additions & 6 deletions lib/buzz_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:buzz/buzz.dart';

import 'infra/registries.dart';
import 'utils.dart';

IBuzzBase? _buzz;
Expand Down Expand Up @@ -45,19 +46,18 @@ class BuzzBase implements IBuzzBase {
UiEventBus get uiEvents => EventBusHolder.of<UiEventBus>();

late Navigator _navigator;
List<ModuleBuzzRegistries>? _moduleRegistries;

@override
void init({
required Navigator navigator,
List<ModuleBuzzRegistries>? moduleRegistries,
}) {
_navigator = navigator;
_moduleRegistries = moduleRegistries;

commands.on<NavigationCommand>().listen((navigationCommand) {
NavigationCommandHandler(
navigator: _navigator,
backDefault: _navigator.backDefaultRoute,
).handle(navigationCommand);
});
_bindNavigationCommandHandler();
_bindRegistries();
}

@override
Expand All @@ -73,4 +73,21 @@ class BuzzBase implements IBuzzBase {
void destroy() {
//TODO: implement
}

void _bindNavigationCommandHandler() {
commands.on<NavigationCommand>().listen((navigationCommand) {
NavigationCommandHandler(
navigator: _navigator,
backDefault: _navigator.backDefaultRoute,
).handle(navigationCommand);
});
}

void _bindRegistries() {
_moduleRegistries?.forEach((moduleRegistry) {
uiEvents.bindRegistries(moduleRegistry.uiEvents);
commands.bindRegistries(moduleRegistry.commands);
appEvents.bindRegistries(moduleRegistry.appEvents);
});
}
}
6 changes: 4 additions & 2 deletions lib/infra/registries.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:buzz/buzz.dart';

abstract class _IModuleBuzzRegistries<T> {
abstract class _IModuleBuzzRegistries {
List<CommandRegistry> get commands;
List<UiEventRegistry> get uiEvents;
List<AppEventRegistry> get appEvents;
}

abstract class ModuleBuzzRegistries<T> extends _IModuleBuzzRegistries<T> {
abstract class ModuleBuzzRegistries extends _IModuleBuzzRegistries {
@override
List<CommandRegistry> get commands => const [];

Expand Down Expand Up @@ -42,6 +42,8 @@ abstract class EventHandlerRegistry<T> {

final Function(T) handler;

dynamic get registryType => T;

@override
String toString() => '$runtimeType type:$T';
}
24 changes: 19 additions & 5 deletions lib/infra/typed_event_bus.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:buzz/buzz.dart';
import 'package:event_bus/event_bus.dart';

import '../utils.dart';
Expand All @@ -18,16 +19,29 @@ class TypedEventBus<T> {
return _eventBus.on<X>();
}

/*TODO: Verify use case still valid or remove.
Stream of(bool Function(dynamic) evaluateCast) {
Stream of(dynamic type) {
return _eventBus.streamController.stream.where(
(event) {
final isType = evaluateCast(event);
//debugPrint('$event isType: $isType');
final isType = isTypeSupported(type);
developerLog('$event isType: $isType');
return isType;
},
).cast();
}*/
}
}

extension TypedEventBusRegistry<T> on TypedEventBus<T> {
void bindRegistries(List<EventHandlerRegistry<T>> registries) {
registries.forEach((registry) {
bindRegistry(registry);
});
}

void bindRegistry(EventHandlerRegistry<T> registry) {
of(registry.registryType).listen(
(event) => registry.handler(event),
);
}
}

abstract class TypedEventHandler<T> {
Expand Down

0 comments on commit ea7494c

Please sign in to comment.