-
-
Save marksteve/79f3cc24119187e75c5ed5c1c5a5f678 to your computer and use it in GitHub Desktop.
Revisions
-
marksteve revised this gist
Jun 4, 2016 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,6 @@ defmodule Example do {_, store} = store |> subscribe(&subscriber/1) store = store |> dispatch(%{ type: :add, value: 10 }) {unsubscribe, store} = store |> subscribe(&another_subscriber/1) store = store |> dispatch(%{ type: :add, value: 20 }) store = store |> unsubscribe.() store = store |> dispatch(%{ type: :add, value: 10 }) -
marksteve revised this gist
Jun 4, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -38,8 +38,9 @@ defmodule Example do {_, store} = store |> subscribe(&subscriber/1) store = store |> dispatch(%{ type: :add, value: 10 }) {unsubscribe, store} = store |> subscribe(&another_subscriber/1) {_, store} = store |> subscribe(fn(x) -> IO.inspect(get_state(x)) end) store = store |> dispatch(%{ type: :add, value: 20 }) store = store |> unsubscribe.() store = store |> dispatch(%{ type: :add, value: 10 }) store -
marksteve revised this gist
Jun 4, 2016 . 2 changed files with 10 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,9 @@ defmodule Redux do Subscribe to a store """ def subscribe(store, subscriber) do unsubscribe = fn(store) -> Map.put(store, :subscribers, store.subscribers -- [subscriber]) end store = store |> Map.put(:subscribers, store.subscribers ++ [subscriber]) {unsubscribe, store} end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,18 +26,21 @@ defmodule Example do Expected output: 2 subscriber: 12 subscriber: 32 another_subscriber: 32 subscriber: 42 """ def run do store = create_store(&reducer/2) store = store |> dispatch(%{ type: :set, value: 2 }) IO.puts(store |> get_state()) {_, store} = store |> subscribe(&subscriber/1) store = store |> dispatch(%{ type: :add, value: 10 }) {unsubscribe, store} = store |> subscribe(&another_subscriber/1) store = store |> dispatch(%{ type: :add, value: 20 }) store = store |> unsubscribe.() store = store |> dispatch(%{ type: :add, value: 10 }) store end -
marksteve revised this gist
Jun 3, 2016 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,6 @@ defmodule Redux do store = create_store fn state, action -> ... end store |> get_state() store |> dispatch(%{ type: "publish" }) """ -
marksteve revised this gist
Jun 3, 2016 . 2 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,7 @@ defmodule Redux do end defp run_subscribers(store, [subscriber | subscribers]) do subscriber.(store) run_subscribers(store, subscribers) end defp run_subscribers(store, []), do: store @@ -48,4 +48,4 @@ defmodule Redux do store |> Map.put(:subscribers, store.subscribers ++ [subscriber]) end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,12 +14,12 @@ defmodule Example do def subscriber(store) do state = get_state(store) IO.puts("subscriber: #{state}") end def another_subscriber(store) do state = get_state(store) IO.puts("another_subscriber: #{state}") end @doc """ @@ -43,4 +43,4 @@ defmodule Example do end end Example.run -
marksteve revised this gist
Jun 3, 2016 . 1 changed file with 28 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,46 +1,46 @@ defmodule Example do import Redux def reducer(state, action) do case action.type do :set -> action.value :add -> state + action.value _ -> state end end def subscriber(store) do state = get_state(store) :ok = IO.puts("subscriber: #{state}") end def another_subscriber(store) do state = get_state(store) :ok = IO.puts("another_subscriber: #{state}") end @doc """ Expected output: 2 subscriber: 12 subscriber: 42 another_subscriber: 42 """ def run do store = create_store(&reducer/2) store = store |> dispatch(%{ type: :set, value: 2 }) IO.puts(store |> get_state()) store = store |> subscribe(&subscriber/1) store = store |> dispatch(%{ type: :add, value: 10 }) store = store |> subscribe(&another_subscriber/1) store = store |> dispatch(%{ type: :add, value: 30 }) store end end Example.run -
marksteve revised this gist
Jun 3, 2016 . 2 changed files with 58 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,15 @@ defmodule Redux do @moduledoc """ Like redux.js, but more elixir-like store = create_store fn state, action -> ... end store |> get_state() store |> subscribe(fn -> IO.puts get_state() end) store |> dispatch(%{ type: "publish" }) """ defstruct reducer: nil, state: nil, subscribers: [] @doc """ Creates a store @@ -30,5 +32,20 @@ defmodule Redux do def dispatch(store, action) do store |> Map.put(:state, store.reducer.(get_state(store), action)) |> run_subscribers(store.subscribers) end defp run_subscribers(store, [subscriber | subscribers]) do :ok = subscriber.(store) run_subscribers(store, subscribers) end defp run_subscribers(store, []), do: store @doc """ Subscribe to a store """ def subscribe(store, subscriber) do store |> Map.put(:subscribers, store.subscribers ++ [subscriber]) end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,46 @@ defmodule Example do import Redux def reducer(state, action) do case action.type do :set -> action.value :add -> state + action.value _ -> state end end def subscriber(store) do state = get_state(store) :ok = IO.puts("subscriber: #{state}") end def another_subscriber(store) do state = get_state(store) :ok = IO.puts("another_subscriber: #{state}") end @doc """ Expected output: 2 subscriber: 12 subscriber: 42 another_subscriber: 42 """ def run do store = create_store(&reducer/2) store = store |> dispatch(%{ type: :set, value: 2 }) IO.puts(store |> get_state()) store = store |> subscribe(&subscriber/1) store = store |> dispatch(%{ type: :add, value: 10 }) store = store |> subscribe(&another_subscriber/1) store = store |> dispatch(%{ type: :add, value: 30 }) store end end Example.run -
rstacruz revised this gist
Jun 3, 2016 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,9 @@ defmodule Example do def reducer(state, action) do case action.type do :set -> action.value :add -> state + action.value _ -> state @@ -15,10 +15,10 @@ defmodule Example do def run do store = create_store(&reducer/2) store = store |> dispatch(%{ type: :set, value: 2 }) IO.puts(store |> get_state()) #=> 2 store = dispatch(store, %{ type: :add, value: 10 }) IO.puts(store |> get_state()) #=> 12 end end -
rstacruz revised this gist
Jun 2, 2016 . 1 changed file with 23 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,26 @@ defmodule Example do import Redux def reducer(state, action) do case action.type do "set" -> action.value "add" -> state + action.value _ -> state end end def run do store = create_store(&reducer/2) store = store |> dispatch(%{ type: "set", value: 2 }) IO.puts(store |> get_state()) #=> 2 store = dispatch(store, %{ type: "add", value: 10 }) IO.puts(store |> get_state()) #=> 12 end end Example.run -
rstacruz revised this gist
Jun 2, 2016 . 2 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ defmodule Redux do """ def create_store(reducer, initial_state \\ nil) do %Redux{reducer: reducer, state: initial_state} |> dispatch(%{ type: :"@@redux/INIT" }) end @doc """ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ store = Redux.create_store fn state, action -> case action.type do :set -> action.value :add -> state + action.value _ -> state end end store = store |> Redux.dispatch(%{ type: :set, value: 2 }) IO.puts(store |> Redux.get_state()) #=> 2 store = Redux.dispatch(store, %{ type: :add, value: 10 }) IO.puts(store |> Redux.get_state()) #=> 12 -
rstacruz revised this gist
Jun 2, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -28,6 +28,7 @@ defmodule Redux do Dispatches an action """ def dispatch(store, action) do store |> Map.put(:state, store.reducer.(get_state(store), action)) end end -
rstacruz revised this gist
Jun 2, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ defmodule Redux do @moduledoc """ Like redux.js, but more elixir-like store = create_store fn state, action -> ... end store |> get_state() store |> dispatch(%{ type: "publish" }) """ defstruct reducer: nil, state: nil -
rstacruz revised this gist
Jun 2, 2016 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
rstacruz revised this gist
Jun 2, 2016 . No changes.There are no files selected for viewing
-
rstacruz created this gist
Jun 2, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ store = Redux.create_store fn state, action -> case action.type do "set" -> action.value "add" -> state + action.value _ -> state end end store = store |> Redux.dispatch(%{ type: "set", value: 2 }) IO.puts(store |> Redux.get_state()) #=> 2 store = Redux.dispatch(store, %{ type: "add", value: 10 }) IO.puts(store |> Redux.get_state()) #=> 12 This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ defmodule Redux do @moduledoc """ Like redux.js, but more elixir-like """ defstruct reducer: nil, state: nil @doc """ Creates a store """ def create_store(reducer, initial_state \\ nil) do %Redux{reducer: reducer, state: initial_state} |> dispatch(%{ type: "@@redux/INIT" }) end @doc """ Returns the current state """ def get_state(store) do store.state end @doc """ Dispatches an action """ def dispatch(store, action) do Map.put(store, :state, store.reducer.(get_state(store), action)) end end