浏览代码

move redux from sample to runtime.

add thunk middleware.
/main
kg 6 年前
当前提交
ae4ceacc
共有 3 个文件被更改,包括 14 次插入4 次删除
  1. 2
      Runtime/redux/redux_logging.cs
  2. 12
      Runtime/redux/redux_thunk.cs
  3. 4
      Samples/ReduxSample/ObjectFinder/Reducer.cs

2
Runtime/redux/redux_logging.cs


var result = next.dispatch(action);
var afterState = store.getState();
var afterStateDump = JsonUtility.ToJson(afterState);
Debug.LogFormat("Action name={0} data={1}", action.GetType().Name, JsonUtility.ToJson(action));
Debug.LogFormat("Action name={0} data={1}", action.ToString(), JsonUtility.ToJson(action));
Debug.LogFormat("previousState=\n{0}", previousStateDump);
Debug.LogFormat("afterState=\n{0}", afterStateDump);
return result;

12
Runtime/redux/redux_thunk.cs


public static Middleware<State> create<State>() {
return (store) => (next) => new DispatcherImpl((action) => {
var thunkAction = action as ThunkAction<State>;
if (thunkAction != null) {
if (thunkAction != null && thunkAction.action != null) {
return thunkAction.action(store.dispatcher, store.getState);
}

public sealed class ThunkAction<State> {
public readonly Func<Dispatcher, Func<State>, object> action;
public readonly string displayName;
public ThunkAction(Func<Dispatcher, Func<State>, object> action) {
public ThunkAction(
Func<Dispatcher, Func<State>, object> action = null,
string displayName = null) {
this.displayName = displayName ?? "";
}
public override string ToString() {
return "ThunkAction(" + this.displayName + ")";
}
}
}

4
Samples/ReduxSample/ObjectFinder/Reducer.cs


public static class SearchAction {
public static ThunkAction<FinderAppState> create(string keyword) {
return new ThunkAction<FinderAppState>((dispatcher, getState) => {
return new ThunkAction<FinderAppState>(
displayName: "SearchAction",
action: (dispatcher, getState) => {
var objects = UnityEngine.Object.FindObjectsOfType(typeof(FinderGameObject)).Where(
obj => keyword == "" || obj.name.ToUpper().Contains(keyword.ToUpper())).Select(
obj => new GameObjectInfo {id = obj.GetInstanceID(), name = obj.name}).ToList();

正在加载...
取消
保存