Skip to content

Commit

Permalink
Reconstruct dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zjuwjf committed Jun 21, 2019
1 parent a958190 commit 76db667
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/lib/todo_list_page/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ToDoListPage extends Page<PageState, Map<String, dynamic>> {
reducer: buildReducer(),
view: buildView,
dependencies: Dependencies<PageState>(
adapter: ToDoListAdapter(),
list: NoneConn<PageState>() + ToDoListAdapter(),
slots: <String, Dependent<PageState>>{
'report': ReportConnector() + ReportComponent()
}),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/redux/connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class ImmutableConn<T, P> implements AbstractConnector<T, P> {
return state;
}
final P newProps = reducer(props, action);
final bool hasChanged = newProps != props;
final bool hasChanged = !identical(newProps, props);
if (hasChanged) {
final T result = set(state, newProps);
assert(result != null, 'Expected to return a non-null value.');
Expand Down
4 changes: 2 additions & 2 deletions lib/src/redux_adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ abstract class Adapter<T> extends Logic<T> implements AbstractAdapter<T> {
Dependencies<T> dependencies,
Object Function(T) key,
}) : assert(adapter != null),
assert(dependencies?.adapter == null,
'Unexpected dependencies.adapter for Adapter.'),
assert(dependencies?.list == null,
'Unexpected dependencies.list for Adapter.'),
_adapter = adapter,
super(
reducer: reducer,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/redux_component/basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ abstract class MixedStore<T> extends Store<T>

/// Seen in view-part or adapter-part
abstract class ViewService {
/// The way to build adapter which is configured in Dependencies.adapter
/// The way to build adapter which is configured in Dependencies.list
ListAdapter buildAdapter();

/// The way to build slot component which is configured in Dependencies.slots
Expand Down
2 changes: 1 addition & 1 deletion lib/src/redux_component/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ abstract class Component<T> extends Logic<T> implements AbstractComponent<T> {
shouldUpdate: protectedShouldUpdate,
name: name,
markNeedsBuild: markNeedsBuild,
sidecarCtx: protectedDependencies?.adapter?.createContext(
sidecarCtx: protectedDependencies?.list?.createContext(
store: store,
buildContext: buildContext,
getState: getState,
Expand Down
24 changes: 19 additions & 5 deletions lib/src/redux_component/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import '../redux/redux.dart';
import '../redux_connector/redux_connector.dart';
import 'basic.dart';

class Dependencies<T> {
final Map<String, Dependent<T>> slots;
final AbstractAdapter<T> adapter;
final Dependent<T> list;

Dependencies({this.slots, this.adapter});
/// Use [list: NoneConn<T>() + Adapter<T>()] instead of [adapter: Adapter<T>()],
/// Which is better reusability and consistency.
Dependencies({
this.slots,
@deprecated AbstractAdapter<T> adapter,
Dependent<T> list,
}) : assert(list == null || list.isAdapter(), ''),
assert(list == null || adapter == null, ''),
list = list ?? (NoneConn<T>() + adapter);

Reducer<T> get reducer {
Reducer<T> slotsReducer;
final List<SubReducer<T>> subs = <SubReducer<T>>[];
if (slots != null && slots.isNotEmpty) {
slotsReducer = combineSubReducers(slots.entries.map<SubReducer<T>>(
subs.addAll(slots.entries.map<SubReducer<T>>(
(MapEntry<String, Dependent<T>> entry) =>
entry.value.createSubReducer(),
));
}
return combineReducers(<Reducer<T>>[slotsReducer, adapter?.reducer]);

if (list != null) {
subs.add(list.createSubReducer());
}

return combineReducers(<Reducer<T>>[combineSubReducers(subs)]);
}

Dependent<T> slot(String type) => slots[type];
Expand Down
4 changes: 2 additions & 2 deletions lib/src/redux_component/dependent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ class _Dependent<T, P> implements Dependent<T> {
}

Dependent<K> createDependent<K, T>(
AbstractConnector<K, T> connector, Logic<T> logic) =>
_Dependent<K, T>(connector: connector, logic: logic);
AbstractConnector<K, T> connector, AbstractLogic<T> logic) =>
logic != null ? _Dependent<K, T>(connector: connector, logic: logic) : null;
12 changes: 0 additions & 12 deletions lib/src/redux_connector/congruent.dart

This file was deleted.

6 changes: 2 additions & 4 deletions lib/src/redux_connector/connector.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../redux/redux.dart';

import '../redux_component/redux_component.dart';
import 'op_mixin.dart';

/// use ConnOp<T, P> instead of Connector<T, P>
@deprecated
Expand All @@ -21,7 +21,7 @@ class Connector<T, P> extends MutableConn<T, P> {
void set(T state, P subState) => _setter(state, subState);
}

class ConnOp<T, P> extends MutableConn<T, P> {
class ConnOp<T, P> extends MutableConn<T, P> with ConnOpMixin<T, P> {
final P Function(T) _getter;
final void Function(T, P) _setter;

Expand All @@ -36,6 +36,4 @@ class ConnOp<T, P> extends MutableConn<T, P> {

@override
void set(T state, P subState) => _setter(state, subState);

Dependent<T> operator +(Logic<P> logic) => createDependent<T, P>(this, logic);
}
10 changes: 10 additions & 0 deletions lib/src/redux_connector/none.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../redux/redux.dart';
import 'op_mixin.dart';

class CongruentConn<T> extends ImmutableConn<T, T> with ConnOpMixin<T, T> {
@override
T get(T state) => state;

@override
T set(T state, T subState) => subState;
}
3 changes: 2 additions & 1 deletion lib/src/redux_connector/op_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import '../redux/redux.dart';
import '../redux_component/redux_component.dart';

mixin ConnOpMixin<T, P> on AbstractConnector<T, P> {
Dependent<T> operator +(Logic<P> logic) => createDependent<T, P>(this, logic);
Dependent<T> operator +(AbstractLogic<P> logic) =>
createDependent<T, P>(this, logic);
}
2 changes: 1 addition & 1 deletion lib/src/redux_connector/redux_connector.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export 'congruent.dart';
export 'connector.dart';
export 'map_like.dart';
export 'none.dart';
export 'op_mixin.dart';
export 'reselect.dart';

0 comments on commit 76db667

Please sign in to comment.