wangshuang
4 年前
当前提交
81cff0c6
共有 10 个文件被更改,包括 356 次插入 和 0 次删除
-
8com.unity.uiwidgets/Runtime/redux.meta
-
19com.unity.uiwidgets/Runtime/redux/redux_logging.cs
-
11com.unity.uiwidgets/Runtime/redux/redux_logging.cs.meta
-
32com.unity.uiwidgets/Runtime/redux/redux_thunk.cs
-
11com.unity.uiwidgets/Runtime/redux/redux_thunk.cs.meta
-
81com.unity.uiwidgets/Runtime/redux/store.cs
-
11com.unity.uiwidgets/Runtime/redux/store.cs.meta
-
172com.unity.uiwidgets/Runtime/redux/widget_redux.cs
-
11com.unity.uiwidgets/Runtime/redux/widget_redux.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: a074d5db1a47743f19fc0f9ac709e173 |
|||
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.ToString(), JsonUtility.ToJson(action)); |
|||
Debug.LogFormat("previousState=\n{0}", previousStateDump); |
|||
Debug.LogFormat("afterState=\n{0}", afterStateDump); |
|||
return result; |
|||
}); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 66d5d5237e5f84f7f870b6e8d2b87815 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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 && thunkAction.action != 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 readonly string displayName; |
|||
|
|||
public ThunkAction( |
|||
Func<Dispatcher, Func<State>, object> action = null, |
|||
string displayName = null) { |
|||
this.action = action; |
|||
this.displayName = displayName ?? ""; |
|||
} |
|||
|
|||
public override string ToString() { |
|||
return "ThunkAction(" + displayName + ")"; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f837de040a3fa4a3fb3e6c344153e1eb |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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) { |
|||
_impl = impl; |
|||
} |
|||
|
|||
public T dispatch<T>(object action) { |
|||
if (_impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return (T) _impl(action); |
|||
} |
|||
|
|||
public object dispatch(object action) { |
|||
if (_impl == null) { |
|||
return default; |
|||
} |
|||
|
|||
return _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) { |
|||
_reducer = reducer; |
|||
_dispatcher = _applyMiddleware(middleware); |
|||
_state = initialState; |
|||
} |
|||
|
|||
public Dispatcher dispatcher { |
|||
get { return _dispatcher; } |
|||
} |
|||
|
|||
public State getState() { |
|||
return _state; |
|||
} |
|||
|
|||
Dispatcher _applyMiddleware(params Middleware<State>[] middleware) { |
|||
return middleware.Reverse().Aggregate<Middleware<State>, Dispatcher>( |
|||
new DispatcherImpl(_innerDispatch), |
|||
(current, middlewareItem) => middlewareItem(this)(current)); |
|||
} |
|||
|
|||
object _innerDispatch(object action) { |
|||
_state = _reducer(_state, action); |
|||
|
|||
if (stateChanged != null) { |
|||
stateChanged(_state); |
|||
} |
|||
|
|||
return action; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 14bfb41ffcfd144f1b32a04eff5f93d0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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); |
|||
_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(objA: _store, objB: ((StoreProvider<State>) old)._store); |
|||
} |
|||
} |
|||
|
|||
public delegate Widget ViewModelBuilder<in ViewModel>(BuildContext context, ViewModel viewModel, Dispatcher dispatcher); |
|||
|
|||
public delegate ViewModel StoreConverter<in State, out ViewModel>(State state); |
|||
|
|||
public delegate bool ShouldRebuildCallback<in 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: builder, |
|||
converter: converter, |
|||
pure: pure, |
|||
shouldRebuild: 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(); |
|||
_init(); |
|||
} |
|||
|
|||
public override void dispose() { |
|||
widget.store.stateChanged -= _handleStateChanged; |
|||
base.dispose(); |
|||
} |
|||
|
|||
public override void didUpdateWidget(StatefulWidget oldWidget) { |
|||
var oldStore = ((_StoreListener<State, ViewModel>) oldWidget).store; |
|||
if (widget.store != oldStore) { |
|||
oldStore.stateChanged -= _handleStateChanged; |
|||
_init(); |
|||
} |
|||
|
|||
base.didUpdateWidget(oldWidget); |
|||
} |
|||
|
|||
void _init() { |
|||
widget.store.stateChanged += _handleStateChanged; |
|||
latestValue = widget.converter(widget.store.getState()); |
|||
} |
|||
|
|||
void _handleStateChanged(State state) { |
|||
if (Window.instance._panel != null) { |
|||
_innerStateChanged(state: state); |
|||
} |
|||
else { |
|||
var isolate = Isolate.current; |
|||
using (Isolate.getScope(isolate: isolate)) { |
|||
_innerStateChanged(state: state); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void _innerStateChanged(State state) { |
|||
var preValue = latestValue; |
|||
latestValue = widget.converter(widget.store.getState()); |
|||
if (widget.shouldRebuild != null) { |
|||
if (!widget.shouldRebuild(preValue, latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
else if (widget.pure) { |
|||
if (Equals(preValue, latestValue)) { |
|||
return; |
|||
} |
|||
} |
|||
|
|||
setState(); |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return widget.builder(context, latestValue, widget.store.dispatcher); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a1765e003a4744e39b381a0bd927735a |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue