Commit 9d14607
committed
feat: Add init_state/0 callback to reducer modules
Reducers now define their own initial state via init_state/0, making them
fully self-contained and reusable.
Changes:
- Added init_state/0 callback to :reducer macro (defaults to %{})
- Modified :process init/1 to call each reducer's init_state/0
- Reducer slices are automatically initialized from reducer's init_state/0
- SessionProcess init_state/1 only needs to define non-reducer state
Benefits:
- Reducers are fully self-contained (state + actions + initialization)
- Reusable across different SessionProcesses
- Clear separation of concerns
- Parent process doesn't need to know reducer's state structure
Example:
```elixir
defmodule UserReducer do
use Phoenix.SessionProcess, :reducer
def init_state do
%{users: [], loading: false, query: nil}
end
def handle_action(%{type: "add-user", payload: user}, state) do
%{state | users: [user | state.users]}
end
end
defmodule MyApp.SessionProcess do
use Phoenix.SessionProcess, :process
def init_state(_), do: %{global_count: 0} # Only non-reducer state
def combined_reducers do
%{users: UserReducer} # UserReducer.init_state/0 called automatically
end
end
```
Tests:
- Added test verifying reducer init_state is called
- Updated reducers to define init_state/0
- All 230 tests passing (13 integration tests)
Documentation:
- Updated :reducer macro docs with init_state/0
- Added State Initialization section
- Updated examples to show init_state/0
Refs: User feedback requesting init_state for reducers1 parent 177c996 commit 9d14607
File tree
2 files changed
+71
-8
lines changed- lib/phoenix
- test/phoenix/session_process
2 files changed
+71
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
824 | 824 | | |
825 | 825 | | |
826 | 826 | | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
827 | 831 | | |
828 | 832 | | |
829 | 833 | | |
| |||
848 | 852 | | |
849 | 853 | | |
850 | 854 | | |
| 855 | + | |
851 | 856 | | |
852 | 857 | | |
853 | 858 | | |
| |||
857 | 862 | | |
858 | 863 | | |
859 | 864 | | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
860 | 871 | | |
861 | 872 | | |
862 | 873 | | |
| |||
873 | 884 | | |
874 | 885 | | |
875 | 886 | | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
876 | 908 | | |
877 | 909 | | |
878 | 910 | | |
| |||
931 | 963 | | |
932 | 964 | | |
933 | 965 | | |
934 | | - | |
| 966 | + | |
935 | 967 | | |
936 | 968 | | |
937 | 969 | | |
| |||
1004 | 1036 | | |
1005 | 1037 | | |
1006 | 1038 | | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
1007 | 1052 | | |
1008 | 1053 | | |
1009 | 1054 | | |
1010 | 1055 | | |
1011 | 1056 | | |
1012 | | - | |
1013 | | - | |
| 1057 | + | |
| 1058 | + | |
1014 | 1059 | | |
1015 | 1060 | | |
1016 | 1061 | | |
| |||
Lines changed: 23 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
31 | 39 | | |
32 | 40 | | |
33 | 41 | | |
| |||
44 | 52 | | |
45 | 53 | | |
46 | 54 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 55 | + | |
| 56 | + | |
52 | 57 | | |
53 | 58 | | |
54 | 59 | | |
| |||
73 | 78 | | |
74 | 79 | | |
75 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
76 | 94 | | |
77 | 95 | | |
78 | 96 | | |
| |||
0 commit comments