dva_dart 0.1.0

dva_dart #

A dart lib for managing app's state.

The idea is from dva.js ,which is a lightweight inspired by redux, redux-saga, elm.

Though in dart, we have redux and flutter-redux developed by community.

However it is not easy to learn and use even I have learned Redux in Javascript.

When I try use bloc from flutter's community, I find Stream in dart and StreamBuilder for flutter is powerful.

But it's the different setup and idea from Redux.

Finnally, I decided to use the idea from dva.js and the basic structure from bloc, using rxdart to combine them toghther.

And it might work in a way.

About flutter #

This library is under development, and I know a lot of use case should be flutter based.

And I will try to ship them into Flutter ,and create another flutter-dva repo somehow.

Will update this changelog until v1.0.0 #

example/example.dart

import 'dart:async';
import 'package:dva_dart/src/Model.dart';
import 'package:dva_dart/src/Effect.dart';
import 'package:dva_dart/src/State.dart';
import 'package:dva_dart/src/Action.dart';
import 'package:dva_dart/src/Store.dart';
import 'package:dva_dart/src/Reducer.dart';

//
//
//
//
class TestState implements DvaState {
  final int a;
  final int b;
  final int c;
  TestState(this.a, this.b, this.c);
  @override
  String toString() {
    // TODO: implement toString
    return 'TestState($a,$b,$c)';
  }
}

class MutatedState implements DvaState {
  final String a;
  MutatedState(this.a);
  @override
  String toString() {
    return 'MutatedState(${this.a})';
  }
}

class MyReducerDelegate implements ReducerDelegate {
  @override
  void onReducer(DvaReducer reducer) {
    print(reducer.toString());
  }
}

void main() async {
  var pl1 = Payload<Map>({'a': 1});

  var pl2 = Payload<String>('i am a payload');

  Future add(p) async {
    return await p + 1;
  }

  //
  //
  //
  // ReducerWatcher().delegate = MyReducerDelegate();

  DvaModel model =
      DvaModel(nameSpace: 'test', initialState: TestState(1, 2, 3), reducers: {
    'updateState': (DvaState state, Payload payload) {
      return MutatedState(payload.toString());
    },
  }, effects: {
    'asyncAdd': (Payload<Map> payload) async* {
      var added = await add(payload.payloadObject['payload']['a']);
      payload.payloadObject['payload']
          .update('a', (value) => value = added, ifAbsent: () => {'a': added});
      await Future<void>.delayed(Duration(seconds: 1));
      yield PutEffect(key: 'updateState', payload: payload);
    },
    'appending': (Payload payload) async* {
      yield PutEffect(key: 'updateState', payload: payload);
    }
  });

  DvaModel model2 =
      DvaModel(nameSpace: 'test2', initialState: TestState(1, 2, 3), reducers: {
    'updateState': (DvaState state, Payload payload) {
      return MutatedState(payload.toString() + 'mutated');
    },
  }, effects: {
    'appending': (Payload payload) async* {
      yield PutEffect(key: 'updateState', payload: payload);
      yield PutEffect(key: 'test/appending', payload: Payload('fuck you'));
    }
  });

  DvaStore store = DvaStore(models: <DvaModel>[model, model2]);
  Action abc1 = createAction('test/asyncAdd')(pl1);
  Action abc2 = createAction('test2/appending')(pl2);

  // final StreamSubscription subscription =
  //     store.storeController.stream.listen((onData) {
  //   print(onData);
  // });
  // store.dispatch(abc1);
  // store.dispatch(abc2);

  // 初始化一个监听

  // store.stateStream.listen((onData) {
  //   print(onData);
  // });
  //var listner = store.getStream('test2');
  // listner.listen((onData) {
  //   print(onData);
  // });
  store.getStream('test').listen((onData) {
    print(onData);
  });
  // store.dispatch(abc1);
  // store.dispatch(abc2);
  // store.dispatch(abc1);
  store.dispatch(abc2);

  // store.dispatch(abc2);

  // store.dispatch(abc3);

  // var initState = State(initialState: {'abc': '@@@'});
  // var result = DvaStore(abc, initState);
  // var putInitialized = PutEffect(actionType: abc.type);
  // var putReducer=Reducer(actionType: abc.type)

  // print(putInitialized.actionType);

  // result.dispatch(abc);
  // print(result.mapActionToState(result.currentState, abc));

  //print(abc.payload.payload['foo']['baz']);

  // var result = asynchronousNaturalsTo(
  //     n: Future.value(100),
  //     k: (d) async {
  //       return await d + 1;
  //     }).last;
  // print(await result);

  // print(result);
  // var eee = ContractStatus.REJECTED.toString();

  // var value = ({Payload payload, Function callFunc}) async* {
  //   yield callFunc(payload);
  // };

  // var key = 'func';
  // Map effect = Map.fromEntries([MapEntry(key, value)]);

  // effect['func'](payload: pl, callFunc: print).toList().then((d) => d);
}

Use this package as a library

1. Depend on it

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


dependencies:
  dva_dart: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:dva_dart/dva_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
9
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
52
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-0.42 points)

99 out of 101 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/Model.dart. (-0.50 points)

Analysis of lib/src/Model.dart reported 1 hint:

line 127 col 30: Use ; instead of {} for empty constructor bodies.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (rxdart).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
meta ^1.1.6 1.1.7
rxdart ^0.20.0 0.20.0 0.22.1+1
Dev dependencies
test ^1.3.2