dynamic_middleware 0.5.1 copy "dynamic_middleware: ^0.5.1" to clipboard
dynamic_middleware: ^0.5.1 copied to clipboard

Dart 1 only

A way to define middleware for redux dynamically

example/main.dart

import 'dart:io';

import 'package:dynamic_middleware/dynamic_middleware.dart';
import 'package:redux/redux.dart';

/// Simple reducer that increments the state
int reducer(int state, Object action) => state+1;

/// Store that holds state value as an integer
Store<int> store;

/// Basic dynamic middleware object used to add and remove middleware
DynamicMiddleware<int> dynamicMiddleware;

/// Simple middleware that adds a line of standard output on all actions
void middleware(Store<int> store, Object action, NextDispatcher next) {
   stdout.writeln("Hello from dynamic middleware!");
   next(action);
}

void main() {
  dynamicMiddleware = new DynamicMiddleware<int>.empty();
  store = new Store<int>(reducer, initialState: 0, middleware: [dynamicMiddleware]);

  String input;
  while(input != "q") {
    stdout.writeln('Hit enter to dispatch an action,\nType "m" to dispatch an action with dynamic middleware,\nType "q" to quit');
    input = stdin.readLineSync();
    if(input == "m") {
      dynamicMiddleware.add(middleware);
    }
    store.dispatch("action");
    stdout
        ..write("Counter: ")
        ..writeln(store.state);

    dynamicMiddleware.clear();
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

A way to define middleware for redux dynamically

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

redux

More

Packages that depend on dynamic_middleware