Skip to content

Instantly share code, notes, and snippets.

@brendanmckenzie
Last active October 5, 2015 20:37
Show Gist options
  • Save brendanmckenzie/a41e3d37a2627fb5be20 to your computer and use it in GitHub Desktop.
Save brendanmckenzie/a41e3d37a2627fb5be20 to your computer and use it in GitHub Desktop.
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