dynamic_middleware 0.5.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 71

Dynamic Middleware for Redux

Build Status

Dart library that allows adding and removing Redux middleware after a Redux Store has already been initialized.

Warning: Some might say that doing this breaks the contract of idempotency you typically have with a Redux store. They're right! There are, however, some instances where this is useful. This library was created to support a new type of Redux store that is not idempotent.

Usage

When creating your Redux Store simple attach a DynamicMiddleware object to it.

dynamicMiddleware = new DynamicMiddleware<State>.empty();
store = new Store<State>(reducer, middleware: [
    dynamicMiddleware,
]);

Then add and remove middleware as you need.

dynamicMiddleware.add((store, action, next) => 
    print("Hello World!")
);

Use the DynamicMiddleware.fromSingle constructor to create new dynamic middleware with a single instance of Middleware.

store = new Store<State>(reducer, middleware: [
    new DynamicMiddleware.fromSingle(middleware1),
]);

Use the DynamicMiddleware.fromList constructor to create new dynamic middleware from a List of Middleware.

waresList = <Middleware<State>>[middleware1, middleware2];
store = new Store<State>(reducer, middleware: [
    new DynamicMiddleware.fromList(waresList),
]);

Adding and Removing Middleware

Add a single piece of middleware.

void add(Middleware<State> middleware);

Add a List of middleware.

void addAll(List<Middleware<State>> middleware) => middlewares.addAll(middleware);

Remove a single piece of middleware.

void remove(Middleware<State> middleware) => middlewares.remove(middleware);

Remove all middleware previously added.

void clear();

Credits

https://github.com/pofigizm/redux-dynamic-middlewares

Changelog

0.5.0

  • Update README.md

0.5.0

  • Adjust analysis_options.yaml rules.
  • Add empty and fromSingle constructors.
  • Follow the NextDispatcher pattern used by regular middleware.
  • Adjust tests and example to match updates.

0.4.0

  • Add toList() constructor and test it

0.3.0

  • Fix README.md

0.2.0

  • Add tests and example, Updated README.md

0.1.0

  • Initial version includes class DynamicMiddleware<State>

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();
  }
}

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dynamic_middleware: "^0.5.1"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dynamic_middleware/dynamic_middleware.dart';
        
Version Uploaded Documentation Archive
0.5.1 Apr 12, 2018 Go to the documentation of dynamic_middleware 0.5.1 Download dynamic_middleware 0.5.1 archive
0.5.0 Apr 12, 2018 Go to the documentation of dynamic_middleware 0.5.0 Download dynamic_middleware 0.5.0 archive
0.4.0 Feb 11, 2018 Go to the documentation of dynamic_middleware 0.4.0 Download dynamic_middleware 0.4.0 archive
0.3.0 Feb 10, 2018 Go to the documentation of dynamic_middleware 0.3.0 Download dynamic_middleware 0.3.0 archive
0.2.0 Feb 10, 2018 Go to the documentation of dynamic_middleware 0.2.0 Download dynamic_middleware 0.2.0 archive

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
43 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
71
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dynamic_middleware/dynamic_middleware.dart.

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.0 <2.0.0
redux >=2.0.0 <3.0.0 2.1.1 3.0.0
Dev dependencies
test ^0.12.30