-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from /issues/31
Issues/33 Multiple AddTo not works well
- Loading branch information
Showing
5 changed files
with
114 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Unidux | ||
namespace Unidux | ||
{ | ||
public class UniduxSubscriber : UniduxSubscriberBase | ||
{ | ||
void OnEnable() | ||
{ | ||
this._reduceSubscriber(true); | ||
this._renderSubscriber(true); | ||
CallReducers(true); | ||
CallRenders(true); | ||
} | ||
|
||
void OnDisable() | ||
{ | ||
this._reduceSubscriber(false); | ||
this._renderSubscriber(false); | ||
CallReducers(false); | ||
CallRenders(false); | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
DisposeReducers(); | ||
DisposeRenders(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace Unidux | ||
{ | ||
public class UniduxSubscriberBase : MonoBehaviour, IUniduxSubscriber | ||
{ | ||
protected Action<bool> _renderSubscriber = (enable) => { }; | ||
protected Action<bool> _reduceSubscriber = (enable) => { }; | ||
private readonly Dictionary<int, Action<bool>> _renderSubscriberMap = new Dictionary<int, Action<bool>>(); | ||
private readonly Dictionary<int, Action<bool>> _reduceSubscriberMap = new Dictionary<int, Action<bool>>(); | ||
|
||
public void AddRenderTo<S>(Store<S> store, Render<S> render) where S : StateBase, new() | ||
{ | ||
_renderSubscriber = (enable) => | ||
Action<bool> renderSubscriber = null; | ||
int key = render.GetHashCode(); | ||
|
||
if (_renderSubscriberMap.ContainsKey(key)) | ||
{ | ||
if (enable) | ||
{ | ||
store.RenderEvent += render; | ||
} | ||
else | ||
renderSubscriber = _renderSubscriberMap[key]; | ||
} | ||
else | ||
{ | ||
renderSubscriber = (enable) => | ||
{ | ||
store.RenderEvent -= render; | ||
} | ||
}; | ||
_renderSubscriber(true); | ||
if (enable) | ||
{ | ||
store.RenderEvent += render; | ||
} | ||
else | ||
{ | ||
store.RenderEvent -= render; | ||
} | ||
}; | ||
} | ||
renderSubscriber(true); | ||
|
||
_renderSubscriberMap[key] = renderSubscriber; | ||
} | ||
|
||
public void AddReducerTo<S, A>(Store<S> store, Reducer<S, A> render) where S : StateBase, new() | ||
public void AddReducerTo<S, A>(Store<S> store, Reducer<S, A> reducer) where S : StateBase, new() | ||
{ | ||
_reduceSubscriber = (enable) => | ||
Action<bool> reduceSubscriber = null; | ||
int key = reducer.GetHashCode(); | ||
|
||
if (_reduceSubscriberMap.ContainsKey(key)) | ||
{ | ||
if (enable) | ||
{ | ||
store.AddReducer(render); | ||
} | ||
else | ||
reduceSubscriber = _reduceSubscriberMap[key]; | ||
} | ||
else | ||
{ | ||
reduceSubscriber = (enable) => | ||
{ | ||
store.RemoveReducer(render); | ||
} | ||
}; | ||
_reduceSubscriber(true); | ||
if (enable) | ||
{ | ||
store.AddReducer(reducer); | ||
} | ||
else | ||
{ | ||
store.RemoveReducer(reducer); | ||
} | ||
}; | ||
} | ||
reduceSubscriber(true); | ||
|
||
_reduceSubscriberMap[key] = reduceSubscriber; | ||
} | ||
|
||
protected void CallRenders(bool subscribe) | ||
{ | ||
foreach (var caller in _renderSubscriberMap.Values) | ||
{ | ||
caller(subscribe); | ||
} | ||
} | ||
|
||
protected void CallReducers(bool subscribe) | ||
{ | ||
foreach (var caller in _reduceSubscriberMap.Values) | ||
{ | ||
caller(subscribe); | ||
} | ||
} | ||
|
||
protected void DisposeRenders() | ||
{ | ||
_renderSubscriberMap.Clear(); | ||
} | ||
|
||
protected void DisposeReducers() | ||
{ | ||
_reduceSubscriberMap.Clear(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters