Skip to content

Commit

Permalink
modify class named LocalState to LocalProps
Browse files Browse the repository at this point in the history
modify example annotation
  • Loading branch information
hzgotb committed Jul 19, 2019
1 parent 83c5048 commit 95b2d7b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
43 changes: 20 additions & 23 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'global_store/store.dart';
import 'todo_edit_page/page.dart';
import 'todo_list_page/page.dart';

/// 创建应用的根Widget
/// 创建应用的根 Widget
/// 1. 创建一个简单的路由,并注册页面
/// 2. 对所需的页面进行和AppStore的连接
/// 3. 对所需的页面进行AOP的增强
/// 2. 对所需的页面进行和 AppStore 的连接
/// 3. 对所需的页面进行 AOP 的增强
Widget createApp() {
final AbstractRoutes routes = PageRoutes(
pages: <String, Page<Object, dynamic>>{
Expand All @@ -20,32 +20,29 @@ Widget createApp() {
'todo_edit': TodoEditPage(),
},
visitor: (String path, Page<Object, dynamic> page) {
/// 只有特定的范围的Page才需要建立和AppStore的连接关系
/// 满足Page<T> T 是GlobalBaseState的之类
/// 只有特定的范围的 Page 才需要建立和 AppStore 的连接关系
/// 满足 Page<T> ,T 是 GlobalBaseState 的子类
if (page.isTypeof<GlobalBaseState>()) {
/// 建立AppStore驱动PageStore的单项数据连接
/// 建立 AppStore 驱动 PageStore 的单向数据连接
/// 1. 参数1 AppStore
/// 2. 参数2 当 AppStore.state 变化时, PageStore.state 该如何变化
page.connectExtraStore<GlobalState>(
GlobalStore.store,
(Object pagestate, GlobalState appState) {
final GlobalBaseState p = pagestate;
if (p.themeColor != appState.themeColor) {
if (pagestate is Cloneable) {
final Object copy = pagestate.clone();
final GlobalBaseState newState = copy;
newState.themeColor = appState.themeColor;
return newState;
}
page.connectExtraStore<GlobalState>(GlobalStore.store, (Object pagestate, GlobalState appState) {
final GlobalBaseState p = pagestate;
if (p.themeColor != appState.themeColor) {
if (pagestate is Cloneable) {
final Object copy = pagestate.clone();
final GlobalBaseState newState = copy;
newState.themeColor = appState.themeColor;
return newState;
}
return pagestate;
},
);
}
return pagestate;
});
}

/// AOP
/// 页面可以有一些私有的AOP的增强, 但往往会有一些AOP是整个应用下,所有页面都会有的。
/// 这些公共的通用AOP,通过遍历路由页面的形式统一加入。
/// 页面可以有一些私有的 AOP 的增强, 但往往会有一些 AOP 是整个应用下,所有页面都会有的。
/// 这些公共的通用 AOP ,通过遍历路由页面的形式统一加入。
page.enhancer.append(
/// View AOP
viewMiddleware: <ViewMiddleware<dynamic>>[
Expand Down Expand Up @@ -85,7 +82,7 @@ Widget createApp() {
);
}

/// 简单的Effect AOP
/// 简单的 Effect AOP
/// 只针对页面的生命周期进行打印
EffectMiddleware<T> _pageAnalyticsMiddleware<T>({String tag = 'redux'}) {
return (AbstractLogic<dynamic> logic, Store<T> store) {
Expand Down
53 changes: 36 additions & 17 deletions lib/src/redux_component/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,59 @@ import 'package:flutter/foundation.dart';

import 'basic.dart';

/// usage
/// class ALocal extends LocalState<ALocal> {
/// /// your fields
///
/// ALocal(Context<Object> ctx) : super(ctx) {
/// /// your constructor
/// }
/// Description:
///
/// LocalProps 所储存的 props 不参与 view 的刷新
///
///
/// Define:
///
/// ```dart
/// class ComponentLocalProps extends LocalProps<ComponentLocalProps> {
///
/// final controller = TextEditingController();
///
/// ComponentLocalProps(Context<Object> ctx) : super(ctx);
///
/// @override
/// void destruct(Context<Object> ctx) {
/// /// your destructor
/// }
/// void destruct(Context<Object> ctx) {}
///
/// factory ALocal.of(ExtraData ctx) =>
/// LocalState.provide<ALocal>((_) => ALocal(_)).of(ctx);
/// factory ComponentLocalProps.of(ExtraData ctx) {
/// return ComponentLocalProps.provide<ALocal>((_) => ComponentLocalProps(_)).of(ctx);
/// }
/// }
abstract class LocalState<T extends LocalState<T>> {
LocalState(Context<Object> ctx) : assert(ctx != null);
/// ```
///
/// Usage:
///
/// in View
/// ```dart
/// ComponentLocalProps.of(viewService).controller
/// ```
/// in effect
/// ```dart
/// ComponentLocalProps.of(context).controller
/// ```
///
abstract class LocalProps<T extends LocalProps<T>> {
LocalProps(Context<Object> ctx) : assert(ctx != null);
void destructor(Context<Object> ctx);

static _LocalStateProvider<T> provide<T extends LocalState<T>>(
static _LocalPropsProvider<T> provide<T extends LocalProps<T>>(
T Function(Context<Object>) construct) =>
_LocalStateProvider<T>(
_LocalPropsProvider<T>(
construct: construct,
destruct: (T local, Context<Object> ctx) => local.destructor(ctx),
);
}

@immutable
class _LocalStateProvider<T> {
class _LocalPropsProvider<T> {
final T Function(Context<Object>) construct;
final void Function(T, Context<Object>) destruct;

const _LocalStateProvider({@required this.construct, this.destruct})
const _LocalPropsProvider({@required this.construct, this.destruct})
: assert(construct != null,
'Please provide a constructor to create <T> instance.');

Expand Down

0 comments on commit 95b2d7b

Please sign in to comment.