-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
194 lines (174 loc) · 5.01 KB
/
app.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import redux = require('redux');
// Routing related types and actions
import { RouteId, routingCallback, RouteState, setRoute,
clone, overlay, overlayIf } from 'vada';
// Interacting with the browser
import { bindRoute, RouteRequest, initializeRouting } from 'vada/lib/src/browser';
// Operation related types
import { DefOp, Operation, createOpReducer } from 'vada';
// Reducer related functions
import { combineReducers, wrapReducer } from 'vada';
// Memoization
import { multiMemo, Builder } from 'vada';
/*
== Routes ==
*/
export const allRoute = new RouteId<{}>("all");
export const activeRoute = new RouteId<{}>("active");
export const completedRoute = new RouteId<{}>("completed");
/*
== Types ==
*/
export interface TodoItem {
id: number;
completed: boolean;
text: string;
}
export interface AppState {
next: number;
entry: string;
route: RouteState;
items: TodoItem[];
active: number;
}
export const initialState: AppState = {
next: 0,
entry: "",
route: allRoute.apply({}),
items: [],
active: 0,
}
/*
== Operations ==
*/
export const entryText = DefOp("entry", (s: AppState, p: string) => {
return overlay(s, s => s.entry = p);
});
export const createNew = DefOp("new", (s: AppState, p: void) => {
return {
next: s.next+1,
entry: "",
route: s.route,
items: [{
id: s.next,
completed: false,
text: s.entry.trim(),
}, ...s.items],
active: s.active,
};
});
export const deleteItem = DefOp("delete", (s: AppState, p: number) => {
let ret = clone(s);
ret.items = [];
s.items.forEach(i => {
if (i.id!==p) {
ret.items.push(i);
}
});
return ret;
});
export const clearCompleted = DefOp("clear", (s: AppState, p: void) => {
let ret = clone(s);
ret.items = [];
s.items.forEach(i => {
if (!i.completed) {
ret.items.push(i);
}
});
return ret;
});
export const editItem = DefOp("edit", (s: TodoItem, p: {id: number, t: string}) => {
return overlayIf(s, p.id===s.id, s => s.text = p.t);
});
export const markAs = DefOp("mark", (s: TodoItem, p: {id: number, as: boolean}) => {
return overlayIf(s, p.id===s.id, s => s.completed = p.as);
});
export const markAllAs = DefOp("markall", (s: TodoItem, p: boolean) => {
return overlay(s, s => s.completed = p);
});
export const toggleCompleted = DefOp("toggle", (s: TodoItem, p: number) => {
return overlayIf(s, p===s.id, s => s.completed = !s.completed);
});
/*
== Reactors ==
*/
const countActive = (prev: AppState, cur: AppState) => {
if (!cur || !prev) return cur;
if (cur.items===prev.items) return cur;
let ret = clone(cur);
ret.active = 0;
cur.items.forEach(i => {
if (!i.completed) {
ret.active++;
}
});
return ret;
};
/*
== Filters ==
*/
export function filter(route: string, items: TodoItem[]): TodoItem[] {
let ret: TodoItem[] = [];
items.forEach(i => {
if (route==allRoute.id ||
(route==activeRoute.id==!i.completed) ||
(route==completedRoute.id==i.completed)) {
ret.push(i);
}
});
return ret;
}
export const memoFilter =
multiMemo((s: {route: string, items: TodoItem[]}) => filter(s.route, s.items));
/*
== Reducers ==
*/
let r = new Builder<AppState>(
// Global actions
[entryText, createNew, deleteItem, clearCompleted], initialState)
// Actions to apply to items
.overlayOps((s, r, a) => { s.items = s.items.map((i: TodoItem) => r(i, a)); },
[editItem, markAs, markAllAs, toggleCompleted])
// Actions to apply to the route
.overlayOps((s, r, a) => { s.route = r(s.route, a) }, [setRoute])
// Perform count
.reactTo(countActive)
export const reducer: redux.Reducer<AppState> = r.reducer();
/*
== Action Provider ==
*/
export class ActionProvider {
all: RouteRequest<{}>;
active: RouteRequest<{}>;
completed: RouteRequest<{}>;
constructor(public store: redux.Store<AppState>) {
this.all = bindRoute(allRoute, "/");
this.active = bindRoute(activeRoute, "/active");
this.completed = bindRoute(completedRoute, "/completed");
initializeRouting(routingCallback(store, () => {
this.all.goto(null);
}));
}
entryText(t: string) {
this.store.dispatch(entryText.request(t));
}
createNew() { this.store.dispatch(createNew.request(null)); }
deleteItem(id: number) {
this.store.dispatch(deleteItem.request(id));
}
clearCompleted() {
this.store.dispatch(clearCompleted.request(null));
}
editItem(id: number, t: string) {
this.store.dispatch(editItem.request({id: id, t: t}));
}
markAs(id: number, as: boolean) {
this.store.dispatch(markAs.request({id: id, as: as}));
}
markAllAs(as: boolean) {
this.store.dispatch(markAllAs.request(as));
}
toggleCompleted(id: number) {
this.store.dispatch(toggleCompleted.request(id));
}
}