-
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.
Merge pull request #22 from alvarobcprado/feature/update-re-listener-…
…modifier Feature/update re listener modifier
- Loading branch information
Showing
10 changed files
with
116 additions
and
78 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
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,3 +1,42 @@ | ||
library re_listener_modifiers; | ||
|
||
import 'package:re_state_action/src/typedefs/re_types.dart'; | ||
import 'package:rxdart/transformers.dart'; | ||
|
||
export 'package:rxdart/transformers.dart'; | ||
|
||
/// A collection of static methods that return [ReListenerModifier] functions. | ||
/// These functions can be used to modify the behavior of a | ||
/// [ReListenerModifier]. | ||
abstract class ReListenerModifiers { | ||
/// Returns a [ReListenerModifier] that applies a mapper function to the | ||
/// listener and returns the flattened result. | ||
static ReListenerModifier<T> flatMap<T>() => | ||
(listener, mapper) => listener.flatMap(mapper); | ||
|
||
/// Returns a [ReListenerModifier] that applies a mapper function to the | ||
/// listener and returns the result of the latest mapped stream. | ||
static ReListenerModifier<T> switchMap<T>() => | ||
(listener, mapper) => listener.switchMap(mapper); | ||
|
||
/// Returns a [ReListenerModifier] that applies a mapper function to the | ||
/// listener and ignores all events until the mapper completes. | ||
static ReListenerModifier<T> exhaustMap<T>() => | ||
(listener, mapper) => listener.exhaustMap(mapper); | ||
|
||
/// Returns a [ReListenerModifier] that applies a mapper function to the | ||
/// listener and returns the result of the mapped stream as soon as it is | ||
/// available. | ||
static ReListenerModifier<T> asyncExpand<T>() => | ||
(listener, mapper) => listener.asyncExpand(mapper); | ||
|
||
/// Returns a [ReListenerModifier] that applies a [Duration] [duration] to | ||
/// the listener and debounces the events. | ||
static ReListenerModifier<T> debounceTime<T>(Duration duration) => | ||
(listener, mapper) => listener.debounceTime(duration).flatMap(mapper); | ||
|
||
/// Returns a [ReListenerModifier] that applies a [Duration] [duration] to | ||
/// the listener and throttles the events. | ||
static ReListenerModifier<T> throttleTime<T>(Duration duration) => | ||
(listener, mapper) => listener.throttleTime(duration).flatMap(mapper); | ||
} |
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
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,26 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:re_state_action/re_listener_modifiers.dart'; | ||
import 'package:re_state_action/src/typedefs/re_types.dart'; | ||
|
||
/// @nodoc | ||
ReListenerMapper<T> reListenerMapper<T>(FutureOr<void> Function(T) callback) => | ||
(data) { | ||
final controller = StreamController<T>.broadcast(sync: true); | ||
|
||
Future<void> handleData() async { | ||
try { | ||
await callback(data); | ||
} catch (_) { | ||
rethrow; | ||
} finally { | ||
await controller.done; | ||
} | ||
} | ||
|
||
handleData(); | ||
return controller.stream; | ||
}; | ||
|
||
/// @nodoc | ||
ReListenerModifier<T> reListenerModifier<T>() => ReListenerModifiers.flatMap(); |
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