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.
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.
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 State {
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 State {
final String a;
MutatedState(this.a);
@override
String toString() {
return 'MutatedState(${this.a})';
}
}
class MyReducerDelegate implements ReducerDelegate {
@override
void onReducer(Reducer 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': (State 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': (State state, Payload payload) {
return MutatedState(payload.toString() + 'mutated');
},
}, effects: {
'appending': (Payload payload) async* {
yield PutEffect(key: 'updateState', payload: payload);
}
});
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.getStreamAsBroadcast('test');
listner.listen((onData) {
print(onData);
});
store.getStreamAsBroadcast('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);
}
Add this to your package's pubspec.yaml file:
dependencies:
dva_dart: ^0.0.24
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 flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:dva_dart/dva_dart.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.0.24 | Feb 16, 2019 |
|
|
0.0.23 | Feb 16, 2019 |
|
|
0.0.22 | Feb 16, 2019 |
|
|
0.0.21 | Feb 16, 2019 |
|
|
0.0.20 | Feb 16, 2019 |
|
|
0.0.19 | Feb 15, 2019 |
|
|
0.0.18 | Feb 15, 2019 |
|
|
0.0.17 | Jan 29, 2019 |
|
|
0.0.16 | Jan 29, 2019 |
|
|
0.0.15 | Jan 29, 2019 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
36
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
90
|
Overall:
Weighted score of the above.
[more]
|
66
|
We analyzed this package on Feb 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:dva_dart/dva_dart.dart
.
Document public APIs. (-0.39 points)
94 out of 96 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.
Package is pre-v0.1 release. (-10 points)
While nothing is inherently wrong with versions of 0.0.*
, it might mean that the author is still experimenting with the general direction of the API.