Skip to content

Commit d079948

Browse files
committed
extract files
1 parent ad8d67d commit d079948

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

lib/src/redux_store_stream_transformer.dart

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'logger.dart';
66
import 'reducer.dart';
77
import 'reducer_exception.dart';
88
import 'side_effect.dart';
9+
import 'utils.dart';
10+
import 'wrapper_action.dart';
911

1012
/// Redux store stream Extension for Stream of actions.
1113
extension ReduxStoreExt<Action> on Stream<Action> {
@@ -94,7 +96,7 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
9496
Stream<S> bind(Stream<A> stream) {
9597
StreamController<S> controller;
9698
List<StreamSubscription<dynamic>> subscriptions;
97-
StreamController<_WrapperAction<A>> actionController;
99+
StreamController<WrapperAction<A>> actionController;
98100

99101
void onListen() {
100102
S state;
@@ -107,13 +109,13 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
107109
return;
108110
}
109111

110-
void onDataActually(_WrapperAction<A> wrapper) {
112+
void onDataActually(WrapperAction<A> wrapper) {
111113
final action = wrapper.action;
112114
final type = wrapper.type;
113115
final currentState = state;
114116

115117
// add initial state
116-
if (type == _ActionType.initial) {
118+
if (identical(type, ActionType.initial)) {
117119
final message = '\n'
118120
' ⟶ Action : $type\n'
119121
' ⟹ Current state: $currentState';
@@ -149,18 +151,18 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
149151
}
150152
}
151153

152-
actionController = StreamController<_WrapperAction<A>>.broadcast();
154+
actionController = StreamController<WrapperAction<A>>.broadcast();
153155

154156
// Call reducer on each action.
155157
final subscriptionActionController =
156158
actionController.stream.listen(onDataActually);
157159

158160
// Add initial action
159-
actionController.add(_WrapperAction(null, _ActionType.initial));
161+
actionController.add(WrapperAction(null, ActionType.initial));
160162

161163
// Listening to upstream actions
162164
final subscriptionUpstream = stream
163-
.map((action) => _WrapperAction(action, _ActionType.external))
165+
.map((action) => WrapperAction(action, ActionType.external))
164166
.listen(
165167
actionController.add,
166168
onError: controller.addError,
@@ -207,7 +209,7 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
207209
}
208210

209211
Iterable<StreamSubscription<dynamic>> _listenSideEffects(
210-
StreamController<_WrapperAction<A>> actionController,
212+
StreamController<WrapperAction<A>> actionController,
211213
GetState<S> getState,
212214
StreamController<S> controller,
213215
) {
@@ -224,8 +226,8 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
224226
}
225227

226228
return actions
227-
.map((action) =>
228-
_WrapperAction(action, _ActionType.sideEffect(index)))
229+
.map(
230+
(action) => WrapperAction(action, ActionType.sideEffect(index)))
229231
.listen(
230232
actionController.add,
231233
onError: controller.addError,
@@ -236,59 +238,3 @@ class ReduxStoreStreamTransformer<A, S> extends StreamTransformerBase<A, S> {
236238
);
237239
}
238240
}
239-
240-
//
241-
// Internal
242-
//
243-
244-
@sealed
245-
abstract class _ActionType {
246-
const _ActionType.empty();
247-
248-
static const initial = _Initial();
249-
static const external = _External();
250-
251-
const factory _ActionType.sideEffect(int index) = _SideEffect;
252-
253-
@override
254-
String toString() {
255-
if (this is _Initial) {
256-
return '⭍';
257-
}
258-
if (this is _External) {
259-
return '↓';
260-
}
261-
if (this is _SideEffect) {
262-
return '⟳${(this as _SideEffect).index}';
263-
}
264-
throw StateError('Unknown $this');
265-
}
266-
}
267-
268-
class _Initial extends _ActionType {
269-
const _Initial() : super.empty();
270-
}
271-
272-
class _External extends _ActionType {
273-
const _External() : super.empty();
274-
}
275-
276-
class _SideEffect extends _ActionType {
277-
final int index;
278-
279-
const _SideEffect(this.index) : super.empty();
280-
}
281-
282-
class _WrapperAction<A> {
283-
final A action;
284-
final _ActionType type;
285-
286-
_WrapperAction(this.action, this.type);
287-
}
288-
289-
extension _MapIndexedIterableExtensison<T> on Iterable<T> {
290-
Iterable<R> mapIndexed<R>(R Function(int, T) mapper) {
291-
var index = 0;
292-
return map((t) => mapper(index++, t));
293-
}
294-
}

lib/src/utils.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ignore_for_file: public_member_api_docs
2+
3+
extension MapIndexedIterableExtensison<T> on Iterable<T> {
4+
Iterable<R> mapIndexed<R>(R Function(int, T) mapper) {
5+
var index = 0;
6+
return map((t) => mapper(index++, t));
7+
}
8+
}

lib/src/wrapper_action.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'package:meta/meta.dart';
2+
3+
// ignore_for_file: public_member_api_docs, missing_return
4+
5+
@sealed
6+
abstract class ActionType {
7+
const ActionType.empty();
8+
9+
static final initial = _Initial();
10+
static final external = _External();
11+
12+
factory ActionType.sideEffect(int index) = _SideEffect;
13+
14+
@override
15+
String toString() {
16+
if (this is _Initial) {
17+
return '⭍';
18+
}
19+
if (this is _External) {
20+
return '↓';
21+
}
22+
if (this is _SideEffect) {
23+
return '⟳${(this as _SideEffect).index}';
24+
}
25+
}
26+
}
27+
28+
class _Initial extends ActionType {
29+
_Initial() : super.empty();
30+
}
31+
32+
class _External extends ActionType {
33+
_External() : super.empty();
34+
}
35+
36+
class _SideEffect extends ActionType {
37+
final int index;
38+
39+
_SideEffect(this.index) : super.empty();
40+
}
41+
42+
class WrapperAction<A> {
43+
final A action;
44+
final ActionType type;
45+
46+
WrapperAction(this.action, this.type);
47+
}

0 commit comments

Comments
 (0)