Skip to content

Commit

Permalink
Merge pull request #34 from /issues/31
Browse files Browse the repository at this point in the history
Issues/33 Multiple AddTo not works well
  • Loading branch information
mattak authored Jun 30, 2016
2 parents a63b5a5 + a341465 commit acba3a8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 48 deletions.
19 changes: 11 additions & 8 deletions Assets/Unidux/UniduxSubscriber.cs
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();
}
}
}
97 changes: 74 additions & 23 deletions Assets/Unidux/UniduxSubscriberBase.cs
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();
}
}
}
12 changes: 6 additions & 6 deletions Assets/Unidux/UniduxSustainSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ public class UniduxSustainSubscriber : UniduxSubscriberBase
{
void Start()
{
_renderSubscriber(true);
_reduceSubscriber(true);
CallReducers(true);
CallRenders(true);
}

void OnDestroy()
{
_renderSubscriber(false);
_reduceSubscriber(false);
_renderSubscriber = null;
_reduceSubscriber = null;
CallReducers(false);
CallRenders(false);
DisposeReducers();
DisposeRenders();
}
}
}
17 changes: 11 additions & 6 deletions Assets/UniduxExample/Counter/Scripts/Unidux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
{
public class Unidux : SingletonMonoBehaviour<Unidux>
{
public Store<State> Store { get; private set; }

protected override void Awake()
private Store<State> _store;
public Store<State> Store
{
base.Awake();
this.Store = new Store<State>();
this.Store.AddReducer<CountAction>(CountReducer.Reduce);
get
{
if (null == _store)
{
_store = new Store<State>();
_store.AddReducer<CountAction>(CountReducer.Reduce);
}
return _store;
}
}

void Update()
Expand Down
17 changes: 12 additions & 5 deletions Assets/UniduxExample/List/Scripts/Unidux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
{
public class Unidux : SingletonMonoBehaviour<Unidux>
{
public Store<State> Store { get; private set; }
private Store<State> _store;

protected override void Awake()
public Store<State> Store
{
base.Awake();
this.Store = new Store<State>();
this.Store.AddReducer<ListAddAction>(ListReducer.Reduce);
get
{
if (null == _store)
{
_store = new Store<State>();
_store.AddReducer<ListAddAction>(ListReducer.Reduce);
}

return _store;
}
}

void Update()
Expand Down

0 comments on commit acba3a8

Please sign in to comment.