-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
137 lines (128 loc) · 3.1 KB
/
Copy pathmain.js
File metadata and controls
137 lines (128 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function createStore(reducer) {
// The store contains of four parts
// 1. The state
// 2. Access the state
// 3. Listen to changes on the state
// 4. Update the state // Action will update the state
let state;
let listeners = [];
const getState = () => state;
const subscribe = listener => {
listeners.push(listener);
return () => {
listeners = listeners.filter(l => l !== listener);
};
};
const dispatch = action => {
// The the reducer we made for updating the state. (addTodo)
state = reducer(state, action);
// Inform all the listener that subscribed about the store update.
listeners.forEach(listener => listener());
};
return {
getState,
subscribe,
dispatch
};
}
// You can not allow anyone to update the state so we need to have some set
// of rules for updating the state.
// How about having a collection of events that changes the list state of the app.
// Example:-
// var addTodo = {
// type: "ADD_TODO",
// todo: {
// id: 0,
// text: "Task 1",
// completed: false
// }
// };
// var removeTodo = {
// type: "REMOVE_TODO",
// id: 0
// };
// Reducer will take a state and action and return a new state.
// To increase the predictability of the state change we will use a pure function.
// Characteristics of a pure function :-
// Pure function always return same output for the same input
// A pure function only depends on the the value passed to it.
// Never produces any side effect
// Example:-
function todo(state = [], action) {
switch (action.type) {
case "ADD_TODO":
return state.concat([action.todo]);
case "TOGGLE_TODO":
return state.map(todo =>
todo.id !== action.id
? todo
: {
...todo,
completed: !todo.completed
}
);
case "REMOVE_TODO":
return state.filter(todo => todo.id !== action.id);
default:
return state;
}
}
function goal(state = [], action) {
switch (action.type) {
case "ADD_GOAL":
return state.concat([action.goal]);
case "TOGGLE_GOAL":
return state.map(goal =>
goal.id !== action.id
? goal
: {
...goal,
completed: !goal.completed
}
);
case "REMOVE_GOAL":
return state.filter(goal => goal.id !== action.id);
default:
return state;
}
}
// We can have multiple reducers so we will have to create a function rootReducer
function rootReducer(state = {}, action) {
return {
todos: todo(state.todos, action),
goals: goal(state.goals, action)
};
}
var store = createStore(rootReducer);
store.subscribe(() =>
console.log("The State Of the app is:", store.getState())
);
store.dispatch({
type: "ADD_TODO",
todo: {
id: 0,
text: "Task 1",
completed: false
}
});
store.dispatch({
type: "ADD_TODO",
todo: {
id: 1,
text: "Task 1",
completed: false
}
});
store.dispatch({
type: "TOGGLE_TODO",
id: 0
});
store.dispatch({
type: "ADD_GOAL",
goal: {
id: 1,
text: "Task 1",
completed: false
}
});
// const unsubscribe = store.subscribe(() => {});