kg
6 年前
当前提交
ee3832fe
共有 26 个文件被更改,包括 478 次插入 和 79 次删除
-
4Runtime/foundation/basic_types.cs
-
2Runtime/foundation/node.cs
-
4Runtime/foundation/node.mixin.gen.cs
-
2Runtime/foundation/node.mixin.njk
-
4Runtime/widgets/navigator.cs
-
20Samples/ReduxSample/CounterApp/CounterAppSample.cs
-
4Samples/ReduxSample/ObjectFinder/FinderGameObject.cs
-
73Samples/ReduxSample/ObjectFinder/ObjectFinderApp.cs
-
93Samples/ReduxSample/ObjectFinder/Reducer.cs
-
6Samples/ReduxSample/ObjectFinder/StoreProvider.cs
-
2Runtime/redux/redux_thunk.cs.meta
-
8Runtime/redux.meta
-
19Runtime/redux/redux_logging.cs
-
24Runtime/redux/redux_thunk.cs
-
81Runtime/redux/store.cs
-
171Runtime/redux/widget_redux.cs
-
24Samples/ReduxSample/ObjectFinder/Middleware.cs
-
8Samples/ReduxSample/redux.meta
-
8Samples/ReduxSample/redux_logging.meta
-
0/Runtime/redux/redux_thunk.cs.meta
-
0/Runtime/redux/store.cs.meta
-
0/Runtime/redux/widget_redux.cs.meta
-
0/Runtime/redux/redux_logging.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 3a7bb5e4fb043482e8bf223c2d3fa76c |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public static class ReduxLogging { |
|||
public static Middleware<State> create<State>() { |
|||
return (store) => (next) => new DispatcherImpl((action) => { |
|||
var previousState = store.getState(); |
|||
var previousStateDump = JsonUtility.ToJson(previousState); |
|||
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("previousState=\n{0}", previousStateDump); |
|||
Debug.LogFormat("afterState=\n{0}", afterStateDump); |
|||
return result; |
|||
}); |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public static class ReduxThunk { |
|||
public static Middleware<State> create<State>() { |
|||
return (store) => (next) => new DispatcherImpl((action) => { |
|||
var thunkAction = action as ThunkAction<State>; |
|||
if (thunkAction != null) { |
|||
return thunkAction.action(store.dispatcher, store.getState); |
|||
} |
|||
|
|||
return next.dispatch(action); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public sealed class ThunkAction<State> { |
|||
public readonly Func<Dispatcher, Func<State>, object> action; |
|||
|
|||
public ThunkAction(Func<Dispatcher, Func<State>, object> action) { |
|||
this.action = action; |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Linq; |
|||
|
|||
namespace Unity.UIWidgets { |
|||
public interface Dispatcher { |
|||
T dispatch<T>(object action); |
|||
|
|||
object dispatch(object action); |
|||
} |
|||
|
|||
public class DispatcherImpl : Dispatcher { |
|||
readonly Func<object, object> _impl; |
|||
|
|||
public DispatcherImpl(Func<object, object> impl) { |
|||
this._impl = impl; |
|||
} |
|||
|
|||
public T dispatch<T>(object action) { |
|||
if (this._impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return (T) this._impl(action); |
|||
} |
|||
|
|||
public object dispatch(object action) { |
|||
if (this._impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return this._impl(action); |
|||
} |
|||
} |
|||
|
|||
public delegate State Reducer<State>(State previousState, object action); |
|||
|
|||
public delegate Func<Dispatcher, Dispatcher> Middleware<State>(Store<State> store); |
|||
|
|||
public delegate void StateChangedHandler<State>(State action); |
|||
|
|||
public class Store<State> { |
|||
public StateChangedHandler<State> stateChanged; |
|||
|
|||
readonly Dispatcher _dispatcher; |
|||
readonly Reducer<State> _reducer; |
|||
State _state; |
|||
|
|||
public Store( |
|||
Reducer<State> reducer, |
|||
State initialState = default, |
|||
params Middleware<State>[] middleware) { |
|||
this._reducer = reducer; |
|||
this._dispatcher = this._applyMiddleware(middleware); |
|||
this._state = initialState; |
|||
} |
|||
|
|||
public Dispatcher dispatcher { |
|||
get { return this._dispatcher; } |
|||
} |
|||
|
|||
public State getState() { |
|||
return this._state; |
|||
} |
|||
|
|||
Dispatcher _applyMiddleware(params Middleware<State>[] middleware) { |
|||
return middleware.Reverse().Aggregate<Middleware<State>, Dispatcher>( |
|||
new DispatcherImpl(this._innerDispatch), |
|||
(current, middlewareItem) => middlewareItem(this)(current)); |
|||
} |
|||
|
|||
object _innerDispatch(object action) { |
|||
this._state = this._reducer(this._state, action); |
|||
|
|||
if (this.stateChanged != null) { |
|||
this.stateChanged(this._state); |
|||
} |
|||
|
|||
return action; |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
|
|||
namespace Unity.UIWidgets.Redux { |
|||
public class StoreProvider<State> : InheritedWidget { |
|||
readonly Store<State> _store; |
|||
|
|||
public StoreProvider( |
|||
Store<State> store = null, |
|||
Widget child = null, |
|||
Key key = null) : base(key: key, child: child) { |
|||
D.assert(store != null); |
|||
D.assert(child != null); |
|||
this._store = store; |
|||
} |
|||
|
|||
public static Store<State> of(BuildContext context) { |
|||
var type = _typeOf<StoreProvider<State>>(); |
|||
StoreProvider<State> provider = context.inheritFromWidgetOfExactType(type) as StoreProvider<State>; |
|||
if (provider == null) { |
|||
throw new UIWidgetsError("StoreProvider is missing"); |
|||
} |
|||
|
|||
return provider._store; |
|||
} |
|||
|
|||
static Type _typeOf<T>() { |
|||
return typeof(T); |
|||
} |
|||
|
|||
public override bool updateShouldNotify(InheritedWidget old) { |
|||
return !Equals(this._store, ((StoreProvider<State>) old)._store); |
|||
} |
|||
} |
|||
|
|||
public delegate Widget ViewModelBuilder<ViewModel>(BuildContext context, ViewModel viewModel, Dispatcher dispatcher); |
|||
|
|||
public delegate ViewModel StoreConverter<State, ViewModel>(State state); |
|||
|
|||
public delegate bool ShouldRebuildCallback<ViewModel>(ViewModel previous, ViewModel current); |
|||
|
|||
public class StoreConnector<State, ViewModel> : StatelessWidget { |
|||
public readonly ViewModelBuilder<ViewModel> builder; |
|||
|
|||
public readonly StoreConverter<State, ViewModel> converter; |
|||
|
|||
public readonly ShouldRebuildCallback<ViewModel> shouldRebuild; |
|||
|
|||
public readonly bool pure; |
|||
|
|||
public StoreConnector( |
|||
ViewModelBuilder<ViewModel> builder = null, |
|||
StoreConverter<State, ViewModel> converter = null, |
|||
bool pure = false, |
|||
ShouldRebuildCallback<ViewModel> shouldRebuild = null, |
|||
Key key = null) : base(key) { |
|||
D.assert(builder != null); |
|||
D.assert(converter != null); |
|||
this.pure = pure; |
|||
this.builder = builder; |
|||
this.converter = converter; |
|||
this.shouldRebuild = shouldRebuild; |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return new _StoreListener<State, ViewModel>( |
|||
store: StoreProvider<State>.of(context), |
|||
builder: this.builder, |
|||
converter: this.converter, |
|||
pure: this.pure, |
|||
shouldRebuild: this.shouldRebuild |
|||
); |
|||
} |
|||
} |
|||
|
|||
public class _StoreListener<State, ViewModel> : StatefulWidget { |
|||
public readonly ViewModelBuilder<ViewModel> builder; |
|||
|
|||
public readonly StoreConverter<State, ViewModel> converter; |
|||
|
|||
public readonly Store<State> store; |
|||
|
|||
public readonly ShouldRebuildCallback<ViewModel> shouldRebuild; |
|||
|
|||
public readonly bool pure; |
|||
|
|||
public _StoreListener( |
|||
ViewModelBuilder<ViewModel> builder = null, |
|||
StoreConverter<State, ViewModel> converter = null, |
|||
Store<State> store = null, |
|||
bool pure = false, |
|||
ShouldRebuildCallback<ViewModel> shouldRebuild = null, |
|||
Key key = null) : base(key) { |
|||
D.assert(builder != null); |
|||
D.assert(converter != null); |
|||
D.assert(store != null); |
|||
this.store = store; |
|||
this.builder = builder; |
|||
this.converter = converter; |
|||
this.pure = pure; |
|||
this.shouldRebuild = shouldRebuild; |
|||
} |
|||
|
|||
public override widgets.State createState() { |
|||
return new _StoreListenerState<State, ViewModel>(); |
|||
} |
|||
} |
|||
|
|||
class _StoreListenerState<State, ViewModel> : State<_StoreListener<State, ViewModel>> { |
|||
ViewModel latestValue; |
|||
|
|||
public override void initState() { |
|||
base.initState(); |
|||
this._init(); |
|||
} |
|||
|
|||
public override void dispose() { |
|||
this.widget.store.stateChanged -= this._handleStateChanged; |
|||
base.dispose(); |
|||
} |
|||
|
|||
public override void didUpdateWidget(StatefulWidget oldWidget) { |
|||
var oldStore = ((_StoreListener<State, ViewModel>) oldWidget).store; |
|||
if (this.widget.store != oldStore) { |
|||
oldStore.stateChanged -= this._handleStateChanged; |
|||
this._init(); |
|||
} |
|||
|
|||
base.didUpdateWidget(oldWidget); |
|||
} |
|||
|
|||
void _init() { |
|||
this.widget.store.stateChanged += this._handleStateChanged; |
|||
this.latestValue = this.widget.converter(this.widget.store.getState()); |
|||
} |
|||
|
|||
void _handleStateChanged(State state) { |
|||
if (Window.hasInstance) { |
|||
this._innerStateChanged(state); |
|||
} |
|||
else { |
|||
using (WindowProvider.of(this.context).getScope()) { |
|||
this._innerStateChanged(state); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void _innerStateChanged(State state) { |
|||
var preValue = this.latestValue; |
|||
this.latestValue = this.widget.converter(this.widget.store.getState()); |
|||
if (this.widget.shouldRebuild != null) { |
|||
if (!this.widget.shouldRebuild(preValue, this.latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
else if (this.widget.pure) { |
|||
if (Equals(preValue, this.latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
|
|||
this.setState(); |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return this.widget.builder(context, this.latestValue, this.widget.store.dispatcher); |
|||
} |
|||
} |
|||
} |
|
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.UIWidgets.Sample.Redux.ObjectFinder { |
|||
public class GameFinderMiddleware { |
|||
public static Middleware<FinderAppState> Create() { |
|||
return (store) => (next) => (action) => { |
|||
if (action is SearchAction) { |
|||
var searchAction = (SearchAction) action; |
|||
var objects = Object.FindObjectsOfType(typeof(FinderGameObject)).Where((obj) => { |
|||
return searchAction.keyword == "" || |
|||
obj.name.ToUpper().Contains(searchAction.keyword.ToUpper()); |
|||
}).Select(obj => new GameObjectInfo {id = obj.GetInstanceID(), name = obj.name}).ToList(); |
|||
|
|||
var result = next(action); |
|||
store.Dispatch(new SearchResultAction() {keyword = searchAction.keyword, results = objects}); |
|||
return result; |
|||
} |
|||
|
|||
return next(action); |
|||
}; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b6da4bbe4d8914b81a78b7d08ad4c7a4 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 5efe762e4ce804533b403e22eedb2e3b |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue