Skip to content

Commit

Permalink
migrate from built value
Browse files Browse the repository at this point in the history
  • Loading branch information
zerokivin committed Mar 20, 2023
1 parent 120e576 commit afbe5f8
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 2,446 deletions.
24 changes: 13 additions & 11 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:built_value/built_value.dart';
import 'package:built_redux/built_redux.dart';

part 'example.g.dart';

void main() {
// Create a redux store holding the state of your app.
// Its API contains three getters: stream, state, and actions.
final store = Store<Counter, CounterBuilder, CounterActions>(
final store = Store<Counter, CounterActions>(
reducerBuilder.build(), // build returns a reducer function
Counter(),
CounterActions(),
Expand All @@ -28,38 +27,41 @@ void main() {
// reducers
abstract class CounterActions extends ReduxActions {
ActionDispatcher<int> get increment;

ActionDispatcher<int> get decrement;

// factory to create on instance of the generated implementation of CounterActions
CounterActions._();

factory CounterActions() => _$CounterActions();
}

// This is a built value. It is an immutable model that implements the Built interface.
// All of the state in your redux store is contained in a single built value model.
abstract class Counter implements Built<Counter, CounterBuilder> {
class Counter {
/// [count] value of the counter
int get count;
int count;

// Built value constructor. The factory is returning the default state
Counter._();
factory Counter() => _$Counter._(count: 0);
Counter({
this.count = 0,
});
}

// These are reducer functions. They have a (state, action, builder) => void signature.
// They describes how an action transforms the state into the next state by applying changes to the builder supplied.
// You are required to use the builder passed, calling state.rebuild will NOT update the state in your redux store.
void increment(Counter state, Action<int> action, CounterBuilder builder) =>
builder.count = state.count + action.payload;
void increment(Counter state, Action<int> action) =>
state.count = state.count + action.payload;

void decrement(Counter state, Action<int> action, CounterBuilder builder) =>
builder.count = state.count - action.payload;
void decrement(Counter state, Action<int> action) =>
state.count = state.count - action.payload;

// This is a reducer builder. Use of ReducerBuilder is not required, however it
// is strongly recommended as it gives you static type checking to make sure
// the payload for action name provided is the same as the expected payload
// for the action provided to your reducer. Calling .build() returns a reducer function
// that can be passed to the store's constructor.
final reducerBuilder = ReducerBuilder<Counter, CounterBuilder>()
final reducerBuilder = ReducerBuilder<Counter>()
..add(CounterActionsNames.increment, increment)
..add(CounterActionsNames.decrement, decrement);
82 changes: 0 additions & 82 deletions example/example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 26 additions & 47 deletions lib/src/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import 'typedefs.dart';

/// [MiddlewareApi] put in scope to your [Middleware] function by redux.
/// When using [MiddlewareBuilder] (recommended) [MiddlewareApi] is passed to your [MiddlewareHandler]
class MiddlewareApi<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions> {
class MiddlewareApi<State, Actions extends ReduxActions> {
final State Function() _state;
final Actions Function() _actions;

MiddlewareApi._(this._state, this._actions);

factory MiddlewareApi(Store<State, StateBuilder, Actions> _store) =>
factory MiddlewareApi(Store<State, Actions> _store) =>
MiddlewareApi._(() => _store.state, () => _store.actions);

/// [state] returns the current state
Expand All @@ -29,23 +26,19 @@ class MiddlewareApi<
/// with many different payload types, while maintaining type safety.
/// Each [MiddlewareHandler] added with add<T> must take a state of type State, an Action of type
/// Action<T>, and a builder of type StateBuilder
class MiddlewareBuilder<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions> {
var _map =
Map<String, MiddlewareHandler<State, StateBuilder, Actions, dynamic>>();
class MiddlewareBuilder<State, Actions extends ReduxActions> {
var _map = Map<String, MiddlewareHandler<State, Actions, dynamic>>();

void add<Payload>(ActionName<Payload> aMgr,
MiddlewareHandler<State, StateBuilder, Actions, Payload> handler) {
MiddlewareHandler<State, Actions, Payload> handler) {
_map[aMgr.name] = (api, next, action) {
handler(api, next, action as Action<Payload>);
};
}

/// [combine] combines this MiddlewareBuilder with another MiddlewareBuilder
/// for the same type
void combine(MiddlewareBuilder<State, StateBuilder, Actions> other) {
void combine(MiddlewareBuilder<State, Actions> other) {
_map.addAll(other._map);
}

Expand All @@ -54,45 +47,35 @@ class MiddlewareBuilder<
NestedState extends Built<NestedState, NestedStateBuilder>,
NestedStateBuilder extends Builder<NestedState, NestedStateBuilder>,
NestedActions extends ReduxActions>(
NestedMiddlewareBuilder<State, StateBuilder, Actions, NestedState,
NestedStateBuilder, NestedActions>
NestedMiddlewareBuilder<State, Actions, NestedState, NestedActions>
other) {
_map.addAll(other._map);
}

/// [build] returns a [Middleware] function that handles all actions added with [add]
Middleware<State, StateBuilder, Actions> build() =>
(MiddlewareApi<State, StateBuilder, Actions> api) =>
(ActionHandler next) => (Action<dynamic> action) {
var handler = _map[action.name];
if (handler != null) {
handler(api, next, action);
return;
}

next(action);
};
Middleware<State, Actions> build() => (MiddlewareApi<State, Actions> api) =>
(ActionHandler next) => (Action<dynamic> action) {
var handler = _map[action.name];
if (handler != null) {
handler(api, next, action);
return;
}

next(action);
};
}

class NestedMiddlewareBuilder<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions,
NestedState extends Built<NestedState, NestedStateBuilder>,
NestedStateBuilder extends Builder<NestedState, NestedStateBuilder>,
class NestedMiddlewareBuilder<State, Actions extends ReduxActions, NestedState,
NestedActions extends ReduxActions> {
final _map =
Map<String, MiddlewareHandler<State, StateBuilder, Actions, dynamic>>();
final _map = Map<String, MiddlewareHandler<State, Actions, dynamic>>();

final NestedState Function(State) _stateMapper;
final NestedActions Function(Actions) _actionsMapper;

NestedMiddlewareBuilder(this._stateMapper, this._actionsMapper);

void add<Payload>(
ActionName<Payload> aMgr,
MiddlewareHandler<NestedState, NestedStateBuilder, NestedActions, Payload>
handler) {
void add<Payload>(ActionName<Payload> aMgr,
MiddlewareHandler<NestedState, NestedActions, Payload> handler) {
_map[aMgr.name] = (api, next, action) {
handler(
MiddlewareApi._(
Expand All @@ -106,10 +89,10 @@ class NestedMiddlewareBuilder<
/// `NestedState`, `NestedStateBuilder`, `NestedActions` and combines it with
/// this `NestedMiddlewareBuilder`.
void combineMiddlewareBuilder(
MiddlewareBuilder<NestedState, NestedStateBuilder, NestedActions> other) {
MiddlewareBuilder<NestedState, NestedActions> other) {
var adapted = other._map.map((name, handler) => MapEntry(
name,
(MiddlewareApi<State, StateBuilder, Actions> api, ActionHandler next,
(MiddlewareApi<State, Actions> api, ActionHandler next,
Action action) =>
handler(
MiddlewareApi._(() => _stateMapper(api.state),
Expand All @@ -123,10 +106,6 @@ class NestedMiddlewareBuilder<
/// [MiddlewareHandler] is a function that handles an action in a middleware. Its is only for
/// use with [MiddlewareBuilder]. If you are not using [MiddlewareBuilder] middleware must be
/// declared as a [Middleware] function.
typedef MiddlewareHandler<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions,
Payload>
= void Function(MiddlewareApi<State, StateBuilder, Actions> api,
ActionHandler next, Action<Payload> action);
typedef MiddlewareHandler<State, Actions extends ReduxActions, Payload>
= void Function(MiddlewareApi<State, Actions> api, ActionHandler next,
Action<Payload> action);
Loading

0 comments on commit afbe5f8

Please sign in to comment.