-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from emanuel-braz/issue/3
feat: add distinct property to handlers
- Loading branch information
Showing
12 changed files
with
696 additions
and
528 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
97 changes: 97 additions & 0 deletions
97
lib/src/controllers/app_event/micro_app_event_controller.dart
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// ignore_for_file: cancel_subscriptions | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_micro_app/flutter_micro_app.dart'; | ||
import 'package:flutter_micro_app/src/services/native_service.dart'; | ||
|
||
import 'micro_app_event_helper.dart'; | ||
|
||
/// [MicroAppEventController] | ||
class MicroAppEventController { | ||
final String _channel = Constants.channelAppEvents; | ||
final StreamController<MicroAppEvent> _controller = | ||
StreamController.broadcast(); | ||
late MicroAppNativeService _microAppNativeService; | ||
late MicroAppEventHelper _handlerRegisterHelper; | ||
|
||
/// Used to listen all micro app events | ||
MicroAppEventSubscription get onEvent => _controller.stream.listen; | ||
|
||
/// Can be used to filter events and listen to app events, anywhere | ||
Stream<MicroAppEvent> get stream => _controller.stream; | ||
bool get hasSubscribers => _controller.hasListener; | ||
bool get hasHandlers => _handlers.isNotEmpty; | ||
|
||
final Map<MicroAppEventHandler, StreamSubscription<MicroAppEvent>> _handlers = | ||
{}; | ||
|
||
/// get all handlers and subscriptions availables | ||
Map<MicroAppEventHandler, StreamSubscription<MicroAppEvent>> get handlers => | ||
_handlers; | ||
|
||
/// Factory instance | ||
factory MicroAppEventController() => instance; | ||
|
||
/// MicroAppController instance | ||
static MicroAppEventController instance = MicroAppEventController._(); | ||
MicroAppEventController._() { | ||
_handlerRegisterHelper = MicroAppEventHelper(); | ||
_microAppNativeService = MicroAppNativeService(_channel, | ||
methodCallHandler: (MethodCall call) async { | ||
_controller | ||
.add(MicroAppEvent(name: call.method, payload: call.arguments)); | ||
}); | ||
} | ||
|
||
/// ⚠️ It is used only for unit tests purposes | ||
factory MicroAppEventController.$testOnlyPurpose() => | ||
MicroAppEventController._(); | ||
|
||
/// [MicroAppEvent] | ||
void emit(MicroAppEvent event) { | ||
if (MicroAppPreferences.config.nativeEventsEnabled) { | ||
_microAppNativeService.emit( | ||
Constants.methodMicroAppEvent, event.toString()); | ||
} | ||
_controller.add(event); | ||
} | ||
|
||
/// register handler | ||
StreamSubscription<MicroAppEvent>? registerHandler<T>( | ||
MicroAppEventHandler? handler) { | ||
if (handler == null) return null; | ||
final subscription = | ||
_handlerRegisterHelper.registerHandler(stream, handler); | ||
_handlers.putIfAbsent(handler, () => subscription); | ||
return subscription; | ||
} | ||
|
||
/// pauseAllHandlers | ||
void pauseAllHandlers() => | ||
_handlerRegisterHelper.pauseAllSubscriptions(_handlers); | ||
|
||
/// resumeAllHandlers | ||
void resumeAllHandlers() => | ||
_handlerRegisterHelper.resumeAllSubscriptions(_handlers); | ||
|
||
/// unregisterAllHandlers | ||
Future<void> unregisterAllHandlers() => | ||
_handlerRegisterHelper.unregisterAllSubscriptions(_handlers); | ||
|
||
/// unregisterHandler | ||
Future<void> unregisterHandler({String? id, List<String>? channels}) => | ||
_handlerRegisterHelper.unregisterSubscription(_handlers, | ||
id: id, channels: channels); | ||
|
||
/// hasHandler | ||
bool hasHandler({String? id, List<String>? channels}) => | ||
_handlerRegisterHelper | ||
.findHandlerEntries(handlers, id: id, channels: channels) | ||
.isNotEmpty; | ||
|
||
/// dispose controller (do not dispose this, if there is not a very specific situation case) | ||
void dispose() { | ||
_controller.close(); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import '../../../flutter_micro_app.dart'; | ||
|
||
/// [MicroAppEventHandler] | ||
class MicroAppEventHandler<T> extends EventChannelsEquatable { | ||
final String? id; | ||
final MicroAppEventOnEvent onEvent; | ||
final MicroAppEventOnDone? onDone; | ||
final MicroAppEventOnError? onError; | ||
final bool? cancelOnError; | ||
final bool distinct; | ||
|
||
const MicroAppEventHandler(this.onEvent, | ||
{this.onDone, | ||
this.onError, | ||
this.cancelOnError, | ||
List<String> channels = const [], | ||
this.id, | ||
this.distinct = true}) | ||
: super(channels); | ||
|
||
@override | ||
List<Object?> get props => | ||
[onEvent, onDone, onError, cancelOnError, channels, id, distinct]; | ||
} |
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.