Last active
October 5, 2015 20:37
-
-
Save brendanmckenzie/a41e3d37a2627fb5be20 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Dynamic; | |
using System.Collections.Generic; | |
namespace Kasbah.Core | |
{ | |
public struct Action | |
{ | |
public string Type { get; set; } | |
public dynamic Data { get; set; } | |
} | |
public class Store | |
{ | |
dynamic _state = null; | |
readonly ICollection<Func<Action, dynamic, dynamic>> _reducers = new List<Func<Action, dynamic, dynamic>>(); | |
public dynamic State { get { return _state; } } | |
public void AddReducer(Func<Action, dynamic, dynamic> reducer) | |
{ | |
_reducers.Add(reducer); | |
} | |
public void Dispatch(Action action) | |
{ | |
dynamic state = (_state ?? new ExpandoObject()).Clone(); | |
foreach (var reducer in _reducers) | |
{ | |
state = reducer(action, state); | |
} | |
_state = state; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment